Pagini recente » Cod sursa (job #1333360) | Cod sursa (job #1399080) | Cod sursa (job #2841764) | Cod sursa (job #1058499) | Cod sursa (job #1777130)
#include <bits/stdc++.h>
using namespace std;
int n,k,best;
vector<int> V;
ifstream fin("transport.in");
ofstream fout("transport.out");
int main()
{
fin>>n>>k;
V.resize(n+1);
V[0]=-1;
int left0=INT_MIN,right0=0;
for(int i=1;i<=n;i+=1){
fin>>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;
}
}
fout<<best;
return 0;
}