Advertisement
Ankit_132

C

Apr 30th, 2024
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define LL long long
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int t;
  9.     cin>>t;
  10.     while(t--)
  11.     {
  12.  
  13.     int n;
  14.     LL k;
  15.     cin >> n;
  16.     cin >> k;
  17.     vector<LL>a_arr(n);
  18.     for(int i=0;i<n;i++)
  19.     {
  20.         cin>>a_arr[i];
  21.     }
  22.     LL l=0,r=1e18;
  23.     while(l<r)
  24.     {
  25.         LL mid=(l+r+1)/2;
  26.         LL ans=mid+(n-1);
  27.         LL need=0,t=0;
  28.         for(int i=0;i<n;i++)
  29.         {
  30.             if(a_arr[i]>ans/n)
  31.             {
  32.                 t++;
  33.             }
  34.             else
  35.             {
  36.                 LL tmp = ans/n-a_arr[i];
  37.                 need += tmp;
  38.             }
  39.         }
  40.         need+=max(ans%n-t,0ll);
  41.         if(need<=k)l=mid;
  42.         else r=mid-1;
  43.     }
  44.     cout<<l;
  45.     cout << endl;
  46.     }
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement