CF BUDDY
← Problems·

1328F · Make k Equal

2200 · greedy

Problem: You are given the array aa consisting of nn elements and the integer knk \le n.

You want to obtain at least kk equal elements in the array aa. In one move, you can make one of the following two operations:

  • Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of aa is mnmn then you choose such index ii that ai=mna_i = mn and set ai:=ai+1a_i := a_i + 1);
  • take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of aa is mxmx then you choose such index ii that ai=mxa_i = mx and set ai:=ai1a_i := a_i - 1).

Your task is to calculate the minimum number of moves required to obtain at least kk equal elements in the array.

Input Format: The first line of the input contains two integers nn and kk (1kn21051 \le k \le n \le 2 \cdot 10^5) — the number of elements in aa and the required number of equal elements.

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \le a_i \le 10^9), where aia_i is the ii-th element of aa.

Output Format: Print one integer — the minimum number of moves required to obtain at least kk equal elements in the array.

Sample Cases

Case 1

Input

6 5
1 2 2 4 2 3

Output

3

Case 2

Input

7 5
3 3 2 1 1 1 3

Output

4

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