#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 nn distinct keys a1,a2,…,ana_1,a_2,\ldots,a_n in insertion order.

Given integers low and high with low≤high\text{low}\le\text{high}, compute the sum of the keys of all nodes whose values lie in the closed interval [low,high][\text{low},\text{high}].

Each node contributes exactly once.

Input

The first line contains nn.

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

The third line contains low high.

Output

Print one integer: the sum of all node keys in [low,high][\text{low},\text{high}].

Subtasks

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

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

Subtask 3 (50 points): 1≤n≤2⋅1041\le n\le 2\cdot 10^4.

In all subtasks, 1≤ai≤1051\le a_i\le 10^5, 1≤low≤high≤1051\le\text{low}\le\text{high}\le 10^5, and all aia_i are distinct.

Examples

Input

7
10 5 15 3 7 18 13
7 15

Output

45

Explanation

The tree contains keys 10,5,15,3,7,18,1310,5,15,3,7,18,13. For the closed interval [7,15][7,15], keys 77, 1010, 1313, and 1515 are included; keys 33 and 55 are below the interval, while 1818 is above it.

Therefore the required sum is 7+10+13+15=457+10+13+15=45, so the program prints 45.