Pagini recente » Cod sursa (job #2451922) | Cod sursa (job #2872455) | Cod sursa (job #2455230) | Cod sursa (job #2402906) | Cod sursa (job #3184532)
#include <fstream>
#include <string.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int oo = 0x3f3f3f3f;
const int GMAX = 1e4 + 2;
int dp[2][GMAX], n, w, p, g, res;
int main()
{
fin >> n >> g;
memset(dp, -oo, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= n; ++i)
{
fin >> w >> p;
int crtIndex = i & 1, antIndex = crtIndex ^ 1;
for (int j = 0; j <= g; ++j)
{
if (j - w >= 0)
dp[crtIndex][j] = max(dp[antIndex][j], dp[antIndex][j - w] + p);
else
dp[crtIndex][j] = dp[antIndex][j];
if (dp[crtIndex][j] > res)
res = dp[crtIndex][j];
}
}
fout << res << '\n';
return 0;
}