結果

問題 No.4 おもりと天秤
ユーザー YamaKasa
提出日時 2018-10-07 15:14:58
言語 Java
(openjdk 23)
結果
AC  
実行時間 157 ms / 5,000 ms
コード長 700 bytes
コンパイル時間 2,489 ms
コンパイル使用メモリ 77,164 KB
実行使用メモリ 48,256 KB
最終ジャッジ日時 2024-10-12 14:14:02
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int[]W = new int[N];
		int sum = 0;
		for(int i = 0; i < N; i++) {
			W[i] = scan.nextInt();
			sum += W[i];
		}
		scan.close();
		if(sum % 2 == 1) {
			System.out.println("impossible");
			System.exit(0);
		}

		int[][]dp = new int[N + 1][10001];
		dp[0][0] = 1;
		for(int i = 0; i < N; i++) {
			for(int j = 0; j <= 10000; j++) {
				if(dp[i][j] == 1) {
					dp[i + 1][j + W[i]] = 1;
					dp[i + 1][j] = 1;
				}
			}
		}
		if(dp[N][sum / 2] == 1) {
			System.out.println("possible");
		}else {
			System.out.println("impossible");
		}
	}
}
0