#BST0000024. Dựng cây (Tree Construction)

Dựng cây (Tree Construction)

Tree Construction

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A sequence of distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n is used to construct a binary search tree in the following order.

Key a1a_1 becomes the root.

For every ii from 22 to nn, start at the root. If aia_i is smaller than the current key, move to the left child; if it is larger, move to the right child. When the required child is empty, create a new node with key aia_i there and finish this insertion.

For every inserted element from the second one onward, determine the key stored in the direct parent of the newly created node.

Input

The first line contains nn.

The second line contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n in insertion order.

Output

Print n−1n-1 integers on one line. For every 2≤i≤n2\le i\le n, the (i−1)(i-1)-th output integer is the key stored in the parent of the node with key aia_i.

Separate consecutive integers by one space.

Subtasks

Subtask 1 (20 points): 2≤n≤202\le n\le 20.

Subtask 2 (30 points): 2≤n≤50002\le n\le 5000.

Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000, 1≤ai≤1091\le a_i\le 10^9.

In all subtasks, all aia_i are distinct.

Examples

Input

5
4 2 3 1 6

Output

4 2 2 4

Explanation

Key 44 is the root. Inserting 22 makes it the left child of 44, so its parent is 44.

Next, 33 goes left from 44 to 22 and becomes the right child of 22, so its parent is 22. Key 11 becomes the left child of 22, and key 66 becomes the right child of 44.

Therefore the parent keys for a2,a3,a4,a5a_2,a_3,a_4,a_5 are 4 2 2 4.