

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点のグラフがあり、頂点には の番号がついています。グラフにははじめ辺がありません。
このグラフに対して の順に以下の操作を行います。
- を満たす各整数 について頂点 と頂点 の間にコスト の無向辺を追加する
すべての操作を終えた後グラフは連結であるか判定し、連結である場合はこのグラフの最小全域木のコストを求めてください。
ただし、最小全域木とはコストが最小の全域木のことを指し、全域木のコストとは全域木に使われた辺のコストの和のことを指します。
制約
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
出力
グラフが連結である場合は最小全域木のコストを出力せよ。そうでない場合は を出力せよ。
入力例 1Copy
4 3 1 2 2 1 3 4 2 4 5
出力例 1Copy
22
以下の辺からなる全域木が最小全域木のひとつとなります。
- 頂点 と を結ぶコスト の辺
- 頂点 と を結ぶコスト の辺
- 頂点 と を結ぶコスト の辺
- 頂点 と を結ぶコスト の辺
- 頂点 と を結ぶコスト の辺
- 頂点 と を結ぶコスト の辺
であるため、 を出力します。
入力例 2Copy
6 2 1 2 10 4 6 10
出力例 2Copy
-1
グラフは非連結です。
入力例 3Copy
200000 4 1 200000 1000000000 1 200000 998244353 1 200000 999999999 1 200000 999999999
出力例 3Copy
199651870599998
Score : points
Problem Statement
There is a graph with vertices, numbered . Initially, the graph has no edges.
For this graph, perform the following operation for in order:
- For each integer satisfying , add an undirected edge with cost between vertices and .
Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.
A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print .
Sample Input 1Copy
4 3 1 2 2 1 3 4 2 4 5
Sample Output 1Copy
22
The following edges form a minimum spanning tree:
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
Since , print .
Sample Input 2Copy
6 2 1 2 10 4 6 10
Sample Output 2Copy
-1
The graph is disconnected.
Sample Input 3Copy
200000 4 1 200000 1000000000 1 200000 998244353 1 200000 999999999 1 200000 999999999
Sample Output 3Copy
199651870599998