#C3243. Frequent Response Finder

    ID: 46649 Type: Default 1000ms 256MiB

Frequent Response Finder

Frequent Response Finder

You are given a list of n integer responses. Your task is to find the top m responses that appear most frequently. The responses should be sorted first by their frequency in descending order, and in case of a tie (i.e. two responses have the same frequency), the smaller integer should come first.

More formally, let \( f(x) \) be the frequency of response \( x \) in the input list. You need to output the list \( [x_1, x_2, \dots] \) of the top \( m \) responses such that

[ \text{if } f(x_i) > f(x_j) \text{ or } (f(x_i)=f(x_j) \text{ and } x_i < x_j), \text{ then } x_i \text{ comes before } x_j, ]

for all valid indices. Print the result as a sequence of numbers separated by a single space.

inputFormat

The input is given via standard input (stdin) and has the following format:

  1. An integer n representing the number of responses.
  2. A line containing n space-separated integers, each representing a response.
  3. An integer m on a new line, specifying the number of top frequent responses to output.

You can assume that all input values are valid.

outputFormat

Output the top m responses in one line, separated by a single space. The order must follow the frequency rules as described above.

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