Ankit_132

D

Sep 9th, 2023
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5. #define ll long long
  6.  
  7. int main()
  8. {
  9.     ll int n, m;
  10.     cin>>n>>m;
  11.  
  12.     vector<ll int> a(n);
  13.     for(auto &e: a)
  14.       cin>>e;
  15.  
  16.     ll int ans=1e18;
  17.     ll int low=1, high=1e18;
  18.  
  19.     auto check = [&](ll int x)
  20.     {
  21.         ll int temp = 1;
  22.         ll int sum = 0;
  23.  
  24.         for(auto e: a)
  25.         {
  26.             if(e > x)       return false;
  27.  
  28.             sum += e + (sum>0);
  29.  
  30.             if(sum > x)
  31.             {
  32.                 sum = e;
  33.                 temp++;
  34.             }
  35.         }
  36.  
  37.         return (temp <= m);
  38.     };
  39.  
  40.     while(low <= high)
  41.     {
  42.         ll int mid = (low + high)/2;
  43.  
  44.         if(check(mid))
  45.             ans=mid, high=mid-1;
  46.         else
  47.             low=mid+1;
  48.     }
  49.  
  50.     cout<<ans<<"\n";
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment