#P3620. Optimizing Office Building Backups
Optimizing Office Building Backups
Optimizing Office Building Backups
In this problem, you are tasked with minimizing the total length of network cables used to pair office buildings for data backup. All office buildings are located along the same street at various distances from a fixed starting point.
You are given an integer representing the number of office buildings and an integer representing the number of available network cables. Each cable can connect a pair of distinct office buildings. Note that each building can be part of at most one pair, so exactly buildings will be used in the pairing.
Your goal is to select disjoint pairs of office buildings so that the sum of the distances (in kilometers) between the buildings in each pair is minimized. The distance between two office buildings located at positions and is given by .
Example:
If there are office buildings located at 1 km, 3 km, 4 km, 6 km, 12 km and , one optimal solution is to pair the first with the second building and the third with the fourth building. The lengths of the cables will then be and respectively, giving a total length of , which is the minimum possible sum.
inputFormat
The input consists of two lines:
The first line contains two integers and where , representing the total number of office buildings and the number of network cables (pairs) available.
The second line contains integers, each representing the position (in km) of an office building along the street. The positions may not be given in sorted order.
outputFormat
Output a single integer: the minimum possible total length of the network cables required to connect the pairs of office buildings.
sample
5 2
1 3 4 6 12
4