# pragma GCC target("avx2") # pragma GCC optimize("O3") # pragma GCC optimize("unroll-loops") #include #include #include #include int main() { using namespace std; ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector a(n); for (auto &e : a) { cin >> e; } constexpr int m = 10001; 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 << "\n"; }