#P3620. [APIO/CTSC2007] 数据备份

    ID: 1798 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心2007WC/CTSC/集训队APIO优先队列差分

[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 KK cables, which means you can arrange backups for only KK pairs of buildings (or a total of 2K2K buildings). Each building belongs to exactly one pair (in other words, those 2K2K buildings are all distinct).

Additionally, the telecom company charges by the length of the cable in kilometers. Hence, you need to choose the KK pairs of buildings so that the total cable length is as short as possible. In other words, you need to choose the KK pairs so that the sum of the distances between each pair (the total distance) is minimized.

Here is an example. Suppose you have 55 clients whose buildings lie on a single street, as shown below. The 55 buildings are located at distances 1km1\rm km, 3km3\rm km, 4km4\rm km, 6km6\rm km, and 12km12\rm km from the start of the street. The telecom company provides only K=2K=2 cables.

In this example, the best pairing is to connect the 11st and 22nd buildings, and the 33rd and 44th buildings. This uses exactly K=2K=2 cables as required. The length of the first cable is 3km1km=2km\rm 3km-1km = 2km, and the length of the second cable is 6km4km=2km\rm 6km―4km = 2 km. This pairing requires a total of 4km4\rm km of cable, achieving the minimum possible sum of distances.

Input Format

The first line of input contains integers NN and KK, where NN (1N1051 \leq N \leq 10^5) is the number of office buildings, and KK (1KN2\displaystyle 1\leq K \leq \frac{N}{2}) is the number of cables available.

Each of the next NN lines contains a single integer ss (0s1090 \leq s \leq 10^9), 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 2K2K distinct buildings into KK pairs.

5 2 
1 
3 
4 
6 
12 
4

Hint

30%30\% of the testdata satisfy N20N \leq 20.

60%60\% of the testdata satisfy N104N \leq 10^4.

Translated by ChatGPT 5