結果

問題 No.4 おもりと天秤
ユーザー Flere0114Flere0114
提出日時 2016-02-17 16:33:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 909 bytes
コンパイル時間 3,688 ms
コンパイル使用メモリ 72,136 KB
実行使用メモリ 55,992 KB
最終ジャッジ日時 2023-09-08 16:35:59
合計ジャッジ時間 7,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,816 KB
testcase_01 AC 127 ms
55,492 KB
testcase_02 AC 126 ms
55,324 KB
testcase_03 AC 127 ms
55,624 KB
testcase_04 AC 124 ms
55,632 KB
testcase_05 AC 142 ms
55,572 KB
testcase_06 AC 124 ms
55,648 KB
testcase_07 AC 144 ms
55,848 KB
testcase_08 AC 131 ms
55,644 KB
testcase_09 AC 141 ms
55,740 KB
testcase_10 AC 142 ms
55,852 KB
testcase_11 AC 125 ms
55,472 KB
testcase_12 AC 127 ms
55,584 KB
testcase_13 AC 124 ms
55,368 KB
testcase_14 AC 130 ms
55,504 KB
testcase_15 AC 125 ms
55,580 KB
testcase_16 AC 126 ms
55,596 KB
testcase_17 AC 124 ms
55,588 KB
testcase_18 AC 144 ms
55,840 KB
testcase_19 AC 143 ms
53,760 KB
testcase_20 AC 145 ms
55,696 KB
testcase_21 AC 142 ms
55,992 KB
testcase_22 AC 140 ms
55,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class No4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] w = new int[n];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            w[i] = Integer.parseInt(sc.next());
            sum += w[i];
        }

        if(sum % 2 == 1){
            System.out.println("impossible");
            return;
        }

        int half = sum / 2;
        boolean[] dp = new boolean[half+1];
        dp[0] = true;

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