LogiQuest

392. Range Sum of a BST

EasyBSTDFS

Print the sum of node values within [low, high] in the BST.

Input format

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

Output format

The sum.

Example 1

Input

7 7 15
10 5 15 3 7 -1 18

Output

32

Example 2

Input

11 6 10
10 5 15 3 7 13 18 1 -1 6

Output

23

Constraints

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