AmazonOA #250
Lexicographically Maximal Resulting Array
估计困难可在线判题
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.
输入格式
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 the result array, separated by spaces.
样例 1
输入:
5 1
4 3 5 5 3
输出:
5 3
样例 2
输入:
5 2
10 5 9 2 5
输出:
10 5
样例 3
输入:
1 0
3
输出:
3
样例说明
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.