Divide by 3, Multiply by 2

Interview

Time limit2sMemory limit512 MB

Summary
Given a shuffled copy B of the sequence produced by a divide-by-3, multiply-by-2 game, reconstruct the original order A.
Level

Medium5 of 10

Topics
Graph, DFS, Hash map, Math
Solved
No attempts yet

Problem

The divide-by-3, multiply-by-2 game uses a single integer. Start with an integer xx and apply an operation N−1N-1 times. There are two operations available.

  • Divide by 3: divide xx by 3. xx must be divisible by 3.
  • Multiply by 2: multiply xx by 2.

Recording every number produced while playing the game gives a sequence AA. For example, if x=9x = 9, N=6N = 6, and the operations applied are multiply by 2, multiply by 2, divide by 3, multiply by 2, divide by 3, then A=[9,18,36,12,24,8]A = [9, 18, 36, 12, 24, 8].

Given a sequence BB obtained by shuffling the order of AA, find AA.

Input

The first line contains the size of the sequence N(2≤N≤100)N(2 \le N \le 100). The second line contains the sequence BB. Each element of BB is a positive integer less than or equal to 101810^{18}.

Output

Output the sequence AA 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.

Examples2

  1. Example 1

    Input
    6
    4 8 6 3 12 9
    
    Expected output
    9 3 6 12 4 8
    
  2. Example 2

    Input
    4
    42 28 84 126
    
    Expected output
    126 42 84 28