2137E.md (2190B)
1 Source: https://codeforces.com/problemset/problem/2137/E 2 3 E. Mexification 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 You are given an array $a$ of size $n$ and an integer $k$. You do the following procedure $k$ times: 22 23 * For each element $a_i$, you set $a_i$ to $\operatorname{mex}$$^{\text{∗}}$$(a_1,a_2,\ldots,a_{i-1},a_{i+1},a_{i+2}, \ldots,a_n)$. In other words, you set $a_i$ to the $\operatorname{mex}$ of all other elements in the array. This is done for all elements in the array at the same time. 24 25 Please find the sum of elements in the array after all $k$ operations. 26 27 $^{\text{∗}}$The minimum excluded (MEX) of a collection of integers $d_1, d_2, \ldots, d_k$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $d$. 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 contains two integers $n$ and $k$ ($2 \leq n \leq 2\cdot 10^5, 1 \leq k \leq 10^9$) – the number of elements in $a$ and the number of operations done. 34 35 The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \leq a_i \leq n$). 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 sum of elements after all $k$ operations on a new line. 42 43 Example 44 45 Input 46 47 ``` 48 5 49 50 3 3 51 52 0 2 1 53 54 2 4 55 56 0 2 57 58 4 1 59 60 0 0 1 1 61 62 8 7 63 64 6 6 2 4 3 0 1 8 65 66 2 2 67 68 0 0 69 ``` 70 71 Output 72 73 ``` 74 3 75 1 76 8 77 25 78 0 79 ``` 80 81 Note 82 83 In the first test case, we performed the operation on the array $[0,2,1]$ three times. Let's compute the result after the first time: 84 85 * The first element becomes $\operatorname{MEX}(2,1)=0$ 86 * The second element becomes $\operatorname{MEX}(0,1)=2$ 87 * The third element becomes $\operatorname{MEX}(0,2)=1$ 88 89 So, after the first operation, $[0,2,1]$ becomes $[0,2,1]$ again. It can be shown that the array will not change no matter how many times we perform the operation, so the final array after three operations is still $[0,2,1]$. The sum is $0+2+1=3$. 90 91 In the third test case, the array becomes $[2,2,2,2]$.