Pagini recente » Cod sursa (job #2907251) | Cod sursa (job #1013224) | Cod sursa (job #1316769) | Cod sursa (job #1414580) | Cod sursa (job #3345028)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
int n, g, weight, profit;
fin >> n >> g;
vector<vector<int>> dp(2, vector<int>(g + 1, 0));
for (int i = 1; i <= n; i++) {
fin >> weight >> profit;
for (int j = 1; j <= g; j++) {
if (j >= weight) {
dp[1][j] = max(dp[0][j - weight] + profit, dp[0][j]);
} else {
dp[1][j] = dp[0][j];
}
}
for (int j = 0; j < g; j++) {
dp[0][j] = dp[1][j];
}
}
fout << dp[1][g];
return 0;
}