#C740. K Most Frequent Elements

    ID: 51267 Type: Default 1000ms 256MiB

K Most Frequent Elements

K Most Frequent Elements

Given a list of n integers and an integer k, your task is to find the k most frequent elements in the list and output them in ascending order.

You should count the frequency of each element and then select the k elements with the highest frequency. In case of a tie (i.e. two numbers have the same frequency), the smaller number is considered first. After selecting the top k frequent elements, sort them in ascending order before outputting.

More formally, let \( f(x) \) denote the frequency of an element \( x \) in the list, and let the sorting order be defined as:

\( (x, f(x)) \) is ranked higher than \( (y, f(y)) \) if \( f(x) > f(y) \) or \( f(x) = f(y) \) and \( x < y \).

The final output should list the chosen k elements in strictly ascending order.

inputFormat

The first line contains two integers n and k separated by a space, where n is the number of elements in the list and k is the number of elements to output.

The second line contains n integers separated by spaces.

outputFormat

Output the k most frequent elements in ascending order, separated by a space.

## sample
8 2
1 1 1 2 2 3 3 3
1 3

</p>