CF2183H.Minimise Cost

NOI/NOI+/CTSC

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

You are given a sequence aa of length nn and an integer kk.

Your task is to partition the sequence aa into exactly kk non-empty subsequences∗^{\text{∗}}, denoted as s1,s2,…,sks_1, s_2, \ldots, s_k. Every element of the original sequence aa 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 cc is a subsequence of a sequence dd if cc can be obtained from dd by the deletion of several (possibly, zero or all) element from arbitrary positions.

对于任意长度为 mm 的序列 bb,我们定义其代价函数 f(b)f(b) 为:

f(b)=m⋅∑i=1mbi.f(b) = m \cdot \sum_{i=1}^m b_i.

给定一个长度为 nn 的序列 aa 和一个整数 kk。

你的任务是将序列 aa 恰好划分为 kk 个非空子序列∗^{\text{∗}},记作 s1,s2,…,sks_1, s_2, \ldots, s_k。原始序列 aa 中的每个元素必须且仅属于这 kk 个子序列中的一个。

求总代价的最小可能值:

∑i=1kf(si).\sum_{i=1}^k f(s_i).

∗^{\text{∗}} 序列 cc 是序列 dd 的一个子序列,当且仅当 cc 可通过从 dd 中删除若干(可能为零个或全部)位于任意位置的元素而得到。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−109≤ai≤109\mathbf{-10^9} \le a_i \le \mathbf{10^9}) — the elements of the sequence aa.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051\le k\le n \le 2\cdot 10^5)—— 分别表示序列的长度以及所需子序列的个数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109\mathbf{-10^9} \le a_i \le \mathbf{10^9})—— 序列 aa 的元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——所有子序列的最小成本总和。

输入输出样例

  • 输入#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

    输出#1

    1
    6
    -239
    5
    131
    -404

说明/提示

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

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

在第一个测试用例中,最优的划分方式是分成 [1,−2][1,-2] 和 [3][3]。总得分为 2(1−2)+1(3)=12(1-2)+1(3)=1。

在第二个测试用例中,唯一可能的划分方式是仅含 11 个子序列 [1,3,−2][1,3,-2]。得分为 3(1+3−2)=63(1+3-2)=6。

输入解题思路,AI测评打分。不知道怎么写?

首页