結果

問題 No.4 おもりと天秤
ユーザー rn4rurn4ru
提出日時 2016-04-12 23:54:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 824 bytes
コンパイル時間 3,323 ms
コンパイル使用メモリ 73,740 KB
実行使用メモリ 56,352 KB
最終ジャッジ日時 2023-09-08 16:39:16
合計ジャッジ時間 6,156 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
56,192 KB
testcase_01 AC 119 ms
55,836 KB
testcase_02 AC 118 ms
55,936 KB
testcase_03 AC 118 ms
55,460 KB
testcase_04 AC 118 ms
56,224 KB
testcase_05 AC 142 ms
56,196 KB
testcase_06 AC 117 ms
55,820 KB
testcase_07 AC 140 ms
55,876 KB
testcase_08 AC 129 ms
56,268 KB
testcase_09 AC 140 ms
55,812 KB
testcase_10 AC 138 ms
55,908 KB
testcase_11 AC 119 ms
55,856 KB
testcase_12 AC 118 ms
56,352 KB
testcase_13 AC 117 ms
55,664 KB
testcase_14 AC 125 ms
55,664 KB
testcase_15 AC 117 ms
54,256 KB
testcase_16 AC 117 ms
55,944 KB
testcase_17 AC 114 ms
55,948 KB
testcase_18 AC 134 ms
56,296 KB
testcase_19 AC 141 ms
55,520 KB
testcase_20 AC 141 ms
55,992 KB
testcase_21 AC 141 ms
55,676 KB
testcase_22 AC 142 ms
55,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[] W = new int[N];
		for (int i = 0; i < N; i++) {
			W[i] = scanner.nextInt();
		}

		System.out.println(f(W));
	}

	private static String f(int[] w) {
		int sum = 0;
		for (int i = 0; i < w.length; i++) {
			sum += w[i];
		}
		if (sum % 2 == 1) {
			return "impossible";
		}
		int target = sum / 2;

		boolean[][] dp = new boolean[target + 1][w.length + 1];
		dp[0][0] = true;

		for (int i = 0; i < target; i++) {
			for (int j = 0; j < w.length; j++) {
				if (dp[i][j]) {
					if (i + w[j] <= target) {
						dp[i + w[j]][j + 1] = true;
					}
					dp[i][j + 1] = true;
				}
			}
		}

		return dp[target][w.length] ? "possible" : "impossible";
	}

}
0