結果

問題 No.4 おもりと天秤
ユーザー tetsutetsu
提出日時 2017-10-17 14:02:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 161 ms / 5,000 ms
コード長 844 bytes
コンパイル時間 3,335 ms
コンパイル使用メモリ 74,200 KB
実行使用メモリ 56,220 KB
最終ジャッジ日時 2023-09-08 17:35:26
合計ジャッジ時間 7,566 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,632 KB
testcase_01 AC 125 ms
55,816 KB
testcase_02 AC 120 ms
55,920 KB
testcase_03 AC 120 ms
55,876 KB
testcase_04 AC 122 ms
55,800 KB
testcase_05 AC 154 ms
55,748 KB
testcase_06 AC 125 ms
55,468 KB
testcase_07 AC 153 ms
56,000 KB
testcase_08 AC 135 ms
56,068 KB
testcase_09 AC 158 ms
55,896 KB
testcase_10 AC 158 ms
55,948 KB
testcase_11 AC 123 ms
55,652 KB
testcase_12 AC 120 ms
55,908 KB
testcase_13 AC 121 ms
55,768 KB
testcase_14 AC 119 ms
55,880 KB
testcase_15 AC 121 ms
55,688 KB
testcase_16 AC 121 ms
55,980 KB
testcase_17 AC 124 ms
55,800 KB
testcase_18 AC 157 ms
55,708 KB
testcase_19 AC 161 ms
55,864 KB
testcase_20 AC 161 ms
55,472 KB
testcase_21 AC 154 ms
56,220 KB
testcase_22 AC 154 ms
55,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.*;

public class P4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int sum = 0;
		int[] W = new int[N];
		for(int i=0; i<N; i++) {
			W[i] = sc.nextInt();
			sum += W[i];
		}
		if(sum%2==1) {
			System.out.println("impossible");
			return;
		}
		boolean[][] possible = new boolean[N+1][sum+1];
		for(int i=0; i<N+1; i++) {
			Arrays.fill(possible[i], false);
		}
		possible[0][0] = true;
		for(int i=0; i<N; i++) {
			for(int w=0; w<=sum; w++) {
				if(w-W[i]>=0&&possible[i][w-W[i]]) {
					possible[i+1][w] = true;
				} else {
					possible[i+1][w] = possible[i][w];
				}
			}
		}
		if(possible[N][sum/2]) {
			System.out.println("possible");
		} else {
			System.out.println("impossible");
		}
		
	}

}
0