


The first line contains a single integer 'T' representing the number of test cases.
The 'T' test cases are as follows:
The first line contains two integers 'N' and 'W', denoting the number of items and the maximum weight the thief can carry, respectively.
The second line contains 'N' space-separated integers, that denote the values of the weight of items.
The third line contains 'N' space-separated integers, that denote the values associated with the items.
The first and only line of output contains the maximum profit that a thief can generate, as described in the task.
The output of every test case is printed in a separate line.
1 <= T <= 10
1 <= N <= 10^3
1 <= W <= 10^3
1<= weights <=10^3
1 <= values <= 10^3
where 'T' is the number of test cases, ‘N’ is the number of items, "weights" is the weight of each item, "values" is the value of each item and ‘W’ is the maximum weight the thief can carry.
This problem can be solved by solving its subproblems and then combining the solutions of the solved subproblems to solve the original problem. We will do this using recursion.
We will consider every subset of the given items and calculate the weight and value of each subset. We will consider only those subsets whose total weight is smaller than or equal to ‘W’. Finally, pick the subset with the maximum value.
The base case of the recursion would be when no items are left or the remaining capacity of the knapsack is 0.
How to generate subsets?
The thief has two options for each item, either pick it or leave it.
Thus, there are two cases for each item:
Note that we can only include an item in the set if and only if the weight of the item is less than or equal to the remaining weight of the knapsack.
Finally, return the maximum value of the items in the knapsack obtained by including or excluding the current item.
We are solving this problem by solving its subproblems and then combining the solutions of those subproblems. If we analyze carefully, we will see that we are solving the same subproblems multiple times. Thus, this problem exhibits overlapping subproblems. Thus, in this approach, we will eliminate the need for solving the same subproblems again and again.
Suppose there are 3 items with weights [2, 2, 3] and the capacity of the knapsack is 5.
The below figure shows some of the recursive calls made for the given problem.
Representation format : N, [weights], W
As we can see, some subproblems are called multiple times.
Same subproblems are shown by the same colour.
That’s why we are storing the solved subproblems in a lookup table so that we don’t have to solve them again. Whenever a call is made to the solved subproblem, we will directly return the answer of that subproblem stored in the lookup table.
Algorithm for the maxProfitHelper function:
Initially, we were breaking the large problem into small problems but now, let us look at it in a different way. Can you solve the small problem first and then reach the final answer? Try to use the bottom-up approach now.
Basically, in the previous approach, you can observe that:
You can observe 2 things here: