A deep-space listening array records a sequence of n signal-strength readings in the exact order they were received. Ground control wants to retain exactly k of these readings for the mission log, discarding the rest, but the retained readings must stay in their original chronological order.
Multiple retention choices can tie for the largest possible total. To make the result unique, among all choices of k positions achieving the maximum sum, output the one whose set of positions is lexicographically smallest (equivalently: build the kept set by always preferring to keep the earliest-occurring reading whenever a tie in value forces a choice).
Output the retained readings' values, separated by single spaces, in their original left-to-right order.
Line 1: two integers n and k. Line 2: n integers, the readings nums[0..n-1], in the order received.
A single line with exactly k integers separated by single spaces: the retained readings' values, in their original order.
Example 1
Input
4 2 2 1 3 3
Expected
3 3
Explanation
The two largest values are the two 3's at positions 2 and 3. Retaining them in their original order gives "3 3", the maximum possible sum (6).
Example 2
Input
4 3 -1 -2 3 4
Expected
-1 3 4
Explanation
The three largest values are -1, 3, and 4 (dropping -2, the smallest). Kept in original order they read "-1 3 4", summing to 6, the maximum possible.
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 →