Pursuit
Contest: Codeforces Round 733 (Div. 1 + Div. 2, based on VK Cup 2021 - Elimination (Engine))
Difficulty rating: 1200
Tags: binary search, brute force, greedy, sortings

You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between $$$0$$$ and $$$100$$$ points, inclusive, for each stage, independently of other contestants.

Points received by contestants in different stages are used for forming overall contest results. Suppose that $$$k$$$ stages of the contest are completed. For each contestant, $$$k - \lfloor \frac{k}{4} \rfloor$$$ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here $$$\lfloor t \rfloor$$$ denotes rounding $$$t$$$ down.)

For example, suppose $$$9$$$ stages are completed, and your scores are $$$50, 30, 50, 50, 100, 10, 30, 100, 50$$$. First, $$$7$$$ stages with the highest scores are chosen — for example, all stages except for the $$$2$$$-nd and the $$$6$$$-th can be chosen. Then your overall result is equal to $$$50 + 50 + 50 + 100 + 30 + 100 + 50 = 430$$$.

As of now, $$$n$$$ stages are completed, and you know the points you and Ilya got for these stages. However, it is unknown how many more stages will be held. You wonder what the smallest number of additional stages is, after which your result might become greater than or equal to Ilya's result, at least in theory. Find this number!

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of completed stages.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$) — your points for the completed stages.

The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i \le 100$$$) — Ilya's points for the completed stages.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case print a single integer — the smallest number of additional stages required for your result to be able to become greater than or equal to Ilya's result.

If your result is already not less than Ilya's result, print $$$0$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first test case, you have scored $$$100$$$ points for the first stage, while Ilya has scored $$$0$$$. Thus, your overall result ($$$100$$$) is already not less than Ilya's result ($$$0$$$).

In the second test case, you have scored $$$0$$$ points for the first stage, while Ilya has scored $$$100$$$. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to $$$100$$$.

In the third test case, your overall result is $$$30 + 40 + 50 = 120$$$, while Ilya's result is $$$100 + 100 + 100 = 300$$$. After three additional stages your result might become equal to $$$420$$$, while Ilya's result might become equal to $$$400$$$.

In the fourth test case, your overall result after four additional stages might become equal to $$$470$$$, while Ilya's result might become equal to $$$400$$$. Three stages are not enough.

Example input:
5
1
100
0
1
0
100
4
20 30 40 50
100 100 100 100
4
10 20 30 40
100 100 100 100
7
7 59 62 52 27 31 55
33 35 50 98 83 80 64

Example output:
0
1
3
4
2

Editorial:
The first thing to notice is that since we're chasing Ilya and we want to reach his score as soon as possible, it only makes sense to add $$$100$$$'s to our scores and $$$0$$$'s to his.
We can also notice that the answer never exceeds $$$n$$$. No matter how bad a stage is for us in terms of points, adding a single stage where we score $$$100$$$ and Ilya scores $$$0$$$ "compensates" it. In particular, in the worst case, when all $$$a_i = 0$$$ and all $$$b_i = 100$$$, the answer is exactly $$$n$$$.
However, if we just add the $$$100/0$$$ stages one-by-one and calculate the overall results from scratch every time, our solution will have $$$O(n^2)$$$ complexity and that's too much.
There are many ways to optimize the solution.
One of them is to transform the given $$$a$$$ and $$$b$$$ arrays into arrays of length $$$101$$$, containing the count of each score (since we are not interested in the order of the scores). Let $$$m = 100$$$ be the maximum achievable score in a stage. Using the transformed arrays, we can calculate the overall scores in $$$O(m)$$$ instead of $$$O(n)$$$, to lower the final complexity to $$$O(mn)$$$.
Alternatively, notice that when we add $$$100$$$ to our scores, it just adds $$$100$$$ to our overall score except for the case when the total number of completed stages becomes divisible by $$$4$$$, when we also need to subtract the score of the worst currently included stage from the sum. We can similarly handle adding $$$0$$$ to Ilya's scores. If we sort all our and Ilya's scores at the beginning and maintain a pointer to the current worst included stage in both scoresheets, we can add a new $$$100/0$$$ stage and recalculate the totals in $$$O(1)$$$.
Finally, we can also notice that whenever adding $$$k$$$ stages works for us, adding $$$k+1$$$ stages will work too. Thus, we can use binary search on $$$k$$$. We can easily check a single value of $$$k$$$ in $$$O(n)$$$ or $$$O(n \log n)$$$, resulting in $$$O(n \log n)$$$ or $$$O(n \log^2 n)$$$ time complexity.