Pagini recente » Cod sursa (job #610123) | Cod sursa (job #1301218) | Cod sursa (job #3267849) | Cod sursa (job #540699) | Cod sursa (job #2970627)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
struct object{
int p;
int g;
};
object a[100000];
int profit[100000];
int main() {
int n, g;
in >> n >> g;
for (int i = 1; i <= n; i++) {
in >> a[i].g >> a[i].p;
}
for (int j = 1; j <= g; j++) {
profit[j] = -1;
}
profit[0] = 0;
for (int i = 0; i <= n; i++) {
for (int j = g - a[i].g; j >= 0; j--) {
if (profit[j] != -1 && profit[j] + a[i].p > profit[j + a[i].g]) {
profit[j + a[i].g] = profit[j] + a[i].p;
}
}
}
int max = 0;
for (int j = 1; j <= g; j++) {
if (profit[j] > max) max = profit[j];
}
out << max;
}