結果

問題 No.4 おもりと天秤
ユーザー spacia
提出日時 2015-12-19 09:48:50
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 827 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 73,732 KB
実行使用メモリ 44,144 KB
最終ジャッジ日時 2024-09-16 08:53:53
合計ジャッジ時間 9,984 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main4 {
	
	static int target = 0;
	static int[] nums;
	
	public static boolean dp (int i, int sum) {
		if (sum > target || i >= nums.length) return false;
		if (sum == target) return true;
		
		if (dp(i + 1, sum + nums[i]) || dp(i + 1, sum))
			return true;
		return false;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		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(dp(0, 0) ? "possible" : "impossible");
		}
	}
}
0