Pagini recente » Cod sursa (job #1229299) | Cod sursa (job #943940) | Cod sursa (job #1123904) | Cod sursa (job #796107) | Cod sursa (job #3345045)
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, max_weight;
fin >> n >> max_weight;
vector<vector<int>> dp(n + 1, vector<int>(max_weight + 1, 0));
for (int i = 1; i <= n; i++) {
int weight, price;
fin >> weight >> price;
for (int j = 0; j <= max_weight; j++) {
if (j < weight) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight] + price);
}
}
}
fout << dp[n][max_weight];
fout.close();
fin.close();
}