#pragma warning(disable:4786)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdio>
#include <ctime>

using namespace std;

#define MEM(a,b) memset(a,(b),sizeof(a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b)  ((a) < (b) ? (a) : (b))
#define istr(S) istringstream sin(S)
#define MP make_pair
#define pb push_back
#define inf 1000000000
#define MAXN 1000000

//typedef long long  LL;
//typedef __int64 LL;

typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pi> vpi;

double pos[200005],ans[200005];
int n;

int check(double d)
{
    int i,j,cnt=0;

    for(i=0;i<n && cnt<3;i=j)
    {
        double x=pos[i]+d;

        for(j=i;j<n;j++)
            if(fabs(pos[j]-x)-1e-9>d) break;

        //printf("%d %lf %lf\n",i,x,d);
        ans[cnt++]=x;
    }

    if(i<n) return 0;

    for(;cnt<3;cnt++)
        ans[cnt]=ans[cnt-1];

    return 1;
}

int main()
{
    int i,j,k,tests,cs=0;
    double x,y,z;

    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%lf",&pos[i]);

    sort(pos,pos+n);

    double lo=0.0,hi=inf+1,res;

    while(hi-lo>1e-9)
    {
        double mid=(lo+hi)/2.0;

    //  mid=.5;
        if(check(mid))
            x=ans[0],y=ans[1],z=ans[2],res=hi=mid;
        else
            lo=mid;
        //break;
    }

    printf("%.6lf\n",res);
    printf("%.6lf %.6lf %.6lf\n",x,y,z);

    return 0;
}


#pragma warning(disable:4786)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdio>
#include <ctime>

using namespace std;

#define MEM(a,b) memset(a,(b),sizeof(a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b)  ((a) < (b) ? (a) : (b))
#define istr(S) istringstream sin(S)
#define MP make_pair
#define pb push_back
#define inf 1000000000
#define MAXN 1000000

//typedef long long  LL;
//typedef __int64 LL;

typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pi> vpi;

double pos[200005],ans[200005];
int n;

int check(double d)
{
	int i,j,cnt=0;

	for(i=0;i<n && cnt<3;i=j)
	{
		double x=pos[i]+d;

		for(j=i;j<n;j++)
			if(fabs(pos[j]-x)-1e-9>d) break;

		//printf("%d %lf %lf\n",i,x,d);
		ans[cnt++]=x;
	}

	if(i<n) return 0;

	for(;cnt<3;cnt++)
		ans[cnt]=ans[cnt-1];

	return 1;
}

int main()
{
	int i,j,k,tests,cs=0;
	double x,y,z;

	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%lf",&pos[i]);

	sort(pos,pos+n);

	double lo=0.0,hi=inf+1,res;

	while(hi-lo>1e-10)
	{
		double mid=(lo+hi)/2.0;

	//	mid=.5;
		if(check(mid))
			x=ans[0],y=ans[1],z=ans[2],res=hi=mid;
		else
			lo=mid;
		//break;
	}

	printf("%.6lf\n",res);
	printf("%.6lf %.6lf %.6lf\n",x,y,z);

	return 0;
}


#pragma warning(disable:4786)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdio>
#include <ctime>

using namespace std;

#define MEM(a,b) memset(a,(b),sizeof(a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b)  ((a) < (b) ? (a) : (b))
#define istr(S) istringstream sin(S)
#define MP make_pair
#define pb push_back
#define inf 1000000000
#define MAXN 1000000

//typedef long long  LL;
//typedef __int64 LL;

typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pi> vpi;

double pos[200005],ans[200005];
int n;

int check(double d)
{
	int i,j,cnt=0;

	for(i=0;i<n && cnt<3;i=j)
	{
		double x=pos[i]+d;

		for(j=i;j<n;j++)
			if(fabs(pos[j]-x)-1e-9>d) break;

		ans[cnt++]=x;
	}

	if(i<n) return 0;

	for(;cnt<3;cnt++)
		ans[cnt]=ans[cnt-1];

	return 1;
}

int main()
{
	int i,j,k,tests,cs=0;
	double x,y,z;

	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%lf",&pos[i]);

	sort(pos,pos+n);

	double lo=0.0,hi=inf+1,res;

	while(hi-lo>1e-7)
	{
		double mid=(lo+hi)/2.0;

	//	mid=.5;
		if(check(mid))
			x=ans[0],y=ans[1],z=ans[2],res=hi=mid;
		else
			lo=mid;
		//break;
	}

	printf("%.6lf\n",res);
	printf("%.6lf %.6lf %.6lf\n",x,y,z);

	return 0;
}


#pragma warning(disable:4786)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdio>
#include <ctime>

using namespace std;

#define MEM(a,b) memset(a,(b),sizeof(a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b)  ((a) < (b) ? (a) : (b))
#define istr(S) istringstream sin(S)
#define MP make_pair
#define pb push_back
#define inf 1000000000
#define MAXN 1000000

//typedef long long  LL;
//typedef __int64 LL;

typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pi> vpi;

double pos[200005],ans[200005];
int n;

int check(double d)
{
    int i,j,cnt=0;

    for(i=0;i<n && cnt<3;i=j)
    {
        double x=pos[i]+d;

        for(j=i;j<n;j++)
            if(fabs(pos[j]-x)-1e-9>d) break;

        //printf("%d %lf %lf\n",i,x,d);
        ans[cnt++]=x;
    }

    if(i<n) return 0;

    for(;cnt<3;cnt++)
        ans[cnt]=ans[cnt-1];

    return 1;
}

