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