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 5.4.1) (judge verdict: Memory Limit Exceeded)

//Dijkstra ABC035-D
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384
 
int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}

//Heap and Priority_queue

typedef struct{
long long node;
long long cost;
}data;
 
data heap[16777216];
long long hsize=0;
 
void resheap(){
    long long i;
    heap[0].node=-1;
    heap[0].cost=-llinf;
    for(i=1;i<16777216;i++){
        heap[i].node=-1;
        heap[i].cost=llinf;
    }
}
 
int swjud(data high,data low){
    if(high.cost > low.cost){return 1;}
    return -1;
}
 
void pqpush(data x){
    long long a,b;
    data m;
    hsize++;
    heap[hsize]=x;
    a=hsize;
    while(1){
        b=a/2;
        if(swjud(heap[b],heap[a]) == 1){
            m=heap[a];heap[a]=heap[b];heap[b]=m;
        }
        else{break;}
        a=b;
    }
}
 
data pqpop(){
    long long a,b,c;
    data m,sw;
    m=heap[1];
    heap[1]=heap[hsize];
    heap[hsize].node=-1;
    heap[hsize].cost=llinf;
    hsize--;
    a=1;
    while(1){
        b=a*2;c=a*2+1;
        if(swjud(heap[a],heap[b])==-1 && swjud(heap[a],heap[c])==-1){break;}
        if(swjud(heap[b],heap[c])==-1){
            sw=heap[a];heap[a]=heap[b];heap[b]=sw;
            a=b;
        }
        else{
            sw=heap[a];heap[a]=heap[c];heap[c]=sw;
            a=c;
        }
    }
    return m;
}

typedef struct{
long long xz;
long long yz;
long long node;
}zhd;

int xsortfnc(const void *a,const void *b){
if(((zhd*)a)->xz == ((zhd*)b)->xz){return 0;}
if(((zhd*)a)->xz < ((zhd*)b)->xz){return -1;}
return 1;
}

int ysortfnc(const void *a,const void *b){
if(((zhd*)a)->yz == ((zhd*)b)->yz){return 0;}
if(((zhd*)a)->yz < ((zhd*)b)->yz){return -1;}
return 1;
}

int main(void){
    resheap();
    zhd xs[131072],ys[131072];
    long long i,j,n,x[131072],y[131072],fl[131072]={0},r=0;
    data hen[131072][4],fd;
    scanf("%lld",&n);
    for(i=0;i<n;i++){
        scanf("%lld%lld",&x[i],&y[i]);
        for(j=0;j<4;j++){
            hen[i][j].node=-1;
            hen[i][j].cost=llinf;
        }
        xs[i].node=i;xs[i].xz=x[i];xs[i].yz=y[i];
        ys[i].node=i;ys[i].xz=x[i];ys[i].yz=y[i];
    }
    qsort(xs,n,sizeof(zhd),xsortfnc);
    qsort(ys,n,sizeof(zhd),ysortfnc);
    for(i=0;i<n;i++){
        if(i!=0){
            hen[xs[i].node][0].node=xs[i-1].node;
            hen[xs[i].node][0].cost=xs[i].xz-xs[i-1].xz;
            hen[ys[i].node][2].node=ys[i-1].node;
            hen[ys[i].node][2].cost=ys[i].yz-ys[i-1].yz;
        }
        if(i!=n-1){
            hen[xs[i].node][1].node=xs[i+1].node;
            hen[xs[i].node][1].cost=xs[i+1].xz-xs[i].xz;
            hen[ys[i].node][3].node=ys[i+1].node;
            hen[ys[i].node][3].cost=ys[i+1].yz-ys[i].yz;
        }
    }
    fd.node=0;fd.cost=0;
    pqpush(fd);
    while(hsize){
        fd=pqpop();
        if(fd.node==-1){continue;}
        if(fl[fd.node]==1){continue;}
        fl[fd.node]=1;
        r+=fd.cost;
        for(i=0;i<4;i++){
            pqpush(hen[fd.node][i]);
        }
    }
    printf("%lld\n",r);
    return 0;
}