题干:

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−1 operations 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 nninteger 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

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

题目大意:

给定n个数,找到他的一个重新排列,使得第一个数开始通过除3或乘2,可以得到这个序列。(n<=100. a[i]<=3e18)

解题报告:

设cnt3是一个数中3的因子个数。

因为只有/3的操作,所以排完的整个数组中的数cnt3一定是单调不递增的,

而相同的cnt3中的数,关系一定是*2的关系,那么一定是单调递增的排序。

所以先按cnt3降序排序,cnt3相同的升序排序,直接输出即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n;
struct Node {ll a,b;bool operator < (const Node & o) const {if(b != o.b) return b > o.b;else return a < o.a;}
} R[MAX];
int main()
{cin>>n;for(int i = 1; i<=n; i++) {scanf("%lld",&R[i].a);ll x = R[i].a;while(x%3 == 0) x/=3,R[i].b++;}sort(R+1,R+n+1);for(int i = 1; i<=n; i++) printf("%lld ",R[i].a);return 0 ;
}

【Codeforces - 977D】Divide by three, multiply by two(思维构造)相关推荐

  1. 【CodeForces - 260D】Black and White Tree (思维构造,猜结论,细节,构造一棵树)

    题干: The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-dire ...

  2. Divide by three, multiply by two CodeForces - 977D (思维排序)

    Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, a ...

  3. Codeforces 977D: Divide by three, multiply by two(暴力)

    题意 有nnn个无序的数,对这些数进行排列,要求ai=3×ai+1a_i=3\times a_{i+1}ai​=3×ai+1​或2×ai=ai+12\times a_i=a_{i+1}2×ai​=ai ...

  4. Divide by three, multiply by two(dfs)

    Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, a ...

  5. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  6. codeforces 465 C. No to Palindromes!(暴力+思维)

    题目链接:http://codeforces.com/contest/465/problem/C 题意:给出一个不存在2个或以上回文子串的字符串,全是由小写字母组成而且字母下表小于p,问刚好比这个字符 ...

  7. codeforces 872 D. Something with XOR Queries(思维)

    题目链接:http://codeforces.com/contest/872/problem/D 题意:给你一个排列p和对应的位置b也就是说p[b[i]]=i,然后给你最多询问2*n次找出所有的p排列 ...

  8. Codeforces Gym101257F:Islands II(求割点+思维)

    http://codeforces.com/gym/101257/problem/F 题意:给出一个n*m的地图,上面相同数字的代表一个国家,问对于每个国家有多少个国家在它内部(即被包围).例如第一个 ...

  9. CodeForces - 1504C Balance the Bits(思维+构造)

    题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...

最新文章

  1. php mssql image,php5连接mssql2005数据库表中的image字段图片显示
  2. 二张图白话广告RTA技术
  3. python 中的理解x[:]
  4. 前端之JQuery(二)
  5. C语言的指针初始化特别注意一点
  6. 2、Android构建本地单元测试
  7. 字符集ASCII、GBK、UNICODE、UTF在储存字符时的区别
  8. arm 模式 Linux,ARM Linux:usr模式转为svc模式的实现原理
  9. 控制连接数量和密码保护-flashcom教程 密码保护
  10. jsf初学解决GlassFish Server 无法启动
  11. java ping 实现的_java实现ping功能
  12. 深入理解深度学习中的【卷积】和 feature map
  13. 开源网管工具 汇总比较
  14. 计算机程序班搞笑口号,二班班级霸气押韵口号(精选50句)
  15. SpringSecurity实战(七)-对接第三方登陆-流程分析
  16. 大数据时代个人隐私权保护机制构建与完善
  17. PLC模拟量传输过程
  18. :[转贴]变态级JAVA程序员面试32问(附答案
  19. 抓取Bilibili哔哩哔哩网站视频(Java和Python双版本实现)
  20. 用html5进行硬件加速

热门文章

  1. 数据结构一—— 数组
  2. next_permutation算法(基于交换)
  3. python对象属性在引用时前面需要加()_python基础-面向对象进阶
  4. okhttp3 请求html页面,OkHttp3源码详解(二) 整体流程
  5. python微信推送消息_Python编程之微信推送模板消息功能示例
  6. Linux环境下创建运行.java文件
  7. 结合JSP与HTML做一个九九乘法表
  8. mysql自增长序号_MySQL rownumber SQL生成自增长序号使用介绍
  9. opendrive匝道
  10. Windows FFMPEG开发环境配置