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<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<sstream>
#include<numeric>
#include<iostream>
#include<algorithm>
#include<functional>

#define For(i,x,y) for (int i=x;i<y;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dprintf(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> Vi;
typedef pair<int,int> pii;

int IN(){
	int c,f,x;
	while (!isdigit(c=getchar())&&c!='-');c=='-'?(f=1,x=0):(f=0,x=c-'0');
	while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return !f?x:-x;
}

const int N=200000+19;
const int oo=(1<<30)-1;

struct Edge{
	int y,nxt;
} E[N];
map<int,int> M,M2;
multiset<int> S;
int A[N],las[N],col[N],chk[N],mn[N],pre[N],suf[N],res[N],f[N],g[N];
int n,x,tot,cnt,c,mx,num,m;
Vi V[N];

void Link(int x,int y){
	E[cnt]=(Edge){y,las[x]};las[x]=cnt++;
	E[cnt]=(Edge){x,las[y]};las[y]=cnt++;
}
void Init(int n){
	if (M2.count(n)) return;
	M2[n]=++m;
	if (V[m].size()) return;
	for (int i=2;i*i<=n;i++)
		if (n%i==0){
			V[m].pb(i);
			while (n%i==0) n/=i;
		}
	if (n>1) V[m].pb(n);
}
void Renew(int x){
	if (!M.count(x)) M[x]=++tot;
}
void dfs(int x,int c){
	if (col[x]) return;
	col[x]=c;
	if (x<=*A) mn[c]=min(mn[c],A[x]);
	for (int i=las[x];~i;i=E[i].nxt) dfs(E[i].y,c);
}
int work(int val){
	M.clear();
	For(i,1,tot+1){
		las[i]=-1;
		col[i]=0;
	}
	tot=*A;cnt=0;c=0;
	For(i,1,*A+1){
		int tmp=A[i];
		for (int x=2;x*x<=tmp;x++)
			if (tmp%x==0){
				Renew(x);
				Link(i,M[x]);
				while (tmp%x==0) tmp/=x;
			}
		if (tmp>1){
			Renew(tmp);
			Link(i,M[tmp]);
		}
	}
	For(i,1,tot+1)
		if (!col[i]){
			mn[++c]=oo;
			dfs(i,c);
		}
	pre[0]=suf[c+1]=0;
	for (int i=1;i<=c;i++) pre[i]=max(pre[i-1],mn[i]);
	for (int i=c;i>=1;i--) suf[i]=max(suf[i+1],mn[i]);
	For(i,1,*A+1){
		if (A[i]>max(pre[col[i]-1],suf[col[i]+1])){
			int pos=0;
			for (int j:V[M2[A[i]]]) pos=max(pos,f[j]);
			if (A[i]<g[pos+1]) return A[i];
		}
	}
	assert(0);
}

int main(){
	memset(las,-1,sizeof(las));
	n=IN();
	For(i,1,n+1){
		x=IN();
		Init(x);
		S.insert(x);
	}
	x=oo;
	g[1]=oo;
	For(t,1,n+1){
		*A=0;
		for (int i:S) A[++*A]=i;
		x=work(x);
		S.erase(S.find(x));
		res[t]=x;
		for (int i:V[M2[x]]) f[i]=max(f[i],t);
		g[t+1]=oo;
		for (int i=t;~i;i--) g[i]=min(g[i+1],res[i]);
	}
	For(i,1,n+1) printf("%d ",res[i]);
	puts("");
}