Pagini recente » Istoria paginii utilizator/cristian_stratulat | Istoria paginii runda/night_time_contest2 | Istoria paginii runda/contest_3 | Cod sursa (job #1917967) | Cod sursa (job #1253487)
#include <fstream>
#include <algorithm>
using namespace std;
const int MAXN = 5005;
const int MAXG = 10005;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, g;
int dp[MAXN][MAXG], W[MAXN], P[MAXN];
int main()
{
fin >> n >> g;
for (int i = 1; i <= n; ++i)
fin >> W[i] >> P[i];
for (int i = 1; i <= n; ++i)
{
for (int cw = 0; cw <= g; ++cw)
{
dp[i][cw] = dp[i - 1][cw];
if (W[i] <= cw)
dp[i][cw] = max(dp[i][cw], dp[i - 1][cw - W[i]] + P[i]);
}
}
fout << dp[n][g];
fin.close(); fout.close();
return 0;
}