#P3620. Optimizing Office Building Backups

    ID: 16871 Type: Default 1000ms 256MiB

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 NN representing the number of office buildings and an integer KK 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 2K2K buildings will be used in the pairing.

Your goal is to select KK 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 xix_i and xjx_j is given by xixj|x_i-x_j|.

Example:
If there are N=5N=5 office buildings located at 1 km, 3 km, 4 km, 6 km, 12 km and K=2K=2, 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 (31)=2 km(3-1)=2\ \text{km} and (64)=2 km(6-4)=2\ \text{km} respectively, giving a total length of 4 km4\ \text{km}, which is the minimum possible sum.

inputFormat

The input consists of two lines:

The first line contains two integers NN and KK where 12KN1 \leq 2K \leq N, representing the total number of office buildings and the number of network cables (pairs) available.

The second line contains NN 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 KK pairs of office buildings.

sample

5 2
1 3 4 6 12
4