結果

問題 No.4 おもりと天秤
ユーザー villachvillach
提出日時 2017-10-05 16:26:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 3,283 ms
コンパイル使用メモリ 77,156 KB
実行使用メモリ 56,712 KB
最終ジャッジ日時 2023-09-08 17:34:58
合計ジャッジ時間 7,469 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,804 KB
testcase_01 AC 126 ms
55,504 KB
testcase_02 AC 129 ms
55,628 KB
testcase_03 AC 123 ms
56,256 KB
testcase_04 AC 127 ms
55,796 KB
testcase_05 AC 137 ms
56,084 KB
testcase_06 AC 121 ms
55,724 KB
testcase_07 AC 139 ms
55,700 KB
testcase_08 AC 127 ms
55,896 KB
testcase_09 AC 140 ms
55,688 KB
testcase_10 AC 136 ms
56,272 KB
testcase_11 AC 125 ms
55,308 KB
testcase_12 AC 122 ms
55,592 KB
testcase_13 AC 120 ms
55,532 KB
testcase_14 AC 127 ms
55,740 KB
testcase_15 AC 124 ms
56,160 KB
testcase_16 AC 123 ms
55,888 KB
testcase_17 AC 128 ms
55,712 KB
testcase_18 AC 139 ms
55,540 KB
testcase_19 AC 135 ms
56,148 KB
testcase_20 AC 136 ms
56,712 KB
testcase_21 AC 141 ms
55,924 KB
testcase_22 AC 141 ms
55,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.ArrayList;
import java.util.Scanner;

public class N4 {
	private int n, sum, half;
	private int[] bobs;
	private boolean[][] dp;
	
	public static void main(String[] args){
		N4 n4 = new N4();
	}

	N4(){
		Scanner sc = new Scanner(System.in);
		this.n = sc.nextInt();
		this.bobs = new int[n];
		this.dp = new boolean[n + 1][10100];
		
		for(int i = 0;i < n;++i){
			bobs[i] = sc.nextInt();
			sum += bobs[i];
		}
		
		if(sum % 2 == 1){
			System.out.println("impossible");
			return;
		}
		
		this.half = sum / 2;
		
		if(this.solve()){
			System.out.println("possible");
		}else{
			System.out.println("impossible");
		}
	}
	
	//めう
	private boolean solve() {
		dp[0][0] = true;
		for(int k = 0;k < n;++k){
			int b = bobs[k];
			for(int weight = 0;weight < 10000;++weight){
				if(dp[k][weight]){
					dp[k + 1][weight + b] = true;
					dp[k + 1][weight] = true;
				}
			}
		}
		
		return dp[this.n - 1][half];
	}
}
0