Cod sursa(job #750608)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 22 mai 2012 18:02:34
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

ifstream in("energii.in");
ofstream out("energii.out");

const int inf = 1 << 30;

int g, w, sol = inf;
int best[5050];

int main()
{
	int i, a, b;
	
	in >> g >> w;
	
	memset(best, -1, sizeof(best));
	best[0] = 0;
	
	while(g--){
		in >> a >> b;
		
		for(i = w - 1; i >= 0; --i)
			if(best[i] != -1)
				if(i + a < w){
					if((best[i] + b < best[i + a]) || (best[i + a] == -1))
						best[i + a] = best[i] + b;
				}
				else 
					if(best[i] + b < sol)
							sol = best[i] + b;
	}
	
	out << (sol == inf ? -1 : sol);
	
	return 0;
}