Problem list
Log in
#coci00106 · COCI 2008/2009 Contest 6

Cuskija

Gold V 1.0 s 32 MB

Rearrange the given array of integers so that the sum of two adjacent elements is never divisible by three.

Input

The first line contains an integer $N$ ($1 \le N \le 10000$), the number of elements in the array.

The second line contains the elements of the array separated by single spaces. The elements will be positive integers less than $1000000$.

Output

If any valid rearrangement exists, output it on a single line. Otherwise, output "impossible".

Example 1

Input
3
1 2 3
Output
2 3 1

Example 2

Input
5
4 6 3 9 8
Output
3 4 6 8 9

Example 3

Input
6
3 7 6 4 2 8
Output
3 7 4 6 2 8

Example 4

Input
3
3 12 9
Output
impossible
Source
COCI 2008/2009 Contest 6
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 1 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code