CF BUDDY
← Problems·

1792B · Stand-up Comedian

1200 · greedy, math

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

Eve prepared a1+a2+a3+a4a_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 00. When a spectator hears a joke he/she likes, his/her mood increases by 11. When a spectator hears a joke he/she doesn't like, his/her mood decreases by 11. 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 tt (1t1041 \le t \le 10^4) — the number of testcases.

The only line of each testcase contains four integers a1,a2,a3,a4a_1, a_2, a_3, a_4 (0a1,a2,a3,a41080 \le a_1, a_2, a_3, a_4 \le 10^8; a1+a2+a3+a41a_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.

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 55. 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-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 22. Then she can tell 22 jokes of the third type. Alice's mood becomes 00. Bob's mood becomes 44. Then 44 jokes of the second type. Alice's mood becomes 44. Bob's mood becomes 00. Then another 44 jokes of the third type. Alice's mood becomes 00. Bob's mood becomes 44. Then the remaining joke of the second type. Alice's mood becomes 11. Bob's mood becomes 33. Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes 1-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 44 jokes of the fourth type until the spectators leave.

Sample Cases

Case 1

Input

4
5 0 0 0
0 0 0 5
2 5 10 6
3 0 0 7

Output

5
1
15
7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback