#include using namespace std; signed main() { ios::sync_with_stdio(false); int N; cin >> N; vector A(N); for (int i = 0; i < N; ++i) cin >> A[i]; int maxa = *max_element(A.begin(), A.end()); vector> divisors(maxa + 1); for (int i = 1; i <= maxa; ++i) for (int j = 1; j * j <= i; ++j) if (i % j == 0) { divisors[i].emplace_back(j); if (j * j != i) divisors[i].emplace_back(i / j); } vector>> d2vi(maxa + 1); // (value, index) for (int i = 0; i < N; ++i) for (int d : divisors[A[i]]) d2vi[d].emplace(A[i], i); for (int i = 0, x = 0; i < N; ++i) { cout << A[x] << " \n"[i + 1 == N]; tuple t = make_tuple(0x3f3f3f3f, 0x3f3f3f3f, x); for (int d : divisors[A[x]]) { d2vi[d].erase(make_pair(A[x], x)); if (d2vi[d].empty()) continue; int v, y; tie(v, y) = *d2vi[d].begin(); int lcm = A[x] * A[y] / __gcd(A[x], A[y]); t = min(t, make_tuple(lcm, A[y], y)); } x = get<2>(t); } }