Cod sursa(job #1426221)
| Utilizator | Data | 29 aprilie 2015 10:10:36 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1 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;
}
}
}
}
}
if (d[gmax]!=-1)
printf("%d\n", d[gmax]);
else
{
for(i=1; i<=n; i++)
{
if (d[i]>pmax)
pmax=d[i];
}
printf("%d\n", pmax);
}
return 0;
}
