結果

問題 No.4 おもりと天秤
ユーザー uafr_csuafr_cs
提出日時 2015-05-29 02:50:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,538 bytes
コンパイル時間 2,675 ms
コンパイル使用メモリ 75,652 KB
実行使用メモリ 60,232 KB
最終ジャッジ日時 2023-09-20 15:20:37
合計ジャッジ時間 11,859 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
56,008 KB
testcase_01 AC 131 ms
55,800 KB
testcase_02 WA -
testcase_03 AC 127 ms
56,348 KB
testcase_04 AC 126 ms
55,908 KB
testcase_05 AC 135 ms
55,884 KB
testcase_06 AC 126 ms
56,004 KB
testcase_07 AC 134 ms
55,820 KB
testcase_08 AC 136 ms
55,728 KB
testcase_09 AC 136 ms
55,588 KB
testcase_10 AC 135 ms
56,036 KB
testcase_11 AC 125 ms
55,880 KB
testcase_12 AC 126 ms
55,684 KB
testcase_13 AC 125 ms
55,932 KB
testcase_14 AC 125 ms
55,736 KB
testcase_15 AC 126 ms
55,972 KB
testcase_16 AC 127 ms
56,072 KB
testcase_17 AC 125 ms
53,552 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 true;
		}
		
		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