Pagini recente » Cod sursa (job #2650720) | Cod sursa (job #2422173) | Cod sursa (job #2322410) | Cod sursa (job #2941781) | Cod sursa (job #988523)
Cod sursa(job #988523)
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#define MAXN 5002
#define MAXG 10002
using namespace std;
struct Object
{
int w = 0;
int v = 0;
};
bool operator < (const Object lhs, const Object rhs)
{
return lhs.w > rhs.w;
}
Object vObjs[MAXN];
int knapsack[MAXG];
int main()
{
int n, g;
fstream fin("rucsac.in", fstream::in);
fstream fout("rucsac.out", fstream::out);
fin >> n >> g;
//cout << n << " " << g << endl;
for (int i=0; i<n; ++i)
{
fin >> vObjs[i].w >> vObjs[i].v;
//cout << vObjs[i].w << " " << vObjs[i].v << endl;
}
sort(begin(vObjs), begin(vObjs) + n);
for (int i=0; i<n; ++i)
{
for (int j=g; j>=vObjs[i].w; --j)
{
knapsack[j] = max(knapsack[j], vObjs[i].v + knapsack[j - vObjs[i].w]);
}
}
fout << knapsack[g] << "\n";
return 0;
}