Cod sursa(job #2672568)

Utilizator NeoxDragos Stefan Neox Data 14 noiembrie 2020 11:06:46
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>
using namespace std;

const int nmax = 5000, gmax=10000;
int i, j, n, g, p[nmax], w[nmax], dp[2][gmax];
bool k;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
	fin >>n >> g;
	for(i=1; i<=n; i++)
		fin >> w[i] >> p[i];
	for(i=1; i<=n; i++) {
		for(j=1; j<w[i]; j++)
			dp[k][j] = dp[!k][j];
		for(j=w[i]; j<=g; j++)
			dp[k][j] = max(dp[!k][j], dp[!k][j-w[i]]+p[i]);
		k=!k;
	}
	k=!k;
	fout << dp[k][g];
	return 0;
}