結果

問題 No.4 おもりと天秤
ユーザー CrazyBBBCrazyBBB
提出日時 2016-03-05 20:37:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 151 ms / 5,000 ms
コード長 723 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 74,544 KB
実行使用メモリ 41,736 KB
最終ジャッジ日時 2024-06-26 09:27:07
合計ジャッジ時間 6,031 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

	static boolean[] can;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] W = new int[N];
        int sum = 0;
		for (int i=0; i<N; i++) {
			W[i] = sc.nextInt();
			sum += W[i];
		}
		if (sum%2 != 0) {
			System.out.println("impossible");
		} else {
			can = new boolean[sum/2+1];
			can[0] = true;

			for (int i=0; i<N; i++) {
				for (int j=sum/2; j>=0; j--) {
					if (can[j] && j+W[i]<=sum/2) {
						can[j+W[i]] = true;
					}
				}
			}

			if (can[sum/2]) {
				System.out.println("possible");
			} else {
				System.out.println("impossible");
			}
		}

        sc.close();
    }
}
0