Cod sursa(job #1199818)

Utilizator howsiweiHow Si Wei howsiwei Data 20 iunie 2014 19:03:34
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	freopen("rucsac.in", "r", stdin);
	freopen("rucsac.out", "w", stdout);
	int n, gold;
	cin >> n >> gold;
	vector<int> weights(n);
	vector<int> profits(n);
	for (int i = 0; i < n; i++) {
		cin >> weights[i] >> profits[i];
	}
	vector<int> totalprofits(gold+1);
	for (int i = 0; i < n; i++) {
		for (int j = gold; j >= 0; j--) {
			if (j >= weights[i]) {
				totalprofits[j] = max(totalprofits[j], totalprofits[j-weights[i]]+profits[i]);
			}
		}
	}
	printf("%d\n", totalprofits[gold]);
	return 0;
}