GerONSo

Untitled

Sep 2nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. private fun readLn() = readLine()!!
  2. private fun readInt() = readLn().toInt()
  3. private fun readStrings() = readLn().split(" ")
  4. private fun readInts() = readStrings().map { it.toLong() }
  5.  
  6. var g : MutableList<MutableList<Int>> = MutableList(0) { MutableList(0) {0}};
  7. var used : MutableList<Int> = MutableList(0) {0};
  8. var order : MutableList<Int> = MutableList(0) {0};
  9. var ans : MutableList<Int> = MutableList(0) {0}
  10.  
  11. fun dfs(u : Int) {
  12. used[u] = 1;
  13. ans.add(u)
  14. for(v in g[u]) {
  15. if(used[v] == 0) {
  16. dfs(v)
  17. }
  18. }
  19. order.add(u)
  20. }
  21.  
  22. fun main() {
  23. var n = readInt();
  24. var a = readInts().toMutableList();
  25. g = MutableList(n) { MutableList(0) {0}};
  26. used = MutableList(n) {0}
  27. for(i in 0 until n) {
  28. for(j in 0 until n) {
  29. if(a[i] * 2 == a[j] || (a[i] % 3 == 0L && a[i] / 3 == a[j])) {
  30. g[i].add(j);
  31. }
  32. }
  33. }
  34. for(i in 0 until n) {
  35. if(used[i] == 0) {
  36. dfs(i)
  37. }
  38. }
  39. order.reverse()
  40. used.fill(0)
  41. ans.clear()
  42. dfs(order[0])
  43. for(i in ans) {
  44. print(a[i])
  45. print(" ")
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment