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 <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= (int)b; i ++)
void read(int & x) {
    x = 0; int flag = 1; char ch = ' ';
    while (ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
void read(ll & x) {
    x = 0; int flag = 1; char ch = ' ';
    while (ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
using namespace std;
const int maxn = 2010;
int n, degree[maxn], a[maxn];
bool vis[maxn], mp[maxn][maxn];
vector < int > ans, edge[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int u) {
    vis[u] = 1;
    rep(i, 1, n)
        if(!vis[i] && mp[u][i]) edge[u].push_back(i), degree[i] ++, dfs(i);
}
void solve() {
    priority_queue < int > pq;
    memset(vis, 0, sizeof(vis));
    rep(i, 1, n) if(degree[i] == 0) pq.push(i), vis[i] = 1;
    while(!pq.empty()) {
        int u = pq.top(); pq.pop();
        ans.push_back(u);
        rep(i, 0, edge[u].size() - 1) {
            int v = edge[u][i];
            if(-- degree[v] == 0) pq.push(v);
        }
    }
}
int main()
{
    read(n);
    rep(i, 1, n) read(a[i]);
    sort(a + 1, a + 1 + n);
    rep(i, 1, n - 1) rep(j, i + 1, n) 
        if(gcd(a[i], a[j]) != 1) mp[i][j] = mp[j][i] = 1;
    rep(i, 1, n) dfs(i);
    solve();
    rep(i, 0, n - 1) printf("%d ", a[ans[i]]);
    return 0;
}