結果

問題 No.4 おもりと天秤
ユーザー shinwisteriashinwisteria
提出日時 2017-03-26 20:24:16
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,171 bytes
コンパイル時間 3,969 ms
コンパイル使用メモリ 81,016 KB
実行使用メモリ 56,632 KB
最終ジャッジ日時 2023-09-20 11:06:58
合計ジャッジ時間 7,704 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,632 KB
testcase_01 AC 124 ms
55,936 KB
testcase_02 AC 127 ms
56,188 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 147 ms
55,988 KB
testcase_06 AC 124 ms
55,704 KB
testcase_07 AC 148 ms
56,288 KB
testcase_08 AC 134 ms
56,236 KB
testcase_09 AC 134 ms
55,884 KB
testcase_10 AC 151 ms
56,052 KB
testcase_11 AC 128 ms
55,876 KB
testcase_12 AC 124 ms
56,140 KB
testcase_13 AC 126 ms
56,032 KB
testcase_14 AC 124 ms
56,040 KB
testcase_15 AC 126 ms
56,428 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 146 ms
55,836 KB
testcase_19 AC 149 ms
56,252 KB
testcase_20 AC 148 ms
56,200 KB
testcase_21 AC 146 ms
56,380 KB
testcase_22 AC 145 ms
56,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class OmoritoTenbin {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt(),rest = 0;
		int[] W = new int[N];
		for(int i = 0;i < N ;i++){
			W[i] = s.nextInt();
			rest += W[i];
		}
		s.close();
		Arrays.sort(W);
		int kw = 0;
		ArrayList<Integer> list = new ArrayList<>();
		if(rest%2 == 1){
			System.out.println("impossible");
		}else{
			rest /=2;
			Deque<Integer> stack = new ArrayDeque<>();
			for(int i :W){
				stack.push(i);
				list.add(i);
			}
			while(stack.size() != 0){
				int w = stack.pop();
				//System.out.println(stack);
				if(rest-w == 0){
					System.out.println("possible");
					break;
				}else if(rest-w > 0 && rest-w-list.get(0) >= 0){
					for(int i = 0;i < list.indexOf(w);i++){
						stack.push(list.get(i));
					}
					rest -= w;
					kw = w;
					//System.out.println(rest + "  " + w);
				}else if(w == list.get(0) && rest - w != 0){
					rest += kw;
				}
				if(stack.size() == 0){
					System.out.println("impossible");
				}
			}
		}
	}
}
0