int main()
{
    int i,j,k,tests,cs=0;
    double x,y,z;

    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%lf",&pos[i]);

    sort(pos,pos+n);

    double lo=0.0,hi=inf+1,res;

    while(hi-lo>1e-5)
    {
        double mid=(lo+hi)/2.0;

    //  mid=.5;
        if(check(mid))
            x=ans[0],y=ans[1],z=ans[2],res=hi=mid;
        else
            lo=mid;
        //break;
    }

    printf("%.6lf\n",res);
    printf("%.6lf %.6lf %.6lf\n",x,y,z);

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <queue>

using namespace std;

#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

#define debug(x) cout << '>' << #x << ':' << x << endl;

#define forit(X,Y) for(typeof((Y).begin()) X = (Y).begin(); X != (Y).end(); ++X)

typedef long long int64;

typedef vector <int> vi;
typedef vector < vi > vvi;

int main () {
	//freopen("", "rt", stdin);
	//freopen("", "wt", stdout);

    int n;
    cin >> n;

    vector < long double > s(n);
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        s[i] = (long double) x;
    }
    sort(all(s));

    long double lo = 0;
    long double hi = 1000000000;

    vector < long double > ans(3, 0);
    for (int i = 0; i < 100; ++i) {
        long double d = (lo + hi) / 2;
        vector < long double > now;

        bool ok = false;
        int ind = 0;
        for (int j = 0; j < 3; ++j) {
            if (ind < n) now.pb(s[ind] + d);
            else now.pb(0);
            long double last = s[ind] + d;
            while ( ind < n && s[ind] <= last + d) ++ind;
        }
        if (ind == n) ok = true;
        if (!ok) {
            lo = d;
            ans = now;
        } else {
            hi = d;
        }

    }

    printf("%.6lf\n", (double) (lo + hi) / 2);
    for (int i = 0; i < 3; ++i) {
        printf("%.6lf ", (double) ans[i]);
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <queue>
#include <assert.h>

using namespace std;

#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

#define debug(x) cout << '>' << #x << ':' << x << endl;

#define forit(X,Y) for(typeof((Y).begin()) X = (Y).begin(); X != (Y).end(); ++X)

typedef long long int64;

typedef vector <int> vi;
typedef vector < vi > vvi;

void print(int64 x) {
    cout << x / 2;
    if (x % 2 == 0) cout << ".000000";
    else cout << ".500000";
}

int main () {
	//freopen("", "rt", stdin);
	//freopen("", "wt", stdout);

    int n;
    cin >> n;

    vector < int64 > a(n);

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        a[i] = 2 * x;
    }
    sort (all (a) );

    int64 lo = -1;
    int64 hi = 2000000000;

    while (hi - lo > 1) {
        int64 d = (hi + lo) / 2;

       int num = 0;
        int64 last = -1;
        for (int ind = 0; ind < n; ++ind) {
            if (a[ind] > last) {
                ++num;
                last = a[ind] + 2*d;
            }
        }
        if (num <= 3) {
            hi = d;
        } else {
            lo = d;
        }

    }
    int64 d = hi;

    int64 last = -1;
    vector <int64> ans;
    for (int ind = 0; ind < n; ++ind) {
        if (a[ind] > last) {
             ans.pb(a[ind] + d);
             last = a[ind] + 2 * d;
        }
    }

    assert(sz(ans) <= 3);
    while (sz(ans) < 3) ans.pb(0);

    print(d);
    cout << endl;
    for (int i = 0; i < 3; ++i) {
        print(ans[i]);
        cout << " ";
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <queue>

using namespace std;

#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

#define debug(x) cout << '>' << #x << ':' << x << endl;

#define forit(X,Y) for(typeof((Y).begin()) X = (Y).begin(); X != (Y).end(); ++X)

typedef long long int64;

typedef vector <int> vi;
typedef vector < vi > vvi;

int main () {
	//freopen("", "rt", stdin);
	//freopen("", "wt", stdout);

    int n;
    cin >> n;

    vector < long double > s(n);
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        s[i] = (long double) x;
    }
    sort(all(s));

    long double lo = 0;
    long double hi = 1000000000;

    vector < long double > ans;
    for (int i = 0; i < 100; ++i) {
        long double d = (lo + hi) / 2;
        vector < long double > now;

        bool ok = false;
        int ind = 0;
        for (int j = 0; j < 3; ++j) {
            if (ind < n) now.pb(s[ind] + d);
            else now.pb(0);
            long double last = s[ind] + d;
            while ( ind < n && s[ind] <= last + d) ++ind;
        }
        if (ind == n) ok = true;
        if (!ok) {
            lo = d;
        } else {
            hi = d;
        }

    }
    long double d = (lo + hi) / 2;
    int ind = 0;
    for (int j = 0; j < 3; ++j) {
        if (ind < n) ans.pb(s[ind] + d);
        else ans.pb(0);
        long double last = s[ind] + d;
        while ( ind < n && s[ind] <= last + d) ++ind;
    }
    printf("%.6lf\n", (double) (lo + hi) / 2);
    for (int i = 0; i < 3; ++i) {
        printf("%.6lf ", (double) ans[i]);
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <queue>

using namespace std;

#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

#define debug(x) cout << '>' << #x << ':' << x << endl;

#define forit(X,Y) for(typeof((Y).begin()) X = (Y).begin(); X != (Y).end(); ++X)

typedef long long int64;

typedef vector <int> vi;
typedef vector < vi > vvi;

int main () {
	//freopen("", "rt", stdin);
	//freopen("", "wt", stdout);

    int n;
    cin >> n;

    vector < long double > s(n);
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        s[i] = (long double) x;
    }
    sort(all(s));

    long double lo = 0;
    long double hi = 1000000000;

    vector < long double > ans;
    for (int i = 0; i < 100; ++i) {
        long double d = (lo + hi) / 2;
        vector < long double > now;

        bool ok = false;
        int ind = 0;
        for (int j = 0; j < 3; ++j) {
            if (ind < n) now.pb(s[ind] + d);
            else now.pb(0);
            long double last = s[ind] + d;
            while ( ind < n && s[ind] <= last + d) ++ind;
        }
        if (ind == n) ok = true;
        if (!ok) {
            lo = d;
        } else {
            hi = d;
        }

    }
    long double d = (lo + hi) / 2;
    int ind = 0;
    for (int j = 0; j < 3; ++j) {
        if (ind < n) {
            ans.pb(s[ind] + d);
            long double last = s[ind] + d;
            while ( ind < n && s[ind] <= last + d) ++ind;
        } else ans.pb(0);
    }
    printf("%.6lf\n", (double) (lo + hi) / 2);
    for (int i = 0; i < 3; ++i) {
        printf("%.6lf ", (double) ans[i]);
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <map>
#include <queue>

using namespace std;

#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

#define debug(x) cout << '>' << #x << ':' << x << endl;

#define forit(X,Y) for(typeof((Y).begin()) X = (Y).begin(); X != (Y).end(); ++X)

typedef long long int64;

typedef vector <int> vi;
typedef vector < vi > vvi;

int main () {
	//freopen("", "rt", stdin);
	//freopen("", "wt", stdout);

    int n;
    cin >> n;

    vector < long double > s(n);
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        s[i] = (long double) x;
    }
    sort(all(s));

    long double lo = 0;
    long double hi = 1000000000;

    vector < long double > ans;
    for (int i = 0; i < 100; ++i) {
        long double d = (lo + hi) / 2;
        vector < long double > now;

        bool ok = false;
        int ind = 0;
        for (int j = 0; j < 3; ++j) {
            if (ind < n) {
                long double last = s[ind] + d;
                while ( ind < n && s[ind] <= last + d) ++ind;
            } else break;
        }
        if (ind == n) ok = true;
        if (!ok) {
            lo = d;
        } else {
            hi = d;
        }

    }
    long double d = (lo + hi) / 2;
    int ind = 0;
    for (int j = 0; j < 3; ++j) {
        if (ind < n) {
            ans.pb(s[ind] + d);
            long double last = s[ind] + d;
            while ( ind < n && s[ind] <= last + d) ++ind;
        } else ans.pb(0);
    }
    printf("%.6lf\n", (double) (lo + hi) / 2);
    for (int i = 0; i < 3; ++i) {
        printf("%.6lf ", (double) ans[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
double x[200100],x1,x2,x3;
bool check(double y)
{
    x1=-1000000000;
    x2=-1000000000;
    x3=-1000000000;
    int i;
    for(i=1;i<=n;i++)
    {
        if(x1+y<x[i])
        {
            x3=x2; x2=x1; x1=x[i]+y;
        }
    }
    if(x3-x[1]>y) return false;
    if(x3<0) x3=0;
    if(x2<0) x2=0;
    if(x1<0) x1=0;
    return true;
}
int main()
{
    //freopen("test.inp","r",stdin);
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%lf",&x[i]);
    }
    sort(x+1,x+1+n);
    double l=0,r=1000000000,cnt=55;
    while(l<r && cnt)
    {
        double mid =(l+r)/2;
        if(check(mid)) r=mid;
        else l=mid;
        cnt--;
    }
    printf("%lf\n",l);
    printf("%.6lf %.6lf %.6lf\n",x1,x2,x3);
}


#include <iostream>
#include <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>

typedef long long ll;

const ll MOD = 1e9 + 7, INF = 1e18;

using namespace std;

ll n, f[1000000];

set < pair <ll, ll> > ans;

ll p[1000000], sz[1000000];

vector <ll> d[1000000];

pair <ll, ll> min_edge[1000000];

set <ll> numbers[10000000];

ll find (ll x)
{
	if (p[x] == x) return x;
	else return p[x] = find (p[x]);
}

void unite (ll a, ll b)
{
	a = find (a);
	b = find (b);

	if (a == b) return;

	if (sz[a] < sz[b])
	{
		unite (b, a);
		return;
	}

	p[b] = a;
	sz[a] += sz[b];
}

struct node
{
	ll num;
	ll next[2];
} a[1000000];

ll ptr = 0;

void add (ll ind)
{
	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (a[cur].next[s] == 0)
			a[cur].next[s] = ++ptr;

		cur = a[cur].next[s];

		a[cur].num++;
	}

	numbers[cur].insert (ind);
}

void remove (ll ind)
{

	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		cur = a[cur].next[s];

		a[cur].num--;
	}

	numbers[cur].erase (ind);
}

pair <ll, ll> find_xor (ll ind)
{
	ll cur = 0, ans = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;

		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (!a[cur].next[s] || a[a[cur].next[s]].num == 0)
		{
			cur = a[cur].next[1 - s];
			ans = ans * 2 + 1 - s;
		}
		else
		{
			cur = a[cur].next[s];
			ans = ans * 2 + s;
		}
	}

	return make_pair (ans ^ f[ind], *numbers[cur].begin ());
}

int main ()
{
	cin >> n;

	for (ll i = 0; i < n; i++)
	{
		scanf ("%lld", &f[i]);

		add (i);
	}

	for (ll i = 0; i < n; i++)
	{
		p[i] = i;
		sz[i] = 1;
	}

	while (sz[find (0)] != n)
	{
		for (ll i = 0; i < n; i++)
			d[i].clear ();

		for (ll i = 0; i < n; i++)
		{
			d[find (i)].push_back (i);
			min_edge[find (i)] = {INF, INF};
		}

		for (ll i = 0; i < n; i++)
		{
			ll l = 0;
			if (d[i].empty ()) continue;

			for (ll x : d[i])
				remove (x);

			for (ll x : d[i])
			{
				auto g = find_xor (x);
				if (min_edge[i] > g)
				{
					min_edge[i] = g;
					l = x;
				}
			}

			for (ll x : d[i])
				add (x);

			ans.insert ({min (l, min_edge[i].second), max (l, min_edge[i].second)});

			unite (i, min_edge[i].second);
		}
	}

	ll sum = 0;

	for (auto it : ans)
	{
		sum += f[it.first] ^ f[it.second];
	}

	cout << sum;
}


#include <iostream>
#include <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>

typedef long long ll;

const ll MOD = 1e9 + 7, INF = 1e18;

using namespace std;

ll n, f[200000];

set < pair <int, int> > ans;

ll p[200000], sz[200000];

vector <ll> d[200000];

pair <ll, ll> min_edge[200000];

ll numbers[7000000];

ll find (ll x)
{
	if (p[x] == x) return x;
	else return p[x] = find (p[x]);
}

void unite (ll a, ll b)
{
	a = find (a);
	b = find (b);

	if (a == b) return;

	if (sz[a] < sz[b])
	{
		unite (b, a);
		return;
	}

	p[b] = a;
	sz[a] += sz[b];
}

struct node
{
	int num;
	int next[2];
};

vector <node> a;

ll ptr = 0;

void add (ll ind)
{
	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (a[cur].next[s] == 0)
		{
			a.push_back ({0, 0, 0});
			a[cur].next[s] = ++ptr;
		}

		cur = a[cur].next[s];

		a[cur].num++;
	}

	numbers[cur] = ind;
}

void remove (ll ind)
{

	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		cur = a[cur].next[s];

		a[cur].num--;
	}
}

pair <ll, ll> find_xor (ll ind)
{
	ll cur = 0, ans = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;

		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (!a[cur].next[s] || a[a[cur].next[s]].num == 0)
		{
			cur = a[cur].next[1 - s];
			ans = ans * 2 + 1 - s;
		}
		else
		{
			cur = a[cur].next[s];
			ans = ans * 2 + s;
		}
	}

	return make_pair (ans ^ f[ind], numbers[cur]);
}

int main ()
{
	cin >> n;

	a.push_back ({0, 0, 0});

	set <int> dist;

	for (ll i = 0; i < n; i++)
	{
		scanf ("%lld", &f[i]);
		dist.insert (f[i]);
	}

	n = 0;

	for (auto it : dist)
	{
		f[n] = it;
		add (f[n]);
		n++;
	}

	for (ll i = 0; i < n; i++)
	{
		p[i] = i;
		sz[i] = 1;
	}

	ll sum = 0;

	while (sz[find (0)] != n)
	{
		ans.clear ();
		for (ll i = 0; i < n; i++)
			d[i].clear ();

		for (ll i = 0; i < n; i++)
		{
			d[find (i)].push_back (i);
			min_edge[find (i)] = {INF, INF};
		}

		for (ll i = 0; i < n; i++)
		{
			ll l = 0;
			if (d[i].empty ()) continue;

			for (ll x : d[i])
				remove (x);

			for (ll x : d[i])
			{
				auto g = find_xor (x);
				if (min_edge[i] > g)
				{
					min_edge[i] = g;
					l = x;
				}
			}

			for (ll x : d[i])
				add (x);

			ans.insert ({min (l, min_edge[i].second), max (l, min_edge[i].second)});
		}

		for (auto it : ans)
		{
			sum += f[it.first] ^ f[it.second];
			unite (it.first, it.second);
		}
	}

	cout << sum;
}


#include <iostream>
#include <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>

typedef long long ll;

const ll MOD = 1e9 + 7, INF = 1e18;

using namespace std;

ll n, f[200000];

set < pair <int, int> > ans;

ll p[200000], sz[200000];

vector <ll> d[200000];

pair <ll, ll> min_edge[200000];

set <ll> numbers[7000000];

ll find (ll x)
{
	if (p[x] == x) return x;
	else return p[x] = find (p[x]);
}

void unite (ll a, ll b)
{
	a = find (a);
	b = find (b);

	if (a == b) return;

	if (sz[a] < sz[b])
	{
		unite (b, a);
		return;
	}

	p[b] = a;
	sz[a] += sz[b];
}

struct node
{
	int num;
	int next[2];
};

vector <node> a;

ll ptr = 0;

void add (ll ind)
{
	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (a[cur].next[s] == 0)
		{
			a.push_back ({0, 0, 0});
			a[cur].next[s] = ++ptr;
		}

		cur = a[cur].next[s];

		a[cur].num++;
	}

	numbers[cur].insert (ind);
}

