#BST0000010. Xóa nút có hai con (Delete a Two-Child Node from a BST)

Xóa nút có hai con (Delete a Two-Child Node from a BST)

Delete a Two-Child Node from a BST

Source: Phuoc Hung OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Build a BST from a1,a2,…,ana_1,a_2,\ldots,a_n in insertion order. Duplicate keys are ignored.

A key xx is guaranteed to exist, and its node has exactly two children. To make the deletion result unique, you must use the inorder successor of xx, defined as the node with the smallest key in the right subtree of xx.

Replace the key at the position of xx by the successor key, then delete the old successor node from its original position. The resulting tree must remain a BST.

Print the preorder traversal after the deletion.

Input

The first line contains nn.

The second line contains nn signed integers a1,a2,…,ana_1,a_2,\ldots,a_n.

The third line contains the key xx to delete.

Output

Print the preorder traversal of the resulting tree on one line, with keys separated by one space.

Subtasks

Subtask 1 (20 points): 3≤n≤203\le n\le 20.

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

Subtask 3 (50 points): 3≤n≤2000003\le n\le 200000.

In all subtasks, aia_i and xx are signed 64-bit integers, duplicate insertion keys are ignored, and the node with key xx exists and has exactly 22 children.

Examples

Input

7
8 3 10 1 6 14 4
3

Output

8 4 1 6 10 14

Explanation

Node 33 has two children. Its right subtree contains keys 66 and 44, whose minimum is 44, so 44 is the inorder successor.

Replace 33 by 44 and remove the old node 44. The resulting preorder is 8 4 1 6 10 14.