def sort_szarkowski(x):
	i=0
	while x%2==0:
		x//=2
		i+=1
	if x==1: #wytrych, gdy mamy do czynienia z potęgami 2
		return (200-i,1)
	return (i, x)


def test():
	linia = input().split()
	n = int(linia[0])
	linia = input()
	dane = [int(i) for i in linia.split()]
	
	dane.sort()
	print(' '.join(str(i) for i in dane)+' ')
	
	dane.sort(key=sort_szarkowski)
	print(' '.join(str(i) for i in dane)+' ')
	
test()