Problem:
Score : 1600 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi and Aoki will arrange these integers in a row, as follows:

First, Takahashi will arrange the integers as he wishes.

Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.

We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible.
Find the eventual sequence that will be produced.

Constraints

1 ≦ N ≦ 2000

1 ≦ A_i ≦ 10^8

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

Print the eventual sequence that will be produced, in a line.

Sample Input 1

5
1 2 3 4 5

Sample Output 1

5 3 2 4 1

If Takahashi arranges the given integers in the order (1,2,3,4,5), they will become (5,3,2,4,1) after Aoki optimally manipulates them.

Sample Input 2

4
2 3 4 6

Sample Output 2

2 4 6 3

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

int gcd(int x,int y){
	if(y == 0)return x;
	return gcd(y,x%y);
}

int main(){
	int n;
	int a[2002];
	scanf("%d",&n);
	rep(i,n)scanf("%d",&a[i]);
	
	sort(a,a+n);
	int cnt[2002] = {};
	vector<int> vec[2002];
	rep(i,n){
		rep(j,n){
			if(gcd(a[i],a[j]) != 1){
				cnt[i] ++;
				vec[j].pb(i);
			}
		}
	}
	
	vector<int> ans[2002];
	bool used[2002];
	rep(i,2002)used[i] = false;
	rep(i,n){
		//cout << i << endl;
		if(!used[i]){
			int loc = i;
			while(1){
				ans[i].pb(loc);
				used[loc] = true;
				int loc_ = loc;
				rep(j,vec[loc_].size()){
					if(!used[vec[loc_][j]]){
						loc_ = vec[loc_][j];
						break;
					}
				}
				if(loc == loc_)break;
				loc = loc_;
			}
		}
	}
	
	vector<int> A;
	priority_queue<P1> que;
	rep(i,2002){
		if(ans[i].size() > 0){
			que.push(mp1(ans[i][0],i,0));
		}
	}
	while(!que.empty()){
		P1 p = que.top(); que.pop();
		A.pb(a[p.fr]);
		if(ans[p.sc.fr].size() > p.sc.sc+1){
			que.push(mp1(ans[p.sc.fr][p.sc.sc+1],p.sc.fr,p.sc.sc+1));
		}
	}
	
	
	/*priority_queue<int> que;
	rep(i,n)if(cnt[i] == 0)que.push(i);
	while(!que.empty()){
		int p = que.top(); que.pop();
		ans.pb(a[p]);
		rep(i,vec[p].size()){
			cnt[vec[p][i]] --;
			if(cnt[vec[p][i]] == 0)que.push(vec[p][i]);
		}
	}*/
	
	rep(i,A.size()){
		printf("%d%c",A[i],i+1==A.size()?'\n':' ');
	}
}