Pagini recente » Cod sursa (job #2735481) | Cod sursa (job #2534987) | Cod sursa (job #2500383) | Cod sursa (job #1358552) | Cod sursa (job #3184461)
#include <fstream>
#include <string.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, g, w, p;
const int WMAX = 1e4 + 1;
int dp[2][WMAX];
int main()
{
fin >> n >> g;
memset(dp, -1, sizeof(dp));
dp[1][0] = 0;
dp[0][0] = 0;
int vMax = 0;
for (int i = 0; i < n; ++i)
{
int crtIndex = i % 2, antIndex = 1 - crtIndex;
fin >> w >> p;
for (int j = g; j >= 0; --j)
{
dp[crtIndex][j] = dp[antIndex][j];
int prev = j - w;
if (prev >= 0 && dp[antIndex][prev] != -1 && dp[antIndex][prev] + p > dp[crtIndex][j])
dp[crtIndex][j] = dp[antIndex][prev] + p;
if (dp[crtIndex][j] > vMax)
vMax = dp[crtIndex][j];
}
}
fout << vMax << '\n';
return 0;
}