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 <vector>
#include <array>
#include <list>
#include <set>
#include <map>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>

using namespace std;

void erase(multimap<int, int>& mm, int i, int j)
{
	auto it = mm.find(i);
	while (it->second != j) {
		it++;
	}
	mm.erase(it);
}

bool count_sides(multimap<int, int>& rel)
{
	int count = 0;
	int prev  = 0;
	auto it = rel.begin();
	while (it != rel.end()) {
		if (prev != it->first) {
			if (count & 1) return false;
			prev = it->first;
			count = 0;
		}
		count++;
		it++;
	}
	if (count & 1) return false;

	return true;
}

bool search(multimap<int, int>& rel, int n)
{
	int  count  = 0;
	int  g_num  = 1;
	int  ind_c  = 1;
	bool is_ind = true;

	int p = rel.begin()->first;

	vector<int> nexts(n + 1);
	vector<int> prevs(n + 1);
	vector<int> gnums(n + 1);

	while (!rel.empty()) {
		auto it = rel.find(p);
		if (it == rel.end()) {
			prevs[p] = 0;

			p = rel.begin()->first;
			if (gnums[p] == 0) {
				g_num++;
				ind_c++;
			}

			count++;
			is_ind = true;
			continue;
		}


		while (it->first == p && it->second == prevs[p]) it++;

		int n_p = it->second;

		nexts[p]   = n_p;

		if (gnums[n_p] != 0 && gnums[n_p] != gnums[p]) {
			is_ind = false;
			ind_c--;
		}

		gnums[p] = g_num;
		gnums[n_p] = g_num;

		if (nexts[n_p] != 0) {
			count++;

			int cur_p  = n_p;
			int cur_np = nexts[n_p];
			while (cur_np != n_p) {
				nexts[cur_p] = 0;
				prevs[cur_np] = 0;

				erase(rel, cur_p, cur_np);
				erase(rel, cur_np, cur_p);

				cur_p  = cur_np;
				cur_np = nexts[cur_p];
			}
			erase(rel, cur_p, cur_np);
			erase(rel, cur_np, cur_p);
			nexts[cur_p] = 0;

			p     = n_p;
		}
		else {
			prevs[n_p] = p;
			p     = n_p;
		}
	}

	cout << "final " << count << " " << ind_c << endl;
	if (count >= 3 && ind_c <= 3) return true;

	return false;
}

int main()
{
	int n, m; cin >> n >> m;

	multimap<int, int> rel;
	for (int i = 0; i < m; i++) {
		int a, b; cin >> a; cin >> b;

		rel.insert(make_pair(a, b));
		rel.insert(make_pair(b, a));
	}

	bool ok = count_sides(rel);

	if (ok) {
		ok = search(rel, n);
	}
	if (ok) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;
}