#C6664. Top M Frequent Species
Top M Frequent Species
Top M Frequent Species
You are given a sequence of integer observations representing species A-ids. Your task is to determine the M species that are observed most frequently. In case two or more species have the same frequency, the species with the lower A-id should be preferred.
Formally, let \( f(x) \) denote the frequency of species \( x \) in the observations. You need to select the \( M \) species such that their frequencies are among the top \( M \) largest. If \( f(a) = f(b) \) for two species \( a \) and \( b \), and \( a < b \), then \( a \) should be chosen before \( b \).
inputFormat
The input is read from standard input (stdin) and has the following format:
- An integer \( N \) representing the number of observations.
- A line with \( N \) space-separated integers, each representing an A-id observation.
- An integer \( M \) which specifies how many top frequent species to output.
It is guaranteed that \( M \) does not exceed the number of distinct species.
outputFormat
Output a single line to standard output (stdout) that contains \( M \) integers separated by a single space. These integers are the A-ids of the species with the highest frequencies, following the tie-breaking rule (lower A-id first). The order should correspond to the order in which they are selected by the specified criteria.
## sample7
5 3 5 2 6 3 3
2
3 5