2224B.md (2083B)
1 Source: https://codeforces.com/problemset/problem/2224/B 2 3 B. Zhily and Mex and Max 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 512 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Deeper in the wilderness, Zhily and Jily discovered a mysterious sequence of numbers. Each prefix of the sequence has an important characteristic value, known as the mex and the max. By rearranging the sequence, it can generate a special kind of magic. 22 23 You are given an array $a$ of $n$ non-negative integers. You can rearrange it arbitrarily. 24 25 Find the maximum possible value of the sum of the MEX$^{\text{∗}}$ and the maximum value over all prefixes. Formally, you need to maximize the following expression: $\sum\limits_{i=1}^n (\operatorname{mex}(a_1,a_2,\cdots,a_i)+\max(a_1,a_2,\cdots,a_i))$. 26 27 $^{\text{∗}}$The minimum excluded (MEX) of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $c$. 28 29 Input 30 31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 32 33 The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2\cdot 10^5$) — the length of the array $a$. 34 35 The second line contains $n$ integers $a_1,a_2,\cdots,a_n$ ($0\leq a_i \leq 10^9$) — the array $a$. 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$. 38 39 Output 40 41 For each test case, output the maximum sum on a new line. 42 43 Example 44 45 Input 46 47 ``` 48 5 49 50 5 51 52 0 0 0 0 0 53 54 2 55 56 0 1 57 58 5 59 60 1 1 1 1 0 61 62 6 63 64 1 1 4 5 1 4 65 66 1 67 68 1000000000 69 ``` 70 71 Output 72 73 ``` 74 5 75 76 4 77 78 13 79 80 30 81 82 1000000000 83 ``` 84 85 Note 86 87 In the first test case, no matter how you rearrange $a$, the MEX and the maximum value for each prefix will always be $1$ and $0$, respectively. 88 89 In the third test case, you can rearrange $a$ into $[0,1,1,1,1]$, so that $\sum\limits_{i=1}^n (\operatorname{mex}(a_1,a_2,\cdots,a_i)+\max(a_1,a_2,\cdots,a_i))=(1+0)+(2+1)+(2+1)+(2+1)+(2+1)=13$. It can be shown that this is the optimal solution.