Advertisement
maycod23

Block_Problem.cpp

Apr 17th, 2022
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. # include   <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef vector<ll> vl;
  5. # define    endl                '\n'
  6. # define    fi                  first
  7. # define    se                  second
  8. # define    all(a)              a.begin(), a.end()
  9. ll cost(ll k, ll n, vl& a)
  10. {
  11.     vl temp;
  12.     for (ll i = 0; i < n; i++)
  13.     {
  14.         ll tempcost = a[i] + (k * (i + 1));
  15.         temp.push_back(tempcost);
  16.     }
  17.     sort(all(temp));
  18.     ll count = 0;
  19.     for (ll i = 0; i < k; i++)
  20.     {
  21.         count += temp[i];
  22.     }
  23.     return count;
  24. }
  25. int main()
  26. {
  27.     ll n, i, s; cin >> n >> s;
  28.     vl a(n);
  29.     for (i = 0; i < n; i++) cin >> a[i];
  30.     ll l = 0, r = n;
  31.     ll ansk = -1, ans2;
  32.     while (l <= r)
  33.         //l<r  ->in any case
  34.         //l<=r ->mid+1,mid-1
  35.     {
  36.         ll k = l + (r - l) / 2;//(l+r)/2
  37.         //k fixed here by binary search
  38.         ll count = cost(k, n, a);
  39.         if (count > s)
  40.         {
  41.             r = (k - 1);
  42.             continue;
  43.         }
  44.         if (count <= s)
  45.         {
  46.             if (k > ansk)
  47.             {
  48.                 ansk = k, ans2 = count;
  49.             }
  50.             l = k + 1;
  51.             continue;
  52.         }
  53.     }
  54.     cout << ansk << " " << ans2 << " " << endl;
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement