Pagini recente » Cod sursa (job #3327016) | Cod sursa (job #3299909) | Cod sursa (job #3318085) | Cod sursa (job #3354581)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, w;
fin >> n >> w;
vector<pair<int, int>> items;
items.push_back({0, 0});
for (int i = 0; i < n; i++) {
int x, y;
fin >> x >> y;
items.push_back({x, y});
}
vector<int> dp(w + 1, 0);
for (int i = 1; i <= n; i++) {
auto [weight, price] = items[i];
for (int j = w; j >= 0; j--) {
int x = j - weight;
if (x >= 0) {
dp[j] = max(dp[j], dp[x] + price);
}
}
}
fout << dp[w] << ' ';
fin.close();
fout.close();
return 0;
}