結果
| 問題 | No.4 おもりと天秤 | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2022-08-10 13:04:47 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 153 ms / 5,000 ms | 
| コード長 | 1,636 bytes | 
| コンパイル時間 | 3,256 ms | 
| コンパイル使用メモリ | 78,680 KB | 
| 実行使用メモリ | 52,216 KB | 
| 最終ジャッジ日時 | 2024-09-20 20:58:20 | 
| 合計ジャッジ時間 | 4,897 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
import java.io.*;
import java.util.*;
public class Main {
    static int[] weight;
    static ArrayList<HashSet<Integer>> dp = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        weight = new int[n];
        for (int i = 0; i < n; i++) {
            weight[i] = sc.nextInt();
            dp.add(new HashSet<>());
        }
        if (dfw(n - 1, 0)) {
            System.out.println("impossible");
        } else {
            System.out.println("possible");
        }
    }
    
    static boolean dfw(int idx, int w) {
        if (idx < 0) {
            return w != 0;
        }
        if (!dp.get(idx).contains(w)) {
            if (dfw(idx - 1, w + weight[idx]) && dfw(idx - 1, w - weight[idx])) {
                dp.get(idx).add(w);
            } else {
                return false;
            }
        }
        return true;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
            
            
            
        