#C6903. Frequency Sort

    ID: 50715 Type: Default 1000ms 256MiB

Frequency Sort

Frequency Sort

The task is to sort an array of integers according to the frequency of each number. Numbers that occur less frequently should come first. If two numbers have the same frequency, then the smaller number should come first.

More formally, for an integer array \(A\), let \(f(x)\) be the frequency of the integer \(x\) in \(A\). The sorting order is defined such that for any two integers \(a\) and \(b\), \(a\) comes before \(b\) if either:

  • \(f(a) < f(b)\), or
  • \(f(a) = f(b)\) and \(a < b\).

Your program should read an array from the standard input and print the sorted array to the standard output. The input will first contain an integer indicating the number of elements in the array, followed by that many integers separated by spaces.

inputFormat

The first line contains an integer \(n\), representing the number of integers in the array. The second line contains \(n\) space-separated integers.

outputFormat

Output the sorted array as a sequence of space-separated integers on a single line. The ordering should follow the frequency first and value order second criteria.

## sample
6
4 5 6 5 4 3
3 6 4 4 5 5