結果

問題 No.4 おもりと天秤
ユーザー tentententen
提出日時 2022-08-10 13:04:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 167 ms / 5,000 ms
コード長 1,636 bytes
コンパイル時間 2,725 ms
コンパイル使用メモリ 78,640 KB
実行使用メモリ 66,344 KB
最終ジャッジ日時 2023-10-20 22:50:30
合計ジャッジ時間 5,491 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,344 KB
testcase_01 AC 56 ms
52,216 KB
testcase_02 AC 61 ms
53,360 KB
testcase_03 AC 55 ms
53,336 KB
testcase_04 AC 55 ms
53,348 KB
testcase_05 AC 121 ms
61,072 KB
testcase_06 AC 54 ms
53,340 KB
testcase_07 AC 107 ms
59,784 KB
testcase_08 AC 69 ms
54,112 KB
testcase_09 AC 57 ms
53,356 KB
testcase_10 AC 125 ms
61,236 KB
testcase_11 AC 55 ms
52,216 KB
testcase_12 AC 54 ms
53,284 KB
testcase_13 AC 54 ms
53,332 KB
testcase_14 AC 55 ms
53,352 KB
testcase_15 AC 53 ms
53,340 KB
testcase_16 AC 57 ms
53,336 KB
testcase_17 AC 55 ms
53,340 KB
testcase_18 AC 167 ms
66,344 KB
testcase_19 AC 115 ms
57,384 KB
testcase_20 AC 114 ms
59,556 KB
testcase_21 AC 108 ms
60,868 KB
testcase_22 AC 115 ms
59,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
}
0