2264B.md (2517B)
1 Source: https://codeforces.com/problemset/problem/2264/B 2 3 B. Knife's Pill Farm 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 Mr. Knife has drafted $n$ absurd posts for a channel on a chat platform. His goal is to farm pill emoji reactions. Unfortunately, the channel's pill-farming bot uses an unnecessarily elaborate scoring rule. 22 23 The drafts have absurdity ratings $a_1, a_2, \ldots, a_n$, which may be negative. Mr. Knife must publish exactly $m$ drafts in their original order. Their ratings form a subsequence$^{\text{∗}}$ $b$ of $a$ with length $m$. 24 25 His pill score starts at $0$. When he publishes the $i$-th chosen draft, the bot changes his score by $i \cdot (b_i - b_{i-1})$, where $b_0 = 0$. A negative change deducts points, and the score is allowed to become negative. Thus, his final pill score is $$ \sum_{i = 1}^{m} i \cdot (b_i - b_{i - 1}). $$ 26 27 What is the maximum pill score Mr. Knife can obtain by choosing which drafts to publish? 28 29 $^{\text{∗}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from arbitrary positions. 30 31 Input 32 33 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. 34 35 The first line of each test case contains two integers $n$ and $m$ ($1 \le m \le n \le 2 \cdot 10^5$) — the number of drafts and the number of posts Mr. Knife must publish. 36 37 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^7 \le a_i \le 10^7$) — the absurdity ratings of the drafts. 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 40 41 Output 42 43 For each test case, print one integer — the maximum pill score Mr. Knife can obtain by publishing exactly $m$ drafts in their original order. 44 45 Example 46 47 Input 48 49 ``` 50 6 51 52 5 3 53 54 0 8 1 7 3 55 56 4 3 57 58 0 -4 10 -2 59 60 4 2 61 62 0 5 -2 4 63 64 6 3 65 66 0 9 8 7 6 5 67 68 1 1 69 70 7 71 72 3 2 73 74 5 -100 4 75 ``` 76 77 Output 78 79 ``` 80 20 81 82 34 83 84 10 85 86 15 87 88 7 89 90 108 91 ``` 92 93 Note 94 95 In the first test case, Mr. Knife can publish the drafts with ratings $[0, 1, 7]$. His final pill score is $$ 1 \cdot (0 - 0) + 2 \cdot (1 - 0) + 3 \cdot (7 - 1) = 20. $$ 96 97 In the second test case, he can publish the drafts with ratings $[0, -4, 10]$. The second post deducts points, but the third post more than makes up for it. His final pill score is $$ 1 \cdot (0 - 0) + 2 \cdot (-4 - 0) + 3 \cdot (10 - (-4)) = 34. $$