PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000302. Các số tương thích (Compatible Numbers)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các số tương thích (Compatible Numbers)

Compatible Numbers

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array a1,a2,…,ana_1,a_2,\ldots,a_n of nn positive integers, call two integers xx and yy compatible if their bitwise AND is zero, i.e. (x&y)=0(x\mathbin{\&}y)=0. They have no bit position at which both bits are 11.

For each position ii, find an entry aja_j at another position in the array that is compatible with aia_i. If no such entry exists, the result for position ii is −1-1. If there are multiple suitable entries, any one is accepted.

Report the value of the chosen entry, not its index.

Input

The first line contains an integer nn, the length of the array.

The second line contains nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n; equal values are allowed.

Output

Print nn integers in input order on one line. Each is a valid compatible value from the sequence or −1-1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤ai≤4⋅1061\le a_i\le4\cdot10^6 (for every 1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 1≤n≤1061\le n\le10^6; 1≤ai≤10231\le a_i\le1023 (for every 1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤1061\le n\le10^6; 1≤ai≤4⋅1061\le a_i\le4\cdot10^6 (for every 1≤i≤n1\le i\le n).

Example 1

Input

2
90 36

Output

36 90

Explanation

90=(1011010)290=(1011010)_2 and 36=(0100100)236=(0100100)_2 share no set bit, so 90&36=090\mathbin{\&}36=0. The first entry selects 3636 and the second selects 9090; print 36 90.

Example 2

Input

4
3 6 3 6

Output

-1 -1 -1 -1

Explanation

The only values are 33 and 66. Since 3&3=33\mathbin{\&}3=3, 3&6=23\mathbin{\&}6=2, and 6&6=66\mathbin{\&}6=6, all possible pairs have nonzero AND. No entry has a compatible value; all four results are -1.

Example 3

Input

5
10 6 9 8 2

Output

-1 8 2 2 8

Explanation

For 1010, its AND with 10,6,9,8,210,6,9,8,2 is respectively 10,2,8,8,210,2,8,8,2. None is zero, so its result is -1.

The other four choices satisfy 6&8=06\mathbin{\&}8=0, 9&2=09\mathbin{\&}2=0, 8&2=08\mathbin{\&}2=0, and 2&8=02\mathbin{\&}8=0. Every chosen value occurs in the array. Hence -1 8 2 2 8 is valid, but it is not the only valid answer.