結果

問題 No.4 おもりと天秤
ユーザー ぴろずぴろず
提出日時 2014-12-21 01:04:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 151 ms / 5,000 ms
コード長 654 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 74,276 KB
実行使用メモリ 56,276 KB
最終ジャッジ日時 2023-09-08 16:04:56
合計ジャッジ時間 6,114 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,828 KB
testcase_01 AC 124 ms
55,688 KB
testcase_02 AC 123 ms
55,700 KB
testcase_03 AC 122 ms
55,492 KB
testcase_04 AC 124 ms
55,844 KB
testcase_05 AC 143 ms
56,052 KB
testcase_06 AC 122 ms
55,476 KB
testcase_07 AC 142 ms
55,512 KB
testcase_08 AC 133 ms
56,276 KB
testcase_09 AC 151 ms
55,428 KB
testcase_10 AC 146 ms
55,892 KB
testcase_11 AC 125 ms
55,904 KB
testcase_12 AC 124 ms
56,008 KB
testcase_13 AC 123 ms
56,096 KB
testcase_14 AC 126 ms
55,740 KB
testcase_15 AC 124 ms
56,024 KB
testcase_16 AC 125 ms
55,944 KB
testcase_17 AC 127 ms
55,828 KB
testcase_18 AC 146 ms
56,104 KB
testcase_19 AC 145 ms
55,588 KB
testcase_20 AC 142 ms
55,600 KB
testcase_21 AC 142 ms
55,948 KB
testcase_22 AC 144 ms
55,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no004;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] w = new int[n];
		int sum = 0;
		for(int i=0;i<n;i++) {
			w[i] = sc.nextInt();
			sum += w[i];
		}
		if (sum % 2 == 1) {
			System.out.println("impossible");
			return;
		}
		sum /= 2;
		boolean[][] dp = new boolean[n+1][sum+1];
		dp[0][0] = true;
		for(int i=1;i<=n;i++) {
			for(int j=0;j<=sum;j++) {
				dp[i][j] = dp[i-1][j];
				if (j - w[i-1] >= 0) {
					dp[i][j] |= dp[i-1][j-w[i-1]];
				}
			}
		}
		System.out.println(dp[n][sum] ? "possible" : "impossible");
	}

}
0