A13785 | Barrels
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$ -th barrel contains $a_i$ liters of water.
You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$ -th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (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 $k$ times.
Some examples:
- if you have four barrels, each containing $5$ liters of water, and $k = 1$ , you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$ , and the difference between the maximum and the minimum is $10$ ;
- if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$ .
You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$ -th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (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 $k$ times.
Some examples:
- if you have four barrels, each containing $5$ liters of water, and $k = 1$ , you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$ , and the difference between the maximum and the minimum is $10$ ;
- if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$ .
输入格式
The first line contains one integer $t$ ( $1 \le t \le 1000$ ) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k < n \le 2 \cdot 10^5$ ) — the number of barrels and the number of pourings you can make.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^{9}$ ), where $a_i$ is the initial amount of water the $i$ -th barrel has.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$ .
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k < n \le 2 \cdot 10^5$ ) — the number of barrels and the number of pourings you can make.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^{9}$ ), where $a_i$ is the initial amount of water the $i$ -th barrel has.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \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 $k$ times.
输入输出样例
输入 #1
2 4 1 5 5 5 5 3 2 0 0 0
输出 #1
10 0
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted