The chef likes to play with numbers. He takes some integer number x, writes it down on his iPad, and then performs with it n−1 operations of the two kinds:
- divide the number x by 3 (x must be divisible by 3);
- multiply the number x by 2.
After each iteration, Chef writes down the result on his iPad and replaces x with the result. So there will be n numbers on the iPad after all.
You are given a sequence of length n — the numbers that Chef wrote down. This sequence is given in the order of the sequence can mismatch the order of the numbers written on the iPad.
Your problem is to rearrange elements of this sequence in such a way that it can match a possible Chef's game in the order of the numbers written on the board. I.e. each next number will be exactly two times the previous number or exactly one-third of the previous number.
I can give a guarantee that the answer exists.

-----Input:-----
- The first line of the input contains an integer number N i.e the number of the elements in the sequence. 
- The second line of the input contains n integer numbers a1,a2,…, an i.e rearranged (reordered) sequence that Chef can write down on the iPad.

-----Output:-----
Print N integer numbers — rearranged (reordered) input sequence that can be the sequence that Chef could write down on the iPad.
It is guaranteed that the answer exists

-----Constraints-----
- $2 \leq N \leq 100$
- $1 \leq A[i] \leq  3* 10^{18} $

-----Sample Input:-----
6
4 8 6 3 12 9

-----Sample Output:-----
9 3 6 12 4 8 

-----EXPLANATION:-----
In the first example, the given sequence can be rearranged in the following way: [9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9.

Difficulty: interview

Starter code:

Solutions:
class Node:
    def __init__(self,x):
        self.x=x
        self.next=None
        self.prev=None
        self.flag=True

for t in range(1):
    n=int(input())
    arr=list(map(int,input().split()))
    for i in range(n):
        arr[i]=Node(arr[i])
    for i in arr:
        d=[i.x%3==0,i.x,i.x//3,i.x*2]
        if d[0]:
            for j in arr:
                if j.x==d[2]:
                    i.next=j
                    j.prev=i
                    break
            else:
                for j in arr:
                    if j.x == d[3]:
                        i.next = j
                        j.prev = i
                        break
        else:
            for j in arr:
                if j.x==d[3]:
                    i.next=j
                    j.prev=i
                    break
    f,l=None,None
    for i in arr:
        if i.prev==None:
            f=i
        elif i.next==None:
            l=i
    while f!=l and l!=None:
        print(f.x,end=" ")
        f=f.next
    print(f.x)

class Node:
    def __init__(self,x):
        self.x=x
        self.next=None
        self.prev=None
        self.flag=True

for t in range(1):
    n=int(input())
    arr=list(map(int,input().split()))
    for i in range(n):
        arr[i]=Node(arr[i])
    for i in arr:
        d=[i.x%3==0,i.x,i.x//3,i.x*2]
        if d[0]:
            for j in arr:
                if j.x==d[2]:
                    i.next=j
                    j.prev=i
                    break
            else:
                for j in arr:
                    if j.x == d[3]:
                        i.next = j
                        j.prev = i
                        break
        else:
            for j in arr:
                if j.x==d[3]:
                    i.next=j
                    j.prev=i
                    break
    for i in arr:
        if i.prev==None:
            f=i
        elif i.next==None:
            l=i
    while f!=l:
        print(f.x,end=" ")
        f=f.next
    print(f.x)

# cook your dish here
    n = int(input())
     
    l = list(map(int, input().split()))
     
    def conv(n):
    	i = 0
    	while n % 2 == 0:
    		i += 1
    		n /= 2
    	return (-n, i)
     
    l.sort(key=conv)
    print(*l)

def abc(v):
    for j in d[v]:
        k.append(j)
    if d[v]:
        abc(d[v][-1])
n=int(input())
l=list(map(int,input().split()))
l.sort()
d={}
k=[]
for i in l:
    a=[]
    b=[]
    req=2*i
    for j in l:
        if j==req:
            a.append(j)
            req = 2*j
    if not i%3:
        req=i//3
    else:
        req=-1
    for j in range(n-1,-1,-1):
        if req==-1:
            break
        if l[j]==req:
            b.append(l[j])
            if not l[j]%3:
                req=l[j]//3
            else:
                req=-1
    if a and b:
        d[i]=[]
    elif a:
        d[i]=a
    elif b:
        d[i]=b
    else:
        d[i]=a
for i in l:
    k=[i]
    try:
        abc(i)
    except:
        pass
    #print(i,k)
    if len(k)==n:
        print(*k)
        break
    k=[]
    
    
        
    
    
    
    
        
    


def abc(v):
    for j in d[v]:
        k.append(j)
    if d[v]:
        abc(d[v][-1])
n=int(input())
l=list(map(int,input().split()))
l.sort()
d={}
k=[]
for i in l:
    a=[]
    b=[]
    req=2*i
    for j in l:
        if j==req:
            a.append(j)
            req = 2*j
    if not i%3:
        req=i//3
    else:
        req=-1
    for j in range(n-1,-1,-1):
        if req==-1:
            break
        if l[j]==req:
            b.append(l[j])
            if not l[j]%3:
                req=l[j]//3
            else:
                req=-1
    if a and b:
        d[i]=[]
    elif a:
        d[i]=a
    elif b:
        d[i]=b
    else:
        d[i]=[]
for i in l:
    k=[i]
    abc(i)
    #print(i,k)
    if len(k)==n:
        print(*k)
        break
    k=[]
    
    
        
    
    
    
    
        
    


def abc(v):
    for j in d[v]:
        k.append(j)
    if d[v]:
        abc(d[v][-1])
n=int(input())
l=list(map(int,input().split()))
l.sort()
d={}
k=[]
for i in l:
    a=[]
    b=[]
    req=2*i
    for j in l:
        if j==req:
            a.append(j)
            req = 2*j
    if not i%3:
        req=i//3
    else:
        req=-1
    for j in range(n-1,-1,-1):
        if req==-1:
            break
        if l[j]==req:
            b.append(l[j])
            if not l[j]%3:
                req=l[j]//3
            else:
                req=-1
    if a and b:
        d[i]=[]
    elif a:
        d[i]=a
    elif b:
        d[i]=b
    else:
        d[i]=a
for i in l:
    k=[i]
    abc(i)
    #print(i,k)
    if len(k)==n:
        print(*k)
        break
    k=[]