Cod sursa(job #202028)

Utilizator piroslPiros Lucian pirosl Data 5 august 2008 17:47:55
Problema Energii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include<fstream>
#include<iostream>
using namespace std;

int g;
int w;
int e[1001];
int c[1001];
long costuri[10000001];

int main(void)
{
	freopen("energii.in", "r", stdin);
	freopen("energii.out", "w", stdout);
	cin >> g;
	cin >> w;
	long max = 0;
	for(int i=0;i<g;++i)
	{
		cin >> e[i] >> c[i];
		max += e[i];
	}

	memset(costuri, -1, sizeof(costuri));
	costuri[0] = 0;
	long min = 20000001;
	for(int j=0;j<g;++j)
	{
		for(long i = max; i>=1; --i)
		{
			if(e[j] <= i)
			{
				if(costuri[i-e[j]] > -1)
				{
					if(costuri[i] == -1)
						costuri[i] = costuri[i-e[j]] + c[j];
					else
						if(costuri[i] < costuri[i-e[j]] + c[j])
							costuri[i] = costuri[i-e[j]] + c[j];
				}
			}else
			{
				if(costuri[i] == -1)
						costuri[i] = c[j];
					else
						if(costuri[i] < c[j])
							costuri[i] = c[j];
			}
			if(i>=w && costuri[i] != -1)
				if(costuri[i] < min)
					min = costuri[i];
		}
	}
	
	if(min == 20000001)
		cout << -1 << endl;
	else
		cout << min << endl;

	return 0;
}