Pagini recente » Cod sursa (job #802958) | Cod sursa (job #2434398) | Cod sursa (job #2549639) | Cod sursa (job #2921575) | Cod sursa (job #2589700)
/** Complexitate
O(N * G)
**/
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
struct object {
int weight, val;
} objects[5001];
long long n, maxWeight, ans[10001];
int main()
{
in >> n >> maxWeight;
for(int i = 1; i <= n; i++) {
in >> objects[i].weight >> objects[i].val;
}
long long finalAns = -1;
for(int i = 1; i <= n; i++) {
for(int j = maxWeight - objects[i].weight; j >= 0; j--) {
ans[j + objects[i].weight] = max(ans[j + objects[i].weight], ans[j] + objects[i].val);
finalAns = max(finalAns, ans[j + objects[i].weight]);
}
}
out << finalAns;
return 0;
}