結果

問題 No.4 おもりと天秤
ユーザー jp_stejp_ste
提出日時 2016-02-02 09:25:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 5,000 ms
コード長 716 bytes
コンパイル時間 3,495 ms
コンパイル使用メモリ 74,500 KB
実行使用メモリ 56,412 KB
最終ジャッジ日時 2023-09-08 16:32:20
合計ジャッジ時間 5,826 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,412 KB
testcase_01 AC 124 ms
55,868 KB
testcase_02 AC 122 ms
55,588 KB
testcase_03 AC 122 ms
55,656 KB
testcase_04 AC 121 ms
55,720 KB
testcase_05 AC 136 ms
55,908 KB
testcase_06 AC 122 ms
56,088 KB
testcase_07 AC 141 ms
55,976 KB
testcase_08 AC 129 ms
55,508 KB
testcase_09 AC 138 ms
55,828 KB
testcase_10 AC 138 ms
55,524 KB
testcase_11 AC 122 ms
55,860 KB
testcase_12 AC 122 ms
55,952 KB
testcase_13 AC 121 ms
56,032 KB
testcase_14 AC 122 ms
55,764 KB
testcase_15 AC 119 ms
55,688 KB
testcase_16 AC 120 ms
55,716 KB
testcase_17 AC 120 ms
55,620 KB
testcase_18 AC 136 ms
55,756 KB
testcase_19 AC 136 ms
55,560 KB
testcase_20 AC 136 ms
55,720 KB
testcase_21 AC 137 ms
55,556 KB
testcase_22 AC 140 ms
55,812 KB
権限があれば一括ダウンロードができます

ソースコード

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 list[] = new int[N];
		int sum = 0;
		for(int i=0; i<N; i++) {
			list[i] = scan.nextInt();
			sum += list[i];
		}
		scan.close();
		
		if(sum % 2 == 1) {
			System.out.println("impossible");
		} else {
			int target = sum / 2;
			boolean dp[] = new boolean[sum+1];
			dp[list[0]] = true;
			
			for(int i=1; i<N; i++) {
				for(int j=sum; j>=1; j--) {
					if(dp[j]) dp[j+list[i]] = true;
				}
				dp[list[i]] = true;
			}
			
			if(dp[target]) {
				System.out.println("possible");
			} else {
				System.out.println("impossible");
			}
		}
    }	
}
0