2124B.md (1626B)
1 Source: https://codeforces.com/problemset/problem/2124/B 2 3 B. Minimise Sum 4 5 time limit per test 6 7 1.5 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 This problem differs from problem G. In this problem, you must output the minimum sum of prefix minimums after at most one operation. 22 23 You are given an array $a$ of length $n$, with elements satisfying $\boldsymbol{0 \le a_i \le n}$. You can perform the following operation at most once: 24 25 * Choose two indices $i$ and $j$ such that $i \lt j$. Set $a_i := a_i + a_j$. Then, set $a_j = 0$. 26 27 Output the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$ that you can get. 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 an integer $n$ ($2 \leq n \leq 2\cdot 10^5$) — the length of $a$. 34 35 The following line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$) — denoting 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 an integer on a new line, the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$. 42 43 Example 44 45 Input 46 47 ``` 48 3 49 50 2 51 52 1 2 53 54 3 55 56 1 2 3 57 58 4 59 60 3 0 2 3 61 ``` 62 63 Output 64 65 ``` 66 2 67 2 68 3 69 ``` 70 71 Note 72 73 In the second test case, it is optimal to perform the operation with $i=2$ and $j=3$. 74 75 In the third test case, it is optimal to not perform any operations. The answer is $3$.