Cod sursa(job #482327)

Utilizator Bogdan_tmmTirca Bogdan Bogdan_tmm Data 3 septembrie 2010 10:03:24
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include<algorithm>
using namespace std;
int n,k,i,s,step;
int st[16002];

bool solve(int C)
{
	int i,j,c,nr=0;
	for(i=1;i<=n;)
	{
		j=i;	c=st[i];
		while(j<=n&&c<=C)
		{
			j++;
			c+=st[j];
		}
		if(j==i)
			return 0;
		if(++nr>k)
			return 0;
		i=j;
	}
	return 1;
}

int main()
{
	freopen("transport.in","r",stdin);
	freopen("transport.out","w",stdout);

	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
		scanf("%d",&st[i]),s+=st[i];

	for(step=1;step<s;(step<<=1));
	for(i=s;step;(step>>=1))
	{
		if(i-step<=0)
			continue;
		if(solve(i-step))
			i-=step;
	}
	printf("%d\n",i);

	return 0;
}