結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,960 KB
testcase_01 AC 139 ms
57,964 KB
testcase_02 WA -
testcase_03 AC 138 ms
58,096 KB
testcase_04 AC 139 ms
57,936 KB
testcase_05 AC 165 ms
58,200 KB
testcase_06 WA -
testcase_07 AC 165 ms
58,216 KB
testcase_08 AC 143 ms
57,968 KB
testcase_09 AC 164 ms
58,088 KB
testcase_10 AC 160 ms
58,088 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 140 ms
57,664 KB
testcase_17 AC 136 ms
57,568 KB
testcase_18 WA -
testcase_19 AC 161 ms
57,940 KB
testcase_20 AC 162 ms
58,080 KB
testcase_21 AC 159 ms
57,960 KB
testcase_22 AC 161 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++) {
            for (int j = 0; j < dp[0].length; j++) {
                if(i == 0 || j == 0){
                    dp[i][j] = true;
                }else{
                    dp[i][j] = false;
                }
            }
        }

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