D. AND, OR and square sum

42 篇文章 0 订阅

Gottfried learned about binary number representation. He then came up with this task and presented it to you.

You are given a collection of nn non-negative integers a1,…,ana1,…,an. You are allowed to perform the following operation: choose two distinct indices 1≤i,j≤n1≤i,j≤n. If before the operation ai=xai=x, aj=yaj=y, then after the operation ai=x AND yai=x AND y, aj=x OR yaj=x OR y, where ANDAND and OROR are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).

After all operations are done, compute ∑ni=1a2i∑i=1nai2 — the sum of squares of all aiai. What is the largest sum of squares you can achieve?

Input

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).

The second line contains nn integers a1,…,ana1,…,an (0≤ai<2200≤ai<220).

Output

Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.

Examples

input

Copy

1
123

output

Copy

15129

input

Copy

3
1 3 5

output

Copy

51

input

Copy

2
349525 699050

output

Copy

1099509530625

题意:给你n个数,我们可以每次执行的操作是:选择两个下标i,j,a[i]=a[i]&a[j],a[j]=a[i]^a[j],可以进行无数次操作,求最后的所有数的平方和最大是多少

思路:设a[i]=x,a[j]=y,我们可以发现x+y==x&y+x^y,可以发现两个数当和一定的时候我们将其中一个数构造的最大的平方和是所有情况里和最大的

比如:x==101,y=110时,我们可以发现把x换成111,y换成100时的两个数的平方和最大

那么我们只需要统计一下二进制每位上的1的个数,

让他们尽量都安排在前面的数上,让他们尽可能大就行了

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
const int N=2e5+10;
int n;
int a[N];
int cnt[22];
void sove(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		for(int j=0;j<=20;j++){
			int op=(x>>j)&1;
			cnt[j]+=op;
		}
	}
//	for(int i=1;i<=20;i++)cout<<cnt[i]<<" ";
	//cout<<endl;
	for(int i=0;i<=20;i++){
		int num=cnt[i];
		for(int j=1;j<=n&&num;j++){
			a[j]+=(1<<i);
			num--;
		}
	}
	int con=0;
	for(int i=1;i<=n;i++){
		con+=a[i]*a[i];
	}
	cout<<con<<endl;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
//	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值