Pagini recente » Cod sursa (job #2749500) | Cod sursa (job #535753) | Cod sursa (job #1850506) | Cod sursa (job #978243) | Cod sursa (job #902699)
Cod sursa(job #902699)
// 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;
}