Pagini recente » Cod sursa (job #1160327) | Cod sursa (job #226343) | Cod sursa (job #1354673) | Cod sursa (job #2607679) | Cod sursa (job #1128499)
#include <fstream>
#include <utility>
using namespace std;
//Constante
const int sz1 = (int)5e3+1;
const int sz2 = (int)1e4+1;
//Definitii
#define weight first
#define cost second
//Variabile
int num, maxWeight;
pair<int, int> object[sz1];
int line1[sz2], line2[sz2];
int *current = line1, *previous = line2;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
//Main
int main()
{
in >> num >> maxWeight;
for(int i=1; i<=num; ++i)
in >> object[i].weight >> object[i].cost;
for(int i=1; i<=num; ++i)
{
swap(current, previous);
for(int j=1; j<object[i].weight; ++j)
current[j] = previous[j];
for(int j=object[i].weight; j<=maxWeight; ++j)
current[j] = max(previous[j], previous[j-object[i].weight]+object[i].cost);
}
out << current[maxWeight] << '\n';
in.close();
out.close();
return 0;
}