void remove (ll ind)
{

	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		cur = a[cur].next[s];

		a[cur].num--;
	}

	numbers[cur].erase (ind);
}

pair <ll, ll> find_xor (ll ind)
{
	ll cur = 0, ans = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;

		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (!a[cur].next[s] || a[a[cur].next[s]].num == 0)
		{
			cur = a[cur].next[1 - s];
			ans = ans * 2 + 1 - s;
		}
		else
		{
			cur = a[cur].next[s];
			ans = ans * 2 + s;
		}
	}

	return make_pair (ans ^ f[ind], *numbers[cur].begin ());
}

int main ()
{
	cin >> n;

	a.push_back ({0, 0, 0});

	for (ll i = 0; i < n; i++)
	{
		scanf ("%lld", &f[i]);

		add (i);
	}

	for (ll i = 0; i < n; i++)
	{
		p[i] = i;
		sz[i] = 1;
	}

	ll sum = 0;

	while (sz[find (0)] != n)
	{
		ans.clear ();
		for (ll i = 0; i < n; i++)
			d[i].clear ();

		for (ll i = 0; i < n; i++)
		{
			d[find (i)].push_back (i);
			min_edge[find (i)] = {INF, INF};
		}

		for (ll i = 0; i < n; i++)
		{
			ll l = 0;
			if (d[i].empty ()) continue;

			for (ll x : d[i])
				remove (x);

			for (ll x : d[i])
			{
				auto g = find_xor (x);
				if (min_edge[i] > g)
				{
					min_edge[i] = g;
					l = x;
				}
			}

			for (ll x : d[i])
				add (x);

			ans.insert ({min (l, min_edge[i].second), max (l, min_edge[i].second)});
		}

		for (auto it : ans)
		{
			sum += f[it.first] ^ f[it.second];
			unite (it.first, it.second);
		}
	}

	cout << sum;
}


