#BST0000021. Tổng giá trị trong đoạn của BST (Range Sum of BST)
Tổng giá trị trong đoạn của BST (Range Sum of BST)
Range Sum of BST
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from the distinct keys in insertion order.
Given integers low and high with , compute the sum of the keys of all nodes whose values lie in the closed interval .
Each node contributes exactly once.
Input
The first line contains .
The second line contains the distinct keys .
The third line contains low high.
Output
Print one integer: the sum of all node keys in .
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, , , and all are distinct.
Examples
Input
7
10 5 15 3 7 18 13
7 15
Output
45
Explanation
The tree contains keys . For the closed interval , keys , , , and are included; keys and are below the interval, while is above it.
Therefore the required sum is , so the program prints 45.