Find K Smallest Distinct ElementsMedium

Find K Smallest Distinct Elements

MediumPractice library · +20 XPheapspriority queuesdistinct elements

Given a list of N integers, find the K smallest distinct elements. If there are fewer than K distinct elements, output all distinct elements in increasing order. The output should be sorted in increasing order. Use a tie-breaking rule such that if multiple elements could be the K-th smallest, any one of them is acceptable.

Input

The first line contains two integers N and K, separated by a space. The second line contains N integers, separated by spaces.

Output

Print K (or fewer, if not enough distinct elements exist) integers in increasing order, separated by spaces.

Constraints

1 <= N <= 200000, 1 <= K <= N. Each integer is between -10^9 and 10^9.

Time limit 2 s (native code; slower languages get more)Memory 256 MB15 hidden tests

Example 1

Input
5 3
10 5 20 5 10
Output
5 10 20

The distinct elements are 10, 5, 20. The 3 smallest are 5, 10, 20.

Example 2

Input
7 2
1 1 1 1 1 1 1
Output
1

There is only 1 distinct element (1). Since K=2, we output all distinct elements: 1.

Hints