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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <tuple>
#include <map>

using namespace std;

const int MAX = 1000000000;

struct Town {
  Town(int i_, int x_, int y_): i{i_}, x{x_}, y{y_} {}
  int i;
  int x;
  int y;
};

using Towns = vector<Town>;
using TownMap = map<int, vector<int>>;

tuple<int, int, int> find_min_path(const TownMap& map)
{
  int min_value = MAX;
  int town1 = -1, town2 = -1;
  auto prev = map.end();
  for (auto itor = map.begin(); itor != map.end(); ++itor) {
    if (prev != map.end()) {
      int d = itor->first - prev->first;
      if (d < min_value) {
        min_value = d;
        town1 = prev->second[0];
        town2 = itor->second[0];
      }
    }
    prev = itor;
  }
  return make_tuple(min_value, town1, town2);
}

tuple<int, int> find_min_path(
  const vector<bool>& visited,
  const TownMap& xs,
  const TownMap& ys,
  const Town& town
) {
  int next_i = -1, next_cost = MAX;

  // x
  for (auto i = xs.find(town.x); i != xs.end(); ++i) {
    for (const auto ti: i->second) {
      if (!visited[ti]) {
        int d = i->first - town.x;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  for (auto i = xs.find(town.x); i != xs.begin(); ) {
    --i;
    for (const auto ti: i->second) {
      if (!visited[ti]) {
        int d = town.x - i->first;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }

  // y
  for (auto i = ys.find(town.y); i != ys.end(); ++i) {
    for (const auto ti: i->second) {
      if (!visited[ti]) {
        int d = i->first - town.y;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  for (auto i = ys.find(town.y); i != ys.begin(); ) {
    --i;
    for (const auto ti: i->second) {
      if (!visited[ti]) {
        int d = town.y - i->first;
        if (d < next_cost) {
          next_i = ti;
          next_cost = d;
        }
        break;
      }
    }
  }
  return make_tuple(next_cost, next_i);
}

tuple<int, int> find_next_path(
  const Towns& towns,
  const vector<int>& vtowns,
  const vector<bool>& visited,
  const TownMap& xs,
  const TownMap& ys
) {
  int min_cost = MAX, min_town = -1;
  for (const auto& i: vtowns) {
    int cost, town;
    tie(cost, town) = find_min_path(visited, xs, ys, towns[i]);
    if (min_town == -1 or cost < min_cost) {
      min_cost = cost;
      min_town = town;
    }
  }
  return make_tuple(min_cost, min_town);
}

long build(const Towns& towns)
{
  map<int, vector<int>> xs, ys;
  for (const auto& town: towns) {
    xs[town.x].push_back(town.i);
    ys[town.y].push_back(town.i);
  }

  vector<bool> visited(towns.size(), false);
  vector<int> vtowns;
  vtowns.reserve(towns.size());

  int x_cost, x1, x2;
  int y_cost, y1, y2;
  tie(x_cost, x1, x2) = find_min_path(xs);
  tie(y_cost, y1, y2) = find_min_path(ys);

  long costs = 0;
  if (x_cost < y_cost) {
    vtowns.push_back(x1);
    vtowns.push_back(x2);
    visited[x1] = visited[x2] = true;
    costs += x_cost;
  } else {
    vtowns.push_back(y1);
    vtowns.push_back(y2);
    visited[y1] = visited[y2] = true;
    costs += y_cost;
  }

  while (vtowns.size() < towns.size()) {
    int town, cost;
    tie(cost, town) = find_next_path(towns, vtowns, visited, xs, ys);
    vtowns.push_back(town);
    visited[town] = true;
    costs += cost;
  }

  return costs;
}

int main()
{
  int N;
  cin >> N;
  Towns towns;
  towns.reserve(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    towns.push_back(Town(i, x, y));
  }

  cout << build(towns) << endl;
  return 0;
}