Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

class iunionfind {
   public:
    // 連続するuintにのみ使える。vectorであるため定数時間がとれる。
    // 基本的にこれだけあればよい。任意の構造体を扱うときは、IDと構造体のマッピングをとるvectorを別途用意する。
    // libs-test.cppの iuf_map あたりを検索。
    vector<ll> partition;
    vector<ll> rank;

    iunionfind(ll n) {
        partition.resize(n);
        rank.resize(n);
        for (ll x = 0; x < n; x++) {
            partition[x] = x;
            rank[x] = 0;
        }
    }
    ll find(ll x) {
        if (partition[x] == x) {
            return x;
        } else {
            partition[x] = find(partition[x]);
            return partition[x];
        }
    }
    void unite(ll x, ll y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return;
        }
        if (rank[x] < rank[y]) {
            partition[x] = y;
        } else {
            partition[y] = x;
            if (rank[x] == rank[y]) {
                rank[x]++;
            }
        }
    }
    bool same(ll x, ll y) {
        return find(x) == find(y);
    }
};

void print() { cout << endl; }
template <typename Head, typename... Tail>
void print(Head head, Tail... tail) {
    int size = sizeof...(Tail);
    cout << head;
    if (size > 0) {
        cout << " ";
    }
    print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
    cout << head;
    print0(tail...);
}

struct xstruct {
    int64_t val;
};
bool operator<=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val <= rhs.val;
}
bool operator<(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val < rhs.val;
}
bool operator>=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val >= rhs.val;
}
bool operator>(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val > rhs.val;
}
bool operator==(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val == rhs.val;
}
bool operator!=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val != rhs.val;
}
ostream& operator<<(ostream& os, xstruct const& xst) {
    return os << xst.val;
}

struct city {
    ll x;
    ll y;
    ll id;
};
struct edg {
    ll u;
    ll v;
    ll cost;
};
int main() {
    ll N;
    cin >> N;
    vector<city> cities(N);
    for (ll i = 0; i < N; i++) {
        cin >> cities[i].x;
        cin >> cities[i].y;
        cities[i].id = i;
    }
    vector<city> cities_x(N);
    vector<city> cities_y(N);
    copy(cities.begin(), cities.end(), cities_x.begin());
    copy(cities.begin(), cities.end(), cities_y.begin());

    sort(cities_x.begin(), cities_x.end(), [](city a, city b) { return a.x < b.x; });
    sort(cities_y.begin(), cities_y.end(), [](city a, city b) { return a.y < b.y; });

    iunionfind* uf = new iunionfind(N);

    map<ll, vector<city>> same_x;
    for (ll i = 0; i < N; i++) {
        if (same_x[cities[i].x].size() > 0) {
            uf->unite(cities[i].id, same_x[cities[i].x][0].id);
        }
        same_x[cities[i].x].push_back(cities[i]);
    }
    map<ll, vector<city>> same_y;
    for (ll i = 0; i < N; i++) {
        if (same_y[cities[i].y].size() > 0) {
            uf->unite(cities[i].id, same_y[cities[i].y][0].id);
        }
        same_y[cities[i].y].push_back(cities[i]);
    }

    vector<edg> dists;
    for (ll i = 1; i < N; i++) {
        city x1 = cities_x[i - 1];
        city x2 = cities_x[i];
        if (x1.x != x2.x) {
            dists.push_back({u : x1.id, v : x2.id, cost : x2.x - x1.x});
        }
    }
    for (ll i = 1; i < N; i++) {
        city y1 = cities_y[i - 1];
        city y2 = cities_y[i];
        if (y1.y != y2.y) {
            dists.push_back({u : y1.id, v : y2.id, cost : y2.y - y1.y});
        }
    }
    sort(dists.begin(), dists.end(), [](edg a, edg b) { return a.cost < b.cost; });

    ll total_cost = 0;
    for (auto edg : dists) {
        if (uf->same(edg.u, edg.v)) {
            continue;
        }
        total_cost += edg.cost;
        uf->unite(edg.u, edg.v);
    }
    cout << total_cost << endl;
}