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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
int A[101],B[101];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>K;
	FOR(i,K) cin>>A[i], B[i]=A[i];
	vector<int> V;
	while(*max_element(A,A+K)) {
		x=*max_element(A,A+K);
		y=*min_element(A,A+K);
		vector<pair<int,int>> C;
		
		
		if(x==2*y) {
			FOR(i,K) {
				if(A[i]==x) {
					C.push_back({i,1});
				}
				else if(A[i]>y) {
					C.push_back({i,0});
				}
			}
			
			while(C.size() && C.back().second==0) C.pop_back();
			int first[101]={};
			FORR(c,C) {
				first[c.first]=1;
				V.push_back(c.first);
				A[c.first]--;
			}
			FOR(i,K) if(first[i]==0) V.push_back(i), A[i]--;
			FORR(c,C) {
				V.push_back(c.first);
				A[c.first]--;
			}
		}
		else if(x==y) {
			FOR(i,K) V.push_back(i), A[i]--;
		}
		else {
			FOR(i,K) {
				if(A[i]==x) {
					V.push_back(i);
					A[i]--;
				}
				else {
					break;
				}
			}
			FOR(j,K) V.push_back((i+j)%K), A[(i+j)%K]--;
		}
	}
	
	FORR(v,V) cout<<(v+1)<<" ";
	cout<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}