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

#DSU0000065. Sắp xếp theo điều kiện XOR (XOUR)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sắp xếp theo điều kiện XOR (XOUR)

XOUR

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn nonnegative integers a1,a2,…,ana_1,a_2,\ldots,a_n. You may rearrange its elements using conditional swaps.

In one operation, choose any two positions i,ji,j. Let xx and yy be their current values. You may swap them only if xXORy<4x\mathbin{\mathrm{XOR}}y<4, where XOR is the bitwise exclusive-or operation. After a swap, conditions for subsequent swaps are evaluated on the values at their new positions.

Find the lexicographically smallest array obtainable after any finite number of valid swaps. If several sequences of swaps produce the same smallest array, only the resulting array is required.

Input

The first line contains nn. The second contains the nn values aia_i.

Output

Print the nn values of the lexicographically smallest reachable array.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30, 0≤ai≤2550\le a_i\le255.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000, 0≤ai≤1060\le a_i\le10^6.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5, 0≤ai≤1090\le a_i\le10^9.

Examples

Input

5
2 7 1 5 6

Output

1 5 2 6 7

Explanation

Values 1 and 2 can swap, and values 5, 6, 7 can be rearranged within their group. Sorting each group of values at its original positions yields the minimum array.