-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack.java
44 lines (42 loc) · 1000 Bytes
/
Knapsack.java
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
39
40
41
42
43
44
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Knapsack
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
StringTokenizer st;
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
if((s=br.readLine())!=null)
{
st=new StringTokenizer(s);
int n=Integer.parseInt(st.nextToken());;
int k=Integer.parseInt(st.nextToken());
s=br.readLine();
st=new StringTokenizer(s);
int wt[]=new int[n];
for(int i=0;i<n;i++)
{
wt[i]=Integer.parseInt(st.nextToken());
}
int[] knapsack=new int[k+1];
for(int i=1;i<=k;i++)
{
for(int j=0;j<n;j++)
{
if(wt[j]<=i)
{
knapsack[i]=Math.max(knapsack[i],wt[j]+knapsack[i-wt[j]]);
}
}
}
System.out.println(knapsack[k]);
}
}
}
}