#include typedef long long ll; typedef unsigned long long ull; #define FOR(i,a,b) for(int (i)=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define RANGE(vec) (vec).begin(),(vec).end() using namespace std; template T gcd(T a, T b) { if ( a < b ) std::swap(a,b); if ( b == 0 ) return a; return gcd(b, a%b); } template T lcm(T a, T b) { return a*b/gcd(a,b); } class LCMsort { public: void solve(void) { int N; cin>>N; vector a(N,0); REP(i,N) cin>>a[i]; vector ans; vector used(N,false); auto calc = [&](int pivot) { used[pivot] = true; int mx = (1<<30); int mi = -1; // O(N*A) pivot が更新されるにつれこのループ速度は高速化されていくはず。 REP(i, N) { if (used[i]) continue; int k = lcm(a[pivot], a[i]); if (k < mx || (k == mx && a[i] < a[mi])) { mx = k; mi = i; } } return mi; }; // O(N^2*A) int pivot = 0; REP(i,N) { ans.push_back(a[pivot]); pivot = calc(pivot); } REP(i,N) cout<solve(); delete obj; return 0; } #endif