#include #include #include #include using namespace std; int lcm(int a, int b){ int ab = a * b; int tmp = 0; while (b){ tmp = a % b; a = b; b = tmp; } return ab / a; } void lcm_sort(vector & a){ sort(a.begin() + 1, a.end()); for (int i = 0; i < a.size() - 2; ++i){ int ai = a[i]; int min_lcm = 100000001; int k = 0; for (int j = i + 1; j != a.size(); ++j){ int aj = a[j]; int lcmij = lcm(ai, aj); if (lcmij < min_lcm){ min_lcm = lcmij; k = j; } } swap(a[i + 1], a[k]); } } int main() { int n = 0; cin >> n; vector a(n, 0); for (auto & ai : a) cin >> ai; lcm_sort(a); for (int i = 0; i != n; ++i) cout << a[i] << ((i == n-1) ? "\n" : " "); return 0; }