結果

問題 No.4 おもりと天秤
ユーザー scachescache
提出日時 2014-11-12 09:53:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 147 ms / 5,000 ms
コード長 793 bytes
コンパイル時間 3,702 ms
コンパイル使用メモリ 76,612 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2023-09-08 16:03:37
合計ジャッジ時間 8,017 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,748 KB
testcase_01 AC 122 ms
56,200 KB
testcase_02 AC 135 ms
56,016 KB
testcase_03 AC 126 ms
55,972 KB
testcase_04 AC 134 ms
55,864 KB
testcase_05 AC 147 ms
53,868 KB
testcase_06 AC 126 ms
55,748 KB
testcase_07 AC 143 ms
56,400 KB
testcase_08 AC 131 ms
54,080 KB
testcase_09 AC 146 ms
56,092 KB
testcase_10 AC 144 ms
55,664 KB
testcase_11 AC 123 ms
55,736 KB
testcase_12 AC 124 ms
53,900 KB
testcase_13 AC 126 ms
55,780 KB
testcase_14 AC 124 ms
55,556 KB
testcase_15 AC 124 ms
55,804 KB
testcase_16 AC 127 ms
55,816 KB
testcase_17 AC 123 ms
56,120 KB
testcase_18 AC 144 ms
55,716 KB
testcase_19 AC 145 ms
56,144 KB
testcase_20 AC 140 ms
55,648 KB
testcase_21 AC 142 ms
55,808 KB
testcase_22 AC 144 ms
56,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main4 {
	public static void main(String[] args) {
		Main4 p = new Main4();
	}

	public Main4() {
		Scanner sc = new Scanner(System.in);
		int n =sc.nextInt();
		int[] w =new int[n];
		for(int i=0;i<n;i++)
			w[i] = sc.nextInt();
		solve(n, w);
	}

	public void solve(int n, int[] w) {
		Arrays.sort(w);
		boolean[] dp = new boolean[w[n-1]*w[n-1]+1];
		
		dp[0] = true;
		for(int i=0;i<w.length;i++){
			for(int j=dp.length-1;j>=0;j--){
				if(j-w[i] >= 0 && dp[j-w[i]]){
					dp[j] = true;
				}
			}
		}
		
		int sum = 0;
		for(int i=0;i<w.length;i++)
			sum += w[i];
		if(sum%2==0 && dp[sum/2])
			System.out.println("possible");
		else
			System.out.println("impossible");
		
	}

}
0