Divide by 3, Multiply by 2
InterviewTime limit2sMemory limit512 MB
Given a shuffled copy B of the sequence produced by a divide-by-3, multiply-by-2 game, reconstruct the original order A.
Problem
The divide-by-3, multiply-by-2 game uses a single integer. Start with an integer and apply an operation times. There are two operations available.
- Divide by 3: divide by 3. must be divisible by 3.
- Multiply by 2: multiply by 2.
Recording every number produced while playing the game gives a sequence . For example, if , , and the operations applied are multiply by 2, multiply by 2, divide by 3, multiply by 2, divide by 3, then .
Given a sequence obtained by shuffling the order of , find .
Input
The first line contains the size of the sequence . The second line contains the sequence . Each element of is a positive integer less than or equal to .
Output
Output the sequence produced by the divide-by-3, multiply-by-2 game. The input is given only when a solution always exists; if there are multiple valid answers, output any one of them.