PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000254. Sabbir và các món quà (Sabbir and gifts)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sabbir và các món quà (Sabbir and gifts)

Sabbir and gifts

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

A shop has nn gifts, and the ii-th gift costs pip_i. Each gift is a separate item, so gifts with the same price are counted separately.

Each query gives two price bounds a,ba,b. Sabbir wishes to buy all gifts whose prices satisfy a≤pi≤ba\le p_i\le b, including both bounds. Report their total price. If no gift qualifies, the total is 00.

Input

  • The first line contains nn.
  • The second line contains nn gift prices p1,…,pnp_1,\ldots,p_n.
  • The third line contains the query count qq.
  • Each of the final qq lines contains a,ba,b.

Output

Print qq lines; for each query, print the sum of the prices of all gifts in the specified range.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤601\le q\le 60; 1≤pi≤1091\le p_i\le 10^9; 1≤a,b≤1091\le a,b\le 10^9.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤pi≤1091\le p_i\le 10^9; 1≤a,b≤1091\le a,b\le 10^9.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤pi≤1091\le p_i\le 10^9; 1≤a,b≤1091\le a,b\le 10^9.

Examples

Input

7
1 3 2 1 5 2 2
4
1 2
1 5
3 5
4 5

Output

8
16
8
5

Explanation

For [1,2][1,2], the selected prices are 1,2,1,2,21,2,1,2,2, summing to 88. The interval [1,5][1,5] includes all seven gifts, totalling 1616. The interval [3,5][3,5] contains prices 33 and 55, summing to 88. The interval [4,5][4,5] includes only the gift priced 55.