Cod sursa(job #1021644)

Utilizator vld7Campeanu Vlad vld7 Data 4 noiembrie 2013 01:02:19
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>

using namespace std;

ifstream f("transport.in");
ofstream g("transport.out");

const int MAX_N = 16005;

int n, k, a[MAX_N];

bool isGood (int val) {
	int crt_sum = 0, trans = 1;
	
	for (int i = 1; i <= n; i++)
		if (crt_sum + a[i] <= val)
			crt_sum += a[i];
		else if (a[i] <= val)
			crt_sum = a[i], trans++;
		else
			return 0;
	
	if (trans <= k)
		return 1;
	return 0;
}

int main() {
	f >> n >> k;
	for (int i = 1; i <= n; i++)
		f >> a[i];
	
	int lo = 1, hi = MAX_N * MAX_N, mid;
	while (lo <= hi) {
		mid = (lo + hi) / 2;
		if (isGood (mid))
			hi = mid - 1;
		else
			lo = mid + 1;
	}
	
	if (!isGood (mid))
		mid++;
	
	g << mid << '\n';
	
	return 0;
}