#abc376c. C - Prepare Another Box
C - Prepare Another Box
Score : points
问题陈述
有 个玩具,编号从 到 ,以及 个盒子,编号从 到 。玩具 的大小为 ,盒子 的大小为 。
高桥想要将所有的玩具分别存放在不同的盒子里,并且他已经决定按照以下步骤进行:
- 选择一个任意的正整数 并购买一个大小为 的盒子。
- 将 个玩具中的每一个都放入 个盒子中的一个( 个已有的盒子加上新购买的盒子)。在这里,每个玩具只能被放入大小不小于玩具大小的盒子里,且没有盒子可以包含两个或更多的玩具。
他想要在步骤 中购买一个足够大的盒子来执行步骤 ,但是更大的盒子更贵,所以他想要购买尽可能小的盒子。
确定是否存在一个 的值,使得他可以执行步骤 ,如果存在,找到这样的最小 。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There are toys numbered from to , and boxes numbered from to . Toy has a size of , and box has a size of .
Takahashi wants to store all the toys in separate boxes, and he has decided to perform the following steps in order:
- Choose an arbitrary positive integer and purchase one box of size .
- Place each of the toys into one of the boxes (the existing boxes plus the newly purchased box). Here, each toy can only be placed in a box whose size is not less than the toy's size, and no box can contain two or more toys.
He wants to execute step by purchasing a sufficiently large box in step , but larger boxes are more expensive, so he wants to purchase the smallest possible box.
Determine whether there exists a value of such that he can execute step , and if it exists, find the minimum such .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If there exists a value of such that Takahashi can execute step , print the minimum such . Otherwise, print -1
.
Sample Input 1
4
5 2 3 7
6 2 8
Sample Output 1
3
Consider the case where (that is, he purchases a box of size in step ).
If the newly purchased box is called box , toys have sizes of , , , and , respectively, and boxes have sizes of , , , and , respectively. Thus, toy can be placed in box , toy in box , toy in box , and toy in box .
On the other hand, if , it is impossible to place all toys into separate boxes. Therefore, the answer is .
Sample Input 2
4
3 7 2 5
8 1 6
Sample Output 2
-1
No matter what size of box is purchased in step , no toy can be placed in box , so it is impossible to execute step .
Sample Input 3
8
2 28 17 39 57 56 37 32
34 27 73 28 76 61 27
Sample Output 3
37