Pagini recente » Cod sursa (job #2814088) | Cod sursa (job #287677) | Cod sursa (job #2305255) | Cod sursa (job #2962222) | Cod sursa (job #2193510)
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
const int NMAX = 1e4 + 5;
int a[NMAX];
int main()
{
int n, G;
f >> n >> G;
for(int i = 1; i <= n; i++)
{
int w, p;
f >> w;
f >> p;
for(int j = G; j >= 1; j--)
if(a[j] > 0 && j + w <= G && a[j + w] < a[j] + p)
a[j + w] = a[j] + p;
if(a[w] < p)
a[w] = p;
}
int sol = 0;
for(int i = G; i >= 1; i--)
if(a[i] > sol)
sol = a[i];
g << sol;
}