Pagini recente » Cod sursa (job #370950) | Cod sursa (job #2863178) | Cod sursa (job #1758759) | Cod sursa (job #2702745) | Cod sursa (job #2047341)
#include <bits/stdc++.h>
using namespace std;
const int GMAX = 10000 + 5;
const int NMAX = 5000 + 5;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int N, G;
int w[GMAX], p[GMAX];
int dp[2][GMAX];
bool line;
void read()
{
fin >> N >> G;
for (int i = 1; i <= N; ++i)
fin >> w[i] >> p[i];
}
int main()
{
read();
for (int i = 1; i <= N; ++i, line = 1 - line)
{
for (int j = 0; j <= G; ++j)
{
dp[line][j] = dp[1 - line][j];
if (j >= w[i])
dp[line][j] = max(dp[1 - line][j], dp[1 - line][j - w[i]] + p[i]);
}
}
int maxim = dp[1 - line][0];
for (int i = 1; i <= G; ++i)
maxim = max(maxim, dp[1 - line][i]);
fout << maxim;
return 0;
}