Pagini recente » Cod sursa (job #2281592) | Cod sursa (job #2683624) | Cod sursa (job #26317) | Cod sursa (job #2283979) | Cod sursa (job #1956670)
#include <stdio.h>
#define FIN "rucsac.in"
#define FOUT "rucsac.out"
#define MAXW 10001
#define MAXC 5001
#define MAXP 1000
int main() {
int i,j,
n,//number of objects
g,//capacity of the knapsack
w[ MAXW ], //weight
c[ MAXC ], //costs
Cost[ 500 ][ MAXP ]; //profits
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
scanf("%d %d", &n, &g);
for(i = 1; i <= n; i++) {
scanf("%d", &w[ i ]);
scanf("%d", &c[ i ]);
}
for(i = 1; i <= n; ++i) {
for(j = 1; j <= g; ++j) {
if(w[i] <= j) {
if( Cost[i - 1 ][ j ] < Cost[i - 1][j - w[i]] + c[ i ])
Cost[i][j] = Cost[i - 1][j - w[i]] + c[ i ];
else
Cost[i][j] = Cost[ i - 1][ j ];
} else Cost[i][j] = Cost[ i - 1][ j ];
}
}
printf("%d", Cost[n][g]);
return(0);
}