結果

問題 No.4 おもりと天秤
ユーザー snzt_rysnzt_ry
提出日時 2017-11-21 10:25:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 146 ms / 5,000 ms
コード長 724 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 73,724 KB
実行使用メモリ 58,248 KB
最終ジャッジ日時 2023-09-08 17:36:13
合計ジャッジ時間 6,221 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,524 KB
testcase_01 AC 123 ms
55,640 KB
testcase_02 AC 125 ms
55,828 KB
testcase_03 AC 123 ms
56,128 KB
testcase_04 AC 123 ms
56,388 KB
testcase_05 AC 143 ms
58,088 KB
testcase_06 AC 119 ms
56,088 KB
testcase_07 AC 145 ms
58,104 KB
testcase_08 AC 130 ms
56,204 KB
testcase_09 AC 143 ms
58,060 KB
testcase_10 AC 143 ms
58,068 KB
testcase_11 AC 120 ms
56,404 KB
testcase_12 AC 123 ms
55,828 KB
testcase_13 AC 122 ms
56,120 KB
testcase_14 AC 122 ms
56,280 KB
testcase_15 AC 123 ms
55,912 KB
testcase_16 AC 125 ms
55,748 KB
testcase_17 AC 122 ms
55,952 KB
testcase_18 AC 146 ms
58,248 KB
testcase_19 AC 140 ms
55,408 KB
testcase_20 AC 144 ms
58,088 KB
testcase_21 AC 146 ms
56,088 KB
testcase_22 AC 143 ms
55,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int sum = 0;
		int[] v = new int[n+1];


		for(int i = 1; i < n+1; i++){
			v[i] = scanner.nextInt();
			sum += v[i];
		}

		if(sum % 2 == 1){
			System.out.println("impossible");
			return;
		}

		int[][] dp = new int[n+1][sum/2+1];

		for(int i = 1; i < n+1; i++){
			for(int j = 1; j < sum/2+1; j++){
				if(j >= v[i]){
					dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-v[i]] + v[i]);
				}else {
					dp[i][j] = dp[i-1][j];
				}
			}
		}

		if(dp[n][sum/2] == sum/2){
			System.out.println("possible");
		}else {
			System.out.println("impossible");
		}
	}
}
0