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

#FT0000042. Chèn phần tử vào dãy (Insert)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Chèn phần tử vào dãy (Insert)

Insert

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Start with an empty array AA. For every i=1,2,…,Ni=1,2,\ldots,N in increasing order, insert integer ii so that it becomes the PiP_i-th element from the beginning, using one-based positions.

More precisely, preserve the first Pi−1P_i-1 existing elements, place ii immediately after them, and append all remaining elements in their original order. If Pi=1P_i=1, insert at the front; if Pi=iP_i=i, append to the end.

After all NN insertions, output the final array from left to right.

Input

  • Line 1: integer NN, the number of insertions.
  • Line 2: exactly NN integers P1,P2,…,PNP_1,P_2,\ldots,P_N, the insertion positions for 1,2,…,N1,2,\ldots,N.

Output

Print the NN integers of the final array AA, separated by spaces.

Subtasks

Subtask 1 (20 points):

  • 1≤N≤201\le N\le 20; 1≤Pi≤i1\le P_i\le i for every 1≤i≤N1\le i\le N; all inputs are integers.

Subtask 2 (30 points):

  • 1≤N≤20001\le N\le 2000; 1≤Pi≤i1\le P_i\le i for every 1≤i≤N1\le i\le N; all inputs are integers.

Subtask 3 (50 points):

  • 1≤N≤5000001\le N\le 500000; 1≤Pi≤i1\le P_i\le i for every 1≤i≤N1\le i\le N; all inputs are integers.

Examples

Examples 1

Input

4
1 1 2 1

Output

4 2 3 1

Explanation

Start from A=[]A=[]. Insert 11 at position 11: [1][1]. Insert 22 at position 11: [2,1][2,1]. Insert 33 at position 22: [2,3,1][2,3,1]. Insert 44 at position 11: [4,2,3,1][4,2,3,1].

Examples 2

Input

5
1 2 3 4 5

Output

1 2 3 4 5

Explanation

At step ii, Pi=iP_i=i, so each new element is appended at the end. The final array is [1,2,3,4,5][1,2,3,4,5].

The successive states are [1][1], [1,2][1,2], [1,2,3][1,2,3], [1,2,3,4][1,2,3,4], and [1,2,3,4,5][1,2,3,4,5]. At step ii, inserting at position ii appends the new value to the end, so the relative order of earlier values never changes.