Under construction

Still being built. Things may change

Lexicographically Maximum

Interview

Time limit1sMemory limit1024 MB

Summary
Bits in the same position can be swapped between any two numbers, so find the lexicographically largest array reachable after any number of swaps.
Level

Medium4 of 10

Topics
Bit manipulation, Greedy
Solved
No attempts yet

Problem

A list of NN integers a1,…,aNa_1, \dots , a_N is stored in the memory of an electronic device. The device has a peculiar operation: bit swapping between numbers. Given integers ii, jj and kk, this operation swaps the kk-th bit of aia_i with the kk-th bit of aja_j (and vice versa).

Very interesting phenomena can occur when this operation is performed one or more times, such as obtaining numbers that did not belong to the original list, or numbers larger or smaller than all the original elements.

Use the operation as many times as needed so that the resulting list is lexicographically maximum: a1a_1 is as large as possible, a2a_2 is as large as possible among the lists that maximize a1a_1, and so on.

Input

The first line contains an integer NN (1≤N≤1051 \le N \le 10^5). The second line contains NN integers separated by spaces, the list a1,…,aNa_1, \dots , a_N (0≤ai≤1090 \le a_i \le 10^9).

Output

Print a single line with NN integers separated by spaces, the lexicographically maximum sequence that can be obtained.

Examples2

  1. Example 1

    Input
    4
    8 4 2 1
    
    Expected output
    15 0 0 0
    
  2. Example 2

    Input
    4
    12 15 1 20
    
    Expected output
    31 13 4 0