LogiQuest

389. K-th Smallest in a BST

MediumBSTInorder

The tree is a BST. Print its k-th smallest value.

Input format

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

Output format

The k-th smallest value.

Example 1

Input

4 1
3 1 4 -1 2

Output

1

Example 2

Input

8 3
5 3 6 2 4 -1 -1 1

Output

3

Constraints

1 ≤ k ≤ 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…