#include <iostream>
#include <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>

typedef long long ll;

const ll MOD = 1e9 + 7, INF = 1e18;

using namespace std;

ll n, f[200000];

set < pair <int, int> > ans;

ll p[200000], sz[200000];

vector <ll> d[200000];

pair <ll, ll> min_edge[200000];

ll numbers[7000000];

ll find (ll x)
{
	if (p[x] == x) return x;
	else return p[x] = find (p[x]);
}

void unite (ll a, ll b)
{
	a = find (a);
	b = find (b);

	if (a == b) return;

	if (sz[a] < sz[b])
	{
		unite (b, a);
		return;
	}

	p[b] = a;
	sz[a] += sz[b];
}

struct node
{
	int num;
	int next[2];
};

vector <node> a;

ll ptr = 0;

void add (ll ind)
{
	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (a[cur].next[s] == 0)
		{
			a.push_back ({0, 0, 0});
			a[cur].next[s] = ++ptr;
		}

		cur = a[cur].next[s];

		a[cur].num++;
	}

	numbers[cur] = ind;
}

void remove (ll ind)
{

	ll cur = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;
		if (x & (1LL << i)) s = 1;
		else s = 0;

		cur = a[cur].next[s];

		a[cur].num--;
	}
}

pair <ll, ll> find_xor (ll ind)
{
	ll cur = 0, ans = 0, x = f[ind];

	for (ll i = 29; i >= 0; i--)
	{
		ll s;

		if (x & (1LL << i)) s = 1;
		else s = 0;

		if (!a[cur].next[s] || a[a[cur].next[s]].num == 0)
		{
			cur = a[cur].next[1 - s];
			ans = ans * 2 + 1 - s;
		}
		else
		{
			cur = a[cur].next[s];
			ans = ans * 2 + s;
		}
	}

	return make_pair (ans ^ f[ind], numbers[cur]);
}

