#P14739. [ICPC 2021 Seoul R] John' s Gift

[ICPC 2021 Seoul R] John' s Gift

题目描述

Every morning, John, a storekeeper, receives nn goods of distinct values and nn price tags all of which have different prices. As John wants to sell as many goods as possible, he sets up a match between the goods and the price tags to minimize the maximum difference (max-difference in short) between the two pair, where different goods should match different price tags. For example, if John has two goods of values 10,3010, 30 and two price tags of prices 10,2010, 20, then the max-difference can be minimized to 1010 by matching (10,10)(10, 10) and (30,20)(30, 20). This smallest max-difference is called the matching score.

Today, Jane, a friend of John, has a birthday party and John decides to pick a birthday gift from his goods. When selecting a good, he does not want to lose too much profit, and therefore wants to select a good whose removal results in the smallest matching score for the remaining n1n-1 goods against the original nn price tags. By the way, when matching n1n-1 goods, John leaves one price tag unpaired to make a proper match.

For instance, John has two goods G1G_1 and G2G_2 whose values are 1010 and 3030, respectively, and two price tags 1010 and 2020. If he picks G1G_1 for a gift, then a possible price for G2G_2 is either 1010 or 2020. Then the matching score is 1010 when G2G_2 is priced at 2020. On the other hand, if he picks G2G_2 for a gift, then the matching score is zero when G1G_1 is priced at 1010. Therefore, in order to obtain the smallest matching score, John would select G2G_2 as a gift. In other words, among nn goods, John can pick any single good as gift, and this defines a new matching score between the remaining n1n-1 goods and the nn price tags. Among nn possible gift choices, John wants to find a good whose removal produces the smallest matching score.

Given nn good values and nn price tags, write a program that prints a value of a gift good that John should pick in order to produce the smallest matching score between the remaining n1n-1 goods and the nn price tags. If there are two or more candidate goods to select, print the smallest value of the candidate goods.

输入格式

Your program is to read from standard input. The input starts with a line containing an integer nn (2n1062 \le n \le 10^6), where nn is the number of goods and the number of price tags. The following line contains nn positive and distinct integers that represent nn good values. The third line contains nn positive and distinct integers that represent nn price tags. The good values and the tag prices are no more than 10910^9.

输出格式

Your program is to write to standard output. Print exactly one line. The line should contain the value of the good that John picks for Jane's birthday gift such that its removal produces the smallest matching score in the remaining n1n-1 goods. If there are multiple candidate goods, print the smallest value among the candidate goods.

2
10 30
10 20
30
3
20 30 40
30 20 10
40
4
24 68 51 10
20 40 50 30
68