## APIO '07 P2 - Backup

View as PDF

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 network cables, which means you can only arrange backups for pairs of offices ( offices in total). No office may belong to more than one pair (that is, these offices must all be different).

Furthermore, the telecommunications company charges by the kilometre. This means that you need to choose these 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 , , , and from the beginning of the street. The telecommunications company will only provide you with cables.

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 cables as required, where the first cable has length , and the second cable has length . This pairing requires a total of km of network cables, which is the smallest total possible.

#### Input

The first line of input will contain the integers and , representing the number of offices on the street and the number of available network cables .

The following lines will each contain a single integer , 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 distinct offices into 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 if the correct answer is written to the output file, and otherwise. For of the available marks, . For of the available marks, .