fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  5. int n,k;
  6. vector<ll>a;
  7. bool check(ll x){
  8. int c=1;
  9. ll s=0;
  10. for(int i=0;i<n;i++){
  11. s+=a[i];
  12. if(s>x){
  13. c++;
  14. s=a[i];
  15. }
  16. }
  17. return c<=k;
  18. }
  19. int main(){
  20. fast;
  21. cin>>n>>k;
  22. a.resize(n);
  23. ll l=0,r=0;
  24. for(int i=0;i<n;i++){
  25. cin>>a[i];
  26. l=max(l,a[i]);
  27. r+=a[i];
  28. }
  29. ll ans=r;
  30. while(l<=r){
  31. ll mid=l+(r-l)/2;
  32. if(check(mid)){
  33. ans=mid;
  34. r=mid-1;
  35. }else{
  36. l=mid+1;
  37. }
  38. }
  39. cout<<ans;
  40. return 0;
  41. }
Success #stdin #stdout 0s 5296KB
stdin
5 2
5 4 3 2 1
stdout
9