結果

問題 No.4 おもりと天秤
ユーザー mobius_bkstmobius_bkst
提出日時 2015-05-02 18:37:47
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,605 bytes
コンパイル時間 4,840 ms
コンパイル使用メモリ 75,384 KB
実行使用メモリ 51,196 KB
最終ジャッジ日時 2023-09-19 05:02:08
合計ジャッジ時間 5,978 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,508 KB
testcase_01 AC 45 ms
49,664 KB
testcase_02 AC 46 ms
49,164 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 45 ms
49,348 KB
testcase_07 WA -
testcase_08 AC 46 ms
49,176 KB
testcase_09 AC 47 ms
49,176 KB
testcase_10 WA -
testcase_11 AC 45 ms
49,256 KB
testcase_12 AC 45 ms
49,308 KB
testcase_13 AC 46 ms
49,148 KB
testcase_14 AC 46 ms
49,216 KB
testcase_15 AC 44 ms
49,420 KB
testcase_16 WA -
testcase_17 AC 45 ms
49,168 KB
testcase_18 AC 46 ms
49,320 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class No004 {

    static long balance;
    static long sum;

    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 w, int i) {
        if (balance == sum) {
            return true;
        }

        if (balance < sum) {
            return false;
        }

        sum = sum + w;
        if(dfs(W, W[i], i + 1)){
            return true;
        }
        if(dfs(W, 0, 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