PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000023. Chuẩn bị thêm một chiếc hộp (Prepare Another Box)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chuẩn bị thêm một chiếc hộp (Prepare Another Box)

Prepare Another Box

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Takahashi has nn toys and n−1n-1 boxes. Toy ii has size aia_i, and box jj has size bjb_j. A toy can be placed in a box only when the box is at least as large as the toy.

Takahashi will purchase exactly one additional box of a positive integer size xx. He must then put all nn toys into the nn boxes, with exactly one toy per box. Find the smallest possible xx. If no choice of xx makes this possible, print -1.

Input

  • The first line contains nn, the number of toys.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n, the toy sizes.
  • The third line contains n−1n-1 integers b1,…,bn−1b_1,\ldots,b_{n-1}, the sizes of the existing boxes.

Output

Print the minimum positive integer size xx of the additional box, or -1 if no solution exists.

Subtasks

  • Subtask 1 (20 points): 2≤n≤82\le n\le 8; 1≤ai≤1091\le a_i\le10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le10^9 for all 1≤j<n1\le j<n.
  • Subtask 2 (30 points): 2≤n≤15002\le n\le 1500; 1≤ai≤1091\le a_i\le10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le10^9 for all 1≤j<n1\le j<n.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤ai≤1091\le a_i\le10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le10^9 for all 1≤j<n1\le j<n.

Examples

Input

4
5 2 3 7
6 2 8

Output

3

Explanation

Buying a new box of size 33 allows toys of sizes 2,3,5,72,3,5,7 to be placed into boxes of sizes 2,3,6,82,3,6,8, respectively. Every toy fits, and each box holds one toy. If x≤2x\le2, the toys of sizes 22 and 33 cannot both be assigned while preserving the two sufficiently large boxes 6,86,8 for toys 5,75,7. Thus 33 is minimal.