Pagini recente » Cod sursa (job #870220) | Cod sursa (job #323450) | Borderou de evaluare (job #165641) | Cod sursa (job #2506229) | Cod sursa (job #3284365)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, W;
fin >> n >> W;
vector<vector<int>> dp(n+1, vector<int>(W + 1));
vector<pair<int, int>> objects(n);
for (int i = 0; i < n; ++i) {
fin >> objects[i].first >> objects[i].second;
}
for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) {
if (w - objects[i - 1].first >= 0) {
dp[i][w] = max(dp[i-1][w], dp[i-1][w - objects[i-1].first] + objects[i-1].second);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
fout << dp[n][W];
return 0;
}