There are NN toys numbered from 11 to NN, and N−1 boxes numbered from 1 to N−1. Toy i (1≤i≤N) has a size of Ai, and box i (1≤i≤N−1)i (1≤i≤N−1) has a size of Bi.
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 xx and purchase one box of size xx.
Place each of the NN toys into one of the NN boxes (the N−1N−1 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 22 by purchasing a sufficiently large box in step 11, but larger boxes are more expensive, so he wants to purchase the smallest possible box.
Determine whether there exists a value of xx such that he can execute step 22, and if it exists, find the minimum such xx.
2≤N≤2×1052≤N≤2×105
1≤Ai,Bi≤1091≤Ai,Bi≤109
All input values are integers.
The input is given from Standard Input in the following format:
NNA1A1A2A2……ANANB1B1B2B2……BN−1BN−1
If there exists a value of xx such that Takahashi can execute step 22, print the minimum such xx. Otherwise, print -1
.
45 2 3 76 2 8
Consider the case where x=3x=3 (that is, he purchases a box of size 33 in step 11).
If the newly purchased box is called box 44, toys 1,…,41,…,4 have sizes of 55, 22, 33, and 77, respectively, and boxes 1,…,41,…,4 have sizes of 66, 22, 88, and 33, respectively. Thus, toy 11 can be placed in box 11, toy 22 in box 22, toy 33 in box 44, and toy 44 in box 33.
On the other hand, if x≤2x≤2, it is impossible to place all NN toys into separate boxes. Therefore, the answer is 33.
43 7 2 58 1 6
-1
No matter what size of box is purchased in step 11, no toy can be placed in box 22, so it is impossible to execute step 22.
--------
排序处理,即可。如果装不下的话,就放到list中,如果list的size大于1了,说明一个盒子是不够的,所以输出-1即可。