結果
問題 |
No.50 おもちゃ箱
|
ユーザー |
![]() |
提出日時 | 2019-12-06 21:17:24 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 966 bytes |
コンパイル時間 | 2,306 ms |
コンパイル使用メモリ | 78,216 KB |
実行使用メモリ | 55,852 KB |
最終ジャッジ日時 | 2024-12-23 21:06:43 |
合計ジャッジ時間 | 10,214 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 29 WA * 9 |
ソースコード
import java.util.*; public class Main { static HashSet<String> set = new HashSet<>(); 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 idx = m; for (int i = n - 1; i >= 0; i--) { if (toys[i] == 0) { continue; } idx--; if (idx < 0 || boxes[idx] < toys[i]) { System.out.println(-1); return; } int remain = boxes[idx] - toys[i]; int left = i - 1; while (remain > 0 && left >= 0) { if (remain >= toys[left]) { remain -= toys[left]; toys[left] = 0; } left--; } } System.out.println(m - idx); } }