APIO '07 P2 - Backup

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 1.0s
Memory limit: 32M

Problem type

You run an IT company that backs up computer data for large offices. Backing up data is not fun, and so you design your system so that the different offices can back up each others' data while you sit at home and play computer games instead.

The offices are all situated along a single street. You decide to pair up the offices, and for each pair of offices you run a network cable between the two buildings so that they can back up each others' data.

However, network cables are expensive. Your local telecommunications company will only give you k network cables, which means you can only arrange backups for k pairs of offices (2k offices in total). No office may belong to more than one pair (that is, these 2k offices must all be different).

Furthermore, the telecommunications company charges by the kilometre. This means that you need to choose these k pairs of offices so that you use as little cable as possible. In other words, you need to choose the pairs so that, when the distances between the two offices in each pair are added together, the total distance is as small as possible.

As an example, suppose you had five clients with offices on a street as illustrated below. These offices are situated 1\text{ km}, 3\text{ km}, 4\text{ km}, 6\text{ km} and 12\text{ km} from the beginning of the street. The telecommunications company will only provide you with k = 2 cables.

12 km 6 km 4 km 3 km 1 km

The best pairing in this example is created by linking the first and second offices together, and linking the third and fourth offices together. This uses k = 2 cables as required, where the first cable has length 3\text{ km} - 1\text{ km} = 2\text{ km}, and the second cable has length 6\text{ km} - 4\text{ km} = 2\text{ km}. This pairing requires a total of 4 km of network cables, which is the smallest total possible.

Input

The first line of input will contain the integers n and k, representing the number of offices on the street (2 \le n \le 100\,000) and the number of available network cables (1 \le k \le \frac n 2).

The following n lines will each contain a single integer (0 \le s \le 1\,000\,000\,000), representing the distance of each office from the beginning of the street. These integers will appear in sorted order from smallest to largest. No two offices will share the same location.

Output

Output should consist of a single positive integer, giving the smallest total length of network cable required to join 2k distinct offices into k pairs.

Sample Input

5 2
1
3
4
6
12

Sample Output

4

Explanation

The sample input above represents the example scenario described earlier.

Scoring

The score for each input scenario will be 100\% if the correct answer is written to the output file, and 0\% otherwise. For 30\% of the available marks, n \le 20. For 60\% of the available marks, n \le 10\,000.


Comments

There are no comments at the moment.