結果

問題 No.4 おもりと天秤
ユーザー uafr_csuafr_cs
提出日時 2015-05-29 02:52:06
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,542 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 75,460 KB
実行使用メモリ 60,720 KB
最終ジャッジ日時 2023-09-20 15:21:39
合計ジャッジ時間 11,410 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,740 KB
testcase_01 AC 123 ms
55,828 KB
testcase_02 AC 122 ms
55,556 KB
testcase_03 AC 122 ms
55,260 KB
testcase_04 AC 122 ms
55,448 KB
testcase_05 AC 129 ms
55,720 KB
testcase_06 AC 122 ms
55,988 KB
testcase_07 AC 133 ms
55,904 KB
testcase_08 AC 135 ms
55,620 KB
testcase_09 AC 137 ms
55,580 KB
testcase_10 AC 135 ms
55,932 KB
testcase_11 AC 125 ms
55,744 KB
testcase_12 AC 126 ms
55,948 KB
testcase_13 AC 124 ms
55,568 KB
testcase_14 AC 128 ms
56,188 KB
testcase_15 AC 123 ms
55,524 KB
testcase_16 AC 127 ms
55,564 KB
testcase_17 AC 125 ms
55,536 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	public static boolean dfs(int deep, int N, int purpose, int current, int use_count, int[] array, int[] partial, boolean[] used){
		//System.out.println(deep + " " + purpose + " " + current);
		
		if(purpose == current){
			return true;
		}else if(purpose < current){
			return false;
		}else if(deep >= N){
			return false;
		}else if(use_count * 2 > N){
			return false;
		}
		
		final int rest_max = partial[N] - partial[deep];
		if(current + rest_max < purpose){
			return false;
		}
		
		if(!used[deep]){
			used[deep] = true;
			if(dfs(deep + 1, N, purpose, current + array[deep], use_count + 1, array, partial, used)){
				return true;
			}
			used[deep] = false;
		}
		
		{
			if(dfs(deep + 1, N, purpose, current, use_count, array, partial, used)){
				return true;
			}
		}
		
		return false;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[] arr = new int[N];
		for(int i = 0; i < N; i++){
			arr[i] = -sc.nextInt();
		}
		Arrays.sort(arr);
		for(int i = 0; i < N; i++){
			arr[i] = -arr[i];
		}
		
		int[] partial = new int[N + 1];
		for(int i = 1; i <= N; i++){
			partial[i] += partial[i - 1] + arr[i - 1];
		}
		
		final int sum = Arrays.stream(arr).sum();
		final int purpose = sum / 2;
		
		System.out.println((sum % 2) == 0 && dfs(0, N, purpose, 0, 0, arr, partial, new boolean[N]) ? "possible" : "impossible");
	}
	
}
0