Problem:
Score : 1500 points

Problem Statement

Given are an integer K and integers a_1,\dots, a_K. Determine whether a sequence P satisfying below exists. If it exists, find the lexicographically smallest such sequence.

Every term in P is an integer between 1 and K (inclusive).

For each i=1,\dots, K, P contains a_i occurrences of i.

For each term in P, there is a contiguous subsequence of length K that contains that term and is a permutation of 1,\dots, K.

Constraints

1 \leq K \leq 100

1 \leq a_i \leq 1000 \quad (1\leq i\leq K)

a_1 + \dots + a_K\leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K
a_1 a_2 \dots a_K

Output

If there is no sequence satisfying the conditions, print -1.
Otherwise, print the lexicographically smallest sequence satisfying the conditions.

Sample Input 1

3
2 4 3

Sample Output 1

2 1 3 2 2 3 1 2 3

For example, the fifth term, which is 2, is in the subsequence (2, 3, 1) composed of the fifth, sixth, and seventh terms.

Sample Input 2

4
3 2 3 2

Sample Output 2

1 2 3 4 1 3 1 2 4 3

Sample Input 3

5
3 1 4 1 5

Sample Output 3

-1

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
const int maxn = 100 + 5;
const int mod = 998244353; 

int k;
int a[maxn], p[1000 + 10], q[maxn];
int last[maxn];

bool check(int sz){
	int mnm = *min_element(a+1,a+k+1), mxm = *max_element(a+1,a+k+1);
	if (mxm < 2*mnm+1)
		return true;
	if (mxm > 2*mnm+1)
		return false;
	for (int i = sz; i >= sz-k+1; i--)
		last[p[i]] = i;
	int m1 = sz, m2 = sz-k+1;
	for (int i = 1; i <= k; i++){
		if (a[i] == mnm)
			m1 = min(m1, last[i]);
		if (a[i] == mxm)
			m2 = max(m2, last[i]);
	}
	return m2 < m1;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> k;
	int sum = 0;
	for (int i = 1; i <= k; i++){
		cin >> a[i];
		sum += a[i];
	}
	if (*max_element(a+1,a+k+1) > 2 * (*min_element(a+1,a+k+1)))
		return cout << -1 << endl, 0;
	int sz = 0;
	for (int i = 1; i <= k; i++)
		p[++sz] = i;
	if (*max_element(a+1,a+k+1) == 2 * (*min_element(a+1,a+k+1))){
		sz = 0;
		int idx = 0;
		for (int i = 1; i <= k; i++)
			if (a[i] == *max_element(a+1,a+k+1))
				idx = i;
		for (int i = 1; i <= idx; i++)
			if (a[i] != *min_element(a+1,a+k+1))
				p[++sz] = i;
		for (int i = 1; i <= k; i++)
			if (i > idx or a[i] == *min_element(a+1,a+k+1))
				p[++sz] = i;
	}
	for (int i = 1; i <= k; i++)
		a[i] --;
	while (sz < sum){
		for (int i = sz-k+1; i <= sz; i++)
			q[i-(sz-k)] = p[i];
		vector<int> then;
		for (int i = 1; i <= k; i++){
			vector<int> A;
			for (int j = 1; j <= i; j++){
				a[q[j]] --;
				A.push_back(q[j]);
			}
			sort(A.begin(), A.end());
			int now = sz;
			int mnm = *min_element(a+1,a+k+1), mxm = *max_element(a+1,a+k+1);
			vector<int> me;
			if (2*mnm+1 == mxm){
				int idx = -1;
				for (int j = 0; j < i; j++)
					if (a[A[j]] == mxm)
						idx = j;
				for (int j = 0; j <= idx; j++){
					if (a[A[j]] == mnm)
						continue;
					p[++now] = A[j];
					me.push_back(A[j]);
				}
				for (int j = 0; j < i; j++)
					if (j > idx or a[A[j]] == mnm)
						p[++now] = A[j], me.push_back(A[j]);
			}
			else
				me = A;
			if (check(now) and (then.empty() or me < then))
				then = me;
			for (int j = 1; j <= i; j++)
				a[q[j]] ++;
		}
		for (auto it : then){
			p[++sz] = it;
			a[it] --;
		}
	}
	for (int i = 1; i <= sz; i++)
		cout << p[i] << " \n"[i == sz];
}