Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun readLn() = readLine()!!
- private fun readInt() = readLn().toInt()
- private fun readStrings() = readLn().split(" ")
- private fun readInts() = readStrings().map { it.toLong() }
- var g : MutableList<MutableList<Int>> = MutableList(0) { MutableList(0) {0}};
- var used : MutableList<Int> = MutableList(0) {0};
- var order : MutableList<Int> = MutableList(0) {0};
- var ans : MutableList<Int> = MutableList(0) {0}
- fun dfs(u : Int) {
- used[u] = 1;
- ans.add(u)
- for(v in g[u]) {
- if(used[v] == 0) {
- dfs(v)
- }
- }
- order.add(u)
- }
- fun main() {
- var n = readInt();
- var a = readInts().toMutableList();
- g = MutableList(n) { MutableList(0) {0}};
- used = MutableList(n) {0}
- for(i in 0 until n) {
- for(j in 0 until n) {
- if(a[i] * 2 == a[j] || (a[i] % 3 == 0L && a[i] / 3 == a[j])) {
- g[i].add(j);
- }
- }
- }
- for(i in 0 until n) {
- if(used[i] == 0) {
- dfs(i)
- }
- }
- order.reverse()
- used.fill(0)
- ans.clear()
- dfs(order[0])
- for(i in ans) {
- print(a[i])
- print(" ")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment