#ds401. 笛卡尔树

笛卡尔树

Cartesian Tree

Problem Description

You are given a permutation p1,p2,…,pnp_1, p_2, \dots, p_n of 1∼n1 \sim n.

Find a permutation qq of 1∼n1 \sim n such that for any interval [l,r][l, r] (1≤l≤r≤n1 \le l \le r \le n), the position of the minimum among pl,pl+1,…,prp_l, p_{l+1}, \dots, p_r equals the position of the minimum among ql,ql+1,…,qrq_l, q_{l+1}, \dots, q_r.

Output the lexicographically smallest such qq.

Input Format

The first line contains an integer nn (1≤n≤1061 \le n \le 10^6).

The next line contains a permutation p1,p2,…,pnp_1, p_2, \dots, p_n of length nn.

Output Format

One line with nn numbers q1,q2,…,qnq_1, q_2, \dots, q_n, the lexicographically smallest permutation.

Sample Input

5
4 3 1 5 2

Sample Output

3 2 1 5 4