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: Accepted)

#include<bits/stdc++.h>
using namespace std;
inline char nchar() {
	static const int bufl=1<<20;
	static char buf[bufl],*a=NULL,*b=NULL;
	return a==b && (b=(a=buf)+fread(buf,1,bufl,stdin),a==b)?EOF:*a++;
}
inline int read() {
	int x=0,f=1;
	char c=nchar();
	for (;!isdigit(c);c=nchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=nchar()) x=x*10+c-'0';
	return x*f;
}
const int maxn=2e3+10;
typedef vector<int> vec;
int n,a[maxn];
vec operator + (const vec &a,const vec &b) {
	vec ret(a.size()+b.size());
	copy(b.begin(),b.end(),copy(a.begin(),a.end(),ret.begin()));
	return ret;
}
vec operator ^ (const vec &a,const vec &b) {
	vec ret(a.size()+b.size());
	vec::const_iterator ai=a.begin(),bi=b.begin();
	vec::iterator ri=ret.begin();
	while (ai!=a.end() || bi!=b.end()) {
		if (ai==a.end()) *ri++=*bi++; else
		if (bi==b.end()) *ri++=*ai++; else
		*ri++=*(*ai>*bi?(ai++):(bi++));
	}
	return ret;
}
namespace graph {
	vec g[maxn],f[maxn];
	bool vis[maxn];
	inline void add(int x,int y) {
		g[x].push_back(y);
		g[y].push_back(x);
	}
	void dfs(int x) {
		vis[x]=true;
		vec &fx=f[x];
		sort(g[x].begin(),g[x].end(),[](int x,int y)->bool{return a[x]<a[y];});
		for (const int &v:g[x]) g[v].erase(find(g[v].begin(),g[v].end(),x));
		for (const int &v:g[x]) if (!vis[v]) {
			dfs(v);
			fx=fx^f[v];
		}
		fx=vec(1,a[x])+fx;
	}
	vec work() {
		dfs(0);
		return f[0];
	}
}
int main() {
	n=read(),generate_n(a+1,n,read);
	for (int i=1;i<=n;++i) graph::add(0,i);
	for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j) if (__gcd(a[i],a[j])>1) graph::add(i,j);
	vec ans(graph::work());
	for (int i=1;i<=n;++i) printf("%d%c",ans[i]," \n"[i==n]);
	return 0;
}