Formula
DP[i][j] = f ( DP[i-1][j] , DP[i-1][j-weight[i]]+values[i] )
For this case, f is the max function.
Variables:
int n:number of cases
int N:number of items
int V:max volume of the knapsack
array values:value of each item
array weight:weight of each item
Implement
|
|
Sample
Input
2
5 10
1 2 3 4 5
5 4 3 2 1
Output
14
the array dp
While weight often can be zero.
5 0
2 4 1 5 1
0 0 1 0 0
the result is 12.