Codeforces Round #618 (Div. 2) (位运算计数)

C. Anu Has a Function

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 <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <functional>
//#include <unordered_map>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>
#include <cstring>
//#include <multimap>
//#include <multiset>
using namespace std;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const ll mod = 1000000007 ;
const int mxn = 2e5+10 ;
ll prime[mxn], isprime[mxn],bit[mxn],a[mxn],dp[11][1000+10];
int ch[27];
ll n,m,t,u,v,w,l,r,k,x;
string str ;
int main()
{
    TLE;
    while(cin>>n)
    {
        vector < vector<int> > vc(32,vector<int>()) ;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            int x = a[i], pos = 0 ;
            while(x)
            {
                if(x%2)
                    vc[pos].pb(i);
                x/=2;
                pos++;
            }
        }
        int ans = -1 ;
        for(int i=vc.size()-1; i>=0; i--)
            if(vc[i].size()==1)
            {
                ans = vc[i][0];
                break;
            }
        if(ans!=-1)
            swap(a[ans],a[1]);
        for(int i=1; i<=n; i++)
            cout<<a[i]<<" ";
        cout<<endl;

    }
    return 0 ;
}
发布了94 篇原创文章 · 获赞 29 · 访问量 8545

猜你喜欢

转载自blog.csdn.net/m0_43382549/article/details/104244234