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)

//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\

#include <bits/stdc++.h>

#define F first
#define S second
#define _sz(x) (int)x.size()

using namespace std ;
using ll = long long ;
using ld = long double ;
using pii = pair <int , int> ;

int const N = 30000 + 20 , M = 30000 + 20 ,  MAX = 1e8 + 20 , S = 1e4 + 20 ;
int n , a[N] ;
vector <int> vec ;

vector <int> g[M] ;
int sz ;
vector <int> comp[N] ;

vector <int> save[N] ;

int ans[N] , mn[N] , last[M] ;
bool dead[N] , vis[M] ;
int mx1 , mx2 , mn_val[N] ;

vector <int> primes ;
void pre () {
	bool np[S] ;
	memset(np , 0 , sizeof np) ;
	for (int i = 2 ; i < S ; i ++) {
		if (np[i]) continue ;
		primes.push_back(i) ;
		for (int j = 2 * i ; j < S ; j += i) np[j] = 1 ;
	}
}

inline vector<int> get (int x) {		
	vector <int> vec ;

	for (int j = 0 ; 1ll * primes[j] * primes[j] <= x ; j ++) {
		if (x % primes[j]) continue ;
		vec.push_back(primes[j]) ;
		while (x % primes[j] == 0) x /= primes[j] ;
	}
	if (x > 1) vec.push_back(x) ;

	return vec ;
}

inline int get_id (int x) {
	return lower_bound(vec.begin() , vec.end() , x) - vec.begin() ;
}

inline bool cmp (int x , int y) { return a[x] < a[y] ; }

void dfs (int v) {
	if (vis[v]) return ;
	if (v < n) {
		if (dead[v]) return ;
		comp[sz].push_back(v) ;
	}
	vis[v] = 1 ;

	for (int u : g[v]) dfs(u) ;
}

int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;

	pre() ;

	cin >> n ;
	for (int i = 0 ; i < n ; i ++) {
		cin >> a[i] ;

		save[i] = get(a[i]) ;
		for (int x : save[i]) vec.push_back(x) ;
	}

	sort(vec.begin() , vec.end()) ;
	vec.resize( unique(vec.begin() , vec.end()) - vec.begin() ) ;

	for (int i = 0 ; i < n ; i ++) {
		for (int x : save[i]) {
			int u = i , v = n + get_id(x) ;
			g[u].push_back(v) ;
			g[v].push_back(u) ;
		}
	}

	memset(last , -1 , sizeof last) ;
	memset(mn , 63 , sizeof mn) ;

	for (int i = 0 ; i < n ; i ++) {
		memset(vis , 0 , sizeof vis) ;
		mx1 = 0 , mx2 = 0 ;
		sz = 0 ;

		for (int v = 0 ; v < n ; v ++) {
			if (!dead[v] && !vis[v]) {
				comp[sz].clear() ;
				dfs(v) ;

				sort(comp[sz].begin() , comp[sz].end() , cmp) ;
		
				mx2 = max(mx2 , a[comp[sz][0]]) ;
				if (mx2 > mx1) swap(mx1 , mx2) ;

				for (int u : comp[sz]) mn_val[u] = a[comp[sz][0]] ;
				
				sz ++ ;
			}
		}

		int res = -1 ;
		for (int j = 0 ; j < n ; j ++) {
			if (dead[j]) continue ;

			int val = (mn_val[j] == mx1 ? mx2 : mx1) ;
			if (val > a[j]) continue ;

			int pp = -1 ;
			for (int x : save[j]) pp = max(pp , last[x]) ;

			if (mn[pp + 1] < a[j]) continue ;

			if (res == -1 || a[res] > a[j]) res = j ;
		}

		dead[res] = 1 ;
		ans[i] = a[res] ;
		for (int j = i ; j >= 0 ; j --) mn[j] = min(mn[j] , a[res]) ;
		for (int x : save[res]) last[x] = i ;
	}

	for (int i = 0 ; i < n ; i ++) cout << ans[i] << ' ' ; cout << '\n' ;
}