codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1622C.md (2123B)


      1 Source: https://codeforces.com/problemset/problem/1622/C
      2 
      3 C. Set or Decrease
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given an integer array $a_1, a_2, \dots, a_n$ and integer $k$.
     22 
     23 In one step you can
     24 
     25 * either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$);
     26 * or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$).
     27 
     28 What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative).
     29 
     30 Input
     31 
     32 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     33 
     34 The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum.
     35 
     36 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself.
     37 
     38 It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
     39 
     40 Output
     41 
     42 For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$.
     43 
     44 Example
     45 
     46 Input
     47 
     48 ```
     49 4
     50 1 10
     51 20
     52 2 69
     53 6 9
     54 7 8
     55 1 2 1 3 1 2 1
     56 10 1
     57 1 2 3 1 2 6 1 6 8 10
     58 ```
     59 
     60 Output
     61 
     62 ```
     63 10
     64 0
     65 2
     66 7
     67 ```
     68 
     69 Note
     70 
     71 In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$.
     72 
     73 In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it.
     74 
     75 In the third test case, you can, for example:
     76 
     77 1. set $a_4 = a_3 = 1$;
     78 2. decrease $a_4$ by one, and get $a_4 = 0$.
     79 
     80 As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps.
     81 
     82 In the fourth test case, you can, for example:
     83 
     84 1. choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$;
     85 2. choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$.
     86 
     87 As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.