QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 256 MB 満点: 100

#13003. 国王的道路

統計

Byteasar 国王想要在 Byteland 引入一种创新的道路网络。Byteland 共有 $n$ 座城市,第 $i$ 座城市居住着 $a_i$ 名公民。

最初,Byteland 没有道路。国王想要修建一些道路,使得所有城市都能(直接或间接)连通。同时,他希望最小化道路的维护费用。

通常情况下,如果一条道路连接了人口较多的城市,它的使用频率更高,因此维护成本也更高。形式化地,连接城市 $i$ 和城市 $j$ 的道路每年的维护成本为 $a_i + a_j$。然而,如果 $a_i + a_j \ge M$,该道路被视为“国道”,它每年会带来 $M$ 的税收收入(因此,该道路的实际年成本为 $a_i + a_j - M$)。注意,所有的 $a_i$ 都小于 $M$。

请帮助 Byteasar 国王找到使所有城市连通的最小年度道路维护成本。

输入格式

第一行包含两个空格分隔的整数 $n$ 和 $M$ ($1 \le n \le 200\,000$, $1 \le M \le 10^9$)。

第二行包含 $n$ 个空格分隔的整数 $a_1, \dots, a_n$ ($0 \le a_i < M$)。

输出格式

输出一个整数,表示使所有城市连通的最小年度道路维护成本。

样例

样例输入 1

5 9
1 3 5 8 8

样例输出 1

6

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.