Pagini recente » Cod sursa (job #1386145) | Cod sursa (job #144294) | Cod sursa (job #1333641) | Cod sursa (job #1277131) | Cod sursa (job #1777128)
#include <bits/stdc++.h>
using namespace std;
int n,k,best;
vector<int> V;
int main()
{
cin>>n>>k;
V.resize(n+1);
V[0]=-1;
int left0=INT_MIN,right0=0;
for(int i=1;i<=n;i+=1){
cin>>V[i];
left0=max(left0,V[i]);
right0+=V[i];
}
while(left0<=right0){
int m=(left0+right0)/2;
int attempts=1;
int howMuchWeCarry=0;
for(int i=1;i<=n;i+=1){
howMuchWeCarry+=V[i];
if(howMuchWeCarry>m){
howMuchWeCarry=V[i];
attempts+=1;
}
}
if(attempts>k)
left0=m+1;
else{
best=m;
right0=m-1;
}
}
cout<<best;
return 0;
}