AmazonOA #250
Lexicographically Maximal Resulting Array
Estimated HardJudge-ready
Repeatedly choose one of the following: discard the current leftmost item; or append the current leftmost weight to the result and remove it together with the k items after it. If fewer than k items remain after it, they are all removed, and the current item can still be taken. The resulting weights must be non-increasing, and among all feasible results take the lexicographically largest: at the first difference the larger one wins, and when one sequence is a prefix of the other, the longer one wins.
Input
The first line contains n k; the second line contains the weights. The original text had no numeric bounds; this site uses 1≤n≤200000, 0≤k≤10^9, 1≤weight≤10^9.
Output
Output the result array, separated by spaces.
Example 1
Input:
5 1
4 3 5 5 3
Output:
5 3
Example 2
Input:
5 2
10 5 9 2 5
Output:
10 5
Example 3
Input:
1 0
3
Output:
3
Example notes
The answers are 5 3, 10 5 and 3. In the first example any number of prefix elements may be discarded; you are not limited to looking at the first k+1 items.
Editorial
Loading solution…