Water the Trees
Contest: Educational Codeforces Round 126 (Rated for Div. 2)
Difficulty rating: 1700
Tags: binary search, greedy, math

There are $$$n$$$ trees in a park, numbered from $$$1$$$ to $$$n$$$. The initial height of the $$$i$$$-th tree is $$$h_i$$$.

You want to water these trees, so they all grow to the same height.

The watering process goes as follows. You start watering trees at day $$$1$$$. During the $$$j$$$-th day you can:

- Choose a tree and water it. If the day is odd (e.g. $$$1, 3, 5, 7, \dots$$$), then the height of the tree increases by $$$1$$$. If the day is even (e.g. $$$2, 4, 6, 8, \dots$$$), then the height of the tree increases by $$$2$$$.
- Or skip a day without watering any tree.

Note that you can't water more than one tree in a day.

Your task is to determine the minimum number of days required to water the trees so they grow to the same height.

You have to answer $$$t$$$ independent test cases.

Input format:
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases.

The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of trees.

The second line of the test case contains $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le 10^9$$$), where $$$h_i$$$ is the height of the $$$i$$$-th tree.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$ ($$$\sum n \le 3 \cdot 10^5$$$).

Output format:
For each test case, print one integer — the minimum number of days required to water the trees, so they grow to the same height.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
Consider the first test case of the example. The initial state of the trees is $$$[1, 2, 4]$$$.

1. During the first day, let's water the first tree, so the sequence of heights becomes $$$[2, 2, 4]$$$;
2. during the second day, let's water the second tree, so the sequence of heights becomes $$$[2, 4, 4]$$$;
3. let's skip the third day;
4. during the fourth day, let's water the first tree, so the sequence of heights becomes $$$[4, 4, 4]$$$.

Thus, the answer is $$$4$$$.

Example input:
3
3
1 2 4
5
4 4 3 5 5
7
2 5 4 8 3 7 4

Example output:
4
3
16

Editorial:
The first observation we need to solve this problem: the required height is either $$$max$$$ or $$$max + 1$$$, where $$$max$$$ is the maximum initial height of some tree. We don't need heights greater than $$$max + 1$$$, because, for example, if the height is $$$max + 2$$$, we can remove some moves and get the answer for the height $$$max$$$. The same thing applies to all heights greater than $$$max + 1$$$. Why do we even need the height $$$max + 1$$$? In some cases (like $$$[1, 1, 1, 1, 1, 1, 2]$$$) the answer for the height $$$max + 1$$$ is better than the answer for the height $$$max$$$ (in this particular case, it is $$$9$$$ vs $$$11$$$).
Now, we have two ways to solve the problem: either use some gross formulas, or just write a binary search on the answer. I won't consider the solution with formulas (but we have one), so let's assume we use binary search. Let the current answer be $$$mid$$$. Then let $$$cnt_1 = \lceil\frac{mid}{2}\rceil$$$ be the number of $$$+1$$$ operations we can do and $$$cnt_2 = \lfloor\frac{mid}{2}\rfloor$$$ be the number of $$$+2$$$ operations we can do. We can use $$$+2$$$ operations greedily and then just check if the number of $$$+1$$$ operations is sufficient to grow up the remaining heights.
Time complexity: $$$O(n \log{n})$$$ per test case.