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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}

	class Edge implements Comparable<Edge> {
		int[] id;
		int cost;

		public Edge(int id1_, int id2_, int cost_) {
			id = new int[] { id1_, id2_ };
			cost = cost_;
		}

		@Override
		public int compareTo(Edge o) {
			return Integer.compare(cost, o.cost);
		}
	}

	int NIL = Integer.MAX_VALUE;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		DJSet ds = new DJSet(n);
		int[][][] lis = new int[2][n][];
		for (int i = 0; i < n; ++i) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			lis[0][i] = new int[] { x, i };
			lis[1][i] = new int[] { y, i };
		}
		int[][] ref = new int[2][n];
		Arrays.sort(lis[0], new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});
		Arrays.sort(lis[1], new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i < n; ++i) {
				ref[t][lis[t][i][1]] = i;
			}
		}
		PriorityQueue<Edge> pq = new PriorityQueue<>();
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i + 1 < n; ++i) {
				pq.add(new Edge(lis[t][i][1], lis[t][i + 1][1], lis[t][i + 1][0] - lis[t][i][0]));
			}
		}
		List[] l = new List[2];
		for (int i = 0; i < 2; ++i) {
			l[i] = new List(n);
		}

		long ans = 0;
		while (!pq.isEmpty()) {
			Edge e = pq.poll();
			if (e.id[0] == NIL || e.id[1] == NIL || ds.equiv(e.id[0], e.id[1])) {
				continue;
			}
			ans += e.cost;
			ds.setUnion(e.id[0], e.id[1]);
			for (int i = 0; i < 2; ++i) {
				for (int j = 0; j < 2; ++j) {
					l[i].del(e.id[j]);
				}
			}
			for (int t = 0; t < 2; ++t) {
				for (int id : e.id) {
					int src = l[t].pre[lis[t][id][1]];
					int dst = l[t].ne[lis[t][id][1]];
					if (src == NIL || dst == NIL || ds.equiv(lis[t][src][1], lis[t][dst][1]))
						continue;
					pq.add(new Edge(src, dst, lis[t][dst][0] - lis[t][src][0]));
				}
			}
		}
		System.out.println(ans);
	}

	class List {
		int n;
		int[] arr;
		int[] ne;
		int[] pre;

		public List(int n_) {
			n = n_;
			arr = new int[n];
			ne = new int[n];
			pre = new int[n];
			Arrays.fill(ne, NIL);
			Arrays.fill(pre, NIL);
			for (int i = 0; i < n; ++i) {
				arr[i] = i;
			}
			for (int i = 0; i < n; ++i) {
				if (i - 1 >= 0) {
					pre[i] = i - 1;
				}
				if (i + 1 < n) {
					ne[i] = i + 1;
				}
			}
		}

		boolean del(int idx) {
			if (!exist(idx)) {
				return false;
			}
			arr[idx] = NIL;
			if (pre[idx] != NIL && exist(pre[idx])) {
				ne[pre[idx]] = ne[idx];
			}
			if (ne[idx] != NIL && exist(ne[idx])) {
				pre[ne[idx]] = pre[idx];
			}
			pre[idx] = NIL;
			ne[idx] = NIL;
			return true;
		}

		boolean exist(int idx) {
			return arr[idx] == NIL;
		}
	}

	class DJSet {
		int n;
		int[] upper;

		public DJSet(int n_) {
			n = n_;
			upper = new int[n];
			Arrays.fill(upper, -1);
		}

		boolean equiv(int x, int y) {
			return root(x) == root(y);
		}

		int root(int x) {
			return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
		}

		void setUnion(int x, int y) {
			x = root(x);
			y = root(y);
			if (x != y) {
				if (upper[x] < upper[y]) {
					int tmp = x;
					x = y;
					y = tmp;
				}
				upper[y] += upper[x];
				upper[x] = y;
			}
		}
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}