結果

問題 No.4 おもりと天秤
ユーザー mobius_bkstmobius_bkst
提出日時 2015-05-02 19:49:05
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,646 bytes
コンパイル時間 4,089 ms
コンパイル使用メモリ 74,528 KB
実行使用メモリ 50,384 KB
最終ジャッジ日時 2023-09-19 05:04:06
合計ジャッジ時間 6,468 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 45 ms
49,444 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 45 ms
49,248 KB
testcase_08 AC 44 ms
49,344 KB
testcase_09 AC 46 ms
49,516 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 46 ms
49,464 KB
testcase_21 WA -
testcase_22 AC 45 ms
49,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class No004 {

    static long balance;

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            int N = Integer.parseInt(br.readLine());
            long[] W = strTointArray(br.readLine());
            for (long l : W) {
                balance = balance + l;
            }
            if (balance % 2 != 0) {
                System.out.println("impossible");
            } else {
                balance = balance / 2;
                if (dfs(W, 0, 0)) {
                    System.out.println("possible");
                } else {
                    System.out.println("impossible");
                }
            }

        } catch (Exception e) {
            System.err.println("Error:" + e.getMessage());
        }
    }

    static boolean dfs(long[] W, long sum, int i) {

        if (balance == sum) {
            return true;
        }

        if (balance < sum) {
            return false;
        }

        if (W.length == i) {
            return false;
        }

        if (dfs(W, sum + W[i + 1], i + 1)) {
            return true;
        }
        if (dfs(W, sum, i + 1)) {
            return true;
        }

        return false;
    }

    static long[] strTointArray(String S) {
        String[] strArray = S.split(" ");
        long[] longArray = new long[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            longArray[i] = Long.parseLong(strArray[i]);
        }
        return longArray;
    }
}
0