Pagini recente » Cod sursa (job #506863) | Cod sursa (job #2407299) | Cod sursa (job #1259035) | Cod sursa (job #917427) | Cod sursa (job #3184465)
#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;
const int oo = -0x3f3f3f3f;
int dp[WMAX];
int main()
{
fin >> n >> g;
memset(dp, -1, sizeof(dp));
dp[0] = 0;
int vMax = 0;
for (int i = 0; i < n; ++i)
{
fin >> w >> p;
for (int j = g; j >= 0; --j)
{
int prev = j - w;
if (prev >= 0 && dp[prev] + p > dp[j])
dp[j] = dp[prev] + p;
if (dp[j] != oo && dp[j] > vMax)
vMax = dp[j];
}
}
fout << vMax << '\n';
return 0;
}