| 4 | import java.io.*; |
| 5 | |
| 6 | class GFG { |
| 7 | public static void main (String[] args) { |
| 8 | //code |
| 9 | Scanner scn = new Scanner(System.in); |
| 10 | int t = scn.nextInt(); |
| 11 | while(t-->0){ |
| 12 | int n = scn.nextInt(); |
| 13 | int w = scn.nextInt(); |
| 14 | |
| 15 | int[][] dp = new int[n+1][w+1]; |
| 16 | for(int[] d: dp) |
| 17 | Arrays.fill(d, -1); |
| 18 | int[] values = new int[n]; |
| 19 | int[] weights = new int[n]; |
| 20 | for(int i = 0;i<n;i++){ |
| 21 | values[i] = scn.nextInt(); |
| 22 | } |
| 23 | for(int i = 0;i<n;i++){ |
| 24 | weights[i] = scn.nextInt(); |
| 25 | } |
| 26 | System.out.println(knapSack_01_Rec(weights, values, w,n, dp)); |
| 27 | } |
| 28 | } |
| 29 | public static int knapSack_01_Rec(int[] weight, int[] value, int cap, int n, int[][] dp){ |
| 30 | int N = n; |
| 31 | int Cap = cap; |
| 32 | for(n = 0;n<=N;n++){ |
| 33 | for(cap = 0;cap<=Cap;cap++){ |
| 34 | if(cap == 0 || n == 0){ |
| 35 | dp[n][cap] =0; |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | int maxProfit = 0; |
| 40 | if(cap-weight[n-1]>=0){ |
| 41 | maxProfit = Math.max(maxProfit,dp[n-1][cap-weight[n-1]]+value[n-1]); |
| 42 | } |
| 43 | maxProfit = Math.max(maxProfit, dp[n-1][cap]); |
| 44 | dp[n][cap] = maxProfit; |
| 45 | } |
| 46 | } |
| 47 | return dp[N][Cap]; |
| 48 | } |
| 49 | } |
nothing calls this directly
no outgoing calls
no test coverage detected