#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			q.push({i,(int)v[i].size()});
			if(v[i].size()==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			v[top.id].pop_back();
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}
		while(p[1].second*2>re){
			p[1].second--;
			ans[v[p[1].first][p[1].second]]=flag;
			v[p[1].first].pop_back();
		}
		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				v[p[1].first].pop_back();
				v[p[cnt].first].pop_back();
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second){
				j++;
			}
			if(j==1){
				break;
			}
			else if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int j=1;j<=y;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int j=1;j<=y-2;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%(y-2)+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%j+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			int s=v[i].size();
			q.push({i,s});
			if(s==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()&&q.top().v*2>re){
			node top=q.top();
			q.pop();
			re--;
			top.v--;
			ans[v[top.id][top.v]]=flag;
			q.push(top);
		}
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}

		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second&&j+1<=cnt){
				j++;
			}
			if(j==1){
				break;
			}
			else if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int i=1;i<=y;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int i=1;i<=y-2;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%(y-2)+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%j+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			int s=v[i].size();
			q.push({i,s});
			if(s==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()&&q.top().v*2>re){
			node top=q.top();
			q.pop();
			re--;
			top.v--;
			ans[v[top.id][top.v]]=flag;
			q.push(top);
		}
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}

		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second&&j+1<=cnt){
				j++;
			}
			if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int i=1;i<=y;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int i=1;i<=y-2;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%(y-2)+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%j+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			int s=v[i].size();
			q.push({i,s});
			if(s==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()&&q.top().v*2>re){
			node top=q.top();
			q.pop();
			re--;
			top.v--;
			ans[v[top.id][top.v]]=flag;
			q.push(top);
		}
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}

		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second){
				j++;
			}
			if(j==1){
				break;
			}
			else if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int j=1;j<=y;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int j=1;j<=y-2;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%(y-2)+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%j+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			q.push({i,(int)v[i].size()});
			if(v[i].size()==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			v[top.id].pop_back();
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}
		while(p[1].second*2>re){
			p[1].second--;
			ans[v[p[1].first][p[1].second]]=flag;
			v[p[1].first].pop_back();
		}
		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				v[p[1].first].pop_back();
				v[p[cnt].first].pop_back();
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second){
				j++;
			}
			if(j==1){
				break;
			}
			else if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int j=1;j<=y;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int j=1;j<=y-2;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%y+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			q.push({i,(int)v[i].size()});
			if(v[i].size()==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			v[top.id].pop_back();
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}
		while(p[1].second*2>re){
			p[1].second--;
			ans[v[p[1].first][p[1].second]]=flag;
			v[p[1].first].pop_back();
		}
		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				v[p[1].first].pop_back();
				v[p[cnt].first].pop_back();
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second){
				j++;
			}
			if(j>=y){
				for(int j=1;j<=y;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int j=1;j<=y-2;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%y+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#define PI acos(-1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define dd(x) cout<<#x<<" = "<<x<<","
#define de(x) cout<<#x<<" = "<<x<<"\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+5;
struct node{
	int id,v;
	bool operator < (node b) const{
		return v<b.v;
	}
};
priority_queue<node> q;
vector<int> v[N];
int a[N],ans[N];
pii p[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		while(!q.empty()){
			q.pop();
		}
		int n,x,y;
		scanf("%d%d%d",&n,&x,&y);
		for(int i=1;i<=n+1;i++){
			ans[i]=0;
			v[i].clear();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			v[a[i]].pb(i);
		}
		int flag;
		for(int i=1;i<=n+1;i++){
			int s=v[i].size();
			q.push({i,s});
			if(s==0){
				flag=i;
			}
		}
		int re=n;
		while(x>0){
			x--;
			y--;
			re--;
			node top=q.top();
			q.pop();
			top.v--;
			ans[v[top.id][top.v]]=top.id;
			q.push(top);
		}
		int cnt=0;
		while(!q.empty()){
			node top=q.top();
			q.pop();
			p[++cnt]={top.id,top.v};
		}
		while(p[1].second*2>re){
			p[1].second--;
			re--;
			ans[v[p[1].first][p[1].second]]=flag;
		}
		while(p[1].second>0&&p[1].second!=p[2].second){
			if(y==0)
			break;
			if(p[cnt].second>0&&y>=2){
				p[1].second--;
				p[cnt].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				ans[v[p[cnt].first][p[cnt].second]]=p[1].first;
				v[p[1].first].pop_back();
				v[p[cnt].first].pop_back();
				y-=2;
			}
			else if(p[cnt].second>0&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[cnt].first;
				y--;
			}
			else{
				cnt--;
			}
		}
		while(p[1].second>0){
			int j=1;
			while(p[1].second==p[j+1].second){
				j++;
			}
			if(j==1){
				break;
			}
			else if(j>y&&y==1){
				p[1].second--;
				ans[v[p[1].first][p[1].second]]=p[2].first;
				y=0;
				break;
			}
			else if(j>=y){
				for(int j=1;j<=y;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%y+1].first;
				}
				y=0;
				break;
			}
			else if(j==y-1){
				for(int j=1;j<=y-2;j++){
					p[j].second--;
					ans[v[p[j].first][p[j].second]]=p[j%(y-2)+1].first;
				}
				y=2;
			}
			else{
				for(int i=1;i<=j;i++){
					p[i].second--;
					ans[v[p[i].first][p[i].second]]=p[i%j+1].first;
				}
				y-=j;
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]==0){
				ans[i]=flag;
			}
		}
		if(y>0){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		for(int i=1;i<=n;i++){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
int tab[100005];
set<int> possible;
int licz[100005];
priority_queue<pair<int,int>> q;
int out[100005];
vector<int> pos[100005];
vector<int> v;
void czysc(int a)
{
    a+=2;
    for(int x=0;x<=a;x++)
        tab[x]=0;
    possible.clear();
    for(int x=0;x<=a;x++)
        licz[x]=0;
    while(!q.empty())
        q.pop();
    for(int x=0;x<=a;x++)
        out[x]=0;
    for(int x=0;x<=a;x++)
        pos[x].resize(0);
    v.resize(0);
}
void solve()
{
    int a,b,c;
    cin>>a>>b>>c;
    for(int x=1;x<=a+1;x++)
        possible.insert(x);
    for(int x=1;x<=a;x++)
    {
        cin>>tab[x];
        possible.erase(x);
        licz[tab[x]]++;
        pos[tab[x]].push_back(x);
    }
    int filler=*possible.begin();
    for(int x=1;x<=a+1;x++)
        q.push(make_pair(licz[x],x));
    c-=b;
    while(b--)
    {
        pair<int,int> p=q.top();
        q.pop();
        int pom=pos[p.second].back();
        pos[p.second].pop_back();
        out[pom]=tab[pom];
        p.first--;
        q.push(p);
    }
    while(!q.empty())
    {
        pair<int,int> p=q.top();
        q.pop();
        while(p.first--)
            v.push_back(p.second);
    }
    for(int x=0;x<v.size();x++)
    {
        if(c==0)
            break;
        if(v[x]!=v[(x+v.size()/2+1)%v.size()])
        {
            int pom=pos[v[x]].back();
            pos[v[x]].pop_back();
            out[pom]=v[(x+v.size()/2+1)%v.size()];
            c--;
        }
    }
    if(c==0)
        cout<<"YES"<<'\n';
    else
        cout<<"NO"<<'\n';
    if(!c)
    {
        for(int x=1;x<=a;x++)
        {
            if(out[x])
                cout<<out[x]<<" ";
            else
                cout<<filler<<" ";
        }
        cout<<'\n';
    }
    czysc(a);
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int t;
    cin>>t;
    while(t--)
        solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	int ans = 0, sum = 0;
	map <int, pair <int, int> > pr;
	set <int> prs;
	for (int i = 29; i >= 0; --i) {
		int cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
			prs.insert(a[j] / (1 << (i + 1)));
		}
		while (!prs.empty()) {
			pr[*prs.begin()] = {0, 0};
			prs.erase(prs.begin());
		}
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	int ans = 0, sum = 0;
	map <int, pair <int, int> > pr;
	for (int i = 30; i >= 0; --i) {
		int cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
		}
		pr.clear();
		//cout << cnt1 << ' ' << cnt2 << '\n';
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	int ans = 0, sum = 0;
	map <int, pair <int, int> > pr;
	for (int i = 29; i >= 0; --i) {
		vector <int> prs;
		int cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
			prs.push_back(a[j] / (1 << (i + 1)));
		}
		for (int j = 0; j < prs.size(); ++j) pr[prs[j]] = {0, 0};
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	int ans = 0, sum = 0;
	map <int, pair <int, int> > pr;
	for (int i = 30; i >= 0; --i) {
		int cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
		}
		pr.clear();
		//cout << cnt1 << ' ' << cnt2 << '\n';
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	ll ans = 0, sum = 0;
	unordered_map <int, pair <int, int> > pr;
	for (int i = 29; i >= 0; --i) {
		ll cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
		}
		pr.clear();
		//cout << cnt1 << ' ' << cnt2 << '\n';
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

void solve() {
	int n;
	cin >> n;
	int a[n];
	for (int i = 0; i < n; ++i) cin >> a[i];
	ll ans = 0, sum = 0;
	map <int, pair <int, int> > pr;
	for (int i = 29; i >= 0; --i) {
		ll cnt1 = 0, cnt2 = 0;
		for (int j = 0; j < n; ++j) {
			if (a[j] & (1 << i)) {
				++pr[a[j] / (1 << (i + 1))].first;
				cnt2 += pr[a[j] / (1 << (i + 1))].second;
			}
			else {
				++pr[a[j] / (1 << (i + 1))].second;
				cnt1 += pr[a[j] / (1 << (i + 1))].first;
			}
		}
		pr.clear();
		//cout << cnt1 << ' ' << cnt2 << '\n';
		if (cnt1 > cnt2) ans += (1 << i);
		sum += min(cnt1, cnt2);
	}
	cout << sum << ' ' << ans;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

void solve(){
	int n; cin >> n;
	vvl vec(1,vl(n));
	rep(i,n) cin >> vec[0][i];
	vvl nv;
	int x = 0;
	ll sum = 0;
	for(int i=29; i>=0; i--){
		ll inv0 = 0, inv1 = 0; //count 1->0, 0->1
		ll cnt0 = 0, cnt1 = 0;
		for(auto v : vec){
			vl l,r;
			cnt0 = cnt1 = 0;
			for(auto z : v){
				if(z>>i & 1){
					cnt0++;
					inv1 += cnt1;
					r.push_back(z);
				}else{
					inv0 += cnt0;
					cnt1++;
					l.push_back(z);
				}
			}
			if(!l.empty()) nv.push_back(l);
			if(!r.empty()) nv.push_back(r);
		}
		if(inv0 > inv1){
			x |= 1<<i;
		}
		sum += min(inv0, inv1);
		vec = nv;
		nv.clear();
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

void solve(){
	int n; cin >> n;
	vvl vec(1,vl(n));
	rep(i,n) cin >> vec[0][i];
	vvl nv;
	int x = 0;
	ll sum = 0;
	vl l,r;
	for(int i=29; i>=0; i--){
		ll inv0 = 0, inv1 = 0; //count 1->0, 0->1
		ll cnt0 = 0, cnt1 = 0;
		for(auto v : vec){
			cnt0 = cnt1 = 0;
			for(auto z : v){
				if(z>>i & 1){
					cnt0++;
					inv1 += cnt1;
					r.push_back(z);
				}else{
					inv0 += cnt0;
					cnt1++;
					l.push_back(z);
				}
			}
			if(!l.empty()) nv.emplace_back(l);
			if(!r.empty()) nv.emplace_back(r);
			l.clear(); r.clear();
		}
		if(inv0 > inv1){
			x |= 1<<i;
		}
		sum += min(inv0, inv1);
		vec = nv;
		nv.clear();
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	vl c;
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int j=n-1; j>=0; j--){
			int t = lower_bound(all(c),a[j]^x) - c.begin();
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		c.clear();
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		vl c;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		Compress<ll> comp(c);
		for(int j=n-1; j>=0; j--){
			int t = comp.get(a[j] ^ x);
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
		rep(j,n){
			bit.add(j,-bit.sum(j));
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC optimize("Ofast")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vector<int> a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	vector<int> b(n),c;
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		for(int j=n-1; j>=0; j--){
			b[j] = a[j] ^ x;
			c.emplace_back(b[j]);
		}
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int j=n-1; j>=0; j--){
			int t = lower_bound(all(c),b[j]) - c.begin();
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		c.clear();
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	for(int i=30; i>=0; i--){
		x += 1<<i;
		BIT<int> bit(n);
		ll s = 0;
		vl c;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		Compress<ll> comp(c);
		for(int j=n-1; j>=0; j--){
			int t = comp.get(a[j] ^ x);
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		vl c;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		Compress<ll> comp(c);
		for(int j=n-1; j>=0; j--){
			int t = comp.get(a[j] ^ x);
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vector<int> a(n); rep(i,n) scanf("%d",&a[i]);
	int x = 0;
	ll sum = 0;
	BIT<ll> bit(n);
	vector<int> b(n),c;
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		for(int j=n-1; j>=0; j--){
			b[j] = a[j] ^ x;
			c.emplace_back(b[j]);
		}
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int j=n-1; j>=0; j--){
			int t = lower_bound(all(c),b[j]) - c.begin();
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		c.clear();
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		vl c;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		Compress<ll> comp(c);
		for(int j=n-1; j>=0; j--){
			int t = comp.get(a[j] ^ x);
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

void solve(){
	int n; cin >> n;
	vvl vec(1,vl(n));
	rep(i,n) cin >> vec[0][i];
	vvl nv;
	int x = 0;
	ll sum = 0;
	for(int i=29; i>=0; i--){
		ll inv0 = 0, inv1 = 0; //count 1->0, 0->1
		ll cnt0 = 0, cnt1 = 0;
		for(auto v : vec){
			vl l,r;
			cnt0 = cnt1 = 0;
			for(auto z : v){
				if(z>>i & 1){
					cnt0++;
					inv1 += cnt1;
					r.push_back(z);
				}else{
					inv0 += cnt0;
					cnt1++;
					l.push_back(z);
				}
			}
			if(!l.empty()) nv.push_back(l);
			if(!r.empty()) nv.push_back(r);
		}
		if(inv0 > inv1){
			x |= 1<<i;
		}
		sum += min(inv0, inv1);
		vec = nv;
		nv.clear();
	}
	cout << sum << " " << x << "\n";
}

int main(){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

void solve(){
	int n; scanf("%d",&n);
	vector<int> a(n); rep(i,n) scanf("%d",&a[i]);
	int x = 0;
	ll sum = 0;
	BIT<ll> bit(n);
	vector<int> b(n),c;
	for(int i=30; i>=0; i--){
		if(i < 30) x += 1<<i;
		ll s = 0;
		for(int j=n-1; j>=0; j--){
			b[j] = a[j] ^ x;
			c.emplace_back(b[j]);
		}
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int j=n-1; j>=0; j--){
			int t = lower_bound(all(c),b[j]) - c.begin();
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		c.clear();
		if(i == 30){
			sum = s;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<int> bit(n);
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		vl c;
		for(int j=n-1; j>=0; j--){
			c.push_back(a[j] ^ x);
		}
		Compress<ll> comp(c);
		for(int j=n-1; j>=0; j--){
			int t = comp.get(a[j] ^ x);
			bit.add(t,1);
			s += bit.sum(t-1);
		}
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
		rep(j,n+1){
			bit.add(j,-bit.sum(j));
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <complex>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
    if(a>b){
        a = b; return true;
    }
    else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
    if(a<b){
        a = b; return true;
    }
    else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
    cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const ll mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}

	ll get(T k){
		if(k <= 0) return 0;
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

void solve(){
	int n; cin >> n;
	vector<int> a(n); rep(i,n) cin >> a[i];
	int x = 0;
	ll sum = 0;
	BIT<ll> bit(n);
	vector<int> b(n),c;
	for(int i=30; i>=0; i--){
		x += 1<<i;
		ll s = 0;
		for(int j=n-1; j>=0; j--){
			b[j] = a[j] ^ x;
			c.emplace_back(b[j]);
		}
		sort(all(c));
		c.erase(unique(all(c)), c.end());
		for(int j=n-1; j>=0; j--){
			int t = lower_bound(all(c),b[j]) - c.begin();
			bit.add(t,1LL);
			s += bit.sum(t-1);
		}
		rep(j,n+1){
			bit.dat[j] = 0;
		}
		c.clear();
		if(i == 30){
			sum = s;
			x -= 1<<i;
			continue;
		}
		if(sum <= s){
			x -= 1<<i;
		}else{
			sum = s;
		}
	}
	cout << sum << " " << x << "\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while(t--){
      	solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define Fo(i,n) for(int i=0;i<n;i++)
#define fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>  pii;
typedef pair<ll, ll>  pl;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vpii;
typedef vector<pl>    vpl;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
  uniform_int_distribution<int> uid(0, lim - 1);
  return uid(rang);
}
ll mpow(ll base, ll exp);
ll power(ll a,ll b);

const ll mod = 1000000007;
//const int N = 3e5, M = N;
  vi g[300001];
  // map<pii,int> w;
  int vis[300001];
  ll res[300001];
  ll sum[300001];

  ll sz[300001];
 //int dist[300001];

//=====================================================================
// struct node
// {
//   ll sum,wt_sum;
// };
// struct operatr
// {
//   int a,d;
// };
struct segTree
{
  int arr[2001];
  int res[8003];

  //int operation_arr[4003];
  int NO_OPERATION=0;

  void init(int x,int lo,int hi)
  {
    //operation_arr[x]=NO_OPERATION;

    if(lo==hi){

      res[x]=arr[lo];
      return;
    }

    int mid=(lo+hi)/2;
    init(2*x,lo,mid);
    init(2*x+1,mid+1,hi);

    //update(x,lo,hi);
    update_point(x);
  }

  // int merge_operation(ll a,ll b)//apply operation b to a
  // {
  //   return a+b;
  // }

  // int operate(int a,int o,int lo,int hi)
  // {
  //   int len=hi-lo+1;
  //   return a+len*o;
  // }

  // ll merge_results(ll a, ll b)
  // {
  //   return (a+b);
  // }

  // void modify(int x,int lo, int hi, int l, int r,int op)
  // {
  //   prop(x,lo,hi);

  //   if(lo>=l&&hi<=r){
  //     operation_arr[x]=merge_operation(operation_arr[x],op);
  //     return;
  //   }

  //   int mid=(lo+hi)/2;

  //   if(lo>r||hi<l)
  //     return;

  //   modify(2*x,lo,mid,l,r,op);
  //   modify(2*x+1,mid+1,hi,l,r,op);

  //   update(x,lo,hi);
  // }
  int get_sum(int x,int lo,int hi,int l,int r)
  {
    if(lo>=l&&hi<=r)
      return res[x];

    if(lo>r||hi<l)
      return 0;

    int mid=(lo+hi)/2;
    return get_sum(2*x,lo,mid,l,r)+get_sum(2*x+1,mid+1,hi,l,r);
  }

  void point_modify(int x,int lo,int hi,int pos,ll val)
  {
    if(lo==hi)
    {
      res[x]+=val;
      return;
    }

    int mid=(lo+hi)/2;

    if(pos<=mid)
      point_modify(2*x,lo,mid,pos,val);
    else
      point_modify(2*x+1,mid+1,hi,pos,val);
    update_point(x);
  }

  void update_point(int x)
  {
    res[x]=min(res[2*x],res[2*x+1]);
  }

  // int query(int x,int lo,int hi,int l,int r)
  // {
  //   //prop(x,lo,hi);

  //   if(lo>=l&&hi<=r)
  //     return operate(res[x],operation_arr[x],lo,hi);

  //   int mid=(hi+lo)/2;
  //   int res;

  //   if(r<=mid)
  //      res=query(2*x,lo,mid,l,r);
  //   else if(l>mid)
  //     res= query(2*x+1,mid+1,hi,l,r);
  //   else
  //     res= merge_results(query(2*x,lo,mid,l,r),query(2*x+1,mid+1,hi,l,r));

  //   return res;

  // }

  ll point_query(int x,int lo, int hi, int val)
  {
    //prop(x,lo,hi);

    if(lo==hi)
      return lo;

    int mid=(lo+hi)/2;

    int v1=res[2*x];

    if(v1==val)
      return point_query(2*x,lo,mid,val);
    else
      return point_query(2*x+1,mid+1,hi,val);
  }

  // void prop(int x,int lo, int hi)
  // {
  //   if(lo==hi)
  //     return;

  //   int mid=(hi+lo)/2;
  //   ll len=mid-lo+1;

  //   operation_arr[2*x]=merge_operation(operation_arr[2*x],operation_arr[x]);
  //   operation_arr[2*x+1]=merge_operation(operation_arr[2*x+1],operation_arr[x]);

  //   operation_arr[x]=NO_OPERATION;

  //   update(x,lo,hi);
  // }

  // void update(int x,int lo,int hi)
  // {
  //   if(lo==hi)
  //     return;
  //   int mid=(lo+hi)/2;
  //   ll len1=(mid-lo+1);
  //   ll len2=(hi-mid);
  //   res[x]=merge_results(operate(res[2*x],operation_arr[2*x],lo,mid),operate(res[2*x+1],operation_arr[2*x+1],mid+1,hi));
  // }

};
struct  FenwickTree
{
  int arr[100];
  int n;

  void update(int i,int delta)
  {
    while(i<=n)
    {
      arr[i]+=delta;
      i=i+(i&(-i));
    }
  }

  int sum(int i)
  {
    if(i==0)
      return 0;
    int res=0;
    while(i>0)
    {
      res=res+arr[i];
      i-=(i&(-i));
    }

    return res;
  }

};

ll dist(pl p1,pl p2)
{
  return abs(p1.F-p2.F)+abs(p1.S-p2.S);
}

ll get_max(vpl pts,int strt,ll t)
{
  int cnt=1;
  ll time=t;

  int mn_idx=strt;
  int mx_idx=strt;

  for(int i=strt;i>=0;i--)
  {
    pl p1=pts[i];
    pl p2=pts[strt];

    if(dist(p1,p2)<=time)
      mn_idx=i;
  }

  cnt+=(strt-mn_idx);

  time-=2*dist(pts[strt],pts[mn_idx]);

  int c1=0;
  int mx_idx2=strt;
  for(int i=strt;i<pts.size();i++)
  {
    pl p1=pts[strt];
    pl p2=pts[i];

    if(dist(p1,p2)<=time)
      mx_idx=i;
    if(dist(p1,p2)<=t)
      mx_idx2=i;
  }

  cnt=cnt+(mx_idx-strt);

  return max(cnt,mx_idx2-strt+1);

}

struct dsu
{
  int par[1001];
  int size_set[1001];

  int get_par(int u)
  {
    vi temp;
    while(par[u]!=u)
    {
      temp.pb(u);
      u=par[u];
    }

    for(int i:temp)
      par[i]=u;

    return u;
  }

  void union_set(int a,int b)
  {
    a=get_par(a);
    b=get_par(b);

    if(a==b)
      return;
    if(size_set[a]<size_set[b])
    {
      int temp=a;
      a=b;
      b=temp;
    }

    //assuming a to have a larger size
    par[b]=a;

    size_set[a]+=size_set[b];
    // transfer all elements in b to a

  }

};
struct query
{
  string s;
  int u,v;
};

int b_search(vi a, int v,int end)
{
  int l=0,r=end;
  int res=-1;

  while(l<=r)
  {
    int mid=l+(r-l)/2;

    if(a[mid]>=v)
      res=mid,r=mid-1;
    else
      l=mid+1;
  }

  return res;
}
ll dist(ll a,ll b,ll c,ll d,ll e)
{
  if(a==-1&&b==-1&&c==-1)
    return abs(d-e);
  if(a==-1&&b==-1)
    return abs(c-e)+abs(d-e);

  return abs(a-e)+abs(b-e)+abs(c-e)+abs(d-e);
}

ll binary_s(ll a,ll b,ll c,ll d)
{
  ll l=1,r=1000000000;

  ll res=-1;

  while(l<=r)
  {
    ll mid=l+(r-l)/2;

    ll d1=dist(a,b,c,d,mid),d2=dist(a,b,c,d,mid-1),d3=dist(a,b,c,d,mid+1);

    if(d1<=d2&&d1<=d3)
    {
      res=d1;
      break;
    }
    if(d2>=d1&&d3<=d1)
      l=mid+1;
    else
      r=mid-1;
  }

  return res;
}
struct result
{
  ll i1,j1,x;
};
void solve()
{
  int n;
  cin>>n;

  ll a[n];

  for(int i=0;i<n;i++)
    cin>>a[i];

  ll anding=0;

  vi o(300005);
  vi z(300005);

  ll pw[64];
  pw[0]=1;
  for(int i=1;i<=32;i++)
    pw[i]=pw[i-1]*2;

  ll res=0;ll inv_cnt=0;

  for(ll i=31;i>=0;i--)
  {
    ll t1=i+1;
    anding=anding+(pw[t1]);
    unordered_map<ll,ll> mp;
    int cnt=0;

    ll over_o_z=0,over_z_o=0;
    ll v=pw[i];

    for(int j=0;j<n;j++)
    {
      ll k=(a[j]&anding);
      if(mp.find(k)==mp.end())
      {
        mp[k]=cnt;

        o[cnt]=0;
        z[cnt]=0;

        ll t2=(a[j]&v);

        if(t2==0)
          over_o_z+=o[cnt],z[cnt]++;
        else
          over_z_o+=z[cnt],o[cnt]++;

        cnt++;
      }
      else
      {
        ll t2=(a[j]&v);

        ll v3=mp[k];

        if(t2==0)
          over_o_z+=o[v3],z[v3]++;
        else
          over_z_o+=z[v3],o[v3]++;
      }
    }

    if(over_o_z>over_z_o)
    {
      ll v1=pw[i];
      res=res+v1;
    }
    inv_cnt+=min(over_z_o,over_o_z);

  }

  cout<<inv_cnt<<" "<<res<<"\n";
}

int main()
{

    #ifndef ONLINE_JUDGE
          freopen("input.txt","r",stdin);
          freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    //seive();

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }

    return 0;
}
//=======================================================================================
ll mpow(ll base, ll exp)
{
    base %= mod;
    ll result = 1;
    while (exp > 0)
    {
        if (exp & 1)
          result = ((ll)result * base) % mod;
        base = ((ll)base * base) % mod;
        exp >>= 1;
  }
  return result;
}

ll power(ll a,ll b)
{
  ll res=1;
  while(b>0)
  {
    if(b%2==0)
    {
      b/=2;
      a=a*a;
    }
    else
    {
      res=res*a;
      b--;
    }
  }
  return res;
}


#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define Fo(i,n) for(int i=0;i<n;i++)
#define fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>  pii;
typedef pair<ll, ll>  pl;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vpii;
typedef vector<pl>    vpl;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
  uniform_int_distribution<int> uid(0, lim - 1);
  return uid(rang);
}
ll mpow(ll base, ll exp);
ll power(ll a,ll b);

const ll mod = 1000000007;
//const int N = 3e5, M = N;
  vi g[300001];
  // map<pii,int> w;
  int vis[300001];
  ll res[300001];
  ll sum[300001];

  ll sz[300001];
 //int dist[300001];

//=====================================================================
// struct node
// {
//   ll sum,wt_sum;
// };
// struct operatr
// {
//   int a,d;
// };
struct segTree
{
  int arr[2001];
  int res[8003];

  //int operation_arr[4003];
  int NO_OPERATION=0;

  void init(int x,int lo,int hi)
  {
    //operation_arr[x]=NO_OPERATION;

    if(lo==hi){

      res[x]=arr[lo];
      return;
    }

    int mid=(lo+hi)/2;
    init(2*x,lo,mid);
    init(2*x+1,mid+1,hi);

    //update(x,lo,hi);
    update_point(x);
  }

  // int merge_operation(ll a,ll b)//apply operation b to a
  // {
  //   return a+b;
  // }

  // int operate(int a,int o,int lo,int hi)
  // {
  //   int len=hi-lo+1;
  //   return a+len*o;
  // }

  // ll merge_results(ll a, ll b)
  // {
  //   return (a+b);
  // }

  // void modify(int x,int lo, int hi, int l, int r,int op)
  // {
  //   prop(x,lo,hi);

  //   if(lo>=l&&hi<=r){
  //     operation_arr[x]=merge_operation(operation_arr[x],op);
  //     return;
  //   }

  //   int mid=(lo+hi)/2;

  //   if(lo>r||hi<l)
  //     return;

  //   modify(2*x,lo,mid,l,r,op);
  //   modify(2*x+1,mid+1,hi,l,r,op);

  //   update(x,lo,hi);
  // }
  int get_sum(int x,int lo,int hi,int l,int r)
  {
    if(lo>=l&&hi<=r)
      return res[x];

    if(lo>r||hi<l)
      return 0;

    int mid=(lo+hi)/2;
    return get_sum(2*x,lo,mid,l,r)+get_sum(2*x+1,mid+1,hi,l,r);
  }

  void point_modify(int x,int lo,int hi,int pos,ll val)
  {
    if(lo==hi)
    {
      res[x]+=val;
      return;
    }

    int mid=(lo+hi)/2;

    if(pos<=mid)
      point_modify(2*x,lo,mid,pos,val);
    else
      point_modify(2*x+1,mid+1,hi,pos,val);
    update_point(x);
  }

  void update_point(int x)
  {
    res[x]=min(res[2*x],res[2*x+1]);
  }

  // int query(int x,int lo,int hi,int l,int r)
  // {
  //   //prop(x,lo,hi);

  //   if(lo>=l&&hi<=r)
  //     return operate(res[x],operation_arr[x],lo,hi);

  //   int mid=(hi+lo)/2;
  //   int res;

  //   if(r<=mid)
  //      res=query(2*x,lo,mid,l,r);
  //   else if(l>mid)
  //     res= query(2*x+1,mid+1,hi,l,r);
  //   else
  //     res= merge_results(query(2*x,lo,mid,l,r),query(2*x+1,mid+1,hi,l,r));

  //   return res;

  // }

  ll point_query(int x,int lo, int hi, int val)
  {
    //prop(x,lo,hi);

    if(lo==hi)
      return lo;

    int mid=(lo+hi)/2;

    int v1=res[2*x];

    if(v1==val)
      return point_query(2*x,lo,mid,val);
    else
      return point_query(2*x+1,mid+1,hi,val);
  }

  // void prop(int x,int lo, int hi)
  // {
  //   if(lo==hi)
  //     return;

  //   int mid=(hi+lo)/2;
  //   ll len=mid-lo+1;

  //   operation_arr[2*x]=merge_operation(operation_arr[2*x],operation_arr[x]);
  //   operation_arr[2*x+1]=merge_operation(operation_arr[2*x+1],operation_arr[x]);

  //   operation_arr[x]=NO_OPERATION;

  //   update(x,lo,hi);
  // }

  // void update(int x,int lo,int hi)
  // {
  //   if(lo==hi)
  //     return;
  //   int mid=(lo+hi)/2;
  //   ll len1=(mid-lo+1);
  //   ll len2=(hi-mid);
  //   res[x]=merge_results(operate(res[2*x],operation_arr[2*x],lo,mid),operate(res[2*x+1],operation_arr[2*x+1],mid+1,hi));
  // }

};
struct  FenwickTree
{
  int arr[100];
  int n;

  void update(int i,int delta)
  {
    while(i<=n)
    {
      arr[i]+=delta;
      i=i+(i&(-i));
    }
  }

  int sum(int i)
  {
    if(i==0)
      return 0;
    int res=0;
    while(i>0)
    {
      res=res+arr[i];
      i-=(i&(-i));
    }

    return res;
  }

};

ll dist(pl p1,pl p2)
{
  return abs(p1.F-p2.F)+abs(p1.S-p2.S);
}

ll get_max(vpl pts,int strt,ll t)
{
  int cnt=1;
  ll time=t;

  int mn_idx=strt;
  int mx_idx=strt;

  for(int i=strt;i>=0;i--)
  {
    pl p1=pts[i];
    pl p2=pts[strt];

    if(dist(p1,p2)<=time)
      mn_idx=i;
  }

  cnt+=(strt-mn_idx);

  time-=2*dist(pts[strt],pts[mn_idx]);

  int c1=0;
  int mx_idx2=strt;
  for(int i=strt;i<pts.size();i++)
  {
    pl p1=pts[strt];
    pl p2=pts[i];

    if(dist(p1,p2)<=time)
      mx_idx=i;
    if(dist(p1,p2)<=t)
      mx_idx2=i;
  }

  cnt=cnt+(mx_idx-strt);

  return max(cnt,mx_idx2-strt+1);

}

struct dsu
{
  int par[1001];
  int size_set[1001];

  int get_par(int u)
  {
    vi temp;
    while(par[u]!=u)
    {
      temp.pb(u);
      u=par[u];
    }

    for(int i:temp)
      par[i]=u;

    return u;
  }

  void union_set(int a,int b)
  {
    a=get_par(a);
    b=get_par(b);

    if(a==b)
      return;
    if(size_set[a]<size_set[b])
    {
      int temp=a;
      a=b;
      b=temp;
    }

    //assuming a to have a larger size
    par[b]=a;

    size_set[a]+=size_set[b];
    // transfer all elements in b to a

  }

};
struct query
{
  string s;
  int u,v;
};

int b_search(vi a, int v,int end)
{
  int l=0,r=end;
  int res=-1;

  while(l<=r)
  {
    int mid=l+(r-l)/2;

    if(a[mid]>=v)
      res=mid,r=mid-1;
    else
      l=mid+1;
  }

  return res;
}
ll dist(ll a,ll b,ll c,ll d,ll e)
{
  if(a==-1&&b==-1&&c==-1)
    return abs(d-e);
  if(a==-1&&b==-1)
    return abs(c-e)+abs(d-e);

  return abs(a-e)+abs(b-e)+abs(c-e)+abs(d-e);
}

ll binary_s(ll a,ll b,ll c,ll d)
{
  ll l=1,r=1000000000;

  ll res=-1;

  while(l<=r)
  {
    ll mid=l+(r-l)/2;

    ll d1=dist(a,b,c,d,mid),d2=dist(a,b,c,d,mid-1),d3=dist(a,b,c,d,mid+1);

    if(d1<=d2&&d1<=d3)
    {
      res=d1;
      break;
    }
    if(d2>=d1&&d3<=d1)
      l=mid+1;
    else
      r=mid-1;
  }

  return res;
}
struct result
{
  ll i1,j1,x;
};
void solve()
{
  int n;
  cin>>n;

  ll a[n];

  for(int i=0;i<n;i++)
    cin>>a[i];

  ll anding=0;

  vi temp[300001];

  ll res=0;ll inv_cnt=0;

  for(ll i=30;i>=0;i--)
  {
    ll t1=i+1;
    anding=anding+(1<<t1);
    unordered_map<ll,ll> mp;
    int cnt=0;

    ll over_o_z=0,over_z_o=0;

    for(int j=0;j<n;j++)
    {
      ll k=(a[j]&anding);
      if(mp.find(k)==mp.end())
      {
        mp[k]=cnt;
        temp[cnt].clear();

        temp[cnt].pb(a[j]);
        cnt++;
      }
      else
      {
        temp[mp[k]].pb(a[j]);
      }
    }

    for(int j=0;j<=cnt;j++)
    {
      vl bits;
      for(int l=0;l<temp[j].size();l++)
      {
        if((temp[j][l]&(1<<i))==0)
          bits.pb(0);
        else
          bits.pb(1);
      }

      ll z_o=0,o_z=0;
      ll o_cnt=0,z_cnt=0;

      for(int t=0;t<bits.size();t++)
      {
        if(bits[t]==0)
        {
          o_z=o_z+o_cnt;
          z_cnt++;
        }
        else
        {
          z_o+=z_cnt;
          o_cnt++;
        }
      }
      over_z_o+=z_o;
      over_o_z+=o_z;

      // if(o_z>z_o)
      // {
      //   res=res+(1<<i);
      // }

      // inv_cnt+=min(o_z,z_o);
    }
    if(over_o_z>over_z_o)
    {
      res=res+(1<<i);
    }
    inv_cnt+=min(over_z_o,over_o_z);

  }

  cout<<inv_cnt<<" "<<res<<"\n";
}

int main()
{

    #ifndef ONLINE_JUDGE
          freopen("input.txt","r",stdin);
          freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    //seive();

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }

    return 0;
}
//=======================================================================================
ll mpow(ll base, ll exp)
{
    base %= mod;
    ll result = 1;
    while (exp > 0)
    {
        if (exp & 1)
          result = ((ll)result * base) % mod;
        base = ((ll)base * base) % mod;
        exp >>= 1;
  }
  return result;
}

ll power(ll a,ll b)
{
  ll res=1;
  while(b>0)
  {
    if(b%2==0)
    {
      b/=2;
      a=a*a;
    }
    else
    {
      res=res*a;
      b--;
    }
  }
  return res;
}


#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define Fo(i,n) for(int i=0;i<n;i++)
#define fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>  pii;
typedef pair<ll, ll>  pl;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vpii;
typedef vector<pl>    vpl;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
  uniform_int_distribution<int> uid(0, lim - 1);
  return uid(rang);
}
ll mpow(ll base, ll exp);
ll power(ll a,ll b);

const ll mod = 1000000007;
//const int N = 3e5, M = N;
  vi g[300001];
  // map<pii,int> w;
  int vis[300001];
  ll res[300001];
  ll sum[300001];

  ll sz[300001];
 //int dist[300001];

//=====================================================================
// struct node
// {
//   ll sum,wt_sum;
// };
// struct operatr
// {
//   int a,d;
// };
struct segTree
{
  int arr[2001];
  int res[8003];

  //int operation_arr[4003];
  int NO_OPERATION=0;

  void init(int x,int lo,int hi)
  {
    //operation_arr[x]=NO_OPERATION;

    if(lo==hi){

      res[x]=arr[lo];
      return;
    }

    int mid=(lo+hi)/2;
    init(2*x,lo,mid);
    init(2*x+1,mid+1,hi);

    //update(x,lo,hi);
    update_point(x);
  }

  // int merge_operation(ll a,ll b)//apply operation b to a
  // {
  //   return a+b;
  // }

  // int operate(int a,int o,int lo,int hi)
  // {
  //   int len=hi-lo+1;
  //   return a+len*o;
  // }

  // ll merge_results(ll a, ll b)
  // {
  //   return (a+b);
  // }

  // void modify(int x,int lo, int hi, int l, int r,int op)
  // {
  //   prop(x,lo,hi);

  //   if(lo>=l&&hi<=r){
  //     operation_arr[x]=merge_operation(operation_arr[x],op);
  //     return;
  //   }

  //   int mid=(lo+hi)/2;

  //   if(lo>r||hi<l)
  //     return;

  //   modify(2*x,lo,mid,l,r,op);
  //   modify(2*x+1,mid+1,hi,l,r,op);

  //   update(x,lo,hi);
  // }
  int get_sum(int x,int lo,int hi,int l,int r)
  {
    if(lo>=l&&hi<=r)
      return res[x];

    if(lo>r||hi<l)
      return 0;

    int mid=(lo+hi)/2;
    return get_sum(2*x,lo,mid,l,r)+get_sum(2*x+1,mid+1,hi,l,r);
  }

  void point_modify(int x,int lo,int hi,int pos,ll val)
  {
    if(lo==hi)
    {
      res[x]+=val;
      return;
    }

    int mid=(lo+hi)/2;

    if(pos<=mid)
      point_modify(2*x,lo,mid,pos,val);
    else
      point_modify(2*x+1,mid+1,hi,pos,val);
    update_point(x);
  }

  void update_point(int x)
  {
    res[x]=min(res[2*x],res[2*x+1]);
  }

  // int query(int x,int lo,int hi,int l,int r)
  // {
  //   //prop(x,lo,hi);

  //   if(lo>=l&&hi<=r)
  //     return operate(res[x],operation_arr[x],lo,hi);

  //   int mid=(hi+lo)/2;
  //   int res;

  //   if(r<=mid)
  //      res=query(2*x,lo,mid,l,r);
  //   else if(l>mid)
  //     res= query(2*x+1,mid+1,hi,l,r);
  //   else
  //     res= merge_results(query(2*x,lo,mid,l,r),query(2*x+1,mid+1,hi,l,r));

  //   return res;

  // }

  ll point_query(int x,int lo, int hi, int val)
  {
    //prop(x,lo,hi);

    if(lo==hi)
      return lo;

    int mid=(lo+hi)/2;

    int v1=res[2*x];

    if(v1==val)
      return point_query(2*x,lo,mid,val);
    else
      return point_query(2*x+1,mid+1,hi,val);
  }

  // void prop(int x,int lo, int hi)
  // {
  //   if(lo==hi)
  //     return;

  //   int mid=(hi+lo)/2;
  //   ll len=mid-lo+1;

  //   operation_arr[2*x]=merge_operation(operation_arr[2*x],operation_arr[x]);
  //   operation_arr[2*x+1]=merge_operation(operation_arr[2*x+1],operation_arr[x]);

  //   operation_arr[x]=NO_OPERATION;

  //   update(x,lo,hi);
  // }

  // void update(int x,int lo,int hi)
  // {
  //   if(lo==hi)
  //     return;
  //   int mid=(lo+hi)/2;
  //   ll len1=(mid-lo+1);
  //   ll len2=(hi-mid);
  //   res[x]=merge_results(operate(res[2*x],operation_arr[2*x],lo,mid),operate(res[2*x+1],operation_arr[2*x+1],mid+1,hi));
  // }

};
struct  FenwickTree
{
  int arr[100];
  int n;

  void update(int i,int delta)
  {
    while(i<=n)
    {
      arr[i]+=delta;
      i=i+(i&(-i));
    }
  }

  int sum(int i)
  {
    if(i==0)
      return 0;
    int res=0;
    while(i>0)
    {
      res=res+arr[i];
      i-=(i&(-i));
    }

    return res;
  }

};

ll dist(pl p1,pl p2)
{
  return abs(p1.F-p2.F)+abs(p1.S-p2.S);
}

ll get_max(vpl pts,int strt,ll t)
{
  int cnt=1;
  ll time=t;

  int mn_idx=strt;
  int mx_idx=strt;

  for(int i=strt;i>=0;i--)
  {
    pl p1=pts[i];
    pl p2=pts[strt];

    if(dist(p1,p2)<=time)
      mn_idx=i;
  }

  cnt+=(strt-mn_idx);

  time-=2*dist(pts[strt],pts[mn_idx]);

  int c1=0;
  int mx_idx2=strt;
  for(int i=strt;i<pts.size();i++)
  {
    pl p1=pts[strt];
    pl p2=pts[i];

    if(dist(p1,p2)<=time)
      mx_idx=i;
    if(dist(p1,p2)<=t)
      mx_idx2=i;
  }

  cnt=cnt+(mx_idx-strt);

  return max(cnt,mx_idx2-strt+1);

}

struct dsu
{
  int par[1001];
  int size_set[1001];

  int get_par(int u)
  {
    vi temp;
    while(par[u]!=u)
    {
      temp.pb(u);
      u=par[u];
    }

    for(int i:temp)
      par[i]=u;

    return u;
  }

  void union_set(int a,int b)
  {
    a=get_par(a);
    b=get_par(b);

    if(a==b)
      return;
    if(size_set[a]<size_set[b])
    {
      int temp=a;
      a=b;
      b=temp;
    }

    //assuming a to have a larger size
    par[b]=a;

    size_set[a]+=size_set[b];
    // transfer all elements in b to a

  }

};
struct query
{
  string s;
  int u,v;
};

int b_search(vi a, int v,int end)
{
  int l=0,r=end;
  int res=-1;

  while(l<=r)
  {
    int mid=l+(r-l)/2;

    if(a[mid]>=v)
      res=mid,r=mid-1;
    else
      l=mid+1;
  }

  return res;
}
ll dist(ll a,ll b,ll c,ll d,ll e)
{
  if(a==-1&&b==-1&&c==-1)
    return abs(d-e);
  if(a==-1&&b==-1)
    return abs(c-e)+abs(d-e);

  return abs(a-e)+abs(b-e)+abs(c-e)+abs(d-e);
}

ll binary_s(ll a,ll b,ll c,ll d)
{
  ll l=1,r=1000000000;

  ll res=-1;

  while(l<=r)
  {
    ll mid=l+(r-l)/2;

    ll d1=dist(a,b,c,d,mid),d2=dist(a,b,c,d,mid-1),d3=dist(a,b,c,d,mid+1);

    if(d1<=d2&&d1<=d3)
    {
      res=d1;
      break;
    }
    if(d2>=d1&&d3<=d1)
      l=mid+1;
    else
      r=mid-1;
  }

  return res;
}
struct result
{
  ll i1,j1,x;
};
void solve()
{
  int n;
  cin>>n;

  ll a[n];

  for(int i=0;i<n;i++)
    cin>>a[i];

  ll anding=0;

  vi o(300005);
  vi z(300005);
  unordered_map<ll,ll> mp;
  ll pw[64];
  pw[0]=1;
  for(int i=1;i<=32;i++)
    pw[i]=pw[i-1]*2;

  ll res=0;ll inv_cnt=0;

  for(ll i=31;i>=0;i--)
  {
    ll t1=i+1;
    anding=anding+(pw[t1]);
    mp.clear();
    int cnt=0;

    ll over_o_z=0,over_z_o=0;
    ll v=pw[i];

    for(int j=0;j<n;j++)
    {
      ll k=(a[j]&anding);
      if(mp.find(k)==mp.end())
      {
        mp[k]=cnt;

        o[cnt]=0;
        z[cnt]=0;

        ll t2=(a[j]&v);

        if(t2==0)
          over_o_z+=o[cnt],z[cnt]++;
        else
          over_z_o+=z[cnt],o[cnt]++;

        cnt++;
      }
      else
      {
        ll t2=(a[j]&v);

        ll v3=mp[k];

        if(t2==0)
          over_o_z+=o[v3],z[v3]++;
        else
          over_z_o+=z[v3],o[v3]++;
      }
    }

    if(over_o_z>over_z_o)
    {
      ll v1=pw[i];
      res=res+v1;
    }
    inv_cnt+=min(over_z_o,over_o_z);

  }

  cout<<inv_cnt<<" "<<res<<"\n";
}

int main()
{

    #ifndef ONLINE_JUDGE
          freopen("input.txt","r",stdin);
          freopen("output.txt","w",stdout);
    #endif

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    //seive();

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }

    return 0;
}
//=======================================================================================
ll mpow(ll base, ll exp)
{
    base %= mod;
    ll result = 1;
    while (exp > 0)
    {
        if (exp & 1)
          result = ((ll)result * base) % mod;
        base = ((ll)base * base) % mod;
        exp >>= 1;
  }
  return result;
}

ll power(ll a,ll b)
{
  ll res=1;
  while(b>0)
  {
    if(b%2==0)
    {
      b/=2;
      a=a*a;
    }
    else
    {
      res=res*a;
      b--;
    }
  }
  return res;
}