#include #include #include #include using namespace std; typedef pair pattern; class GCD { private: map memo; public: int gcd(pattern *p); private: int _gcd(pattern *p); void swapPattern(pattern *p); public: int operator ()(pattern *p){return gcd(p);} int operator ()(int x, int y){pattern p = make_pair(x, y); return gcd(&p);} }; int GCD::gcd(pattern *p) { if(p->first < p->second) swapPattern(p); return _gcd(p); } int GCD::_gcd(pattern *p) { auto f = memo.find((*p)); if(f != memo.end()) { return (*f).second; } int r = p->first % p->second; if(r == 0) { memo.insert(make_pair((*p), p->second)); return p->second; } pattern q = make_pair(p->second, r); return _gcd(&q); } void GCD::swapPattern(pattern *p) { int temp = p->first; p->first = p->second; p->second = temp; return; } int main() { GCD gcd; int n; cin >> n; pattern list[n]; for(int i = 0; i < n; i++) { cin >> list[i].first; } list[0].second = INT_MAX; for(int i = 0; i < (n - 1); i++) { int min = 0; for(int j = i + 1; j < n; j++) { list[j].second = (list[i].first * list[j].first) / gcd(list[i].first, list[j].first); if(list[min].second >= list[j].second) { if(list[min].second == list[j].second) min = (list[min].first > list[j].first)? j:min; else min = j; } } int temp = list[i + 1].first; list[i + 1].first = list[min].first; list[min].first = temp; } cout << list[0].first; for(int i = 1; i < n; i++) { cout << " " << list[i].first; } cout << endl; return 0; }