結果

問題 No.4 おもりと天秤
ユーザー mobius_bkstmobius_bkst
提出日時 2015-05-02 19:10:23
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,687 bytes
コンパイル時間 3,720 ms
コンパイル使用メモリ 77,696 KB
実行使用メモリ 55,116 KB
最終ジャッジ日時 2023-09-19 05:03:05
合計ジャッジ時間 11,521 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,744 KB
testcase_01 AC 43 ms
49,140 KB
testcase_02 AC 50 ms
49,256 KB
testcase_03 AC 44 ms
49,376 KB
testcase_04 AC 44 ms
49,196 KB
testcase_05 AC 45 ms
49,232 KB
testcase_06 AC 43 ms
49,264 KB
testcase_07 AC 45 ms
49,476 KB
testcase_08 AC 44 ms
49,268 KB
testcase_09 AC 45 ms
49,400 KB
testcase_10 AC 44 ms
49,148 KB
testcase_11 AC 43 ms
49,276 KB
testcase_12 AC 43 ms
49,224 KB
testcase_13 AC 46 ms
49,388 KB
testcase_14 AC 44 ms
49,276 KB
testcase_15 AC 44 ms
49,276 KB
testcase_16 AC 44 ms
49,168 KB
testcase_17 AC 44 ms
49,324 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

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, -1)) {
                    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, long sum, int i) {

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

        if (balance < sum) {
            return false;
        }

        sum = sum + w;
        if (W.length <= i + 1) {
            return false;
        }

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