結果

問題 No.4 おもりと天秤
ユーザー uafr_csuafr_cs
提出日時 2015-05-29 02:38:17
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,122 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 80,236 KB
実行使用メモリ 55,832 KB
最終ジャッジ日時 2023-09-20 15:12:56
合計ジャッジ時間 12,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
55,256 KB
testcase_01 AC 131 ms
55,312 KB
testcase_02 AC 861 ms
55,528 KB
testcase_03 AC 129 ms
55,648 KB
testcase_04 AC 131 ms
55,508 KB
testcase_05 AC 138 ms
55,756 KB
testcase_06 AC 131 ms
55,808 KB
testcase_07 AC 140 ms
55,712 KB
testcase_08 AC 137 ms
55,832 KB
testcase_09 AC 140 ms
55,732 KB
testcase_10 AC 140 ms
55,808 KB
testcase_11 AC 128 ms
55,732 KB
testcase_12 AC 133 ms
55,744 KB
testcase_13 AC 129 ms
55,616 KB
testcase_14 AC 130 ms
53,900 KB
testcase_15 AC 128 ms
55,688 KB
testcase_16 AC 128 ms
55,608 KB
testcase_17 AC 131 ms
55,268 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[] array, 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;
		}
		
		for(int i = deep; i < N; i++){
			if(!used[i]){
				used[i] = true;
				if(dfs(deep + 1, N, purpose, current + array[i], array, used)){
					return true;
				}
				used[i] = false;
			}
		}
		
		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];
		}
		
		final int sum = Arrays.stream(arr).sum();
		final int purpose = sum / 2;
		
		System.out.println((sum % 2) == 0 && dfs(0, N, purpose, 0, arr, new boolean[N]) ? "possible" : "impossible");
	}
	
}
0