显示代码纯文本
#include<cstdio>
#include<algorithm>
int n,m,a[2001],f[2001];
long long ans=0;
struct edge{
int u,v,c;
}b[4000000];
bool cmp(edge a,edge b){
return a.c>b.c;
}
int find(int x){
return f[x]!=x?f[x]=find(f[x]):x;
}
int main(){
freopen("superbull.in","r",stdin);
freopen("superbull.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),f[i]=i;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
b[++m]=(edge){i,j,a[i]^a[j]};
std::sort(b+1,b+m+1,cmp);
for(int i=1,j=1;j<n;i++){
int x=find(b[i].u),y=find(b[i].v);
if(x==y) continue;
j++,f[y]=x,ans+=b[i].c;
}
printf("%lld",ans);
}