Cod sursa(job #2632898)

Utilizator StefanSanStanescu Stefan StefanSan Data 5 iulie 2020 14:29:44
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include      <iostream>
#include      <fstream>
#include      <algorithm>

using namespace std;

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

int v[5000], w[5000], n, W;
int mat[5000][5000]; 

int kn(int n, int c) {
	if (mat[n][c]) return mat[n][c];
	else {
		if (n == 0 || c == 0) mat[n][c] = -1;
		else {
			if (w[n] > c) kn(n - 1, c);
			else {
				int case1 = kn(n - 1, c);
				int case2 = v[n] + kn(n - 1, c - w[n]);
				mat[n][c] = max(case1, case2);
			}
		}
	}

}

int main() {

	ios_base::sync_with_stdio(false);
	in.tie(NULL); out.tie(NULL);

	in >> n >> W;
	v[0] = 0;
	for (int i = 1; i <= n; i++)in >> w[i] >> v[i];
	out << kn(n, W);

}