結果

問題 No.50 おもちゃ箱
ユーザー tenten
提出日時 2020-11-30 09:47:54
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,171 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 79,928 KB
実行使用メモリ 56,176 KB
最終ジャッジ日時 2024-09-13 02:18:23
合計ジャッジ時間 9,634 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 29 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] toys = new int[n];
		for (int i = 0; i < n; i++) {
		    toys[i] = sc.nextInt();
		}
		Arrays.sort(toys);
		int m = sc.nextInt();
		int[] boxes = new int[m];
		for (int i = 0; i < m; i++) {
		    boxes[i] = sc.nextInt();
		}
		Arrays.sort(boxes);
		int right = m - 1;
		TreeMap<Integer, Integer> using = new TreeMap<>();
		using.put(Integer.MAX_VALUE, 0);
		for (int i = n - 1; i >= 0; i--) {
		    int key = using.ceilingKey(toys[i]);
		    if (key == Integer.MAX_VALUE) {
		        if (right < 0 || boxes[right] < toys[i]) {
		            System.out.println(-1);
		            return;
		        }
		        using.put(boxes[right] - toys[i], using.getOrDefault(boxes[right] - toys[i], 0) + 1);
		        right--;
		    } else {
		        if (using.get(key) == 1) {
		            using.remove(key);
		        } else {
		            using.put(key, using.get(key) - 1);
		        }
	            using.put(key - toys[i], using.getOrDefault(key - toys[i], 0) + 1);
		    }
		}
		System.out.println(m - right - 1);
	}
}

0