#abc376c. C - Prepare Another Box

C - Prepare Another Box

Score : 350350 points

问题陈述

NN 个玩具,编号从 11NN,以及 N1N-1 个盒子,编号从 11N1N-1。玩具 i (1iN)i\ (1 \leq i \leq N) 的大小为 AiA_i,盒子 i (1iN1)i\ (1 \leq i \leq N-1) 的大小为 BiB_i

高桥想要将所有的玩具分别存放在不同的盒子里,并且他已经决定按照以下步骤进行:

  1. 选择一个任意的正整数 xx 并购买一个大小为 xx 的盒子。
  2. NN 个玩具中的每一个都放入 NN 个盒子中的一个(N1N-1 个已有的盒子加上新购买的盒子)。在这里,每个玩具只能被放入大小不小于玩具大小的盒子里,且没有盒子可以包含两个或更多的玩具。

他想要在步骤 11 中购买一个足够大的盒子来执行步骤 22,但是更大的盒子更贵,所以他想要购买尽可能小的盒子。

确定是否存在一个 xx 的值,使得他可以执行步骤 22,如果存在,找到这样的最小 xx

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN toys numbered from 11 to NN, and N1N-1 boxes numbered from 11 to N1N-1. Toy i (1iN)i\ (1 \leq i \leq N) has a size of AiA_i, and box i (1iN1)i\ (1 \leq i \leq N-1) has a size of BiB_i.

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 N1N-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.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1Ai,Bi1091 \leq A_i, B_i \leq 10^9
  • All input values are integers.

Input

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

NN

A1A_1 A2A_2 \dots ANA_N

B1B_1 B2B_2 \dots BN1B_{N-1}

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

4
5 2 3 7
6 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,\dots,4 have sizes of 55, 22, 33, and 77, respectively, and boxes 1,,41,\dots,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 x2x \leq 2, it is impossible to place all NN toys into separate boxes. Therefore, the answer is 33.

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 11, no toy can be placed in box 22, so it is impossible to execute step 22.

Sample Input 3

8
2 28 17 39 57 56 37 32
34 27 73 28 76 61 27

Sample Output 3

37