#include #include #include #include int main() { using namespace std; int n; cin >> n; vector a(n); for (auto &e : a) { cin >> e; } constexpr int m = 10010; vector LCM(m, vector(m)); for (int i = 1; i < m; i++) { for (int j = 1; j < m; j++) { LCM[i][j] = lcm(i, j); } } for (int i = 0; i < n; i++) { sort(a.begin() + i + 1, a.end(), [&](const int x, const int y) { if (LCM[x][a[i]] != LCM[y][a[i]]) { return LCM[x][a[i]] < LCM[y][a[i]]; } else { return x < y; } }); } for (auto e : a) { cout << e << " "; } cout << endl; }