Pagini recente » Cod sursa (job #882430) | Cod sursa (job #2959785) | Cod sursa (job #690586) | Cod sursa (job #199594) | Cod sursa (job #1525801)
#include <iostream>
#include <fstream>
using namespace std;
int k,n;
int v[16001];
bool check(int cap){
int size = 0, steps = 0, i = 1;
while(steps <= k && i <= n){
steps++;
while(size + v[i] <= cap && i<=n){
size+=v[i];
i++;
}
size = 0;
}
if(steps <= k && i>=n)
return true;
return false;
}
int binary(){
int step = 1;
while(step < 1048576)
step<<=1;
int i, lastGood =0;
for(i = 0; step != 0; step>>=1)
if(check(i+step))
lastGood = i+step;
else
i+=step;
return lastGood;
}
int main()
{
ifstream fin("transport.in");
ofstream fout("transport.out");
fin>>n>>k;
for(int i=1; i<=n; i++)
fin>>v[i];
fout<<binary();
return 0;
}