Problem:
Score : 800 points

Problem Statement

Niwango has N cards, numbered 1,2,\ldots,N.
He will now arrange these cards in a row.

Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below.
To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.

To the immediate right of Card 1 (if any) is NOT Card a_1.

To the immediate right of Card 2 (if any) is NOT Card a_2.

\vdots

To the immediate right of Card N (if any) is NOT Card a_N.

Constraints

2 \leq N \leq 10^{5}

1 \leq a_i \leq N

a_i \neq i

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

If no arrangements satisfy the conditions, print -1. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:

b_1 b_2 \ldots b_N

Here, b_i represents the i-th card from the left.

Sample Input 1

4
2 3 4 1

Sample Output 1

1 3 2 4

The arrangement (1,2,3,4) is lexicographically smaller than (1,3,2,4), but is invalid, since it violates the condition "to the immediate right of Card 1 is not Card 2."

Sample Input 2

2
2 1

Sample Output 2

-1

If no arrangements satisfy the conditions, print -1.

Sample Input 3

13
2 3 4 5 6 7 8 9 10 11 12 13 12

Sample Output 3

1 3 2 4 6 5 7 9 8 10 12 11 13

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)
#define print(x,n) for(int i_ = 0;i_ < n;i_++) printf("%lld%c",x[i_]," \n"[i_ == n - 1])

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }

int n,a[100010],ans[100010];

signed main(){
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> a[i]; a[i]--;
	}
	if(n == 2){
		if(a[0] == 1 && a[1] == 0) cout << -1 << endl;
		else if(a[0] == 1) cout << "2 1" << endl;
		else cout << "1 2" << endl;
		return 0;
	}
	int cnt[100010] = {};
	priority_queue<P> que;
	for(int i = 0;i < n;i++){
		if(i != a[i]) cnt[a[i]]++;
	}
	for(int i = 0;i < n;i++){
		que.emplace(cnt[i],i);
	}
	int prv = -1;
	set<int> unused;
	for(int i = 0;i < n;i++) unused.insert(i);
	for(int i = 0;i < n;i++){
		while(!unused.count(que.top().second) || cnt[que.top().second] != que.top().first) que.pop();
		P p = que.top();
		if(p.first == n - i - 1){
			ans[i] = p.second;
		}else{
			auto it = unused.begin();
			if(*it == prv) it++;
			ans[i] = *it;
		}
		prv = a[ans[i]];
		if(ans[i] != a[ans[i]]) cnt[a[ans[i]]]--;
		que.emplace(cnt[a[ans[i]]],a[ans[i]]);
		unused.erase(ans[i]);
	}
	for(int i = 0;i < n;i++) ans[i]++;
	print(ans,n);
}