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 <bits/stdc++.h>
 
const int maxn = 2030, oo = 123456789, maxm = 16010;
 
int n, m, T, ans, mn, a[maxn], foo[maxn], lst[maxm], tag[maxn];
bool vis[maxn], f[maxn], G[maxn][maxn], F[maxn];
std::vector<int> g[maxn], has[maxn], rhas[maxm], all;
std::pair<int, int> v[2];
 
void GEN() {
	int mx = 0;
	for (int i = 1; i <= n; ++i) {
		if(!foo[i]) {
			if(a[i] == 1) {
				F[i] = 1;
				continue;
			}
			F[i] = 0;
			for (int j: has[i]) {
				if(lst[j] >= mx) {
					F[i] = 1;
					break;
				}
			}
		}
		else {
			(foo[i] > mx) && (mx = foo[i]);
		}
	}
	return;
}
 
inline void dfs(const int &u) {
	tag[u] = T;
	vis[u] = 1;
	(F[u] && a[u] < mn) && (mn = a[u]);
	for (register int &v: g[u]) {
		if(!vis[v] && !f[v]) {
			dfs(v);
		}
	}
	return;
}
 
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", a + i);
		int t = a[i];
		for (int x = 2; x * x <= t; ++x) {
			if(t % x == 0) {
				all.push_back(x);
				while(t % x == 0) {
					t /= x;
				}
			}
		}
		if(t > 1) {
			all.push_back(t);
		}
	}
	std::sort(a + 1, a + n + 1);
	std::sort(all.begin(), all.end());
	all.resize(std::unique(all.begin(), all.end()) - all.begin());
	m = (int)(all.size());
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < m; ++j) {
			if(a[i] % all[j] == 0) {
				has[i].push_back(j);
				rhas[j].push_back(i);
			}
		}
	}
	for (int j = 0, sz; j < m; ++j) {
		sz = (int)(rhas[j].size());
		for (register int i = 0; i < sz; ++i) {
			for (register int k = 0; k < i; ++k) {
				int &u = rhas[j][i], &v = rhas[j][k];
				if(!G[u][v]) {
					g[u].push_back(v);
					g[v].push_back(u);
					G[u][v] = G[v][u] = 1;
				}
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		GEN();
		memset(vis + 1, 0, sizeof(vis[0]) * n);
		v[0] = v[1] = std::make_pair(oo, 0);
		for (register int u = 1; u <= n; ++u) {
			if(F[u] && !vis[u] && !f[u]) {
				++T;
				mn = oo;
				dfs(u);
				if(mn < v[0].first) {
					v[1] = v[0];
					v[0] = std::make_pair(mn, T);
				}
				else if(mn < v[1].first) {
					v[1] = std::make_pair(mn, T);
				}
			}
		}
//		ans = 0;
		int sz = v[1].first == oo ? 0 : 1;
		for (register int u = 1; u <= n; ++u) {
			if(F[u] && !f[u]) {
				int i = sz;
				if(v[i].second == tag[u]) {
					--i;
				}
				if(v[i].first <= a[u] || i < 0) {
					ans = u;
					break;
				}
			}
		}
//		assert(ans);
		f[ans] = 1;
		foo[ans] = i;
		printf("%d ", a[ans]);
		for (int j: has[ans]) {
			lst[j] = i;
		}
	}
	return 0;
}