Cod sursa(job #533956)

Utilizator Catah15Catalin Haidau Catah15 Data 14 februarie 2011 21:16:46
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#define MAXG 1010
#define MAXW 5010
#define INF 999999
using namespace std;

int eg[MAXG], cg[MAXG], sol[MAXG][MAXW];

int main()
{
	ifstream f("energii.in");
	ofstream g("energii.out");
	
	int G, w;
	
	f >> G >> w;
	
	for(int i = 1; i <= G; ++i)
		f >> eg[i] >> cg[i];
	
	for(int i = 1; i <= w; ++i)
		sol[0][i] = INF;
	
	for(int i = 1; i <= G; ++i)
		for(int j = 1; j <= w; ++j)
			if(j < eg[i])
				sol[i][j] = min( sol[i - 1][j], cg[i] );
			else
				sol[i][j] = min( sol[i - 1][j], sol[i - 1][j - eg[i]] + cg[i] );
			
	if(sol[G][w] == INF)
		g << "-1";
	else
		g << sol[G][w];
	
	f.close();
	g.close();
	
	return 0;
}