結果

問題 No.4 おもりと天秤
ユーザー fkwnw3_1243fkwnw3_1243
提出日時 2017-05-07 23:14:41
言語 Java19
(openjdk 21)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,353 bytes
コンパイル時間 4,127 ms
コンパイル使用メモリ 72,384 KB
実行使用メモリ 35,388 KB
最終ジャッジ日時 2023-09-08 17:26:28
合計ジャッジ時間 4,496 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
34,200 KB
testcase_01 AC 40 ms
33,640 KB
testcase_02 AC 45 ms
34,608 KB
testcase_03 AC 44 ms
34,704 KB
testcase_04 AC 45 ms
34,532 KB
testcase_05 AC 60 ms
35,272 KB
testcase_06 AC 41 ms
34,516 KB
testcase_07 AC 59 ms
35,276 KB
testcase_08 AC 40 ms
33,512 KB
testcase_09 AC 55 ms
34,620 KB
testcase_10 AC 58 ms
35,384 KB
testcase_11 AC 42 ms
34,636 KB
testcase_12 AC 42 ms
34,372 KB
testcase_13 AC 41 ms
34,512 KB
testcase_14 AC 41 ms
34,628 KB
testcase_15 AC 41 ms
34,624 KB
testcase_16 AC 44 ms
34,572 KB
testcase_17 AC 42 ms
34,440 KB
testcase_18 AC 56 ms
34,740 KB
testcase_19 AC 59 ms
35,152 KB
testcase_20 AC 59 ms
35,172 KB
testcase_21 AC 60 ms
35,388 KB
testcase_22 AC 59 ms
35,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.in;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        int N = Integer.parseInt(reader.readLine());
        String[] inputs = reader.readLine().split(" ");
        int[] weights = new int[N];
        int sumOfWeights = 0;

        for (int i = 0; i < N; i++) {
            int weight = Integer.parseInt(inputs[i]);
            weights[i] = weight;
            sumOfWeights += weight;
        }

        if (sumOfWeights % 2 != 0) {
            System.out.println("impossible");
        } else {

            int requiredWeight = sumOfWeights / 2;
            boolean[][] dp = new boolean[101][10001];
            dp[0][0] = true;

            for (int i = 0; i < N; i++) {
                for (int j = 0; j <= 10000; j++) {
                    if (dp[i][j]) {
                        dp[i + 1][j + weights[i]] = true;
                        dp[i + 1][j] = true;
                    }
                }
            }
            if (dp[N][requiredWeight]) {
                System.out.println("possible");
            } else {
                System.out.println("impossible");
            }
        }
    }
}
0