C. Anu Has a Function

194 篇文章 1 订阅
71 篇文章 0 订阅

链接:https://codeforces.com/contest/1300/problem/C

Anu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for any nonnegative numbers xx and yy value of f(x,y)f(x,y) is also nonnegative.

She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.

A value of an array [a1,a2,…,an][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an−1),an)f(f(…f(f(a1,a2),a3),…an−1),an) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?

Input

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

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109). Elements of the array are not guaranteed to be different.

Output

Output nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.

Examples

input

Copy

4
4 0 11 6

output

Copy

11 6 4 0

input

Copy

1
13

output

Copy

13 

Note

In the first testcase, value of the array [11,6,4,0][11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.

[11,4,0,6][11,4,0,6] is also a valid answer.

 

#include<bits/stdc++.h>
using namespace std;
long long n,t,s,k,max1;
struct node{
	int s[100];
	int x;
	int sum;
	int id;
}a[200005];
bool cmp(node q,node g)
{
	int k1=q.sum;
	int k2=g.sum;
	while(q.s[k1]==g.s[k2]&&k1>1&&k2>1)
	{
		k1--;
		k2--;
	}
	if(q.s[k1]!=g.s[k2])
	return q.s[k1]>g.s[k2];
	else
	return q.sum>g.sum;
}
map<long long,long long>m;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].x;
		k=a[i].x;
		int j=1;
		while(k)
		{
			if(k%2==1)
			{
				m[j]++;
			}
			k/=2;
			j++;
		}	
	}
	max1=0;
	for(int i=1;i<=n;i++)
	{
		k=a[i].x;
		int j=1,p=0;
		while(k)
		{
			if(k%2==1)
			{
				if(m[j]==1)
				{
					p++;
					a[i].s[p]=j;
				}
			}
			k/=2;
			j++;
		}
		a[i].sum=p;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++)
	{
		cout<<a[i].x<<" ";
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值