int main ()
{
	cin >> n;

	a.push_back ({0, 0, 0});

	for (ll i = 0; i < n; i++)
	{
		scanf ("%lld", &f[i]);

		add (i);
	}

	for (ll i = 0; i < n; i++)
	{
		p[i] = i;
		sz[i] = 1;
	}

	ll sum = 0;

	while (sz[find (0)] != n)
	{
		ans.clear ();
		for (ll i = 0; i < n; i++)
			d[i].clear ();

		for (ll i = 0; i < n; i++)
		{
			d[find (i)].push_back (i);
			min_edge[find (i)] = {INF, INF};
		}

		for (ll i = 0; i < n; i++)
		{
			ll l = 0;
			if (d[i].empty ()) continue;

			for (ll x : d[i])
				remove (x);

			for (ll x : d[i])
			{
				auto g = find_xor (x);
				if (min_edge[i] > g)
				{
					min_edge[i] = g;
					l = x;
				}
			}

			for (ll x : d[i])
				add (x);

			ans.insert ({min (l, min_edge[i].second), max (l, min_edge[i].second)});
		}

		for (auto it : ans)
		{
			sum += f[it.first] ^ f[it.second];
			unite (it.first, it.second);
		}
	}

	cout << sum;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n, d[maxn];
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn];
ll tr[maxn][2], nd_cnt, max_bits = 2; //TODO

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n, d[maxn];
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn];
ll tr[maxn][2], nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 80];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 60];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 40];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 30];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 60];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 60];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1 << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        long long ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5 * 35; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n, d[maxn];
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn];
ll tr[maxn][2], nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1ll << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1ll)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


