#FT0000254. Sabbir và các món quà (Sabbir and gifts)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 gifts, and the -th gift costs . Each gift is a separate item, so gifts with the same price are counted separately.
Each query gives two price bounds . Sabbir wishes to buy all gifts whose prices satisfy , including both bounds. Report their total price. If no gift qualifies, the total is .
Input
- The first line contains .
- The second line contains gift prices .
- The third line contains the query count .
- Each of the final lines contains .
Output
Print lines; for each query, print the sum of the prices of all gifts in the specified range.
Subtasks
-
Subtask 1 (20 points): ; ; ; .
-
Subtask 2 (30 points): ; ; ; .
-
Subtask 3 (50 points): ; ; ; .
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 , the selected prices are , summing to . The interval includes all seven gifts, totalling . The interval contains prices and , summing to . The interval includes only the gift priced .