Swap

Given a permutation, decide for each k = 2..n whether to swap position k with floor(k/2), producing the lexicographically smallest reachable sequence.

Medium6GreedyTreeImplementationNo attempts yetTime limit1sMemory limit256 MB

Problem

A sequence x1,x2,,xnx_1, x_2, \dots, x_n of length nn is given. Every integer from 11 to nn appears in it exactly once.

You can change the sequence with a "swap" operation that exchanges two numbers. Going through k=2,3,,nk = 2, 3, \dots, n in that order, for each kk you can choose whether to exchange xkx_k and xk/2x_{\lfloor k/2 \rfloor}. You cannot go back to a value of kk that you already passed.

A sequence a1,a2,,ana_1, a_2, \dots, a_n comes before a sequence b1,b2,,bnb_1, b_2, \dots, b_n in lexicographic order when some jj (1jn)(1 \le j \le n) satisfies ak=bka_k = b_k for every k<jk < j and aj<bja_j < b_j.

Among the sequences you can build by choosing the "swap" operations in this order, which one comes first in lexicographic order?

Input

The first line contains the integer nn. (1n5000)(1 \le n \le 5000)

The second line contains the nn integers of the sequence, separated by spaces. They form a permutation of the integers from 11 to nn.

Output

On the first line, print the nn integers of the sequence that comes first in lexicographic order among the sequences you can build by choosing the "swap" operations in order, separated by spaces.