There is a vending machine that sells lemonade. The machine has a total of nn slots. You know that initially, the ii-th slot contains aiai cans of lemonade. There are also nn buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot.
When you press the button corresponding to the ii-th slot, one of two events occurs:
- If there is a can of lemonade in the ii-th slot, it will drop out and you will take it. At this point, the number of cans in the ii-th slot decreases by 11.
- If there are no cans of lemonade left in the ii-th slot, nothing will drop out.
After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: a1,a2,…,ana1,a2,…,an.
Determine the minimum number of button presses needed to guarantee that you receive at least kk cans of lemonade.
Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least kk cans of lemonade in total in the machine. In other words, k≤a1+a2+…+ank≤a1+a2+…+an.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1091≤k≤109) — the number of slots in the machine and the required number of cans of lemonade.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the number of cans in the slots.
It is guaranteed that k≤a1+a2+…+ank≤a1+a2+…+an, meaning there are at least kk cans of lemonade in the machine.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output a single integer — the minimum number of button presses needed to guarantee that you receive at least kk cans of lemonade.
Examples
Inputcopy | Outputcopy |
---|---|
5 2 1 1 1 2 2 1 2 3 4 2 1 3 10 50 1 1 3 8 8 9 12 13 27 27 2 1000000000 1000000000 500000000 | 1 2 5 53 1000000000 |
Note
In the first test case, we can simply press the first button and receive one can of lemonade.
In the second test case, we can press each button once and guarantee that we receive 22 cans of lemonade. Note that if we simply press one button twice, we might not be lucky, and that button could correspond to the first slot, in which case we would only receive 11 can of lemonade for two presses.
In the third test case, one of the optimal strategies is as follows:
Press the first button twice. After the first press, a can of lemonade will definitely drop out. Then there are two options:
- If no can of lemonade drops after the second press, we know that this button must correspond to the second slot, since a2=1a2=1 and a1,a3>1a1,a3>1. Then we can press the second button twice and the third button once. Since a1,a3≥2a1,a3≥2, we will definitely receive three cans of lemonade for these three presses. Thus, after 55 presses, we will have 44 cans of lemonade.
- If a can of lemonade drops after the second press, we can make one press on the second button and one press on the third button. After each of these presses, we will definitely receive a can of lemonade. Thus, after 44 presses, we will have 44 cans of lemonade.
It can be shown that it is impossible to guarantee receiving 44 cans of lemonade with only 44 presses, so the answer is 55.、
最优策略肯定是尽可能少的按没有柠檬水的按钮,所以一开始先把所有按钮都按 mini=1nai 下,如果中途就有 k 瓶了答案就是 k。但是按完后就会有一个空了的按钮,考虑最坏情况,一开始就按到了这个按钮,之后就可以排除掉。接下来的操作跟之前一样,把每个没排除掉的按钮按最小值下,中途就有 k 瓶了就直接输出答案。实现时可以先排一遍序,从左到右每次把一个按钮以及它后面的按钮都按 ai−ai−1 下。
#include<bits/stdc++.h>
#define int long long
#define N 1000005
#define INF 0x3f3f3f3f
using namespace std;
int a[N];
signed main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
a[0]=0;
int n,cnt;
int ans=0;
cin>>n>>cnt;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
if((a[i]-a[i-1])*(n-i+1)>=cnt) {
cout<<ans+cnt<<endl;
break;
}
else {
ans+=(a[i]-a[i-1])*(n-i+1)+1;
cnt-=(a[i]-a[i-1])*(n-i+1);
}
}
}
return 0;
}