Cod sursa(job #732297)

Utilizator feelshiftFeelshift feelshift Data 10 aprilie 2012 10:00:47
Problema Problema rucsacului Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
using namespace std;

#define weight first
#define cost second

const int MAXSIZE = 5000;
const int MAXWEIGHT = 10000;

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

int objects,capacity;
int best[MAXWEIGHT+1][MAXWEIGHT+1];
pair<int,int> object[MAXSIZE+1];

int main() {
	in >> objects >> capacity;

	for(int i=1;i<=objects;i++)
		in >> object[i].weight >> object[i].cost;

	for(int i=1;i<=objects;i++) {
		for(int k=0;k<=capacity;k++) {
			best[i][k] = best[i-1][k];

			if(object[i].weight <= k)
				best[i][k] = max(best[i][k],best[i-1][k - object[i].weight] + object[i].cost);
		}
	}

	out << best[objects][capacity] << "\n";

	return (0);
}