Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define _test int _TEST; cin>>_TEST; while(_TEST--)
- #define pb push_back
- int main()
- {
- _test
- {
- ll int n, k;
- cin>>n>>k;
- vector<ll int> a(n);
- for(auto &e: a)
- cin>>e;
- sort(a.rbegin(), a.rend());
- ll int low=0, high=1e18;
- ll int ans = 0;
- auto possible = [&](ll int x)
- {
- ll int tot=0, got=0;
- for(int i=0; i<n; i++)
- {
- if(got == 0)
- {
- if(a[i] >= x)
- {
- tot++;
- continue;
- }
- else
- got += a[i];
- }
- else
- {
- got += a[i];
- if(got >= x)
- {
- got -= x;
- tot++;
- }
- }
- }
- return (tot>=k);
- };
- while(low <= high)
- {
- ll int mid = (low + high)/2;
- if(possible(mid))
- ans=mid, low=mid+1;
- else
- high=mid-1;
- }
- cout<<ans<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment