LogiQuest

390. LCA in a BST

EasyBST

The tree is a BST containing p and q. Print their lowest common ancestor's value.

Input format

Line 1: n p q Line 2: the values in level order, -1 for a missing child

Output format

The LCA value.

Example 1

Input

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

Output

6

Example 2

Input

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

Output

2

Constraints

2 ≤ nodes ≤ 10⁴

Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.

Loading the MyLoginn IDE…