Pagini recente » Cod sursa (job #2055937) | Cod sursa (job #2574150) | Cod sursa (job #338399) | Cod sursa (job #1206992) | Cod sursa (job #2493045)
#include <bits/stdc++.h>
#define NMAX 5005
#define GMAX 10005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int N, G;
int mxProfit[5][GMAX];
int weight[NMAX], profit[NMAX];
int main()
{
fin >> N >> G;
for (int i = 1; i <= N; i++)
fin >> weight[i] >> profit[i];
for (int i = 1; i <= N; i++)
for (int j = 0; j <= G; j++)
{
int row = i % 2;
int antiRow = 1 - row;
mxProfit[row][j] = max(mxProfit[antiRow][j], mxProfit[row][j - 1]);
if (j >= weight[i])
mxProfit[row][j] = max(mxProfit[row][j], mxProfit[antiRow][j - weight[i]] + profit[i]);
}
fout << mxProfit[N % 2][G];
return 0;
}