/*
字典树0根和0不可同一个，否则会错乱
query时使用fa的root
*/

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
#define fu(type, a, b, c) for (type a = b; a <= c; a++)
#define fd(type, a, b, c) for (type a = b; a >= c; a--)
#define mem(a) memset(a, 0, sizeof(a))
#define endl "\n"
#define blank " "
#define debug(a) #a << "=" << a << blank
#define square(i) ((i) * (i))
#define mk make_pair
#define pll pair<ll, ll>
// #define pii pair<int, int>
#define fr first
#define sc second
const ll xxx = 2e5; //TODO
const ll maxn = xxx + 5;
const ll inf = 0x3f3f3f3f;
// const ll inf = (1ll << 60);
// const ll mod = 1e9 + 7;
const double Pi = acos(-1.0);
ll a[maxn], n;
ll root[maxn], fa[maxn];
pll minn[maxn];
struct Node
{
    ll siz, val;
    ll to[2];
} node[maxn * 60];
ll nd_cnt, max_bits = 30;

ll getfa(ll x)
{
    return (fa[x] == x) ? x : fa[x] = getfa(fa[x]);
}

void insert(ll p, ll x, ll dep, ll i)
{
    if (dep < 0)
    {
        node[p].val = i;
        node[p].siz++;
        return;
    }
    ll bt = (x >> dep) & 1;
    node[p].siz++;
    if (!node[p].to[bt])
    {
        node[p].to[bt] = ++nd_cnt;
    }
    insert(node[p].to[bt], x, dep - 1, i);
}

pll query(ll rt, ll lt, ll x)
{
    ll ans = 0;
    fd(ll, dep, max_bits, 0)
    {
        ll bt = (x >> dep) & 1;
        ll siz1 = node[rt].to[bt] ? node[node[rt].to[bt]].siz : 0;
        ll siz2 = node[lt].to[bt] ? node[node[lt].to[bt]].siz : 0;
        if ((siz1 > siz2))
        {
            rt = node[rt].to[bt];
            lt = node[lt].to[bt];
        }
        else
        {
            ans += (1 << dep);
            rt = node[rt].to[bt ^ 1];
            lt = node[lt].to[bt ^ 1];
        }
    }
    return mk(ans, node[rt].val);
}

