cswork / OAOpen practice workspace

MicrosoftOA #59

Retain Top K Values

Estimated EasyJudge-ready

Topics (AI-labelled)In-place ArraysStacks & Heaps

Select the K largest elements of the array and output them in their original relative order. When equal values straddle the cutoff, prefer the ones that appear earliest, so that exactly K are chosen. These rules are stated in full in the source problem's two example explanations.

This site uses a standard input/output protocol.

Input

The first line contains N K, and the second line contains N integers. The source only gives 0≤K≤N; this site uses 1≤N≤100000 and elements in −10⁹..10⁹.

Output

Output exactly K values in their original relative order; output an empty line when K is 0.

Example 1

Input:

5 3
5 1 3 5 2

Output:

5 3 5

Example 2

Input:

4 2
4 4 4 2

Output:

4 4

Example 3

Input:

2 0
1 2

Output:

Example notes

Example 1: choose indices 0, 2, 3 and output them in original order as 5 3 5. Example 2: take the earliest two of the three 4s and output 4 4. Example 3: K is 0, so output an empty line.

Editorial

Loading solution…