CF BUDDY
← Problems·

675D · Tree Construction

1800 · data structures, trees

Problem: During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.

You are given a sequence aa, consisting of nn distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.

  1. First element a1a_1 becomes the root of the tree.
  2. Elements a2,a3,,ana_2, a_3, \ldots, a_n are added one by one. To add element aia_i one needs to traverse the tree starting from the root and using the following rules: The pointer to the current node is set to the root. If aia_i is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node. If at some point there is no required child, the new node is created, it is assigned value aia_i and becomes the corresponding child of the current node.

Input Format: The first line of the input contains a single integer nn (2n1000002 \leq n \leq 100\,000) — the length of the sequence aa.

The second line contains nn distinct integers aia_i (1ai1091 \leq a_i \leq 10^9) — the sequence aa itself.

Output Format: Output n1n - 1 integers. For all i>1i > 1 print the value written in the node that is the parent of the node with value aia_i in it.

Sample Cases

Case 1

Input

3
1 2 3

Output

1 2

Case 2

Input

5
4 2 3 1 6

Output

4 2 2 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback