Pagini recente » Cod sursa (job #1924780) | Cod sursa (job #2282721)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
int maxx,st,dr,mij,v[1000009],n,k,ctr,cmij,s;
int main()
{
f >> n>>k;
for (int i=1;i<=n;i++) {
f >> v[i];
s+=v[i];
if (v[i]>maxx)
maxx=v[i];
}
st=maxx;
dr=s;
mij=(st+dr)/2;
while (st!=mij && dr!=mij) {
cmij=mij;
ctr=0;
for (int j=1;j<=n;j++){
if (v[j]<cmij)
cmij-=v[j];
else {
ctr++;
cmij=mij;
cmij-=v[j];
}
}
if (ctr<k)
dr=mij;
else
st=mij;
mij=(st+dr)/2;
}
g<< mij;
return 0;
}