Algorithm


D. Divide by three, multiply by two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n1�−1 operations of the two kinds:

  • divide the number x by 33 (x must be divisible by 33);
  • multiply the number x by 22.

After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.

You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.

Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

It is guaranteed that the answer exists.

Input

The first line of the input contatins an integer number n (2n1002≤�≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,,an�1,�2,…,�� (1ai310181≤��≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.

It is guaranteed that the answer exists.

Examples
input
Copy
6
4 8 6 3 12 9
output
Copy
9 3 6 12 4 8 
input
Copy
4
42 28 84 126
output
Copy
126 42 84 28 
input
Copy
2
1000000000000000000 3000000000000000000
output
Copy
3000000000000000000 1000000000000000000 
Note

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

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 101;
long long const MAX = 3e18 + 500;
int n;
long long a[N], sol[N];
vector<long long> all;
map<long long, int> fr;

void rec(int idx, long long num) {
  sol[idx] = num;

  if(idx == n - 1) {
    for(int i = 0; i < n; ++i) {
      if(i != 0)
        cout << ' ';
      cout << sol[i];
    }
    cout << endl;
    exit(0);
  }

  if(num % 3 == 0 && fr.count(num / 3) && fr[num / 3] > 0) {
    --fr[num / 3];
    rec(idx + 1, num / 3);
    ++fr[num / 3];
  }

  if(num < MAX && fr.count(num * 2) && fr[num * 2] > 0) {
    --fr[num * 2];
    rec(idx + 1, num * 2);
    ++fr[num * 2];
  }
}

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i) {
    cin >> a[i];
    ++fr[a[i]];
    all.push_back(a[i]);
  }

  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());

  for(int i = 0; i < all.size(); ++i) {
    --fr[all[i]];
    rec(0, all[i]);
    ++fr[all[i]];
  }

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
6
4 8 6 3 12 9

Output

x
+
cmd
9 3 6 12 4 8
Advertisements

Demonstration


Codeforces Solution-D. Divide by three, multiply by two-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+