#P6470. Reordering Sequence to Avoid Sums Divisible by 3

    ID: 19684 Type: Default 1000ms 256MiB

Reordering Sequence to Avoid Sums Divisible by 3

Reordering Sequence to Avoid Sums Divisible by 3

Given a sequence of n integers, reorder the sequence such that for every adjacent pair of numbers in the new sequence, the sum is not divisible by 3. Formally, given a sequence \(a_1,a_2,\dots,a_n\), find a permutation \(b_1,b_2,\dots,b_n\) such that for every \(1 \le i < n\), \[ b_i + b_{i+1} \not\equiv 0 \pmod{3}. \] If there is no valid reordering, output -1.

inputFormat

The first line contains an integer \(n\) (the length of the sequence). The second line contains \(n\) integers, representing the sequence \(a_1, a_2, ..., a_n\), separated by spaces.

outputFormat

If there exists a valid reordering, output the reordered sequence in one line, with elements separated by a single space. Otherwise, output -1.

sample

3
1 0 2
1 0 2

</p>