Cod sursa(job #497068)

Utilizator caliuxSegarceanu Calin caliux Data 1 noiembrie 2010 16:16:56
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<stdio.h>
long x[16005],n,k,s2;
int ok(int c)
{
	int u,s,i,nt=0,l;
	u=1;
	s=0;
	for (i=1;i<=n;i++)
	{
		l=c;
		if (x[i]>c) return k+1;
			else
			{
				if (s+x[i]<=c)
					s=s+x[i];
				else
				{
					nt++;
					s=x[i];
				}
			}
	}
	if (s<c) nt++;
	return nt;
}
int cb()
{
	int st,dr,med,last=-1;
	st=1;
	dr=s2;
	while (st<=dr)
	{
		med=(st+dr)/2;
		if (ok(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);
	scanf("%ld%ld",&n,&k);
	int i;
	for (i=1;i<=n;i++)
	{
		scanf("%ld",&x[i]);
		s2=s2+x[i];
	}
	printf("%d",cb());
}