#K60492. Minimize Highway Construction Cost
Minimize Highway Construction Cost
Minimize Highway Construction Cost
You are given n cities located on a straight line, and m highways to build between them. The cities are given as coordinates in non-decreasing order. The cost of building a highway is defined as the distance between the two cities it connects. To minimize the total construction cost, you can choose to connect some cities directly such that the sum of the distances between adjacent connected cities is minimized.
One useful strategy is to build highways such that the largest gaps between consecutive cities are left unconnected. Formally, if the distances between consecutive cities are \(d_1, d_2, \dots, d_{n-1}\), then by removing the largest \(m-1\) distances, the minimum cost equals the sum of the remaining distances. Note that if m is greater than or equal to n, then every city can be connected individually (or isolated), resulting in a cost of 0.
Your task is to compute the minimum possible total cost given the coordinates of all cities.
inputFormat
The first line contains two integers n and m (the number of cities and the number of highways, respectively).
The second line contains n integers representing the coordinates of each city in non-decreasing order.
outputFormat
Output a single integer representing the minimum total cost for building the highways.
## sample4 2
1 3 5 7
4
</p>