結果

問題 No.50 おもちゃ箱
ユーザー GrenacheGrenache
提出日時 2016-07-03 10:57:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 293 ms / 5,000 ms
コード長 1,841 bytes
コンパイル時間 6,704 ms
コンパイル使用メモリ 76,892 KB
実行使用メモリ 61,268 KB
最終ジャッジ日時 2023-09-08 04:08:29
合計ジャッジ時間 14,161 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,852 KB
testcase_01 AC 258 ms
60,668 KB
testcase_02 AC 123 ms
55,876 KB
testcase_03 AC 114 ms
55,800 KB
testcase_04 AC 115 ms
56,524 KB
testcase_05 AC 278 ms
60,636 KB
testcase_06 AC 194 ms
59,040 KB
testcase_07 AC 121 ms
55,828 KB
testcase_08 AC 120 ms
55,800 KB
testcase_09 AC 121 ms
56,464 KB
testcase_10 AC 119 ms
55,676 KB
testcase_11 AC 122 ms
55,956 KB
testcase_12 AC 121 ms
55,592 KB
testcase_13 AC 119 ms
55,720 KB
testcase_14 AC 129 ms
55,812 KB
testcase_15 AC 123 ms
55,644 KB
testcase_16 AC 129 ms
55,892 KB
testcase_17 AC 268 ms
60,744 KB
testcase_18 AC 197 ms
59,156 KB
testcase_19 AC 293 ms
60,820 KB
testcase_20 AC 136 ms
55,516 KB
testcase_21 AC 210 ms
59,148 KB
testcase_22 AC 287 ms
60,908 KB
testcase_23 AC 208 ms
59,184 KB
testcase_24 AC 125 ms
55,444 KB
testcase_25 AC 125 ms
55,784 KB
testcase_26 AC 134 ms
55,948 KB
testcase_27 AC 200 ms
59,904 KB
testcase_28 AC 272 ms
60,476 KB
testcase_29 AC 264 ms
61,160 KB
testcase_30 AC 267 ms
60,864 KB
testcase_31 AC 123 ms
55,752 KB
testcase_32 AC 271 ms
60,388 KB
testcase_33 AC 248 ms
60,396 KB
testcase_34 AC 265 ms
60,436 KB
testcase_35 AC 258 ms
61,032 KB
testcase_36 AC 264 ms
60,484 KB
testcase_37 AC 255 ms
61,268 KB
testcase_38 AC 269 ms
61,112 KB
testcase_39 AC 273 ms
60,812 KB
testcase_40 AC 268 ms
60,528 KB
testcase_41 AC 279 ms
60,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder50 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        final int INF = Integer.MAX_VALUE;

        int n = sc.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
        	a[i] = sc.nextInt();
        }

        int m = sc.nextInt();

        List<Integer> b = new ArrayList<>();
        for (int i = 0; i < m; i++) {
        	b.add(sc.nextInt());
        }
        Collections.sort(b);

        Deque<List<Integer>> st = new ArrayDeque<>();
        Deque<Integer> stn = new ArrayDeque<>();
        st.add(new ArrayList<Integer>());
        stn.add(0);

        int min = INF;
        while (!st.isEmpty()) {
        	List<Integer> e = st.pop();
        	int en = stn.pop();

        	if (en == n) {
        		Collections.sort(e);

        		boolean flag = true;
        		for (int i = e.size() - 1, j = b.size() - 1; i >= 0; i--, j--) {
        			if (j < 0 || b.get(j) < e.get(i)) {
        				flag = false;
        				break;
        			}
        		}
        		if (flag) {
        			min = Math.min(min, e.size());
        		}
        	} else {
        		for (int i = 0; i < e.size(); i++) {
        			List<Integer> tmp = new ArrayList<>(e);
        			tmp.set(i, tmp.get(i) + a[en]);
        			st.push(tmp);
        			stn.push(en + 1);
        		}
    			List<Integer> tmp = new ArrayList<>(e);
    			tmp.add(a[en]);
    			st.push(tmp);
    			stn.push(en + 1);
        	}
        }

        if (min == INF) {
        	pr.println(-1);
        } else {
        	pr.println(min);
        }

        pr.close();
        sc.close();
    }

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0