Equalizing


Submit solution

Points: 12
Time limit: 2.0s
Memory limit: 64M

Problem types

You are given an array \(A\) consisting of \(N\) integers. In one move you can choose any \(A_i\) and divide it by 2 rounding down.

You can perform such an operation any (possibly, zero) number of times with any \(A_i\).

Your task is to calculate the minimum possible number of operations required to obtain at least \(K\) equal numbers in the array.

Don't forget that it is possible to have \(A_i\) = 0 after some operations, thus the answer always exists.

Input Specifications

The first line of the input contains two integers \(N\) and \(K\) \((1 \le K \le N \le 10^5)\) the number of elements in the array and the number of equal numbers required.

The second line of the input contains \(N\) integers \((1 \le A_i \le 2 \times 10^5)\) where \(A_i\) is the \(i^{th}\) element of \(A\).

Output Specifications

Print one integer - the minimum possible number of operations required to obtain at least \(K\) equal numbers in the array.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

1

Sample Input 2

5 3
1 2 3 4 5

Sample Output 2

2

Sample Input 3

5 3
1 2 3 3 3

Sample Output 3

0

Comments

There are no comments at the moment.