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: Runtime Error)

#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];
bool in[2005];
vector<int>nxt;
P q;
void dfs(int v){
	if(used[V][v]) return; used[V][v] = 1;
	nxt.pb(v); //q = min(q,mp(a[v],v));
	rep(i,edge[v].size()){
		if(used[V][edge[v][i]] || !in[edge[v][i]]) continue;
		dfs(edge[v][i]);
	}
}
vector<int>make(vector<int>L,int cut){
//rep(i,L.size()) cout<<L[i]<<" "; cout<<cut<<endl;
	vector<int>ret;
	if(L.size() == 1){
		ret.pb(cut); return ret;
	}
	memset(in,0,sizeof(in));
	rep(i,L.size()){
		if(L[i] != cut) in[L[i]] = 1;
	}
	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()){
			//	assert(nxt[j] != cut);
				if(__gcd(a[nxt[j]],a[cut]) > 1){
					q = min(q,mp(a[nxt[j]],nxt[j]));
				//	cout<<a[nxt[j]] << " "<< a[cut] << " "<< nxt[j] << " " << cut << endl;
				}//cout<<nxt[j]<<" ";
			}//cout<<q.sc<<endl;
			vector<int>hoge = make(nxt,q.sc);
		//	for(int x=0;x<hoge.size();x++) cout<<hoge[x]<<" "; cout<<endl;
			vector<int>ret2;
			int c = ret.size(),b = hoge.size();
			int i = 0,j = 0;
			while(i!=c || j!=b){
				if(i==c) ret2.pb(hoge[j++]);
				else if(j==b) ret2.pb(ret[i++]);
				else{
					if(a[ret[i]] > a[hoge[j]]) ret2.pb(ret[i++]);
					else ret2.pb(hoge[j++]);
				}
			}
			ret = ret2;
		}
	}
	reverse(ret.begin(),ret.end());
	ret.pb(cut);
	reverse(ret.begin(),ret.end());
	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);
		}
	}
	repn(i,n){
		if(U[i]) continue; p = mp(INF,INF); vi.clear();
		DFS(i);
		vector<int>vec = make(vi,p.sc);
		vector<int>res2;
		int c = 0,b = 0;
		while(c != res.size() || b != vec.size()){
			if(c == res.size()) res2.pb(vec[b++]);
			else if(b == vec.size()) res2.pb(res[c++]);
			else{
				if(a[res[c]] > a[vec[b]]) res2.pb(res[c++]);
				else res2.pb(vec[b++]);
			}
		}
		res = res2;
	}
	rep(i,n) cout<<a[res[i]]<<" ";
	cout<<endl;
}