Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/saralash-chastota-boyicha

Sort by frequency

Medium 1000 ms 128 MB 58%
Share

$n$ integers are given. Arrange them so that more frequent values come first; among values occurring the same number of times, the smaller one comes first. Each value is printed consecutively as many times as it occurs.

Values gathered into groups: a bigger group comes first, among equal groups the smaller value comes first

Input

The first line contains a positive integer $n$. The second line contains $n$ integers $a_{i}$ separated by spaces.

Output

One line with $n$ integers separated by spaces.

Note

$1 \le n \le 2 * 10^{5}$
$-10^{9} \le a_{i} \le 10^{9}$

Examples

Input #1
9
4 6 2 6 4 4 2 9 6
Output #1
4 4 4 6 6 6 2 2 9
Input #2
3
5 -1 5
Output #2
5 5 -1

Sign in to submit a solution

Signing up is free and takes a minute.