#BST0000020. Tổ tiên chung gần nhất trong BST (Lowest Common Ancestor of a Binary Search Tree)

Tổ tiên chung gần nhất trong BST (Lowest Common Ancestor of a Binary Search Tree)

Lowest Common Ancestor of a Binary Search Tree

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Build a BST from the nn distinct keys a1,a2,…,ana_1,a_2,\ldots,a_n in insertion order.

Two different keys pp and qq are guaranteed to exist. A node uu is a common ancestor of pp and qq if both target nodes lie in the subtree rooted at uu. A node is allowed to be a descendant of itself.

The lowest common ancestor (LCA) is the common ancestor with maximum depth, i.e. the lowest such node in the tree.

Print the key stored at the LCA of pp and qq.

Input

The first line contains nn.

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

The third line contains pp and qq.

Output

Print one integer: the key of the LCA of pp and qq.

Subtasks

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

Subtask 2 (30 points): 2≤n≤5002\le n\le 500.

Subtask 3 (50 points): 2≤n≤1052\le n\le 10^5.

In all subtasks, −109≤ai,p,q≤109-10^9\le a_i,p,q\le 10^9, all aia_i are distinct, p≠qp\ne q, and both pp and qq occur in the tree.

Examples

Input

9
6 2 8 0 4 7 9 3 5
2 8

Output

6

Explanation

Key 22 lies in the left subtree of root 66, while key 88 lies in its right subtree. No node below 66 can have both targets in its subtree.

Therefore their LCA has key 66.