## DP Multiple Knapsack

Given an array of items, each item has weight, value and quantity and the knapsack’s maximum holding weight. Output the the maximum value that can be obtained for holding these items in knapsack.

Input format In the first row, there are two numbers n, m, where n represents the number of items that you want to pick, and m represents the maximum knapsack holding weight.

In the next n rows, 3 numbers in each row, w, v and q, respectively represent the weight, value and the maximum quantity that can be picked (pick 0 up to q).

Output format One line: a number, indicating the maximum value that can be obtained for this purchase.

Sample Input:

5 1000
80 20 4
40 50 9
30 50 7
40 30 6
20 20 1


Sample Output:

1040


### Solution 3: Binary Grouping Optimization

There are n[i] items for the i-th item, this n[i] can be expanded to n[i]=2^0+2^1+…+2^k+c (where 2^(k+1) < n[i] < 2^(k+2)). 2^0+2^1+…+2^k can represent the interval [0,2^(k+1)-1 ], by adding the number c, it can represent all the numbers on the interval [0,n[i]])

For each item, create new items which is equivalent to packing together 2^j (j=0..log2(s[i])) items. Then solve the original problem with 0-1 knapsack.