There are ‘N’ items numbered from 1, 2, 3, .. N. Each i-th item has a weight, ‘WEIGHT[i]’ and a value, ‘VALUE[i]’.
Ninja has decided to choose some of ‘N’ items and carry them home in a knapsack. The capacity of the knapsack is ‘W’, which means the maximum total weight of items he can carry home is ‘W’.
Find the maximum possible sum of values of items that Ninja takes home.
For Example:
Let ‘WEIGHT’ = [1, 2, 3], ‘VALUE’ = [10, 20, 30] and ‘W’ = 4.
In this case, Ninja can take item 1 and item 3. Then the sum of weights will be 1 + 3 = 4, and the sum of values will be 10 + 30 = 40, which is the maximum possible sum of values Ninja can take home.
The first line contains an integer 'T', which denotes the number of test cases.
The next line contains two integers ‘N’ and ‘W’ denoting the number of items and the maximum total weight of items Ninja can take home respectively.
The next line contains ‘N’ space-separated positive integers denoting the elements of array ‘WEIGHT’.
The next line contains ‘N’ space-separated positive integers denoting the elements of array ‘VALUE’.
Output Format :
For each test case, return the maximum possible sum of values of items that Ninja takes home.
Note :
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 ≤ T ≤ 10
1 ≤ N ≤ 50
1 ≤ W ≤ 10^7
1 ≤ WEIGHT[i] ≤ W
1 ≤ VALUE[i] ≤ 100
WEIGHT.length = VALUE.length = N
Time limit: 1 sec
2
3 4
1 2 3
10 20 30
4 5
2 3 5 7
10 30 50 70
40
50
Test Case 1:
In this case, Ninja can take item 1 and item 3. Then the sum of weights will be 1 + 3 = 4, and the sum of values will be 10 + 30 = 40, which is the maximum possible sum of values Ninja can take home.
Test Case 2:
In this case, Ninja can take either item 1 and item 2, whose sum of weights is 2 + 3 = 5 or item 3 only whose sum of weight is 5. He will choose item 3 only because it will give him the sum of values maximum i.e. 50.
2
2 4
1 3
10 20
1 2
3
10
30
0
Test Case 1:
In this case, Ninja can take both items home, because their sum of weights is less than the capacity of the knapsack. Hence he will get sum of values as 10 + 20 = 30.
Test Case 2:
In this case, Ninja can’t take any item home, because the only item available has a weight more than the the capacity of his knapsack. Hence he will get sum of values as 0.
Generate all possible weights for all possible values of items.
A simple solution is to generate weights for all possible values of items and take the one which has sum of values as maximum and total weight less than or equal to ‘W’.
The maximum total value of items will be simply sum of values of all items. We can loop through all these values and check for a particular value what weight these items have. This weight must be less than or equal to our knapsack capacity ‘W’. And we take a maximum of all these values.
Now, for finding the total weight for a particular value we can use recursion because we will generate all possible subsets of items. We are just finding what is the least weight Ninja need to carry to get a particular value.
For i-th item there will be two choices:
Then we will just take the minimum of these two cases as we want to find the least weight Ninja need to carry.
The steps are as follows :
O( VMAX * ( 2 ^ N ) ), where ‘N’ is the number of items and ‘VMAX’ is the total value of all items.
We are generating all subsets of items and doing this for every value from 0 to ‘VMAX’.
Hence the time complexity is O( VMAX * ( 2 ^ N ) ).
O( N ), where ‘N’ is the number of items.
The depth of the recursion tree will be ‘N’. Apart from that, we are not using any space.
Hence the space complexity is O( N ).