結果
| 問題 | No.4 おもりと天秤 | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2023-04-21 13:01:16 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 69 ms / 5,000 ms | 
| コード長 | 2,291 bytes | 
| コンパイル時間 | 2,723 ms | 
| コンパイル使用メモリ | 89,596 KB | 
| 実行使用メモリ | 50,720 KB | 
| 最終ジャッジ日時 | 2024-11-06 08:49:42 | 
| 合計ジャッジ時間 | 5,057 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
    static int[] weights;
    static boolean[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int total = 0;
        weights = new int[n];
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
            total += weights[i];
        }
        if (total % 2 == 1) {
            System.out.println("impossible");
            return;
        }
        dp = new boolean[n][total / 2 + 1];
        if (dfw(n - 1, total / 2)) {
            System.out.println("impossible");
        } else {
            System.out.println("possible");
        }
    }
    
    static boolean dfw(int idx, int v) {
        if (v == 0) {
            return false;
        }
        if (v < 0 || idx < 0) {
            return true;
        }
        if (!dp[idx][v]) {
            dp[idx][v] = dfw(idx - 1, v) && dfw(idx - 1, v - weights[idx]);
        }
        return dp[idx][v];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
            
            
            
        