当前位置:首页|资讯

Abc376 C - Prepare Another Box

作者:您是打尖儿还是住店呢发布时间:2024-10-20

Problem Statement

There are NN toys numbered from 11 to NN, and N−1 boxes numbered from 1 to N1. Toy i (1≤i≤N) has a size of Ai, and box i (1≤i≤N−1)i (1iN1) 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:

  1. Choose an arbitrary positive integer xx and purchase one box of size xx.

  2. Place each of the NN toys into one of the NN boxes (the N−1N1 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.

Constraints

  • 2≤N≤2×1052N2×105

  • 1≤Ai,Bi≤1091Ai,Bi109

  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NNA1A1A2A2ANANB1B1B2B2BN−1BN1

Output

If there exists a value of xx such that Takahashi can execute step 22, print the minimum such xx. Otherwise, print -1.

Sample Input 1

45 2 3 76 2 8

Sample Output 1

3

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 552233, and 77, respectively, and boxes 1,…,41,,4 have sizes of 662288, 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≤2x2, it is impossible to place all NN toys into separate boxes. Therefore, the answer is 33.

Sample Input 

43 7 2 58 1 6

Sample Output 

-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即可。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1