Pagini recente » Cod sursa (job #3363776) | Cod sursa (job #3364138) | Cod sursa (job #3364368) | Cod sursa (job #3364547) | Cod sursa (job #3364567)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int nmax = 5000;
const int gmax = 10000;
int n, g, w[nmax + 5], p[nmax + 5], dp[3][gmax + 5];
// dp[0][w] = 0
// dp[1][w] =
// dp[2][w] =
// dp[3][w] =
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 j = 1; j <= g; j++)
{
dp[i % 2][j] = dp[(i - 1) % 2][j];
if (w[i] <= j)
{
dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][j - w[i]] + p[i]);
}
}
}
fout << dp[n % 2][g];
return 0;
}