File Recovery

Delete elements from a sequence so the remainder parses as length-prefixed blocks that end exactly at the last position, minimizing the largest likelihood among deleted elements.

Medium7Dynamic programmingBinary searchGreedyPrefix sumNo attempts yetTime limit2sMemory limit512 MB

Problem

Company shake! represents each user's data as positive integers, converts each one into a data piece, concatenates the pieces, and saves the result in a file. A data piece starts with its length. If the length is LL, the piece consists of LL followed by L1L-1 numbers, and a piece with L=1L=1 consists of the single number 11.

Suppose three users have data {2,5,5}\{2, 5, 5\}, {1,4,5,1}\{1, 4, 5, 1\}, and {2,3,1}\{2, 3, 1\}. Their pieces are {4,2,5,5}\{4, 2, 5, 5\}, {5,1,4,5,1}\{5, 1, 4, 5, 1\}, and {4,2,3,1}\{4, 2, 3, 1\}, and the file stores the concatenation {4,2,5,5,5,1,4,5,1,4,2,3,1}\{4, 2, 5, 5, 5, 1, 4, 5, 1, 4, 2, 3, 1\}.

The damaged file may contain extra numbers that were not in the original file. For each position, an integer is given that represents the likelihood that the number was also in the original file. Delete numbers so that the rest forms a correct file while minimizing the maximum likelihood among the deleted numbers.

Input

The first line gives the count NN of numbers in the damaged file. It satisfies 1N1000001 \le N \le 100000.

The second line gives NN integers forming the damaged file. Each is between 11 and NN inclusive.

The third line gives NN integers representing the likelihood that each number was also in the original file. Each is between 11 and 100000100000 inclusive.

Output

Print the minimized maximum likelihood among the deleted numbers.

If no deletion is needed, print 00.

Hint

To check the remaining sequence, scan from the front. If the first number of a piece is LL, the next L1L-1 numbers belong to the same piece, and the following piece starts right after them. The file is correct when the last piece ends exactly at the end of the sequence.

Deleting positions keeps the order of the remaining numbers, and the first number of each piece must equal the count of remaining numbers assigned to that piece.