Cod sursa(job #695214)

Utilizator federerUAIC-Padurariu-Cristian federer Data 28 februarie 2012 11:13:26
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include<fstream>
#define Wmax 5001
#define Gmax 1001
#define INF 32001
using namespace std;

int best[Gmax][Wmax], c[Gmax], i, j;
int W, w[Gmax], G;
int main()
{
	freopen("energii.in", "r", stdin);
	freopen("energii.out", "w", stdout);
	scanf("%d%d", &G, &W);
	for(i=1;i<=G;++i)
		scanf("%d%d", &w[i], &c[i]);
	for(i=0;i<=G;++i)
		for(j=1;j<=W;++j)
			best[i][j]=INF;
	for(i=1;i<=G;++i)
		for(j=1;j<=W;++j)
			if(j>=w[i])
				best[i][j]=min(best[i-1][j], best[i-1][j-w[i]]+c[i]);
			else
				best[i][j]=min(best[i-1][j], c[i]);
	if(best[G][W]==INF)
		printf("-1\n");
	else
		printf("%d\n", best[G][W]);
	fclose(stdin);
	fclose(stdout);
	return 0;
}