Hello 2026
H
Minimise Cost
Codeforces
3500
原题
← GI1 →
翻译中…

For any sequence $b$ of length $m$, we define its cost function $f(b)$ as: $$ f(b) = m \cdot \sum_{i=1}^m b_i.$$

翻译中…

You are given a sequence $a$ of length $n$ and an integer $k$.

翻译中…

Your task is to partition the sequence $a$ into exactly $k$ non-empty subsequences$^{\text{∗}}$, denoted as $s_1, s_2, \ldots, s_k$. Every element of the original sequence $a$ must belong to exactly one of these subsequences.

翻译中…

Find the minimum possible value of the total cost: $$ \sum_{i=1}^k f(s_i).$$

翻译中…

$^{\text{∗}}$A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) element from arbitrary positions.

翻译中…

Input

翻译中…

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.

翻译中…

The first line contains two integers $n$ and $k$ ($1\le k\le n \le 2\cdot 10^5$) — the length of the sequence and the number of subsequences required.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($\mathbf{-10^9} \le a_i \le \mathbf{10^9}$) — the elements of the sequence $a$.

翻译中…

It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer – the minimum sum of cost over all subsequences.

翻译中…

Note

翻译中…

In the first test case, it is optimal to split into $[1,-2]$ and $[3]$. The total score is $2(1-2)+1(3)=1$.

翻译中…

In the second test case, the only possible way to partition is with $1$ subsequence $[1,3,-2]$. The score is $3(1+3-2)=6$.

翻译中…

样例

Input #1
6
3 2
1 3 -2
3 1
1 3 -2
10 4
-4 -6 -8 6 -3 -7 -3 1 6 -5
10 9
1 -2 6 -2 -6 4 3 3 7 -1
20 5
-5 9 -4 10 -2 4 -1 3 5 6 7 9 8 1 0 -6 4 5 8 9
50 26
7 10 10 2 2 1 7 4 4 8 5 8 -10 6 1 4 7 8 0 0 -8 1 1 5 0 0 6 0 7 4 6 0 7 4 -2 0 0 8 1 4 -7 0 6 -9 4 10 8 2 0 9
Output #1
1
6
-239
5
131
-404
InputCopy OutputCopy
题目信息
题号
2183H
所属比赛
时间限制
6 s
内存限制
512 MB
标签
二分
动态规划
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。