Pagini recente » Cod sursa (job #1353496) | Cod sursa (job #11557) | Cod sursa (job #2401092) | Cod sursa (job #1389184) | Cod sursa (job #2550559)
#define MAX_N 5000
#define MAX_G 10000
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, gMax, G[MAX_N + 1], V[MAX_N + 1], PD[2][MAX_G + 1];
int main()
{
fin >> n >> gMax;
for (int i = 1; i <= n; ++i)
{
fin >> G[i] >> V[i];
}
int x = 1;
for (int i = 1; i <= n; ++i)
{
for (int g = 0; g <= gMax; ++g)
{
//PD[x][g] = PD[1 - x][g];
PD[x][g] = max(PD[x][g], PD[1 - x][g]);
if ((g + G[i]) <= gMax)
{
PD[x][g + G[i]] = max(PD[1 - x][g] + V[i], PD[1 - x][g + G[i]]);
}
}
x = 1 - x;
}
fout << PD[1 - x][gMax];
fin.close();
fout.close();
return 0;
}