# 除以3，乘以2（STL+排序）- Codeforces 997D

（友情提示：不喜欢看英文的童鞋，可以先翻到下面看翻译

Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, 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 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 (2≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅10^18) — 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

```6
4 8 6 3 12 9```

Output

`9 3 6 12 4 8 `

Input

```4
42 28 84 126```

Output

`126 42 84 28 `

Input

```2
1000000000000000000 3000000000000000000```

Output

`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.

（以前太懒了这次帮忙画个图？(｀・ω・´)）

```#include<bits/stdc++.h>
using namespace std;
typedef long long ll;//3*10^18已经爆int，需要longlong

map<ll, bool>m; //map记录是否存在某个数
stack<ll>s;
queue<ll>q;

int main()
{
int n;
ll a[105];
//输入
scanf("%d", &n);
for (int i = 1; i <= n; i++) cin >> a[i], m[a[i]] = true;
//用栈储存在a[1]前面的
s.push(a[1]);
ll k = s.top();
do
{
if (m[k * 3]) s.push(k * 3), k *= 3;
else if (k % 2 == 0 && m[k / 2]) s.push(k / 2), k /= 2;
else break;
} while (true);
//用队列储存在a[1]后面的
k = a[1];
do
{
if (k % 3 == 0 && m[k / 3]) q.push(k / 3), k /= 3;
else if (m[k * 2]) q.push(k * 2), k *= 2;
else break;
} while (true);
//输出
while (s.size())
{
cout << s.top() << ' ';
s.pop();
}
while (q.size())
{
cout << q.front() << ' ';
q.pop();
}

return 0;
}```

```#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int count3(LL x) {
int ret = 0;
while (x % 3 == 0) {
ret++;
x /= 3;
}
return ret;
}

int n;
vector<pair<int, LL> > v;

int main() {
cin >> n;
v.resize(n);//设置vector大小为n

for (int i = 0; i < n; i++) {
cin >> v[i].second;
v[i].first = -count3(v[i].second); //这里要count3更大的数排前面，所以取负
}

sort(v.begin(), v.end());

for (int i = 0; i < n; i++)
printf("%lld%c", v[i].second, " \n"[i + 1 == n]);//学下这波语法

return 0;
}```

（都看到这里了，给我们的小伙伴一个赞呗！

0 条评论

• ### 独角兽与数列（置换群循环）- HDU 4985

群论是法国数学家伽罗瓦（Galois）的发明。伽罗瓦是一个极具传奇性的人物，年仅21岁就英年早逝于一场近乎自杀的决斗中。他用该理论，具体来说是伽罗瓦群，解决了五...

• ### 搜索专题4 | 旋转棋盘 POJ - 2286

The rotation game uses a # shaped board, which can hold 24 pieces of square bloc...

• ### POJ2318 TOYS 判断点与直线位置关系 【计算几何】

Calculate the number of toys that land in each bin of a partitioned toy box.

• ### Web前端学习（打卡机）

在补齐了Java后端的短板之后，我准备补补前端的课。这篇文章可以作为我前端学习的过程记录（打开机）。

• ### POJ-1953 World Cup Noise（线性动规）

World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissio...

• ### Codeforces 1291 Round #616 (Div. 2) C. Mind Control（超级详细）

You and your n−1 friends have found an array of integers a1,a2,…,an. You have de...

• ### 不知道IDEA的这些个性化配置，你怎么能说自己会用它

这里默认提供了三套主题：IntelliJ，Darcula，Windows。这里可以根据自己的喜 好进行选择。

• ### ZOJ 3623 Battle Ships

Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a ne...

• ### CodeForces - 140A New Year Table （几何题）当时没想出来-----补题

A. New Year Table time limit per test2 seconds memory limit per test256 megaby...

• ### POJ1611 (并查集)

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetio...