#K74867. Minimum Elevator Installation Cost

    ID: 34293 Type: Default 1000ms 256MiB

Minimum Elevator Installation Cost

Minimum Elevator Installation Cost

You are given n buildings, each with a given number of floors. Your task is to install exactly k elevators between any two buildings. The installation cost for an elevator between building i and building j is given by the absolute difference of their floor counts, i.e., \( |\text{floors}_i - \text{floors}_j| \). Find the minimum total installation cost by choosing exactly k pairs of distinct buildings such that the sum of the corresponding costs is minimized.

Note: It is guaranteed that \( k \leq \frac{n(n-1)}{2} \), so a valid selection is always possible.

inputFormat

The input is read from standard input. The first line contains two integers n and k, where n is the number of buildings and k is the number of elevators to install. The second line contains n space-separated integers, where the i-th integer represents the number of floors in the i-th building.

outputFormat

Output a single integer representing the minimum total installation cost to install exactly k elevators.

## sample
5 2
1 5 2 3 6
2

</p>