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

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

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

You are given a sequence of length nn — 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 nn (2≤n≤1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print nn 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=9x=9.

题目大意:给定的数组按照以下要求排序:后一个数是前一个数的三分之一,或者是前一个数的二倍。

思路:如果a[v]是a[u]的三分之一或者二倍,就给u->v加一条有向边,然后跑一遍拓扑排序就行了,注意得到的拓扑数组是下标

代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
typedef long long ll;
using namespace std;
const int maxn = 200000 + 100;
int G[100 +10][100 + 10];
int c[maxn];
ll topo[maxn], t;
int n; bool dfs(int u){
c[u] = -1;
for(int i = 0; i < n; i++)if(G[u][i]){
if(c[i] < 0)return false;
else if(!c[i] && !dfs(i))return false;
}
c[u] = 1;topo[--t] = u;
return true;
}
bool toposort(){
t = n;
memset(c, 0, sizeof(c));
for(int i = 0; i < n; i++)if(!c[i]){
if(!dfs(i)) return false;
}
return true;
}
int main(){
scanf("%d", &n);
ll a[maxn];
memset(G, 0, sizeof(G));
for(int i = 0; i < n; i++){
scanf("%lld", &a[i]); }
sort(a, a+n);
for(int i = 0; i < n ; i++){
int it = lower_bound(a, a+n, a[i]/3)-a;
int itt = lower_bound(a, a+n, a[i]*2)-a;
if(a[i]%3==0&&a[it]==a[i]/3)G[i][it] = 1;
if(a[i]*2==a[itt])G[i][itt] = 1; } toposort();
for(int i = 0; i < n; i++)printf("%lld ", a[topo[i]]);
}

Codeforces 977D Divide by three, multiply by two(拓扑排序)的更多相关文章

  1. Codeforces Global Round 8 E. Ski Accidents(拓扑排序)

    题目链接:https://codeforces.com/contest/1368/problem/E 题意 给出一个 $n$ 点 $m$ 边的有向图,每条边由编号较小的点通向编号较大的点,每个点的出度 ...

  2. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  3. codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】

    Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. 【CodeForces 129 B】Students and Shoelaces(拓扑排序)

    Anna and Maria are in charge of the math club for junior students. When the club gathers together, t ...

  5. Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]

    题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的value等与这条路径上出现次数最多的字母的数目,求该图的最大value 比赛时,用dfs超时,看官方题解用的dp和拓扑排序,a--z用0-2 ...

  6. Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two

    传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...

  7. codeforces 792C. Divide by Three

    题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...

  8. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  9. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

随机推荐

  1. MySQL数据库用户、角色、授权

    权限包括  insert   delete   update   select   all privileges 登录MySQL > mysql -uroot -p Enter password ...

  2. APP开通支付宝支付 转账功能

    注意:支付宝单笔转账到账户功能的准入条件 首先要在商家中心注册,从商家中心可以跳转到开放平台. 登陆蚂蚁金服开放平台 1 开发中心->网页&移动应用 2 根据需求选择应用类型创建应用 3 ...

  3. python切片(获取一个子列表(数组))

    切片: 切片指从现有列表中,获取一个子列表 返回一个新列表,不影响原列表. 下标以 0 开始: list = ['红','绿','蓝','白','黑','黄','青']# 下标 0 1 2 3 4 5 ...

  4. 【xinsir】webpack实践

    webpack现在是前端必会的技能了,也是在工作中必定用到的.所以,如果我们现在还不会webpack,那么在将来面试中肯定会被扣分的. webpack中文官网:https://www.webpackj ...

  5. Spring学习记录6——ThreadLocal简介

    Spring通过各种模板类降低了开发者使用各种数据持久化技术的难度.这些模板类是线程安全的,所以 多个DAO可以复用同一个模板实例而不会发生冲突.在使用模板类访问底层数据时,模板类需要绑定数据连接或者 ...

  6. 三个css属性 设置文字竖直居中

    display: flex; justify-content:center; align-items:Center;

  7. DOCKER学习_017:Docker-Compose介绍

    dockers三驾马车 Docker Machine Docker Swarm Docker Compose 一 Docker Compose介绍 Docker Compose是一个定义和运行多容器应 ...

  8. Ndarry对象

    创建一个 ndarray 只需调用 NumPy 的 array 函数即可: numpy.array(object, dtype = None, copy = True, order = None, s ...

  9. Nginx配置不同端口号映射二级域名

    upstream xx{ #ip_hash; server 127.0.0.1:1008; } server { listen 80; server_name xx.xxx.com; location ...

  10. 【WPF学习】第十章 WPF布局示例

    前几章用了相当大的篇幅研究有关WPF布局容器的复杂内容.在掌握了这些基础知识后,就可以研究几个完整的布局示例.通过研究完整的布局示例,可更好的理解各种WPF布局概念在实际窗口中的工作方式. 一.列设置 ...