#include using namespace std; #define ll long long int main(){ cin.tie(nullptr)->sync_with_stdio(false); int N; cin >> N; vector A(N, 0); for(int& x: A) cin >> x; int mx = *max_element(A.begin(), A.end()); vector divisors(mx + 1, vector(0, 0)); for(int i = 1; i <= mx; ++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(mx + 1); // (val, 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 == N - 1]; tuple t = {0x3f3f3f3f, 0x3f3f3f3f, x}; for(int d: divisors[A[x]]){ d2vi[d].erase({A[x], x}); if(d2vi[d].empty()) continue; const auto [val, idx] = *d2vi[d].begin(); int lc = lcm(A[x], A[idx]); t = min(t, {lc, A[idx], idx}); } x = get<2>(t); } return 0; }