結果

問題 No.4 おもりと天秤
ユーザー mosmos_21mosmos_21
提出日時 2017-06-17 12:15:22
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,153 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 77,860 KB
実行使用メモリ 58,224 KB
最終ジャッジ日時 2024-04-08 17:26:17
合計ジャッジ時間 6,619 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,812 KB
testcase_01 AC 142 ms
57,960 KB
testcase_02 WA -
testcase_03 AC 142 ms
57,728 KB
testcase_04 AC 143 ms
57,940 KB
testcase_05 AC 166 ms
58,116 KB
testcase_06 WA -
testcase_07 AC 163 ms
57,840 KB
testcase_08 AC 148 ms
58,088 KB
testcase_09 AC 166 ms
57,684 KB
testcase_10 AC 165 ms
57,704 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 144 ms
58,060 KB
testcase_17 AC 142 ms
57,852 KB
testcase_18 WA -
testcase_19 AC 154 ms
57,968 KB
testcase_20 AC 155 ms
58,224 KB
testcase_21 AC 161 ms
57,684 KB
testcase_22 AC 159 ms
58,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int N = in.nextInt();
        int[] w = new int[N];
        int sum = 0;

        for (int i = 0; i < N; i++) {
            w[i] = in.nextInt();
            sum += w[i];
        }

        if (sum % 2 == 1) {
            System.out.println("impossible");
            return;
        }
        int h = sum / 2;
        boolean[][] dp = new boolean[h + 1][N + 1];
        for (int i = 0; i < dp.length; i++) {
            dp[i][0] = true;
        }
        for (int i = 0; i < dp[0].length; i++) {
            dp[0][i] = true;
        }

        for (int i = 0; i < dp.length; i++) {
            for (int j = 1; j < dp[0].length; j++) {
                if (dp[i][j - 1] && i + w[j - 1] <= h) {
                    dp[i + w[j - 1]][j] = true;
                } else {
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
        if (dp[h][N - 1]) {
            System.out.println("possible");
        } else {
            System.out.println("impossible");
        }
    }
}
0