Cod sursa(job #1813662)

Utilizator tonisnakesBoar Antonio tonisnakes Data 23 noiembrie 2016 09:53:05
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#define NMAX 5005
#define GMAX 10005
using namespace std;

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

int opt[NMAX][GMAX];

int main () 
{
	int n, v;
	fin >> n >> v;
	for (int i = 0; i <= n; ++i) {
		for (int j = 1; j <= v; ++j) {
			opt[i][j] = -1;
		}
	}
	int maxn = 0;
	for (int i = 1; i <= n; ++i) {
		int x, y;
		fin >> x >> y;
		for (int j = x; j <= v; ++j) {
			if (opt[i-1][j-x] != -1) {
				opt[i][j] = opt[i-1][j-x] + y;
			}
			opt[i][j] = max(opt[i][j],opt[i-1][j]);
			if (opt[i][j] > maxn) {
				maxn = opt[i][j];
			}
		}
	}
	/*cout << endl;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= v; ++j) {
			cout << opt[i][j] << " ";
		}
		cout << endl;
	}*/
	fout << maxn;
	
	return 0;
}