void merge(ll &x, ll y)
{
    if (x == 0 || y == 0)
    {
        x = x + y;
        return;
    }
    merge(node[x].to[0], node[y].to[0]);
    merge(node[x].to[1], node[y].to[1]);
    node[x].siz += node[y].siz;
    node[x].val = max(node[x].val, node[y].val); //TODO
}

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    ll tim;
    // cin >> tim;
    tim = 1;
    while (tim--)
    // fu(ll, tt, 1, tim)
    // while (cin >> n)
    {
        ll ans = 0;
        cin >> n;
        fu(ll, i, 1, n)
        {
            cin >> a[i];
            fa[i] = i;
            root[i] = i;
        }
        root[0] = n + 1;
        nd_cnt = n + 1;
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - a - 1;
        fu(ll, i, 1, n)
        {
            insert(root[0], a[i], max_bits, i);
            insert(root[i], a[i], max_bits, i);
        }
        while (1)
        {
            ll tag = 1;
            memset(minn, 0x3f, sizeof(minn));
            fu(ll, i, 1, n)
            {
                ll fx = getfa(i), y, w;
                pll tmp = query(root[0], root[fx], a[i]);
                // cout << debug(fx) << debug(node[root[fx]].siz) << endl;
                w = tmp.fr, y = tmp.sc;
                ll fy = getfa(y);
                if (fx != fy)
                {
                    if (w < minn[fx].first)
                        minn[fx] = mk(w, fy);
                    if (w < minn[fy].first)
                        minn[fy] = mk(w, fx);
                }
            }
            fu(ll, i, 1, n)
            {
                if (minn[i].fr < inf && minn[i].sc && getfa(i) != getfa(minn[i].sc))
                {
                    ll fx = getfa(i), fy = getfa(minn[i].sc);
                    // cout << debug(fx) << debug(fy) << endl;
                    // cout << debug(minn[i].fr) << debug(minn[i].sc) << endl;
                    ans += minn[i].fr;
                    tag = 0;
                    merge(root[fx], root[fy]);
                    fa[fy] = fx;
                }
            }
            if (tag)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<pair<int, long long>, int> min(int a, int i) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(make_pair(len, (long long)std::min(ends[v], i) * N + std::max(ends[v], i)), ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
int no[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<pair<int, long long>, int> res(make_pair(~(1 << 31), (long long)0), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<pair<int, long long>, int> t = tr.min(a[comp[i][j]], comp[i][j]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct dsu {
	dsu *p;
	int size;
	dsu() {
		p = this;
		size = 1;
	}
	dsu *find();
};
dsu *dsu::find() {
	if (p == this) return this;
	return p = p->find();
}
bool same(dsu *a, dsu *b)
{
	return a->find() == b->find();
}
void unite(dsu *a, dsu *b)
{
	a = a->find();
	b = b->find();
	if (a->size < b->size) swap(a, b);
	a->size += b->size;
	b->p = a;
}

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<int, int> min(int a) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(len, ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
dsu ds[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (res.first > t.first) {
					res = t;
					from = comp[i][j];
				}
			}
			if (!same(ds + from, ds + res.second)) {
				g[from].push_back(res.second);
				g[res.second].push_back(from);
				unite(ds + from, ds + res.second);
				cost += res.first;
			}
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<int, int> min(int a) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(len, ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<pair<int, long long>, int> min(int a, int i) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(make_pair(len, (long long)std::min(ends[v], i) * N + std::max(ends[v], i)), ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
int no[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<pair<int, long long>, int> res(make_pair(~(1 << 31), (long long)0), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<pair<int, long long>, int> t = tr.min(a[comp[i][j]], comp[i][j]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<pair<int, long long>, int> min(int a, int i) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(make_pair(len, (long long)std::min(ends[v], i) * N + std::max(ends[v], i)), ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
int no[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<pair<int, long long>, int> res(make_pair(~(1 << 31), (long long)0), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<pair<int, long long>, int> t = tr.min(a[comp[i][j]], comp[i][j]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

unordered_set<int, custom_hash> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct dsu {
	dsu *p;
	int size;
	dsu() {
		p = this;
		size = 1;
	}
	dsu *find();
};
dsu *dsu::find() {
	if (p == this) return this;
	return p = p->find();
}
bool same(dsu *a, dsu *b)
{
	return a->find() == b->find();
}
void unite(dsu *a, dsu *b)
{
	a = a->find();
	b = b->find();
	if (a->size < b->size) swap(a, b);
	a->size += b->size;
	b->p = a;
}

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<int, int> min(int a) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(len, ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
dsu ds[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	int any = comp.size();
	while (any > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) if (comp[i].size()) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (res.first > t.first) {
					res = t;
					from = comp[i][j];
				}
			}
			if (!same(ds + from, ds + res.second)) {
				g[from].push_back(res.second);
				g[res.second].push_back(from);
				unite(ds + from, ds + res.second);
				cost += res.first;
			}
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		comp.resize(n);
		any = 0;
		for (int i = 0; i < n; ++i) {
			if (comp[ds[i].find() - ds].empty()) ++any;
			comp[ds[i].find() - ds].push_back(i);
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <unordered_set>
#include <cstring>
#include <vector>
using namespace std;

struct dsu {
	dsu *p;
	int size;
	dsu() {
		p = this;
		size = 1;
	}
	dsu *find();
};
dsu *dsu::find() {
	if (p == this) return this;
	return p = p->find();
}
bool same(dsu *a, dsu *b)
{
	return a->find() == b->find();
}
void unite(dsu *a, dsu *b)
{
	a = a->find();
	b = b->find();
	if (a->size < b->size) swap(a, b);
	a->size += b->size;
	b->p = a;
}

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<int, int> min(int a) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(len, ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
dsu ds[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	int any = comp.size();
	while (any > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (res.first > t.first) {
					res = t;
					from = comp[i][j];
				}
			}
			if (!same(ds + from, ds + res.second)) {
				g[from].push_back(res.second);
				g[res.second].push_back(from);
				unite(ds + from, ds + res.second);
				cost += res.first;
			}
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		comp.resize(n);
		any = 0;
		for (int i = 0; i < n; ++i) {
			if (comp[ds[i].find() - ds].empty()) ++any;
			comp[ds[i].find() - ds].push_back(i);
		}
	}
	return cost;
}

unordered_set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;

const int INF = ~(1 << 31);

const int N = 200000;
const int LG = 29;
int a[N];

int minxor(int *first1, int *last1, int *first2, int *last2, int bit)
{
	if (first1 == last1 || first2 == last2) return INF;
	if (bit == -1) return 0;
	int *i1, *i2;
	for (i1 = first1; i1 < last1 && !(*i1 & (1 << bit)); ++i1);
	for (i2 = first2; i2 < last2 && !(*i2 & (1 << bit)); ++i2);
	if ((i1 == first1 && i2 == last2) || (i1 == last1 && i2 == first2)) return (1 << bit) + minxor(first1, last1, first2, last2, bit - 1);
	int res = min(minxor(first1, i1, first2, i2, bit - 1), minxor(i1, last1, i2, last2, bit - 1));
	return res;
}
long long mst(int *first, int *last, int bit)
{
	if (bit == -1 || first == last) return 0;
	int *i;
	for (i = first; i < last && !(*i & (1 << bit)); ++i);
	return mst(first, i, bit - 1) + mst(i, last, bit - 1) + ((i != first && i != last) ? (1 << bit) + minxor(first, i, i, last, bit - 1) : 0);
}

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", a + i);
	sort(a, a + n);
	printf("%lld\n", mst(a, a + n, LG));
}


#include <cstdio>
#include <set>
#include <cstring>
#include <vector>
using namespace std;

const int N = 200000;
const int LG = 30;
int n;
int a[N];

const vector<int> ZERO_TWO = {0, 0};

struct trie {
	vector<vector<int> > tr;
	vector<int> size;
	trie() {}
	trie(int *a, int n) {
		tr.push_back(ZERO_TWO);
		size.push_back(0);
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int j = LG - 1; j >= 0; --j) {
				if (tr[v][(a[i] >> j) & 1]) v = tr[v][(a[i] >> j) & 1];
				else {
					v = tr[v][(a[i] >> j) & 1] = tr.size();
					tr.push_back(ZERO_TWO);
					size.push_back(0);
				}
				++size[v];
			}
		}
	}
	void insert(int a) {
		int v = 0;
		for (int i = LG - 1; i >= 0; --i) {
			v = tr[v][(a >> i) & 1];
			++size[v];
		}
	}
	void erase(int a) {
		int v = 0;
		--size[v];
		for (int i = LG - 1; i >= 0; --i) {
			v = tr[v][(a >> i) & 1];
			--size[v];
		}
	}
	pair<int, int> min(int a) {
		int with = 0;
		int len = 0;
		int v = 0;
		for (int i = LG - 1; i >= 0; --i) {
			if (size[tr[v][(a >> i) & 1]] > 0) {
				with |= a & (1 << i);
				v = tr[v][(a >> i) & 1];
			}
			else {
				with |= ((~ a) & (1 << i));
				len |= (1 << i);
				v = tr[v][((a >> i) & 1) ^ 1];
			}
		}
		return make_pair(len, with);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
trie tr;
set<pair<int, int> > s;

void fill_set()
{
	for (int i = 0; i < n; ++i) s.insert(make_pair(a[i], i));
}

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		set<pair<int, int> > edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j) {
				s.erase(make_pair(a[comp[i][j]], comp[i][j]));
				tr.erase(a[comp[i][j]]);
			}
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (t.first < res.first) {
					res = t;
					from = comp[i][j];
				}
			}
			int to = s.lower_bound(make_pair(res.second, 0))->second;
			if (!edge.count(make_pair(min(from, to), max(from, to)))) {
				cost += res.first;
				g[from].push_back(to);
				g[to].push_back(from);
				edge.insert(make_pair(min(from, to), max(from, to)));
			}
			for (int j = 0; j < comp[i].size(); ++j) {
				s.insert(make_pair(a[comp[i][j]], comp[i][j]));
				tr.insert(a[comp[i][j]]);
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", a + i);
	tr = trie(a, n);
	fill_set();
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int sz[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++sz[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++sz[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--sz[v];
		}
	}
	pair<pair<int, long long>, int> min(int a, int i) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (sz[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(make_pair(len, (long long)std::min(ends[v], i) * N + std::max(ends[v], i)), ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
int no[N];
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<pair<int, long long>, int> res(make_pair(~(1 << 31), (long long)0), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<pair<int, long long>, int> t = tr.min(a[comp[i][j]], comp[i][j]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <chrono>
#include <cassert>
#include <unordered_set>
#include <set>
#include <cstring>
#include <vector>
using namespace std;

struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 200000;
const int LG = 30;
int n;
int a[N];

int t[N * LG][2];
int size[N * LG];
int ends[N * LG];
struct trie {
	trie() {}
	trie(int *a, int n) {
		int cnt = 1;
		for (int i = 0; i < n; ++i) {
			int v = 0;
			for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
				int b = (bool)(a[i] & bit);
				if (t[v][b]) v = t[v][b];
				else v = t[v][b] = cnt++;
				++size[v];
			}
			ends[v] = i;
		}
	}
	void insert(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			++size[v];
		}
	}
	void erase(int a) {
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			v = t[v][(bool)(a & bit)];
			--size[v];
		}
	}
	pair<int, int> min(int a) {
		int len = 0;
		int v = 0;
		for (int bit = 1 << (LG - 1); bit; bit >>= 1) {
			int b = (bool)(a & bit);
			if (size[t[v][b]] > 0) {
				v = t[v][b];
			}
			else {
				len |= bit;
				v = t[v][b ^ 1];
			}
		}
		return make_pair(len, ends[v]);
	}
};

vector<int> g[N];
vector<vector<int> > comp;
trie tr;

bool u[N];
void dfs(int v, vector<int> &compv)
{
	u[v] = true;
	compv.push_back(v);
	for (int i = 0; i < g[v].size(); ++i) if (!u[g[v][i]]) dfs(g[v][i], compv);
}

long long mst()
{
	comp.resize(n);
	for (int i = 0; i < n; ++i) comp[i].push_back(i);
	long long cost = 0;
	while (comp.size() > 1) {
		vector<pair<pair<int, int>, int> > add_edge;
		for (int i = 0; i < comp.size(); ++i) {
			for (int j = 0; j < comp[i].size(); ++j)
				tr.erase(a[comp[i][j]]);
			pair<int, int> res(~(1 << 31), 0);
			int from;
			for (int j = 0; j < comp[i].size(); ++j) {
				pair<int, int> t = tr.min(a[comp[i][j]]);
				if (res > t) {
					res = t;
					from = comp[i][j];
				}
			}
			add_edge.push_back(make_pair(make_pair(min(from, res.second), max(from, res.second)), res.first));
			for (int j = 0; j < comp[i].size(); ++j)
				tr.insert(a[comp[i][j]]);
		}
		sort(add_edge.begin(), add_edge.end());
		for (int i = 0; i < add_edge.size(); ++i) {
			if (!i || add_edge[i].first != add_edge[i - 1].first) {
				g[add_edge[i].first.first].push_back(add_edge[i].first.second);
				g[add_edge[i].first.second].push_back(add_edge[i].first.first);
				cost += add_edge[i].second;
			}
		}
		memset(u, 0, sizeof(*u) * n);
		comp.clear();
		for (int i = 0; i < n; ++i) if (!u[i]) {
			comp.push_back(vector<int>());
			dfs(i, comp.back());
		}
	}
	return cost;
}

set<int> distinct;

int main()
{
	scanf("%d", &n);
	int v;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &v);
		distinct.insert(v);
	}
	n = 0;
	for (int i : distinct) a[n++] = i;
	tr = trie(a, n);
	printf("%lld\n", mst());
	return 0;
}