#include #include #include using namespace std; struct sequence { int a, lcm; }; bool comp(const sequence &a, const sequence &b) { return a.lcm <= b.lcm; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int LCM(int a, int b) { return a * b / gcd(a, b); } int main() { size_t N; sequence tmp1, tmp2; vector array; cin >> N; for (size_t i = 0; i < N; ++i) { cin >> tmp1.a; array.push_back(tmp1); } for (size_t i = 0; i < N; ++i) { tmp1 = array[i]; for (size_t j = i + 1; j < N; ++j) { tmp2 = array[j]; tmp2.lcm = LCM(tmp1.a, tmp2.a); array[j] = tmp2; } stable_sort(array.begin() + i + 1, array.end(), comp); } for (size_t i = 0; i < N; ++i) { tmp1 = array[i]; cout << tmp1.a << ' '; } }