結果

問題 No.4 おもりと天秤
ユーザー mosmos_21
提出日時 2017-06-17 12:15:22
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,153 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 77,444 KB
実行使用メモリ 56,144 KB
最終ジャッジ日時 2024-10-01 09:30:23
合計ジャッジ時間 6,101 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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