Problem D
King's Decree
SoCCat the Cat King is a very fair cat. Currently, SoCCat wants to ensure all cats can live in peace and harmony!
The Cat Kingdom is a very large kingdom, with $n$ cats living in it. The cats are numbered from $1$ to $n$.
King SoCCat has noticed a glaring problem in the kingdom — the uneven distribution of wealth among the cats. To solve this problem, King SoCCat has decreed that wealthy cats should give some of their wealth to poorer cats — to ensure that all cats can live in peace and harmony.
After surveying each cat in the kingdom, King SoCCat has determined that:
-
Cat $i$ has $w_ i$ units of wealth.
-
Cat $i$ is not willing to have less than $l_ i$ ($l_ i \leq w_ i$) units of wealth.
As a fair king, SoCCat wants to ensure that all cats have at least $l_ i$ units of wealth, and the poorest cat has the maximum possible wealth.
To achieve this, King SoCCat can do the following any number of times:
-
Choose a cat $i$ with wealth strictly greater than $l_ i$.
-
Choose any other cat $j$, and give $1$ unit of wealth from cat $i$ to cat $j$.
Unfortunately, King SoCCat is not very good at math. Therefore, as a friend of the Cat Kingdom, help King SoCCat!
Input
The input starts with a line containing an integer $C$, denoting the number of test cases ($1 \leq C \leq 300\, 000$). Each test case has the following format.
The first line contains an integer $n$, denoting the number of cats in the kingdom ($1 \leq n \leq 300\, 000$). The sum of $n$ over all test cases does not exceed $300\, 000$.
The second line contains $n$ integers $w_1, w_2, \ldots , w_ n$, denoting the wealth of each cat ($1 \leq w_ i \leq 10^9$).
The third line contains $n$ integers $l_1, l_2, \ldots , l_ n$, denoting the minimum wealth that each cat is willing to have ($1 \leq l_ i \leq w_ i$).
Output
For each test case, output a line containing a single integer denoting the maximum possible wealth that the poorest cat can have, if King SoCCat distributes the wealth of the cats in the kingdom fairly and optimally.
Sample Input 1 | Sample Output 1 |
---|---|
2 5 4 2 3 6 12 3 2 3 3 10 8 998 244 353 3109 4650 4305 6507 9699 100 200 300 2040 4230 4236 5234 3233 |
4 3233 |