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: Runtime Error)

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>
using namespace std;
int main() {
	int K;
	scanf("%d", &K);
	int a[K];
	for (int i = 0; i < K; ++i) scanf("%d", &a[i]);
	int mn = *min_element(a, a + K), mx = *max_element(a, a + K);
	if (mx <= 2 * mn) {
      printf("-1");
      return 0;
		vector<int> yay[mn];
		int ptr = 0, sfx[K];
		for (int i = 0; i < K; ++i) {
			a[i] -= mn;
			sfx[i] = mn - a[i];
		}
		for (int i = K - 2; i >= 0; --i) sfx[i] = min(sfx[i], sfx[i + 1]);
		for (int i = 0; i < K; ++i) {
			if (ptr >= sfx[i]) {
				ptr = sfx[i];
				for (int j = i; j < K; ++j) {
					if (ptr < sfx[j]) ptr = sfx[j];
					for (int k = ptr; k < ptr + a[j]; ++k) yay[k].push_back(j);
				}
				break;
			} else {
				for (int j = ptr; j < ptr + a[i]; ++j) yay[j].push_back(i);
				ptr = ptr + max(0, a[i] - 1);
				if (i > 0 && a[i] == 0 && a[i - 1] != 0) ++ptr;
			}
		}
		for (int i = 0; i < mn; ++i) {
			for (int j: yay[i]) printf("%d ", j + 1);
			for (int j = 0; j < K; ++j) {
				vector<int>::iterator it = lower_bound(yay[i].begin(), yay[i].end(), j);
				if (it == yay[i].end() || *it != j) printf("%d ", j + 1);
			}
			for (int j: yay[i]) printf("%d ", j + 1);
		}
	} else assert(false);
	return 0;
}