結果

問題 No.4 おもりと天秤
ユーザー mitsuomitsuo
提出日時 2016-05-05 20:53:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 1,245 bytes
コンパイル時間 3,907 ms
コンパイル使用メモリ 81,508 KB
実行使用メモリ 56,196 KB
最終ジャッジ日時 2023-09-08 16:43:40
合計ジャッジ時間 6,417 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,792 KB
testcase_01 AC 120 ms
55,880 KB
testcase_02 AC 121 ms
55,744 KB
testcase_03 AC 119 ms
55,688 KB
testcase_04 AC 119 ms
55,900 KB
testcase_05 AC 133 ms
55,736 KB
testcase_06 AC 121 ms
55,688 KB
testcase_07 AC 132 ms
56,008 KB
testcase_08 AC 121 ms
55,760 KB
testcase_09 AC 134 ms
55,496 KB
testcase_10 AC 131 ms
55,776 KB
testcase_11 AC 119 ms
55,776 KB
testcase_12 AC 123 ms
55,984 KB
testcase_13 AC 126 ms
55,572 KB
testcase_14 AC 125 ms
56,032 KB
testcase_15 AC 120 ms
55,580 KB
testcase_16 AC 119 ms
56,196 KB
testcase_17 AC 119 ms
56,132 KB
testcase_18 AC 132 ms
54,268 KB
testcase_19 AC 130 ms
56,196 KB
testcase_20 AC 130 ms
56,016 KB
testcase_21 AC 129 ms
55,856 KB
testcase_22 AC 132 ms
55,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.fedom.challange.yuki.l2.q4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<String> input = new ArrayList<>();
		while (sc.hasNext()) {
			input.add(sc.nextLine());
		}

		System.out.println(solve(input));

		sc.close();
	}

	static int[] Ws;
	static boolean[][] dp;

	public static String solve(List<String> in) {
		int N = Integer.valueOf(in.get(0));
		String[] W = in.get(1).split(" ");

		boolean res = false;

		Ws = new int[N];
		int total = 0;
		for (int i = 0; i < N; i++) {
			Ws[i] = Integer.valueOf(W[i]);
			total += Ws[i];
		}
		Arrays.sort(Ws);

		if (total % 2 == 1) {
			res = false;
		} else {
			res = dfs(N, Ws, total / 2);
		}

		return res ? "possible" : "impossible";
	}

	private static boolean dfs(int N, int[] a, int K) {
		dp = new boolean[N + 1][];
		for (int i = 0; i < dp.length; i++) {
			dp[i] = new boolean[K + 1];
		}
		dp[0][0] = true;

		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= K; j++) {
				dp[i + 1][j] |= dp[i][j];
				if (0 <= j - a[i]) {
					dp[i + 1][j] |= dp[i][j - a[i]];
				}
			}
		}
		return dp[N][K];
	}
}
0