Logo
(追記) (追記ここまで)

19464번 - King's Roads 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 256 MB78272436.923%

문제

King Byteasar wants to introduce an innovative road network in Byteland. There are $n$ cities in Byteland. There are $a_i$ citizens living in $i$-th city.

Initially, there are no roads in Byteland. The king wants to build several roads in such a way that all cities are connected (directly or indirectly). He also wants to minimize expenses on maintaining the roads.

Naturally, if a road connects populous cities, it is used more heavily and thus is more expensive to maintain. Formally, maintaining a road between cities $i$ and $j$ costs $a_i + a_j$ bytalers per year. However, if $a_i + a_j$ is at least $M,ドル the road is considered a national highway, and it brings revenue of $M$ bytalers per year from taxation (thus, the effective cost for this road is $a_i + a_j - M$ per year). Note that all $a_i$ are less than $M$.

Help King Byteasar find the minimum annual cost for maintaining a set of roads so that all cities become connected.

입력

The first line of input contains two space-separated integers $n$ and $M$ (1ドル \leq n \leq 200,000円,ドル 1ドル \leq M \leq 10^9$).

The second line contains $n$ space-separated integers $a_1,ドル $\ldots,ドル $a_n$ (0ドル \leq a_i < M$).

출력

Print one integer --- the minimum annual cost for maintaining a spanning set of roads in bytalers.

제한

예제 입력 1

5 9
1 3 5 8 8

예제 출력 1

6

힌트

출처

Camp > Petrozavodsk Programming Camp > Summer 2016 > Day 3: Moscow IPT Contest K번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /