Cod sursa(job #1513592)
| Utilizator | Data | 29 octombrie 2015 19:01:47 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.9 kb |
#include <cstdio>
#include <algorithm>
using namespace std;
const int DMAX=10005;
int d[DMAX];
int main()
{
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
int n, gmax, g, p, i, j, dr, pmax;
scanf("%d%d", &n, &gmax);
for(i=1; i<=gmax; i++)
d[i]=-1;
dr=pmax=0;
for(i=1; i<=n; i++)
{
scanf("%d%d", &g, &p);
for(j=dr; j>=0; j--)
{
if (j+g<=gmax)
{
if (d[j]!=-1)
{
if (d[j+g]<d[j]+p)
{
d[j+g]=d[j]+p;
if (j+g>dr)
dr=j+g;
}
}
}
}
}
pmax=0;
for(i=1; i<=gmax; i++)
if(pmax<d[i])
pmax=d[i];
printf("%d\n", pmax);
return 0;
}
