#BST0000009. Xóa nút có một con (Delete a One-Child Node from a BST)

Xóa nút có một con (Delete a One-Child Node from a BST)

Delete a One-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 one child. When deleting xx, that unique child takes the position of xx:

  • if xx is not the root, the parent of xx is connected directly to the unique child;
  • if xx is the root, the unique child becomes the new root.

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): 2≤n≤202\le n\le 20.

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

Subtask 3 (50 points): 2≤n≤2000002\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 11 child.

Examples

Input

4
8 3 10 14
10

Output

8 3 14

Explanation

Node 1010 is the right child of 88 and has exactly one child, 1414. After deleting 1010, node 1414 is connected directly as the right child of 88.

The resulting preorder traversal is 8 3 14.