#K58867. Sum of Elements between k-th Smallest Elements
Sum of Elements between k-th Smallest Elements
Sum of Elements between k-th Smallest Elements
You are given an array of n distinct integers and q queries. For each query, you are given two integers k1 and k2. Your task is to compute the sum of the elements between the k1-th smallest and the k2-th smallest elements in the array (inclusive). In other words, if the sorted array is \(a_{(1)}, a_{(2)}, \dots, a_{(n)}\), then for each query, you need to calculate
[ S = \sum_{i=k_1}^{k_2} a_{(i)} ]
Print each result on a new line.
inputFormat
The input is given from stdin and has the following format:
- The first line contains a positive integer n, which is the number of elements in the array.
- The second line contains n distinct integers separated by spaces.
- The third line contains a positive integer q, the number of queries.
- The next q lines each contain two integers k1 and k2 separated by a space.
outputFormat
For each query, output the sum of elements between the k1-th smallest and the k2-th smallest elements in the sorted array. Each answer should be printed on a new line.
## sample6
3 1 5 2 4 6
2
2 5
1 3
14
6
</p>