cswork / OAOpen practice workspace

OracleOA #3

Cardinality Sorting

Estimated EasyJudge-ready

Topics (AI-labelled)Bits & MathIn-place Arrays

Sort first by the number of 1s in the binary representation in ascending order, then by integer value in ascending order for equal counts; all duplicate elements are kept.

Input

The first line contains n, and the second line contains n integers. Full original bounds: 1≤n≤100000, 1≤a[i]≤1000000.

Output

Output the n sorted integers, separated by spaces.

Example 1

Input:

5
31 15 7 3 2

Output:

2 3 7 15 31

Example 2

Input:

5
1 2 3 4 5

Output:

1 2 4 3 5

Example 3

Input:

3
3 3 1

Output:

1 3 3

Example notes

The outputs of the three public examples are, in order: 2 3 7 15 31; 1 2 4 3 5; 1 3 3. They were verified by independent enumeration or direct simulation.

Editorial

Loading solution…