Cod sursa(job #902699)

Utilizator fhandreiAndrei Hareza fhandrei Data 1 martie 2013 16:13:53
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
// Include
#include <fstream>
#include <algorithm>
#include <utility>
using namespace std;

// Definitii
#define type_object pair<int, int>
#define wgh first
#define cost second

// Constante
const int sz = 5001;

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

int num, maxWgh;
type_object object[sz];

// Main
int main()
{
	in >> num >> maxWgh;
	
	for(int i=1 ; i<=num ; ++i)
		in >> object[i].wgh >> object[i].cost;
	
	int *previous = (int*) calloc(maxWgh+1, sizeof(int));
	int *current = (int*) calloc(maxWgh+1, sizeof(int));
	
	for(int i=1 ; i<=num ; ++i)
	{
		for(int j=1 ; j<=maxWgh ; ++j)
			if(j < object[i].wgh)
				current[j] = previous[j];
			else
				current[j] = max(previous[j], previous[j-object[i].wgh] + object[i].cost);
		swap(previous, current);
	}
	
	swap(previous, current);
	
	out << current[maxWgh] << '\n';
	
	in.close();
	out.close();
	return 0;
}