Skip to content

Commit eefbb20

Browse files
authored
Add files via upload
1 parent 6702d17 commit eefbb20

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Practice9.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package DP;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class Practice9 {
7+
8+
public static int fun(int ind , int wts[] ,int wt,int costs[], int dp[][]){
9+
if(ind == wts.length - 1){
10+
return wt >= wts[ind] ? costs[ind] : 0;
11+
}
12+
if(dp[ind][wt] != -1){
13+
return dp[ind][wt];
14+
}
15+
16+
int take = 0;
17+
if(wts[ind] <= wt){
18+
take = costs[ind] + fun(ind + 1, wts, wt - wts[ind], costs,dp);
19+
}
20+
int notTake = fun(ind + 1, wts, wt , costs,dp);
21+
22+
return dp[ind][wt] = Math.max(take, notTake);
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
28+
Scanner sc = new Scanner(System.in);
29+
30+
int n = sc.nextInt();
31+
int wt = sc.nextInt();
32+
33+
int arr[] = new int[n];
34+
int arr2[] = new int[n];
35+
36+
for(int i = 0; i < n; i++){
37+
arr[i] = sc.nextInt();
38+
}
39+
for(int i = 0; i < n; i++){
40+
arr2[i] = sc.nextInt();
41+
}
42+
43+
int dp[][] = new int[n][wt + 1];
44+
45+
46+
for(int it[] : dp){
47+
Arrays.fill(it, -1);
48+
}
49+
50+
51+
52+
System.out.println(fun(0, arr, wt, arr2 , dp));
53+
54+
}
55+
}

0 commit comments

Comments
 (0)