Cod sursa(job #1469215)

Utilizator o_micBianca Costin o_mic Data 7 august 2015 18:25:32
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 10005
#define LL long long
using namespace std;

int pos[MAX];
int prof[MAX];

int main() {
	ifstream fin("rucsac.in");
	ofstream fout("rucsac.out");
	int n, g, x, maxv = 0;
	vector <int> w, p;
	fin >> n >> g;
	for (int i = 0; i < n; ++i) {
		fin >> x;
		w.push_back(x);
		fin >> x;
		p.push_back(x);
	}
	pos[0] = 1;
	prof[0] = 0;
	for(int i = 0; i < n; ++i) {
		for(int j = g - w[i]; j >= 0; --j) {
			if(pos[j]) {
				pos[j + w[i]] = 1;
				prof[j + w[i]] = max(prof[j+w[i]], prof[j] + p[i]);
				maxv = max(prof[j + w[i]], maxv);
			}
		}
	}
	fout << maxv;
	return 0;
}