#C3931. Top K Frequent Elements

    ID: 47413 Type: Default 1000ms 256MiB

Top K Frequent Elements

Top K Frequent Elements

You are given an array of n integers and an integer k. The frequency of an element x is denoted as \( f(x) \). Your task is to find the \( k \) numbers that occur most frequently in the array. In case of a tie (i.e. two numbers have the same frequency), the smaller number is considered to have higher priority. Finally, output the chosen \( k \) numbers in ascending order.

Input Format: The first line contains two integers \( n \) and \( k \). The second line contains \( n \) space-separated integers.

Output Format: Output the \( k \) most frequent numbers in ascending order, separated by spaces.

Constraints: It is guaranteed that \( k \) is at most the number of distinct integers in the array.

inputFormat

The first line of input contains two integers \( n \) and \( k \), representing the number of elements in the array and the number of most frequent elements to output, respectively.

The second line contains \( n \) space-separated integers denoting the array elements.

outputFormat

Output a single line containing \( k \) integers in ascending order, which are the \( k \) most frequent elements. In case of a tie in frequency, the smaller number should be output.

## sample
6 2
1 1 1 2 2 3
1 2

</p>