#154Easy
Kuriyama's stones
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
Kuriyama has stones; stone costs . She asks questions of two types:
1 l r— the sum .2 l r— the sum of the costs of stones through if the stones are sorted by cost in increasing order.
Answer every question.
Input format
The first line contains (). The second line contains integers (). The third line contains (). The next lines contain , , (, ).
Output format
For every question print one number on a separate line.
Examples
Input
6 6 4 2 7 2 7 3 2 3 6 1 3 4 1 1 6
Output
24 9 28
Input
4 5 5 2 3 10 1 2 4 2 1 4 1 1 1 2 1 4 2 1 2 1 1 1 1 3 3 1 1 3 1 4 4 1 2 2
Output
10 15 5 15 5 5 2 12 3 5
Note
Build two prefix sum arrays: one for the original array and one for a sorted copy. Every question is in the right array. Sorting again for every question is — too slow.
The sum reaches — a 64-bit type is needed. In the first example the sorted array is , and the first question is .
Source: Codeforces, problem 433B.
