CF1430B.Barrels

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have nn barrels lined up in a row, numbered from left to right from one. Initially, the ii-th barrel contains aia_i liters of water.

You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels xx and yy (the xx-th barrel shouldn't be empty) and pour any possible amount of water from barrel xx to barrel yy (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them.

Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most kk times.

Some examples:

  • if you have four barrels, each containing 55 liters of water, and k=1k = 1, you may pour 55 liters from the second barrel into the fourth, so the amounts of water in the barrels are [5,0,5,10][5, 0, 5, 10], and the difference between the maximum and the minimum is 1010;
  • if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still 00.

你有 nn 个桶排成一排,从左到右依次编号为 11 到 nn。初始时,第 ii 个桶中装有 aia_i 升水。

你可以将水从一个桶倒入另一个桶。每次倒水操作中,你可以任选两个不同的桶 xx 和 yy(其中第 xx 个桶不能为空),并将任意数量的水(可以是全部)从桶 xx 倒入桶 yy。你可以假设所有桶的容量无限,因此每个桶中可容纳任意多的水。

在最多进行 kk 次倒水操作的前提下,求桶中水量最大值与最小值之间可能达到的最大差值。

一些示例:

  • 若你有四个桶,每个桶初始均装有 55 升水,且 k=1k = 1,则你可以将第二个桶中的全部 55 升水倒入第四个桶,此时各桶水量为 [5,0,5,10][5, 0, 5, 10],最大值与最小值之差为 1010;
  • 若所有桶初始均为空,则无法进行任何操作,此时最大值与最小值之差仍为 00。

输入格式

The first line contains one integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤k<n≤2⋅1051 \le k \lt n \le 2 \cdot 10^5) — the number of barrels and the number of pourings you can make.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^{9}), where aia_i is the initial amount of water the ii-th barrel has.

It's guaranteed that the total sum of nn over test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k<n≤2⋅1051 \le k \lt n \le 2 \cdot 10^5)—— 分别表示桶的数量以及你可以进行的倾倒操作次数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1090 \le a_i \le 10^{9}),其中 aia_i 表示第 ii 个桶初始所含的水量。

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

输出格式

For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most kk times.

对于每个测试用例,输出在最多倾倒 kk 次水的情况下,桶中水量的最大值与最小值之间可能达到的最大差值。

输入输出样例

  • 输入#1

    2
    4 1
    5 5 5 5
    3 2
    0 0 0

    输出#1

    10
    0

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

首页