Pagini recente » Cod sursa (job #1512915) | Cod sursa (job #2305631) | Cod sursa (job #837314) | Cod sursa (job #2098614) | Cod sursa (job #2472429)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int N = 5001;
const int GMAX = 10001;
int n, G, profit[GMAX], p[N], g[N];
int main()
{
in>>n>>G;
for(int i=1; i<=n; i++)
in>>g[i]>>p[i];
for(int j=1; j<=G; j++)
profit[j] = -1;
for(int i=1; i<=n; i++)
{
for(int j = G-g[i]; j>=0; j--)
{
if(profit[j] != -1 && profit[j] + p[i] > profit[j + g[i]])
profit[j + g[i]] = profit[j] + p[i];
}
}
out<<profit[G];
return 0;
}