Stand-up Comedian
Contest: Educational Codeforces Round 142 (Rated for Div. 2)
Difficulty rating: 1200
Tags: greedy, math

Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.

Eve prepared $$$a_1 + a_2 + a_3 + a_4$$$ jokes to tell, grouped by their type:

- type 1: both Alice and Bob like them;
- type 2: Alice likes them, but Bob doesn't;
- type 3: Bob likes them, but Alice doesn't;
- type 4: neither Alice nor Bob likes them.

Initially, both spectators have their mood equal to $$$0$$$. When a spectator hears a joke he/she likes, his/her mood increases by $$$1$$$. When a spectator hears a joke he/she doesn't like, his/her mood decreases by $$$1$$$. If the mood of a spectator becomes negative (strictly below zero), he/she leaves.

When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.

Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.

The only line of each testcase contains four integers $$$a_1, a_2, a_3, a_4$$$ ($$$0 \le a_1, a_2, a_3, a_4 \le 10^8$$$; $$$a_1 + a_2 + a_3 + a_4 \ge 1$$$) — the number of jokes of each type Eve prepared.

Output format:
For each testcase, print a single integer — the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.

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

Note:
In the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes $$$5$$$. The show ends after Eve runs out of jokes.

In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. She tells a joke, and neither Alice, nor Bob likes it. Their mood decrease by one, becoming $$$-1$$$. They both have negative mood, thus, both leave, and the show ends.

In the third testcase, first, Eve tells both jokes of the first type. Both Alice and Bob has mood $$$2$$$. Then she can tell $$$2$$$ jokes of the third type. Alice's mood becomes $$$0$$$. Bob's mood becomes $$$4$$$. Then $$$4$$$ jokes of the second type. Alice's mood becomes $$$4$$$. Bob's mood becomes $$$0$$$. Then another $$$4$$$ jokes of the third type. Alice's mood becomes $$$0$$$. Bob's mood becomes $$$4$$$. Then the remaining joke of the second type. Alice's mood becomes $$$1$$$. Bob's mood becomes $$$3$$$. Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes $$$-1$$$, she leaves, and the show ends.

In the fourth testcase, Eve should first tell the jokes both spectators like, then the jokes they don't. She can tell $$$4$$$ jokes of the fourth type until the spectators leave.

Example input:
4
5 0 0 0
0 0 0 5
2 5 10 6
3 0 0 7

Example output:
5
1
15
7

Editorial:
First, let Eve tell the jokes of the first type — they will never do any harm. At the same time, let her tell the jokes of the fourth time at the very end — they will not do any good.
Types two and three are kind of opposites of each other. If you tell jokes of each of them one after another, then the moods of both spectators don't change. Let's use that to our advantage. Tell the jokes of these types in pairs until one of them runs out. There's a little corner case here, though. If there were no jokes of the first type, then you can't use a single pair because of the spectators leaves after one joke.
Finally, try to tell the remaining jokes of the same type before the fourth type. So the construction looks like $$$1, 1, \dots, 1, 2, 3, 2, 3, \dots, 2, 3, 2, 2, 2, \dots, 2, 4, 4, 4, \dots, 4$$$ with $$$2$$$ and $$$3$$$ possibly swapped with each other.
Let's recover the answer from that construction. After the first type, both moods are $$$a_1$$$. After the alternating jokes, the moods are still the same. After that, one of the spectators will have his/her mood only decreasing until the end. Once it reaches $$$-1$$$, the show ends.
Thus, Eve can tell $$$a_1 + min(a_2, a_3) \cdot 2 + min(a_1 + 1, abs(a_2 - a_3) + a_4)$$$ jokes if $$$a_1 \neq 0$$$. Otherwise, it's always $$$1$$$.
Overall complexity: $$$O(1)$$$.