Cod sursa(job #198630)

Utilizator hadesgamesTache Alexandru hadesgames Data 13 iulie 2008 12:12:56
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#include <sstream>
#include <fstream>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define ss second
#define all(c) c.begin(),c.end()
#define pf push_front
#define popb pop_back
#define popf pop_front
stack<int> a;
int k;
bool ver(int x)
{
	stack<int>b(a);
	int s=0,nr=0;
	while (!b.empty())
	{
		if (b.top()>x)
			return false;
		if (s+b.top()<=x)
			s+=b.top();
		else
		{
			s=b.top();
			nr++;
		}
		b.pop();
	}
	if (nr>=k)
		return false;
	return true;
	
}
vector<int> c;
int main()
{
	int n,u=0,x,i;
	ifstream in("transport.in");
	ofstream out("transport.out");
	in>>n>>k;
	for(i=1;i<=n;i++)
	{
		in>>x;
		u+=x;
		c.pb(x);
	}
	for(i=n-1;i>=0;i--)
		a.push(c[i]);
	for (u=0,i=30;i>=0;i--)
	{
		u+=1<<i;
		if (ver(u))
			u-=1<<i;
	}
	out<<u+1;
	in.close();
	out.close();
	return 0;
}