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: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,a[2005],V;
vector<int>edge[2005];
bool used[2005][2005],ex[2005][2005];
int in[2005];
vector<int>nxt;
P q;
void dfs(int v){
	if(used[V][v]) return; used[V][v] = 1;
	nxt.pb(v);
	rep(i,edge[v].size()){
		if(used[V][edge[v][i]] || in[edge[v][i]] != V) continue;
		dfs(edge[v][i]);
	}
}
vector<int>mer(vector<int>ret,vector<int>hoge){
		int c = ret.size(),b = hoge.size();
		int i = 0,j = 0;
		vector<int>ret2; ret2.resize(c+b); int nxt = 0;
		while(i!=c || j!=b){
			if(i==c) ret2[nxt++] = (hoge[j++]);
			else if(j==b) ret2[nxt++] = (ret[i++]);
			else{
				if(a[ret[i]] > a[hoge[j]]) ret2[nxt++] = (ret[i++]);
				else ret2[nxt++] = (hoge[j++]);
			}
		}
		return ret2;
}
vector<int>make(vector<int>L,int cut){
	vector<int>ret;
	if(L.size() == 1){
		ret.pb(cut); return ret;
	}
	rep(i,L.size()){
		if(L[i] != cut){
		    in[L[i]] = cut;
		}
	}
	vector<pair<vector<int>,int> >vv;
	rep(i,L.size()){
		if(L[i] == cut) continue;
		if(!used[cut][L[i]]){
			nxt.clear(); q = mp(INF,INF);
			V = cut; dfs(L[i]);
			rep(j,nxt.size()){
				if(ex[nxt[j]][cut]){
					q = min(q,mp(a[nxt[j]],nxt[j]));
				}
			}
			vv.pb(mp(nxt,q.sc));
		}
	}
	rep(ii,vv.size()){
		vector<int>&nxt = vv[ii].fi; int Q = vv[ii].sc;
		vector<int>hoge = make(nxt,Q);
		ret = mer(ret,hoge);
	}
	ret.insert(ret.begin(),cut);
	return ret;
}
bool U[2005];
vector<int>vi;
P p;
void DFS(int x){
	if(U[x]) return; U[x] = 1;
	vi.pb(x); p = min(p,mp(a[x],x));
	rep(i,edge[x].size()){
		DFS(edge[x][i]);
	}
}
vector<int>res;
int main(){
	cin>>n;
	repn(i,n){
		cin>>a[i];
	}
	repn(i,n) repn(j,n){
		if(i != j && __gcd(a[i],a[j]) > 1){
			edge[i].pb(j); ex[i][j] = 1;
		}
	}
	repn(i,n){
		if(U[i]) continue; p = mp(INF,INF); vi.clear();
		DFS(i);
		vector<int>vec = make(vi,p.sc);
		res = mer(res,vec);
	}
	rep(i,n) cout<<a[res[i]]<<" ";
	cout<<endl;
}