Pagini recente » Cod sursa (job #1164065) | Cod sursa (job #2518701) | Cod sursa (job #920255) | Borderou de evaluare (job #133046) | Cod sursa (job #3241334)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int MAX_WEIGHT = 10005;
const int MAX_OBJECTS = 5005;
int n, g;
int w[MAX_OBJECTS], p[MAX_OBJECTS];
vector<int> maxProfit(MAX_WEIGHT, 0);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
fin >> n >> g;
for (int i = 0; i < n; ++i) {
fin >> w[i] >> p[i];
}
for (int i = 0; i < n; ++i) {
for (int j = g; j >= 0; --j) {
if (j + w[i] <= g) {
maxProfit[j + w[i]] = max(maxProfit[j + w[i]], maxProfit[j] + p[i]);
}
}
}
fout << *max_element(maxProfit.begin(), maxProfit.end());
return 0;
}