結果

問題 No.4 おもりと天秤
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-01 11:42:31
言語 Java19
(openjdk 21)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,281 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 73,880 KB
実行使用メモリ 50,100 KB
最終ジャッジ日時 2023-09-08 16:23:17
合計ジャッジ時間 4,112 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,180 KB
testcase_01 AC 44 ms
49,204 KB
testcase_02 AC 44 ms
49,204 KB
testcase_03 AC 43 ms
49,264 KB
testcase_04 AC 44 ms
49,092 KB
testcase_05 AC 51 ms
49,924 KB
testcase_06 AC 44 ms
49,212 KB
testcase_07 AC 51 ms
50,040 KB
testcase_08 AC 45 ms
49,180 KB
testcase_09 AC 52 ms
50,100 KB
testcase_10 AC 53 ms
49,956 KB
testcase_11 AC 44 ms
49,092 KB
testcase_12 AC 44 ms
49,280 KB
testcase_13 AC 44 ms
49,296 KB
testcase_14 AC 43 ms
49,356 KB
testcase_15 AC 44 ms
49,280 KB
testcase_16 AC 43 ms
49,324 KB
testcase_17 AC 43 ms
49,456 KB
testcase_18 AC 52 ms
49,848 KB
testcase_19 AC 48 ms
49,412 KB
testcase_20 AC 48 ms
49,100 KB
testcase_21 AC 48 ms
49,680 KB
testcase_22 AC 47 ms
49,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        String[] strW = br.readLine().split(" ");

        int[] W = new int[strW.length];
        int sum = 0;
        for (int i = 0; i < strW.length; i++) {
            W[i] = Integer.parseInt(strW[i]);
            sum += W[i];
        }

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

        int[] d = new int[sum / 2 + 1];

        Arrays.fill(d, Integer.MAX_VALUE);
        d[0] = -1;

        for (int i = 0; i < W.length; i++) {
            int tmpW = W[i];
            for (int j = 0; j < d.length - tmpW; j++) {
                if (d[j] != Integer.MAX_VALUE && d[j] != i) {
                    if (sum / 2 == j + tmpW) {
                        System.out.println("possible");
                        return;
                    } else if (j + tmpW < d.length && d[j + tmpW] == Integer.MAX_VALUE) {
                        d[j + tmpW] = i;
                    }
                }
            }
        }

        System.out.println("impossible");
    }
}
0