#P8957. Elastic Mushrooms MST

    ID: 22118 Type: Default 1000ms 256MiB

Elastic Mushrooms MST

Elastic Mushrooms MST

There are (n) elastic mushrooms, each having two attributes (a) and (b). For every pair of mushrooms (i) and (j), an undirected elastic channel is built with weight [ \max(a_i,a_j)+\max(b_i,b_j), ] forming a complete graph. Your task is to compute the total weight of the Minimum Spanning Tree (MST) of this graph.

Input: The first line contains an integer (n) ((2 \le n \le 10^5)), the number of mushrooms. Each of the following (n) lines contains two integers (a_i) and (b_i) ((1 \le a_i, b_i \le 10^9)).

Output: Output a single integer representing the total weight of the MST.

Hint: It is sufficient to consider candidate edges between vertices that are adjacent when the mushrooms are sorted by either (a) or (b).

inputFormat

The input begins with an integer (n) ((2 \le n \le 10^5)), the number of elastic mushrooms. Each of the next (n) lines contains two space-separated integers (a_i) and (b_i) ((1 \le a_i, b_i \le 10^9)).

outputFormat

Output a single line with one integer: the total weight of the minimum spanning tree of the graph.

sample

2
1 2
3 4
7