Pagini recente » Cod sursa (job #74384) | Cod sursa (job #1990196) | Cod sursa (job #601239) | Cod sursa (job #2568135) | Cod sursa (job #3139802)
#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(n, 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][j] = dp[i - 1][j];
}
for (int j = w; j <= max_weight; ++j) {
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - w] + p);
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
}
}
fout << dp[n - 1][max_weight];
return 0;
}