luogu#P4785. [BalticOI 2016] 交换 (Day2)

[BalticOI 2016] 交换 (Day2)

Problem Description

You are given a sequence x1,x2,,xnx_1, x_2, \dots, x_n containing nn numbers. Each number 1,2,,n1, 2, \dots, n appears in the sequence exactly once.

You may modify this sequence by swapping. You must perform n1n - 1 consecutive rounds of operations, numbered k=2,3,,nk = 2, 3, \dots, n. In round kk, you may choose to swap xkx_k and xk/2x_{\lfloor k/2 \rfloor}, or do nothing.

If there exists a number jj (1jn)(1 \leq j \leq n) such that for all k<jk < j, ak=bka_k = b_k holds, and aj<bja_j < b_j, then the sequence a1ana_1 \dots a_n is "lexicographically smaller" than the sequence b1bnb_1 \dots b_n.

What is the lexicographically smallest sequence you can obtain?

Input Format

The first line contains an integer nn.
The second line contains nn integers, representing the sequence x1,x2,,xnx_1, x_2, \dots, x_n.

Output Format

Output nn integers, representing the lexicographically smallest sequence you can obtain.

5
3 4 2 5 1
2 1 3 4 5

Hint

Subtask Score Constraints
1 10 1n201 \leq n \leq 20
2 11 1n401 \leq n \leq 40
3 27 1n10001 \leq n \leq 1000
4 20 1n51041 \leq n \leq 5 \cdot 10^4
5 32 1n21051 \leq n \leq 2 \cdot 10^5

Translated by ChatGPT 5