Cod sursa(job #2378131)

Utilizator Paulet.StefanPauletStefan Paulet.Stefan Data 11 martie 2019 18:30:24
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#define GMAX 10005
#define NMAX 5005
using namespace std;

ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

int g[NMAX], c[NMAX], cmax[GMAX];
int n, greut;
int main()
{
	int i, j, costmax = 0;
    fin >> n >> greut;
    for (i = 1; i <= n; i ++)
    	fin >> g[i] >> c[i];
	for (i = 1; i <= n; i ++)
	{
		for (j = greut - g[i]; j >= 0; j --)
			cmax[j + g[i]] = max(cmax[j + g[i]], cmax[j] + c[i]);
		//cmax[g[i]] = max(cmax[g[i]], c[i]);
	}
	for (i = 1; i <= greut; i ++)
		costmax = max(costmax, cmax[i]);
	fout << costmax << '\n';
    return 0;
}