QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#13198. 备份

Statistics

你经营着一家为大型办公室备份计算机数据的 IT 公司。备份数据并不是一件有趣的事,因此你设计了一套系统,让不同的办公室可以互相备份数据,而你则可以坐在家里玩电脑游戏。

所有的办公室都位于同一条街道上。你决定将这些办公室两两配对,并为每一对办公室铺设一条网络电缆,以便它们可以互相备份数据。

然而,网络电缆非常昂贵。当地电信公司只会提供给你 $k$ 条网络电缆,这意味着你只能为 $k$ 对办公室安排备份(总共 $2k$ 个办公室)。每个办公室最多只能属于一对(也就是说,这 $2k$ 个办公室必须互不相同)。

此外,电信公司按公里收费。这意味着你需要选择这 $k$ 对办公室,使得所使用的电缆长度尽可能短。换句话说,你需要选择配对方式,使得每一对办公室之间的距离之和最小。

举个例子,假设你有五个客户,他们的办公室位于街道上,如下图所示。这些办公室距离街道起点的距离分别为 $1\,\text{km}$、$3\,\text{km}$、$4\,\text{km}$、$6\,\text{km}$ 和 $12\,\text{km}$。电信公司只会提供给你 $k = 2$ 条电缆。

在这个例子中,最佳的配对方式是将第一和第二个办公室配对,并将第三和第四个办公室配对。这使用了所需的 $k = 2$ 条电缆,其中第一条电缆的长度为 $3\,\text{km} - 1\,\text{km} = 2\,\text{km}$,第二条电缆的长度为 $6\,\text{km} - 4\,\text{km} = 2\,\text{km}$。这种配对方式总共需要 $4\,\text{km}$ 的网络电缆,这是可能达到的最小总长度。

输入格式

第一行包含两个整数 $n$ 和 $k$,分别表示街道上的办公室数量($2 \le n \le 100\,000$)和可用的网络电缆数量($1 \le k \le \frac{n}{2}$)。

接下来的 $n$ 行,每行包含一个整数($0 \le s \le 1\,000\,000\,000$),表示每个办公室距离街道起点的距离。这些整数将按从小到大的顺序给出。没有两个办公室位于同一位置。

输出格式

输出应包含一个正整数,表示将 $2k$ 个不同的办公室配成 $k$ 对所需的最短网络电缆总长度。

子任务

对于每个输入场景,如果输出文件中写入了正确答案,则得分为 100%,否则为 0%。对于 30% 的分数,满足 $n \le 20$。对于 60% 的分数,满足 $n \le 10\,000$。

样例

样例输入 1

5 2
1
3
4
6
12

样例输出 1

4

说明

上述样例输入代表了前面描述的示例场景。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.