Cod sursa(job #1773262)
| Utilizator | Data | 7 octombrie 2016 18:15:18 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include<stdio.h>
using namespace std;
int w[5001], p[5001];
int best[10001];
int main()
{
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
int n, g;
scanf("%d %d", &n, &g);
for(int i=1; i<=n; ++i)
scanf("%d %d", &w[i], &p[i]);
best[0]=0;
int max=0;
for(int i=1; i<=n; ++i)
for(int j=g-w[i]; j>=0; --j)
{
if(best[j+w[i]]<best[j]+p[i])
{
best[j+w[i]]=best[j]+p[i];
if(best[j+w[i]]>max)
max=best[j+w[i]];
}
}
printf("%d", max);
return 0;
}
