-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path977D.cpp
125 lines (124 loc) · 2.97 KB
/
977D.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
// हर हर महादेव
#define F first
#define S second
#define dbl double
using namespace std;
#define pb push_back
#define vin(name, size) \
for (i = 0; i < size; i++) \
cin >> name[i]
#define vout(name) \
for (i = 0; i < name.size(); i++) \
cout << name[i] << " "; \
cout << endl
#define vvin(name, rowss, colmn) \
for (i = 0; i < rowss; i++) \
{ \
for (j = 0; j < colmn, j++) \
cin >> name[i][j]; \
}
#define vvout(name, rowss, colmn) \
for (i = 0; i < rowss; i++) \
{ \
for (j = 0; j < colmn, j++) \
cout << name[i][j] << " "; \
cout << endl; \
}
#include <bits/stdc++.h>
#define ll long long int
void dfs(vector<ll> adj[], ll node, vector<bool> &vis)
{
vis[node] = 1;
for (auto x : adj[node])
if (!vis[x])
dfs(adj, x, vis);
}
// ll dream[];
ll mod = 1000000007;
#define all(x) x.begin(), x.end()
#define mem memset(dream, -1, sizeof(dream))
#define Pi 3.1415926535897932384626
#define deb(v) cout << #v << " " << v << "\n"
#define deb2(v, k) cout << #v << " " << v << "\n" \
<< #k << " " << k << "\n"
void HHM()
{
// mem;
ll i, j, k;
ll n;
cin >> n;
vector<ll> v(n);
for (auto &x : v)
cin >> x;
map<ll, bool> mp;
mp[0] = 1;
vector<ll> r, r1;
r.pb(v[0]), r1.pb(v[0]);
while (1)
{
bool flag = 0;
for (i = 1; i < n; i++)
{
if (mp[i])
continue;
if (r[r.size() - 1] % 3 == 0 && v[i] == r[r.size() - 1] / 3)
{
r.pb(v[i]);
mp[i] = 1;
flag = 1;
break;
}
else if (r[r.size() - 1] * 2 == v[i])
{
r.pb(v[i]);
mp[i] = 1;
flag = 1;
break;
}
}
if (!flag)
break;
}
while (1)
{
bool flag = 0;
for (i = 1; i < n; i++)
{
if (mp[i])
continue;
if (v[i] == r1[r1.size() - 1] * 3)
{
r1.pb(v[i]);
mp[i] = 1;
flag = 1;
break;
}
else if (r1[r1.size() - 1] % 2 == 0 && r1[r1.size() - 1] / 2 == v[i])
{
r1.pb(v[i]);
mp[i] = 1;
flag = 1;
break;
}
}
if (!flag)
break;
}
for (i = r1.size() - 1; i > 0; i--)
cout << r1[i] << " ";
vout(r);
return;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0);
ll t = 1;
// cin >> t;
for (ll i = 1; i <= t; i++)
{
// cout << "Case #" <<i<< ": ";
HHM();
}
}
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);