Pagini recente » Cod sursa (job #784170) | Cod sursa (job #1188072) | Cod sursa (job #720125) | Cod sursa (job #210816) | Cod sursa (job #876974)
Cod sursa(job #876974)
// 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;
}