CF BUDDY
← Problems·

1622C · Set or Decrease

1600 · binary search, brute force, greedy

Problem: You are given an integer array a1,a2,,ana_1, a_2, \dots, a_n and integer kk.

In one step you can

  • either choose some index ii and decrease aia_i by one (make ai=ai1a_i = a_i - 1);
  • or choose two indices ii and jj and set aia_i equal to aja_j (make ai=aja_i = a_j).

What is the minimum number of steps you need to make the sum of array i=1naik\sum\limits_{i=1}^{n}{a_i} \le k? (You are allowed to make values of array negative).

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1n21051 \le n \le 2 \cdot 10^5; 1k10151 \le k \le 10^{15}) — the size of array aa and upper bound on its sum.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \le a_i \le 10^9) — the array itself.

It's guaranteed that the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output Format: For each test case, print one integer — the minimum number of steps to make i=1naik\sum\limits_{i=1}^{n}{a_i} \le k.

Note: In the first test case, you should decrease a1a_1 1010 times to get the sum lower or equal to k=10k = 10.

In the second test case, the sum of array aa is already less or equal to 6969, so you don't need to change it.

In the third test case, you can, for example:

  1. set a4=a3=1a_4 = a_3 = 1;
  2. decrease a4a_4 by one, and get a4=0a_4 = 0.

In the fourth test case, you can, for example:

  1. choose a7a_7 and decrease in by one 33 times; you'll get a7=2a_7 = -2;
  2. choose 44 elements a6a_6, a8a_8, a9a_9 and a10a_{10} and them equal to a7=2a_7 = -2.

Sample Cases

Case 1

Input

4
1 10
20
2 69
6 9
7 8
1 2 1 3 1 2 1
10 1
1 2 3 1 2 6 1 6 8 10

Output

10
0
2
7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback