Cod sursa(job #876974)

Utilizator fhandreiAndrei Hareza fhandrei Data 12 februarie 2013 14:02:29
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
// Include
#include <fstream>
#include <memory.h>
using namespace std;

// Definitii
#define obj pair<int, int>
#define weight first
#define cost second

// Constante
const int sz = 5001;

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

int num;
int maxW;
obj object[sz];

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