Cod sursa(job #1052924)

Utilizator thehuntestshadowDragomir Alexandru thehuntestshadow Data 11 decembrie 2013 22:01:39
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;

int a[16001], n, k, sol;

bool check(int q)
{
	int sum = 0, l = k;
	for (int i = 1; i <= n; i++)
	{
		sum += a[i];
		if (q == sum)
		{
			sum = 0;
			l--;
		}
		else if (q<sum)
		{
			i--;
			sum = 0;
			l--;
		}

		if (l == 0)
		{
			if (i == n)
				return true;
			else return false;
		}
	}
	return true;
}

void bynar()
{
	int pz = 0;
	long long t = (1 << 28);
	while (t>0)
	{
		if (check(pz + t)) sol = pz + t;
		else pz += t;
		t >>= 1;
	}
}

int main()
{
	ifstream fin("transport.in");
	ofstream fout("transport.out");
	fin >> n >> k;
	for (int i = 1; i <= n; i++)
		fin >> a[i];

	bynar();
	fout << sol;
}