Language: C++

/*
Though leaves are many , the root is one.
Through all the lying days of my youth
I swayed my leaves and flowers in the sun.
Now I may wither into the truth.
	  	  	- William Butler Yeats
*/
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
//reason of WA:The same out edge should be combined. 
#include<bits/stdc++.h>
#pragma comment("-Wl,--stack=1024000000")
//#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
const int mod=1000000007;
typedef long long ll;
#ifndef LOCAL
#define cerr if(0)cout
#define eprintf(...) 0
#else
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#endif
inline string getstr(string &s,int l,int r){string ret="";for(int i=l;i<=r;i++)ret.push_back(s[i]);return ret;}
int modpow(int x,int y,int md=mod){if(y==0)return 1;int ret=modpow(x,y>>1,md);ret=(ll)ret*ret%md;if(y&1)ret=(ll)ret*x%md;return ret;}
int n;
int ban[100005];
vector<int>out[100005];
int pos[100005];
bool check(vector<int>ans){
	for(int i=0;i+1<ans.size();i++){
		if(ban[ans[i]]==ans[i+1])return false;
	}
	return true;
}
map<int,int>cnt;
set<int>have;
vector<int>ans;
void del(int x){
	used[x]=1;cnt[ban[x]]--;if(!cnt[ban[x]])cnt.erase(ban[x]);
	have.erase(x);while(pos[ban[x]]<out[ban[x]].size()&&used[out[ban[x]][pos[ban[x]]]])pos[ban[x]]++;
	if(pos[ban[x]]!=out[ban[x]].size())have.insert(out[ban[x]][pos[ban[x]]]);
}
int used[100005];
bool can_put(int x){
	if(ans.size()&&ban[ans.back()]==x)return false;
	have.erase(x);//cnt[ban[x]]--;if(!cnt[ban[x]])cnt.erase(ban[x]);
	bool ret=cnt.size()==2&&
	((cnt.begin()->second==1&&have.count(cnt.rbegin()->first))||
	((cnt.rbegin()->second==1&&have.count(cnt.begin()->first))));
	//cnt[ban[x]]++;
	have.insert(x);
	return 1^(ret);
}
int perm[6];
int main(){
	cin>>n;cur=n;
	for(int i=1;i<=n;i++)cin>>ban[i];
	for(int i=1;i<=n;i++)cnt[ban[i]]++,out[ban[i]].push_back(i);
	for(int i=1;i<=n;i++)if(out[i].size())have.insert(out[i][0]);
	for(int i=1;i<=n-5;i++){
		int ins=-1;
		if(cnt.size()==2){
			if(cnt.begin()->second==1)ins=cnt.rbegin()->first;
			else ins=cnt.begin()->first;
		}
		if(~ins)have.insert(ins);
		if(can_put(*have.begin())){
			ans.push_back(*have.begin());
			del(*have.begin());
		}else{
			ans.push_back(*next(have.begin()));
			del(*next(have.begin()));
		}
		if(have.count(ins))have.erase(ins);
		cur--;
	}
//	for(auto x:ans)cerr<<x<<" ";cerr<<endl;
	const int sz=min(n,5);
	const int base=max(0,n-5);
	for(int i=1;i<=sz;i++)perm[i]=i;
	vector<int>vec(have.begin(),have.end());
	for(int i=1;i<=n;i++)for(int j=pos[i]+1;j<out[i].size();j++)vec.push_back(out[i][j]);
	vector<int>pre=ans;for(int i=1;i<=sz;i++)ans.push_back(n+1);
	do{
		vector<int>tmp=pre;
		for(int i=1;i<=sz;i++)tmp.push_back(vec[perm[i]-1]);
		if(check(tmp))ans=min(ans,tmp);
	}while(next_permutation(perm+1,perm+sz+1));
	if(ans.back()==n+1){
		cout<<-1<<endl;
		return 0;
	}
	for(auto x:ans)cout<<x<<" ";cout<<endl;
	return 0;
}

Compiler output:
a.cc: In function 'void del(int)':
a.cc:43:9: error: 'used' was not declared in this scope
   43 |         used[x]=1;cnt[ban[x]]--;if(!cnt[ban[x]])cnt.erase(ban[x]);
      |         ^~~~
a.cc: In function 'int main()':
a.cc:60:16: error: 'cur' was not declared in this scope
   60 |         cin>>n;cur=n;
      |                ^~~