Language: C++

#pragma GCC optimize(2)
// Happy TLE and WA every day!
// by: zxb the vegetable chicken
#include<bits/stdc++.h>
#define mp				make_pair
#define rep(i,n)		for(int i = 0; i < n; i++)
#define foreach(i,c)	for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define BINF			0x7fffffff
#define INF				0x3f3f3f3f
#define LINF			3223372036854775807
#define END(s)			{cout<<s; return 0;}
#define CON(s)			{cout<<s; continue;}
#define BRE(s)			{cout<<s; break;}
#define pb				push_back
//#define int				long long
#define All(a)			a.begin(), a.end()  //with A in CAPITAL!!!
#define sz(a)			(int)a.size()
#define F				first
#define S				second
//#define usingFiles
using namespace std;

const int rp = 666666;
const bool debug = 1;
const bool I_good_vegetable_a = 1;

int a[2020];
int n;
vector<int> v[2020];
int ltksize;
int mnstart;
bool used[2020];
vector<int> ltk[2020];
int index[2020];
vector<int> ans;
int cnt = 0;
int num = 0;

int count(int now){
	if(a[now] < a[mnstart]) mnstart = now;
	int ans = 1;
	used[now] = true;
	rep(i, v[now].size()){
		if(!used[v[now][i]]) ans += count(v[now][i]);
	}
	return ans;
}

void uncount(int now){
	used[now] = false;
	rep(i, v[now].size()){
		if(used[v[now][i]]) uncount(v[now][i]);
	}
}

bool dfs(int now){
	used[now] = true;
	num++;
	if(num == ltksize){
		ltk[cnt].pb(now);
		return 1;
	}
	rep(i, v[now].size()){
		if(used[v[now][i]]) continue;
		if(dfs(v[now][i])){
			ltk[cnt].pb(now);
			return 1;
		}
	}
	used[now] = false;
	return 0;
}

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

bool cmp(pair<int, int> x, pair<int, int> y){
	return x > y;
}

signed main(){
	ios::sync_with_stdio(false);
	#ifdef usingFiles
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	memset(index, 0, sizeof(index));
	cin >> n;
	rep(i, n) cin >> a[i];
	rep(i, n){
		for(int j = 0; j < i; j++){
			if(__gcd(a[i], a[j]) > 1){
				v[i].pb(j);
				v[j].pb(i);
			}
		}
	}
	rep(i, n){
		sort(v[i].begin(), v[i].end(), cmp1);
	}
	rep(i, n){
		if(!used[i]){
			mnstart = i; 
			ltksize = count(i);
			uncount(i);
			num = 0;
			dfs(mnstart);
			reverse(ltk[cnt].begin(), ltk[cnt].end());
			cnt++;
		}
	}
	while(1){
		int mx = -1, mxpos;
		rep(i, cnt){
			if(index[i] >= ltk[i].size()) continue;
			if(a[ltk[i][index[i]]] > mx){
				mx = a[ltk[i][index[i]]];
				mxpos = i;
			}
		}
		if(mx == -1) break;
		index[mxpos]++;
		ans.pb(mx);
	}
	rep(i, ans.size()) cout << ans[i] << " ";
	cout << endl;
	return 0;
}
/*
5
1 2 3 4 5
*/

Compiler output:
a.cc:34:15: error: 'int index [2020]' redeclared as different kind of entity
   34 | int index[2020];
      |               ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:4:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'int main()':
a.cc:88:26: error: ISO C++ forbids applying 'sizeof' to an expression of function type [-fpermissive]
   88 |         memset(index, 0, sizeof(index));
      |                          ^~~~~~~~~~~~~
a.cc:88:15: error: cannot resolve overloaded function 'index' based on conversion to type 'void*'
   88 |         memset(index, 0, sizeof(index));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:116:33: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  116 |                         if(index[i] >= ltk[i].size()) continue;
      |                                 ^
a.cc:117:42: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  117 |                         if(a[ltk[i][index[i]]] > mx){
      |                                          ^
a.cc:118:52: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  118 |                                 mx = a[ltk[i][index[i]]];
      |                                                    ^
a.cc:123:22: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  123 |                 index[mxpos]++;
      |                      ^