結果

問題 No.4 おもりと天秤
ユーザー spaciaspacia
提出日時 2015-12-19 11:37:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 1,128 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 74,408 KB
実行使用メモリ 49,596 KB
最終ジャッジ日時 2023-09-08 16:30:21
合計ジャッジ時間 4,118 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,592 KB
testcase_01 AC 42 ms
49,164 KB
testcase_02 AC 43 ms
49,380 KB
testcase_03 AC 43 ms
49,384 KB
testcase_04 AC 44 ms
49,344 KB
testcase_05 AC 51 ms
49,372 KB
testcase_06 AC 43 ms
49,492 KB
testcase_07 AC 51 ms
49,380 KB
testcase_08 AC 43 ms
47,392 KB
testcase_09 AC 51 ms
49,328 KB
testcase_10 AC 51 ms
49,476 KB
testcase_11 AC 43 ms
49,360 KB
testcase_12 AC 42 ms
49,408 KB
testcase_13 AC 43 ms
49,568 KB
testcase_14 AC 43 ms
49,596 KB
testcase_15 AC 43 ms
49,264 KB
testcase_16 AC 43 ms
49,468 KB
testcase_17 AC 42 ms
49,328 KB
testcase_18 AC 50 ms
49,460 KB
testcase_19 AC 51 ms
49,272 KB
testcase_20 AC 50 ms
49,348 KB
testcase_21 AC 50 ms
49,472 KB
testcase_22 AC 50 ms
49,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main4 {
	
	public static boolean check (int[] nums, int target) {
		boolean[] dp = new boolean[target + 1];
		
		Arrays.sort(nums);
		dp[0] = true;
		for (int i = 0; i < nums.length; i++) {
			boolean[] addFlg = new boolean[target + 1];
			for (int j = 0; j <= target; j++) {
				if (!dp[j] || addFlg[j]) continue;
				int res = nums[i] + j;
				if (res <= target) {
					if (dp[res]) continue;
					dp[res] = true;
					addFlg[res] = true;
				} else
					break;
			}
			//System.out.println(Arrays.toString(dp));
		}
		return dp[target];
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		int target = 0;
		int[] nums = new int[n];
		String[] str = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			nums[i] = Integer.parseInt(str[i]);
			target += nums[i];
		}
		if (target % 2 == 1)
			System.out.println("impossible");
		else {
			target /= 2;
			System.out.println(check(nums, target) ? "possible" : "impossible");
		}
	}
}
0