A tournament awards a prize pool equal to the sum of the top k player scores. Given n scores (which may be negative) and an integer k, print the sum of the k largest scores. If several scores are tied at the cutoff, they all count by position, so exactly k scores contribute.
Line 1: two integers n and k.
Line 2: n space-separated integers, the scores.
A single integer: the sum of the k largest scores.
Example 1
Input
5 3 10 40 20 50 30
Expected
120
Explanation
The three largest scores are 50, 40, and 30; their sum is 120.
Example 2
Input
4 4 5 5 5 5
Expected
20
Explanation
k equals n, so the sum is over all four scores: 5+5+5+5 = 20.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →