#P9667. Equalizing Tower Heights

    ID: 22814 Type: Default 1000ms 256MiB

Equalizing Tower Heights

Equalizing Tower Heights

Prof. Pang built \(n\) block towers with different heights. The \(i\)-th tower has height \(a_i\). Prof. Shou dislikes the arbitrary heights. He decides to first remove exactly \(m\) of them and then perform an arbitrary number (possibly zero) of the following operations (in any order) on the remaining towers:

  • Choose a tower and increase its height \(a_i\) by 1.
  • Choose a tower and decrease its height \(a_i\) by 1.
  • Choose a tower and divide its height \(a_i\) by 2. (If the new height is not an integer, it is rounded down.)

An operation is not allowed if it makes the tower’s height become 0. Prof. Shou cannot operate on a tower that has been removed. His goal is to make all the towers which remain have the same height. Calculate the minimum number of operations needed to achieve this.

Note: The removals are free but exactly \(m\) towers must be removed.

inputFormat

The first line contains two integers \(n\) and \(m\) \((1 \leq n \leq 10^5,\ 0 \leq m < n)\) — the total number of towers and the number of towers to remove.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((1 \leq a_i \leq 10^9)\) representing the heights of the towers.

outputFormat

Output a single integer representing the minimum number of operations required to make all the remaining towers have the same height.

sample

3 1
4 8 2
1