#C9149. Top K Scores Sum
Top K Scores Sum
Top K Scores Sum
You are given a list of scores and several queries. For each query you are asked to output the sum of the top k scores in the list.
To accomplish this, you should first process the list by sorting it in non-increasing order and then precompute the prefix sums. When answering a query, the sum of the top k scores can be directly retrieved from the prefix sums.
The prefix sums array P is defined as:
\( P[i] = s_0 + s_1 + \cdots + s_i \)
where \( s_0, s_1, \ldots, s_{n-1} \) is the sorted array in descending order and indices start from 0. For a query with integer k, output \( P[k-1] \).
inputFormat
The first line contains an integer n (\(1 \leq n \leq 2 \times 10^5\)) representing the number of scores.
The second line contains \(n\) space-separated integers representing the scores.
The third line contains an integer q representing the number of queries.
Each of the next q lines contains an integer k (\(1 \leq k \leq n\)), representing a query to compute the sum of the top k scores.
outputFormat
For each query, output a single line with the sum of the top k scores.
## sample6
4 1 7 3 2 6
4
2
3
5
6
13
17
22
23
</p>