欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2042A.Greedy Monocarp
Greedy Monocarp
There are chests; the -th chest initially contains coins. For each chest, you can choose any non-negative ( or greater) number of coins to add to that chest, with one constraint: the total number of coins in all chests must become at least .
After you've finished adding coins to the chests, greedy Monocarp comes, who wants the coins. He will take the chests one by one, and since he is greedy, he will always choose the chest with the maximum number of coins. Monocarp will stop as soon as the total number of coins in chests he takes is at least .
You want Monocarp to take as few coins as possible, so you have to add coins to the chests in such a way that, when Monocarp stops taking chests, he will have exactly coins. Calculate the minimum number of coins you have to add.
Input
The first line contains one integer () — the number of test cases.
Each test case consists of two lines:
- the first line contains two integers and (; );
- the second line contains integers ().
Output
For each test case, print one integer — the minimum number of coins you have to add so that, when Monocarp stops taking the chests, he has exactly coins. It can be shown that under the constraints of the problem, it is always possible.
Note
In the first test case of the example, you don't have to add any coins. When Monocarp arrives, he will take the chest with coins, so he will have exactly coins.
In the second test case of the example, you can add coin to the -th chest, so, when Monocarp arrives, he will take a chest with coins, then another chest with coins, and a chest with coins.
In the third test case of the example, you can add coins to the -st chest and coins to the -nd chest.
In the fourth test case of the example, you can add coin to the -st chest and coin to the -rd chest.
Samples
4
5 4
4 1 2 3 2
5 10
4 1 2 3 2
2 10
1 1
3 8
3 3 3
0
1
8
2
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |