-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapSack.java
More file actions
38 lines (29 loc) · 1.06 KB
/
KnapSack.java
File metadata and controls
38 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 0-1 KnapSack Geeks for Geeks version
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
final int T = Integer.parseInt(br.readLine());
for(int c = 0; c < T; c++){
int n = Integer.parseInt(br.readLine());
int k = Integer.parseInt(br.readLine());
String[] line = br.readLine().split("\\s+");
String[] line2 = br.readLine().split("\\s+");
int[] v = new int[n];
int[] w = new int[n];
for(int i = 0; i < n; i++){
v[i] = Integer.parseInt(line[i]);
w[i] = Integer.parseInt(line2[i]);
}
int[][] ans = new int[n+1][k+1];
for(int col = 1; col < n+1; col++){
for(int row = 1; row < k+1; row++){
ans[col][row] = row - w[col - 1] < 0? ans[col-1][row] : Math.max(v[col-1] + ans[col-1][row - w[col - 1]], ans[col-1][row]);
}
}
System.out.println(ans[n][k]);
}
}
}