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<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#define N 5010
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
priority_queue<pii> q;
vector<int> g[N];int vis[N],a[N];
int gcd(int a,int b) { return a?gcd(b%a,a):b; }
int dfs(int x,int n)
{
	for(int y=1;y<=n;y++)
		if(!vis[y]&&gcd(a[x],a[y])>1)
			vis[y]=1,dfs(y,n),g[x].push_back(y);
	return 0;
}
int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);while(!q.empty()) q.pop();
	for(int i=1;i<=n;i++)
		if(!vis[i]) vis[i]=1,dfs(i,n),q.push(mp(a[i],i));
	while(!q.empty())
	{
		int x=q.top().second;
		q.pop(),printf("%d ",a[x]);
		for(int i=0;i<(int)g[x].size();i++)
			q.push(mp(a[g[x][i]],g[x][i]));
	}
	return !printf("\n");
}