結果

問題 No.4 おもりと天秤
ユーザー mosmos_21mosmos_21
提出日時 2017-06-17 13:22:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 1,105 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 76,708 KB
実行使用メモリ 56,792 KB
最終ジャッジ日時 2023-09-08 17:30:47
合計ジャッジ時間 6,186 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,636 KB
testcase_01 AC 119 ms
55,556 KB
testcase_02 AC 120 ms
55,496 KB
testcase_03 AC 122 ms
55,960 KB
testcase_04 AC 123 ms
55,372 KB
testcase_05 AC 156 ms
56,156 KB
testcase_06 AC 121 ms
56,116 KB
testcase_07 AC 151 ms
55,664 KB
testcase_08 AC 128 ms
55,628 KB
testcase_09 AC 159 ms
55,556 KB
testcase_10 AC 156 ms
55,804 KB
testcase_11 AC 120 ms
55,748 KB
testcase_12 AC 120 ms
56,476 KB
testcase_13 AC 119 ms
56,792 KB
testcase_14 AC 120 ms
55,852 KB
testcase_15 AC 120 ms
55,940 KB
testcase_16 AC 123 ms
55,864 KB
testcase_17 AC 119 ms
55,964 KB
testcase_18 AC 153 ms
56,100 KB
testcase_19 AC 152 ms
55,944 KB
testcase_20 AC 152 ms
56,324 KB
testcase_21 AC 152 ms
55,556 KB
testcase_22 AC 150 ms
55,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
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[N + 1][h + 1];
        for (int i = 0; i < dp.length; i++) {
            Arrays.fill(dp[i], false);
        }
        dp[0][0] = true;
        for (int i = 1; i < N + 1; i++) {
            for (int j = 1; j < h + 1; j++) {
                if(0 <= j - w[i - 1] && dp[i - 1][j - w[i - 1]]){
                    dp[i][j] = true;
                }else{
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        if (dp[N][h]) {
            System.out.println("possible");
        } else {
            System.out.println("impossible");
        }
    }
}
0