-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd.java
47 lines (45 loc) · 1.32 KB
/
d.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
import java.io.*;
import java.util.*;
public class Program{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static StringTokenizer st = new StringTokenizer("");
static int T, N, a[];
static boolean ok;
static void dfs(int i, int sum){
if(sum == a[N-1]){
ok = true;
return;
}
if(ok || i == N-1) return;
dfs(i+1, sum);
if(ok) return;
dfs(i+1, sum + a[i]);
if(ok) return;
dfs(i+1, sum - a[i]);
}
public static void main(String args[]) throws IOException{
T = nexti();
while(T-- > 0){
N = nexti();
a = new int[N];
for(int i = 0; i < N; i++) a[i] = nexti();
ok = false;
for(int i = 0; i < N; i++){
dfs(0, 0);
int tmp = a[i];
a[i] = a[N-1];
a[N-1] = tmp;
}
pw.println((ok ? "YES" : "NO"));
}
br.close(); pw.close();
}
static String next() throws IOException{
while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nexti() throws IOException{
return Integer.parseInt(next());
}
}