#P3620. [APIO/CTSC2007] 数据备份
[APIO/CTSC2007] 数据备份
Description
You work at an IT company backing up computer data for large office buildings. However, data backup is boring, so you want to design a system that lets different office buildings back up each other’s data while you sit at home enjoying computer games.
All the office buildings are located on the same street. You decide to pair the buildings (two per group). Each pair can back up each other by laying a network cable between the two buildings.
However, network cables are expensive. The local telecom company can provide only cables, which means you can arrange backups for only pairs of buildings (or a total of buildings). Each building belongs to exactly one pair (in other words, those buildings are all distinct).
Additionally, the telecom company charges by the length of the cable in kilometers. Hence, you need to choose the pairs of buildings so that the total cable length is as short as possible. In other words, you need to choose the pairs so that the sum of the distances between each pair (the total distance) is minimized.
Here is an example. Suppose you have clients whose buildings lie on a single street, as shown below. The buildings are located at distances , , , , and from the start of the street. The telecom company provides only cables.

In this example, the best pairing is to connect the st and nd buildings, and the rd and th buildings. This uses exactly cables as required. The length of the first cable is , and the length of the second cable is . This pairing requires a total of of cable, achieving the minimum possible sum of distances.
Input Format
The first line of input contains integers and , where () is the number of office buildings, and () is the number of cables available.
Each of the next lines contains a single integer (), the distance of each building from the start of the street. These integers are given in increasing order.
Output Format
Output a single positive integer: the minimum total cable length required to connect distinct buildings into pairs.
5 2
1
3
4
6
12
4
Hint
of the testdata satisfy .
of the testdata satisfy .
Translated by ChatGPT 5
京公网安备 11011102002149号