Problem:
Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1

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

#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) {for(auto x:obj)cout<<x<<' ';cout<<endl;}
#define line "----------"
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

vector<int> a;

int shorter(int n){
	if(n>a.size())return 0;
	int res = 0;
	for(int i = n-1; i<a.size(); ++i){
		if(a[i] <= n) ++res;
	}
	return res;
}

int main(){
	int N; cin>>N;
	vector<int> b(N);
	rep(i,N)cin>>b[i];
	// is possible
	bool ok = true;
	rep(i,N)if(b[i] > i+1)ok = false;
	if(!ok){cout<<-1<<endl; return 0;}

	//way
	vector<int> need(N,0);
	rep(i,N){
		int cnt = 0;
		for(int j = i+1; j<N; ++j){
			if(b[j]<=b[i]) ++cnt;
		}
		need[i] = cnt;
	}
	map<int,int> rest;
	vector<int> ans;
	a.push_back(1);
	ans.push_back(1);
	for(int i = N-1; i>0; --i){
		if(b[i] <= a.size()+1 && need[i] <= shorter(b[i])){
			a.insert(a.begin()+b[i]-1, b[i]);
			ans.push_back(b[i]);
		}else{
			rest[rest.size()]  = i;
		}
		for(auto ele : rest){
			int x = ele.second;
			if(b[x] <= a.size()+1 && need[x] <= shorter(b[x])){
				a.insert(a.begin()+b[x]-1,b[x]);
				ans.push_back(b[x]);
				rest.erase(ele.first);
			}
		}
	}
	while(ans.size() != N){
		for(auto ele : rest){
			int x = ele.second;
			if(b[x] <= a.size()+1 && need[x] <= shorter(b[x])){
				a.insert(a.begin()+b[x]-1,b[x]);
				ans.push_back(b[x]);
				rest.erase(ele.first);
			}
		}
	}
	show(ans);
	return 0;
}

// vector<int> a;
// vector<int> ans;

// int shorter(int n){
// 	if(n>a.size())return 0;
// 	int res = 0;
// 	for(int i = n-1; i<a.size(); ++i){
// 		if(a[i] <= n) ++res;
// 	}
// 	return res;
// }

// int solve(int N){
// 	vector<int> b(N);
// 	srand(time(nullptr));
// 	rep(i,N){
// 		b[i] = rand()%(i+1)+1;
// 	}
// 	cout<<line<<endl;
// 	show(b);
// 	cout<<line<<endl;
// 	// is possible
// 	bool ok = true;
// 	rep(i,N)if(b[i] > i+1)ok = false;
// 	if(!ok){cout<<-1<<endl; return 0;}

// 	//way
// 	vector<int> need(N,0);
// 	rep(i,N){
// 		int cnt = 0;
// 		for(int j = i+1; j<N; ++j){
// 			if(b[j]<=b[i]) ++cnt;
// 		}
// 		need[i] = cnt;
// 	}
// 	map<int,int> rest;
// 	a.push_back(1);
// 	ans.push_back(1);
// 	for(int i = N-1; i>0; --i){
// 		if(b[i] <= a.size()+1 && need[i] <= shorter(b[i])){
// 			a.insert(a.begin()+b[i]-1, b[i]);
// 			ans.push_back(b[i]);
// 		}else{
// 			rest[rest.size()]  = i;
// 		}
// 		for(auto ele : rest){
// 			int x = ele.second;
// 			if(b[x] <= a.size()+1 && need[x] <= shorter(b[x])){
// 				a.insert(a.begin()+b[x]-1,b[x]);
// 				ans.push_back(b[x]);
// 				rest.erase(ele.first);
// 			}
// 		}
// 		cout<<line<<endl;
// 		show(a);
// 		cout<<line<<endl;
// 	}
// 	while(ans.size() != N){
// 		for(auto ele : rest){
// 			int x = ele.second;
// 			if(b[x] <= a.size()+1 && need[x] <= shorter(b[x])){
// 				a.insert(a.begin()+b[x]-1,b[x]);
// 				ans.push_back(b[x]);
// 				rest.erase(ele.first);
// 			}
// 		}
// 	}
// 	cout<<"ANS -> "; show(ans);
// 	cout<<"solve_end"<<endl<<endl;
// 	return 0;
// }

// int main(){
// 	int N; cin>>N;
// 	solve(N);
// 	vector<int> p;
// 	rep(i,N){
// 		int t = ans[i];
// 		p.insert(p.begin()+(t-1),t);
// 	}
// 	show(p);
// }

// int main(){
// 	int N; cin>>N;
// 	vector<int> b(N);
// 	rep(i,N)cin>>b[i];
// 	bool ok = true;
// 	rep(i,N)if(b[i] > i+1)ok = false;
// 	if(!ok){cout<<-1<<endl; return 0;}
// 	vector<int> need(N,0);
// 	rep(i,N){
// 		int cnt = 0;
// 		for(int j = i+1; j<N; ++j){
// 			if(b[i] >= b[j])++cnt;
// 		}
// 		need[i] = cnt;
// 	}
// 	stack<int> Q;
// 	vector<int> a;
// 	vector<int> ans;
// 	ans.push_back(1);
// 	a.push_back(1);
// 	for(int i = N-1; i>0; --i){
// 		// cout<<line<<endl;
// 		if((a.size() >= b[i]-1) && (a.size()-(b[i]-1) >= need[i])){
// 			a.insert(a.begin()+b[i]-1, b[i]);
// 			ans.push_back(b[i]);
// 		}else{
// 			// cout<<"in with "<<i<<endl;
// 			Q.push(i);
// 		}
// 		// cout<<Q.top()<<endl;
// 		if(!Q.empty()){
// 			int t = Q.top();
// 			if(a.size() >= b[t] && a.size()-(b[t]-1) >= need[t]){
// 				a.insert(a.begin()+b[t]-1, b[t]);
// 				ans.push_back(b[t]);
// 				Q.pop();
// 			}
// 		}
// 		// show(a);
// 	}
// 	while(!Q.empty()){
// 		int t = Q.top();
// 		Q.pop();
// 		a.insert(a.begin()+b[t]-1, b[t]);
// 		ans.push_back(b[t]);
// 	}
// 	show(ans);
// 	return 0;
// }