Pagini recente » Cod sursa (job #2601523) | Cod sursa (job #3183533) | Cod sursa (job #2222002) | Cod sursa (job #2119398) | Cod sursa (job #3139807)
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n; fin >> n;
int max_weight; fin >> max_weight;
vector<pair<int, int>> objects(n);
for (int i = 0; i < n; ++i) {
fin >> objects[i].first;
fin >> objects[i].second;
}
vector<vector<int>> dp(2, vector<int>(max_weight + 1));
for (int j = objects[0].first; j <= max_weight; ++j) {
dp[0][j] = objects[0].second;
}
for (int i = 1; i < n; ++i) {
int w = objects[i].first;
int p = objects[i].second;
for (int j = 0; j < w; ++j) {
dp[i % 2][j] = dp[(i - 1) % 2][j];
}
for (int j = w; j <= max_weight; ++j) {
dp[i % 2][j] = max(dp[i % 2][j - 1], dp[(i - 1) % 2][j - w] + p);
dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][j]);
}
}
fout << dp[(n - 1) % 2][max_weight];
return 0;
}