import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable int MAX = 100001; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; int[][int] P; RedBlackTree!(int, "a < b", true)[int] B; auto rbt = new RedBlackTree!(int, "a < b", true)(); foreach (i; 1..N) rbt.insert(A[i]); foreach (i; 0..N) { if (A[i] in P) continue; int x = A[i]; for (int j = 2; j * j <= A[i]; ++j) { if (x % j == 0) P[A[i]] ~= j; while (x % j == 0) x /= j; } if (x > 1) P[A[i]] ~= x; } foreach (i; 1..N) { if (A[i] == 1) continue; foreach (p; P[A[i]]) { if (p !in B) B[p] = new RedBlackTree!(int, "a < b", true)(); B[p].insert(A[i]); } } int[] ans = [A[0]]; int cur = A[0]; foreach (i; 0..N-1) { if ((1 in rbt) || cur == 1) { int x = rbt.front; rbt.removeKey(x); if (x in P) foreach (p; P[x]) B[p].removeKey(x); ans ~= x; cur = x; continue; } int min_lcm = 1 << 29; int min_num = -1; foreach (p; P[cur]) { if (B[p].empty) continue; int x = B[p].front; int lcm = x * cur / gcd(x, cur); if (lcm < min_lcm) { min_lcm = lcm; min_num = x; } else if (lcm == min_lcm && x < min_num) { min_num = x; } } int x = rbt.front; int lcm = x * cur / gcd(x, cur); if (lcm < min_lcm) { min_lcm = lcm; min_num = x; } else if (lcm == min_lcm && x < min_num) { min_num = x; } x = min_num; ans ~= x; if (x in P) foreach (p; P[x]) B[p].removeKey(x); rbt.removeKey(x); cur = x; } ans.map!(to!string).join(" ").writeln; }