-
Notifications
You must be signed in to change notification settings - Fork 1
/
D.cpp
163 lines (150 loc) · 3.54 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
/***template***/
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);
#define dd double
#define scl(n) scanf("%lld",&n)
#define scd(n) scanf("%lf",&n)
#define pi pair<ll,ll>
#define pb push_back
#define mp make_pair
#define uu first
#define vv second
#define FOR(i,n) for(ll i=1;i<=n;i++)
#define LOOP(i,n) for(ll i=0;i<n;i++)
#define FOOR(i,a,b) for(ll i=a;i<=b;i++)
#define LOP(i,a,b) for(ll i=a;i<b;i++)
#define sorted(s) sort(s.begin(),s.end())
#define sortedd(s) sort(s.begin(),s.end(),cmp)
#define reversed(s) reverse(s.begin(),s.end())
#define contains(a,b) (find((a).begin(), (a).end(), (b)) != (a).end())
#define MAXN 100005
#define pii 3.1415926536
#define mod 1000000007
#define eps 0.0000000001
#define inf 1e18
#define INF (1LL<<62)
#define mstt(a,b) memset((a),(b),sizeof (a))
#define sz(x) (ll)x.size()
#define uniquee(x) x.erase(unique(x.begin(), x.end()),x.end())
/****only for priority queue******/
/**struct node
{
ll nod,d;
node(ll a,ll b)
{
nod=a;
d=b;
}
bool operator<(const node &p)const
{
return p.d<d;
}
};**/
ll Set(ll n,ll pos)
{
return n=n|(1LL<<pos);
}
ll reset(ll n,ll pos)
{
return n=n&~(1LL<<pos);
}
bool check(ll n,ll pos)
{
return (bool)(n&(1LL<<pos));
}
ll mul(ll a,ll b)
{
ll c;
c = (a%mod * b%mod)%mod;
return c;
}
ll add(ll a,ll b)
{
ll c;
c = (a%mod + b%mod)%mod;
return c;
}
ll sub(ll a,ll b)
{
ll c;
c = ((a%mod - b%mod)%mod + mod)%mod;
return c;
}
ll power(ll x,ll y)
{
ll res = 1;
x = x;
while (y > 0)
{
if (y & 1)
res = (res*x);
y = y>>1;
x = (x*x);
}
return res;
}
long long exgcd(long long x, long long y, long long &a, long long &b)
{
// ax + by = gcd(x,y)
int flag = 0;
long long t, la = 1, lb = 0, ra = 0, rb = 1;
while(x%y)
{
if(flag == 0)
la -= x/y*ra, lb -= x/y*rb;
else
ra -= x/y*la, rb -= x/y*lb;
t = x, x = y, y = t%y;
flag = 1 - flag;
}
if(flag == 0)
a = ra, b = rb;
else
a = la, b = lb;
return y;
}
/***template***/
ll n, ara[110];
int main()
{
cin >> n;
for(ll i = 1; i <= n; i++)
scanf("%lld", &ara[i]);
deque <ll> st;
st.push_back(ara[1]);
bool checker[110] = {};
for(ll i = 1; i <= n; i++) {
ll x = st.back();
for(ll j = 1; j <= n; j++) {
if(checker[j])
continue;
if(ara[j] == x * 2 || (x % 3 == 0 && ara[j] == x / 3)) {
st.push_back(ara[j]);
checker[j] = 1;
break;
}
}
}
for(ll i = 1; i <= n; i++) {
ll x = st.front();
for(ll j = 1; j <= n; j++) {
if(checker[j])
continue;
if(ara[j] == x * 3 || (x % 2 == 0 && ara[j] == x / 2)) {
st.push_front(ara[j]);
checker[j] = 1;
break;
}
}
}
while(!st.empty()) {
ll x = st.front();
st.pop_front();
printf("%lld ", x);
}
cout << endl;
return 0;
}