Cod sursa(job #1453429)

Utilizator PetruZZatic Petru PetruZ Data 23 iunie 2015 15:02:25
Problema Transport Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.57 kb
#include <iostream>

using namespace std;

int a[16002];
int n, k, s, mn, m, h;

bool pr(int l)
{
	int h=0, j=0;
	for (int i=0; i<n; i++)
		{
		h+=a[i];
		if (h>=l) h=a[i], j++;
	}
	return(j>=k);
}
int main ()
{
	freopen("transport.in","r",stdin);
	freopen("transport.out","w",stdout);
	
	
	cin >> n >> k;

	for (int i=0; i<n; i++)
	{
		cin >> a[i];
		s+=a[i];
		if (a[i]>mn) mn=a[i];
	}

    bool u;
	while(mn!=s)
	{
		u=false;
		m=(mn+s)/2;
		if(pr(m)) mn=m+1, u=true;
		else s=m; 
	}
	if (u) cout << mn-1;
	else cout << mn;

return 0;
}