Pagini recente » Cod sursa (job #213806) | Cod sursa (job #875529) | Cod sursa (job #277027) | Cod sursa (job #3249183) | Cod sursa (job #501346)
Cod sursa(job #501346)
#include<stdio.h>
int x[16005],N,K,s;
int trans (int c)
{
int i,nt=0,V=0;
for (i=1;i<=N;i++)
if (x[i]>c) return K+1;
else
{
if (V+x[i]<=c) V+=x[i];
else
{
V=x[i];
nt++;
}
}
if (V<c) nt++;
return nt;
}
int bsd()
{
int st,dr,med,last=-1;
st=1;
dr=s;
while (st<=dr)
{
med=st+(dr-st)/2;
if (trans(med)<=K)
{
dr=med-1;
last=med;
}
else st=med+1;
}
return last;
}
int main()
{
freopen("transport.in","r",stdin);
freopen("transport.out","w",stdout);
int i;
scanf("%d%d",&N,&K);
for (i=1;i<=N;i++)
{
scanf("%d",&x[i]);
s+=x[i];
}
printf("%d",bsd());
return 0;
}