Pagini recente » Cod sursa (job #2427497) | Cod sursa (job #2870696) | Cod sursa (job #1604507)
#include <stdio.h>
#include <stdlib.h>
#define IN "rucsac.in"
#define OUT "rucsac.out"
#define NMAX 5001
#define CMAX 10001
struct OBJECT{
int w, p;
};
inline void read (int *n, int *g, struct OBJECT object[]){
int x, i;
scanf ("%d", &x), *n = x;
scanf ("%d", &x), *g = x;
for (i = 1; i <= *n; ++i)
scanf ("%d%d", &object[i].w, &object[i].p);
}
inline void copy (int cost1[], int cost2[], int g){
int i;
for (i = 1; i <= g; ++ i)
cost1 [i] = cost2 [i];
}
inline void init (int cost[], int g){
int i;
for (i = 0; i <= g; ++ i)
cost [i] = 0;
}
inline int mx (int a, int b){
if (a >= b)
return a;
return b;
}
int best (int n, int g, struct OBJECT object[]){
int cost1 [g + 1], cost2 [g + 1], i, cw;
init (cost1, g);
for (i = 1; i <= n; ++ i){
for (cw = 1; cw <= g; ++ cw){
cost2 [cw] = cost1 [cw];
if (cw >= object[i].w)
cost2 [cw] = mx (cost1 [cw], cost1 [cw - object[i].w] + object[i].p);
}
copy (cost1, cost2, g);
}
return cost1[g];
}
int main()
{
freopen (IN, "r", stdin);
freopen (OUT, "w", stdout);
int n, g;
struct OBJECT object[NMAX];
read (&n, &g, object);
printf ("%d\n", best (n, g, object));
fclose (stdin);
fclose (stdout);
return 0;
}