/* -*- coding: utf-8 -*- * * 14.cc: No.14 最小公倍数ソート - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 10000; const int MAX_A = 10000; const int INF = 1 << 30; /* typedef */ typedef vector vi; typedef pair pii; typedef set spii; /* global variables */ pii ais[MAX_N]; vi dvs[MAX_N]; spii dais[MAX_A + 1]; /* subroutines */ inline int lcm(int a, int b) { return a * b / __gcd(a, b); } /* main */ int main() { int n; cin >> n; for (int i = 0; i < n; i++) (cin >> ais[i].first), ais[i].second = i; for (int i = 0; i < n; i++) { int &ai = ais[i].first; for (int d = 1; d * d <= ai; d++) if (ai % d == 0) dvs[i].push_back(d), dais[d].insert(ais[i]); if (ai != 1) dvs[i].push_back(ai), dais[ai].insert(ais[i]); //for (int j = 0; j < dvs[i].size(); j++) printf("%d ", dvs[i][j]); //putchar('\n'); } vi ans; pii fai(ais[0]); for (int i = 0; i < n; i++) { int mincm = INF; pii minai; ans.push_back(fai.first); vi &df = dvs[fai.second]; for (int j = 0; j < df.size(); j++) { int dj = df[j]; dais[dj].erase(fai); if (dais[dj].size() > 0) { pii top = *(dais[dj].begin()); int cm = top.first / dj; if (mincm > cm || (mincm == cm && minai > top)) mincm = cm, minai = top; } } fai = minai; } for (int i = 0; i < n; i++) { if (i) putchar(' '); printf("%d", ans[i]); } putchar('\n'); return 0; }