-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSubsetSum.java
executable file
·54 lines (42 loc) · 1.26 KB
/
SubsetSum.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
45
46
47
48
49
50
51
52
53
54
/* package joney_000
*/
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class SubsetSum
{
public static long ans=0;
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
ans=0;
int tests=Integer.parseInt(br.readLine());
for(int t=0;t<tests;t++){
String[] s=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);int S = Integer.parseInt(s[1]);
int num[]=new int[n];
//s=br.readLine().split(" ");
for(int i=0;i<n;i++){
num[i]=Integer.parseInt(br.readLine());
}
boolean[][] dp=new boolean[n+1][S+1];
for(int i=0;i<=n;i++)dp[i][0]=false;
for(int i=0;i<=S;i++)dp[0][1]=false;
for(int i=1;i<=n;i++){
for(int j=1;j<=S;j++){
if(num[i-1]<=j){
dp[i][j]=(num[i-1]==j||(dp[i-1][j-num[i-1]])||dp[i-1][j]);
}else{
dp[i][j]=dp[i-1][j];
}
}
}
if(dp[n][S]){
out.write("Yes"+"\n");out.flush();}else{
out.write("No"+"\n");out.flush();
}
}
}
}