結果

問題 No.4 おもりと天秤
ユーザー mobius_bkstmobius_bkst
提出日時 2015-05-02 19:51:06
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,660 bytes
コンパイル時間 3,838 ms
コンパイル使用メモリ 74,584 KB
実行使用メモリ 55,200 KB
最終ジャッジ日時 2023-09-19 05:04:36
合計ジャッジ時間 11,789 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,784 KB
testcase_01 AC 45 ms
49,508 KB
testcase_02 AC 50 ms
49,656 KB
testcase_03 AC 46 ms
49,280 KB
testcase_04 AC 44 ms
49,344 KB
testcase_05 AC 46 ms
49,540 KB
testcase_06 AC 45 ms
49,112 KB
testcase_07 AC 46 ms
49,412 KB
testcase_08 AC 45 ms
49,300 KB
testcase_09 AC 46 ms
49,408 KB
testcase_10 AC 47 ms
49,168 KB
testcase_11 AC 47 ms
49,416 KB
testcase_12 AC 46 ms
49,688 KB
testcase_13 AC 45 ms
49,284 KB
testcase_14 AC 45 ms
49,756 KB
testcase_15 AC 46 ms
49,344 KB
testcase_16 AC 45 ms
49,728 KB
testcase_17 AC 46 ms
49,832 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)) {
                    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], 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