結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
57,544 KB
testcase_01 AC 131 ms
57,532 KB
testcase_02 WA -
testcase_03 AC 133 ms
57,560 KB
testcase_04 AC 141 ms
57,544 KB
testcase_05 AC 155 ms
57,740 KB
testcase_06 WA -
testcase_07 AC 151 ms
57,708 KB
testcase_08 AC 139 ms
57,692 KB
testcase_09 AC 149 ms
57,672 KB
testcase_10 AC 151 ms
57,840 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 132 ms
57,568 KB
testcase_17 AC 132 ms
57,572 KB
testcase_18 WA -
testcase_19 AC 154 ms
57,684 KB
testcase_20 AC 153 ms
57,656 KB
testcase_21 AC 150 ms
57,848 KB
testcase_22 AC 157 ms
57,796 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]) {
            System.out.println("possible");
        } else {
            System.out.println("impossible");
        }
    }
}
0