結果

問題 No.4 おもりと天秤
ユーザー wildwild
提出日時 2017-05-04 22:10:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 914 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 73,144 KB
実行使用メモリ 56,136 KB
最終ジャッジ日時 2023-09-08 17:25:54
合計ジャッジ時間 6,763 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,112 KB
testcase_01 AC 129 ms
55,964 KB
testcase_02 AC 127 ms
56,016 KB
testcase_03 AC 127 ms
55,800 KB
testcase_04 AC 127 ms
55,728 KB
testcase_05 AC 144 ms
55,560 KB
testcase_06 AC 128 ms
55,540 KB
testcase_07 AC 145 ms
55,888 KB
testcase_08 AC 136 ms
55,796 KB
testcase_09 AC 145 ms
55,916 KB
testcase_10 AC 143 ms
56,136 KB
testcase_11 AC 128 ms
55,932 KB
testcase_12 AC 127 ms
55,700 KB
testcase_13 AC 128 ms
56,136 KB
testcase_14 AC 130 ms
56,064 KB
testcase_15 AC 128 ms
55,744 KB
testcase_16 AC 129 ms
55,912 KB
testcase_17 AC 128 ms
56,080 KB
testcase_18 AC 144 ms
55,944 KB
testcase_19 AC 144 ms
55,728 KB
testcase_20 AC 145 ms
55,564 KB
testcase_21 AC 144 ms
55,740 KB
testcase_22 AC 143 ms
55,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by whiled on 17/05/04.
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] v = new int[N];
        for (int i = 0; i < N; i++) v[i] = scan.nextInt();

        int sum = Arrays.stream(v).sum();
        int comp = sum / 2;
        if (sum % 2 == 1) {
            System.out.println("impossible");
            return;
        }


        boolean[] res = new boolean[sum + 1];

        for (int i = 0; i < res.length; i++) res[i] = false;
        res[0] = true;

        for (int i = 0; i < N; i++) {
            for (int j = res.length - 1; j > -1; j--) {
                if (res[j]) res[j + v[i]] = true;
            }
        }

        if (res[comp]) System.out.println("possible");
        else System.out.println("impossible");
    }
}
0