Given n items, where the i-th one has weight wi and value vi. We only have a knapsack with capacity W which might not be large enough to hold all the items. Fortunately, we can use magic — for item i, each time the magic can reduce its weight by 1 for the cost of ci. On the other hand, the weight of any item must always be positive — meaning that we cannot use magic to reduce the weight to 0 or less. We can use the magic any number of times. The profit of packing item i into the knapsack is vi−ki×ci where ki is the number of times we apply the magic on item i.
The question is: what is the maximum profit we can get?
It is guaranteed that 1≤n≤200,1≤W≤200,1≤wi≤200,1≤vi≤106,1≤ci≤106.
int knapsack(int n, int W, int w[], int v[], int c[]);
where n
represents the number of items. W
represents the capacity of the knapsack. The arrays w[0…n-1]
,v[0…n-1]
,c[0…n-1]
are the weights, values, and costs of the items.
#include <stdio.h>
int knapsack(int n, int W, int w[], int v[], int c[]);
const int maxn = 200;
int main()
{
int n, W;
int v[maxn], w[maxn], c[maxn];
scanf(“%d %d”, &n, &W);
for(int i = 0; i < n; i++)
scanf(“%d%d%d”, &w[i], &v[i], &c[i]);
printf(“%d\n”, knapsack(n, W, w, v, c));
return 0;
}
/* Fill your program here*/
4 10 8 10 2 12 15 3 5 9 2 3 8 1
17