1792B.md (2998B)
1 Source: https://codeforces.com/problemset/problem/1792/B 2 3 B. Stand-up Comedian 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob. 22 23 Eve prepared $a_1 + a_2 + a_3 + a_4$ jokes to tell, grouped by their type: 24 25 * type 1: both Alice and Bob like them; 26 * type 2: Alice likes them, but Bob doesn't; 27 * type 3: Bob likes them, but Alice doesn't; 28 * type 4: neither Alice nor Bob likes them. 29 30 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. 31 32 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. 33 34 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. 35 36 Input 37 38 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. 39 40 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. 41 42 Output 43 44 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. 45 46 Example 47 48 Input 49 50 ``` 51 4 52 53 5 0 0 0 54 55 0 0 0 5 56 57 2 5 10 6 58 59 3 0 0 7 60 ``` 61 62 Output 63 64 ``` 65 5 66 1 67 15 68 7 69 ``` 70 71 Note 72 73 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. 74 75 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. 76 77 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. 78 79 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.