Skip to content

ORIGINAL CODING QUESTION

Shortlist score total

A hiring panel shortlists the k highest-scoring candidates. Scores can repeat and can be negative; repeated scores are different candidates. Return the total score of the shortlist.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

n k, then n candidate scores.

Constraints

1 ≤ k ≤ n ≤ 200; -1000 ≤ score ≤ 1000.

Example 1

Input

5 2 3 9 1 7 5

Output

16

The two best scores are 9 and 7.

Example 2

Input

4 3 -2 -8 -1 -5

Output

-8

The best three are -1, -2 and -5, totalling -8.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Sorting everything works, but only k scores matter.

Hint 2

A min-heap of size k keeps the current shortlist.

Hint 3

Replace the heap minimum whenever a better score arrives, then add up the heap.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →