#C802. Taco's Candy Chain
Taco's Candy Chain
Taco's Candy Chain
You are given n distinct candy pieces, each with a sweetness level. Your task is to arrange these candy pieces in a chain such that:
- The total sweetness of the chain, \(S = s_1 + s_2 + \cdots + s_n\), is even.
- For every three consecutive candy pieces in the chain, the sum of their sweetness levels is not divisible by 3. In other words, for every \(i\) with \(1 \le i \le n-2\), \[ s_i + s_{i+1} + s_{i+2} \not\equiv 0 \pmod{3}. \]
If such an arrangement exists, print any one valid ordering; otherwise, print -1
.
Note: All candy pieces have distinct sweetness levels.
inputFormat
Input is read from standard input (stdin). The first line contains a single integer n denoting the number of candy pieces. The second line contains n space-separated integers representing the distinct sweetness levels of each candy piece.
outputFormat
If a valid arrangement exists, output a single line with n space-separated integers corresponding to a valid candy chain. If no valid arrangement exists, output -1.## sample
4
3 1 7 5
3 1 7 5