Cod sursa(job #672572)

Utilizator federerUAIC-Padurariu-Cristian federer Data 2 februarie 2012 16:28:01
Problema Problema rucsacului Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include<fstream>
#define Nmax 5001
#define Gmax 10001
using namespace std;

int best[Gmax][Nmax], c[Nmax], g[Nmax];
int i, j, G, n, k;

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