|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + public static boolean[][] dp; |
| 8 | + public static boolean[] possible; |
| 9 | + public static int[] weights; |
| 10 | + public static int N; |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + |
| 15 | + N = Integer.parseInt(br.readLine()); |
| 16 | + weights = new int[N]; |
| 17 | + dp = new boolean[N + 1][15001]; |
| 18 | + possible = new boolean[15001]; |
| 19 | + |
| 20 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 21 | + |
| 22 | + for (int i = 0; i < N; i++) { |
| 23 | + weights[i] = Integer.parseInt(st.nextToken()); |
| 24 | + } |
| 25 | + |
| 26 | + dfs(0, 0); |
| 27 | + |
| 28 | + int M = Integer.parseInt(br.readLine()); |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + |
| 31 | + StringBuilder sb = new StringBuilder(); |
| 32 | + for (int i = 0; i < M; i++) { |
| 33 | + int marble = Integer.parseInt(st.nextToken()); |
| 34 | + if (marble > 15000) sb.append("N "); |
| 35 | + else sb.append(possible[marble] ? "Y " : "N "); |
| 36 | + } |
| 37 | + |
| 38 | + System.out.println(sb); |
| 39 | + } |
| 40 | + |
| 41 | + public static void dfs(int idx, int w) { |
| 42 | + if (idx > N || dp[idx][w]) return; |
| 43 | + |
| 44 | + dp[idx][w] = true; |
| 45 | + possible[w] = true; |
| 46 | + |
| 47 | + if (idx == N) return; |
| 48 | + |
| 49 | + dfs(idx + 1, w + weights[idx]); |
| 50 | + |
| 51 | + dfs(idx + 1, Math.abs(w - weights[idx])); |
| 52 | + |
| 53 | + dfs(idx + 1, w); |
| 54 | + } |
| 55 | +} |
0 commit comments