Advertisement
tepyotin2

Berry Picking

Oct 10th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef long double ld;
  5. #define mpa make_pair
  6. #define pb push_back
  7. #define ins insert
  8. #define f first
  9. #define s second
  10. #define all(x) x.begin(), x.end()
  11. #define nl "\n"
  12. void fileIO(string filename) {
  13.     freopen((filename + ".in").c_str(), "r", stdin);
  14.     freopen((filename + ".out").c_str(), "w", stdout);
  15. }
  16. int MOD = 1;
  17. bool cmp(int &a, int &b) {
  18.     //Sorts by the max mod, so Bessie can get the max amount of leftovers
  19.     return (a % MOD) > (b % MOD);
  20. }
  21. void solve() {
  22.     int N, K;
  23.     cin >> N >> K;
  24.     vector<int> A(N);
  25.     int maxD = 0;
  26.     for(int i = 0; i < N; i++) {
  27.         cin >> A[i];
  28.         maxD = max(maxD, A[i]);
  29.     }
  30.     int mx = 0;
  31.     for(int i = 1; i <= maxD; i++) {
  32.         int amount = 0;
  33.         //For loop calculates how many groups of "i" berries can be put into a basket
  34.         for(int j = 0; j < N; j++) {
  35.             amount += A[j] / i;
  36.         }
  37.         //If the amount is not enough for K / 2 baskets, it is not valid
  38.         if(amount < K / 2) {
  39.             continue;
  40.         }
  41.         if(amount >= K) {
  42.             //If there is greater than or equal to "i" sections for both Bessie and Ellie, then Bessie can collect (K / 2) * i berries
  43.             mx = max(mx, (K / 2) * i);
  44.             continue;
  45.         }
  46.         MOD = i;
  47.         sort(all(A), cmp);
  48.         //Gives the maximum amount of leftovers to Bessie
  49.         int cur = (amount - K / 2) * i;
  50.         for(int j = 0; j < N && j + amount < K; j++) {
  51.             cur += A[j] % i;
  52.         }
  53.         mx = max(mx, cur);
  54.     }
  55.     cout << mx << nl;
  56. }
  57. int main() {
  58.     ios::sync_with_stdio(false); cin.tie(nullptr);
  59.     fileIO("berries");
  60.     solve();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement