1832B.md (1878B)
1 Source: https://codeforces.com/problemset/problem/1832/B 2 3 B. Maximum Sum 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_1, a_2, \dots, a_n$, where all elements are different. 22 23 You have to perform exactly $k$ operations with it. During each operation, you do exactly one of the following two actions (you choose which to do yourself): 24 25 * find two minimum elements in the array, and delete them; 26 * find the maximum element in the array, and delete it. 27 28 You have to calculate the maximum possible sum of elements in the resulting array. 29 30 Input 31 32 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 33 34 Each test case consists of two lines: 35 36 * the first line contains two integers $n$ and $k$ ($3 \le n \le 2 \cdot 10^5$; $1 \le k \le 99999$; $2k \lt n$) — the number of elements and operations, respectively. 37 * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; all $a_i$ are different) — the elements of the array. 38 39 Additional constraint on the input: the sum of $n$ does not exceed $2 \cdot 10^5$. 40 41 Output 42 43 For each test case, print one integer — the maximum possible sum of elements in the resulting array. 44 45 Example 46 47 Input 48 49 ``` 50 6 51 52 5 1 53 54 2 5 1 10 6 55 56 5 2 57 58 2 5 1 10 6 59 60 3 1 61 62 1 2 3 63 64 6 1 65 66 15 22 12 10 13 11 67 68 6 2 69 70 15 22 12 10 13 11 71 72 5 1 73 74 999999996 999999999 999999997 999999998 999999995 75 ``` 76 77 Output 78 79 ``` 80 21 81 11 82 3 83 62 84 46 85 3999999986 86 ``` 87 88 Note 89 90 In the first testcase, applying the first operation produces the following outcome: 91 92 * two minimums are $1$ and $2$; removing them leaves the array as $[5, 10, 6]$, with sum $21$; 93 * a maximum is $10$; removing it leaves the array as $[2, 5, 1, 6]$, with sum $14$. 94 95 $21$ is the best answer. 96 97 In the second testcase, it's optimal to first erase two minimums, then a maximum.