304. Order by How Often
Print the values sorted by how often they appear, most frequent first. Values appearing equally often are printed in increasing order of value, and each value is repeated as many times as it occurs.
2 3 2 4 5 12 2 3 3 3 12 -> 3 3 3 3 2 2 2 12 12 4 5
Three appears four times, two appears three times, twelve twice, and four and five once each with four printed before five.
Count first, then sort the DISTINCT values by count descending and value ascending, then expand. The tie rule matters: without it the output order for equally frequent values depends on the language's sort and stops being reproducible.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n integers.
Output
Print the n values reordered as described, separated by single spaces.