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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::{fastout, input};
use std::cmp;

fn make_seq(v: &Vec<usize>) -> Vec<usize> {
    let n = v.len();
    let mut u: Vec<usize> = Vec::new();
    let mut mx: usize = v[0];
    let mut mn: usize = v[0];
    for i in 1..n {
        mx = cmp::max(mx, v[i]);
        mn = cmp::min(mn, v[i]);
    }
    if mx < mn * 2 {
        for i in 1..=n {
            u.push(i);
        }
    } else {
        let mut j: usize = 0;
        for i in 1..=n {
            if v[i - 1] == mx {
                j = i;
            }
        }
        for i in 1..=n {
            if v[i - 1] != mn {
                u.push(i);
            }
            if i == j {
                break;
            }
        }
        for i in 1..=n {
            if v[i - 1] == mn {
                u.push(i);
            }
            if i == j {
                break;
            }
        }
        if j < n {
            for i in (j + 1)..=n {
                u.push(i);
            }
        }
    }
    u
}

fn ok(v: &Vec<usize>) -> bool {
    let n = v.len();
    let mut mx: usize = v[0];
    let mut mn: usize = v[0];
    for i in 1..n {
        mx = cmp::max(mx, v[i]);
        mn = cmp::min(mn, v[i]);
    }
    if mx > 2 * mn {
        false
    } else {
        true
    }
}

#[fastout]
fn main() {
    input! {
        k: usize,
        mut a: [usize; k],
    }

    //可能かどうかの判定
    let mut mx: usize = a[0];
    let mut mn: usize = a[0];

    for i in 1..k {
        mx = cmp::max(mx, a[i]);
        mn = cmp::min(mn, a[i]);
    }

    //不可能な場合を除外
    if mx > 2 * mn {
        println!("{}", -1);
        return;
    }

    let mut all: usize = 0;
    for i in 0..k {
        all += a[i];
    }

    let mut ans: Vec<usize> = Vec::with_capacity(1010);

    let mut num: usize = 0;

    loop {
        if ans.len() == 0 {
            let mut now: Vec<usize> = make_seq(&a);
            ans.append(&mut now);
            for i in 0..k {
                a[i] -= 1;
            }
            num += k;
        } else {
            let n: usize = ans.len();
            let mut v: Vec<Vec<usize>> = Vec::new();
            let mut flag: bool;
            for i in 0..k {
                let mut v_tmp: Vec<usize> = Vec::with_capacity(k);
                let mut c = a.clone();
                flag = false;
                for j in 0..=i {
                    v_tmp.push(ans[n - k + j]);
                    if a[ans[n - k + j] - 1] == 0 {
                        flag = true;
                        break;
                    }
                    c[ans[n - k + j] - 1] -= 1;
                }
                if flag {
                    continue;
                }
                if !ok(&c) {
                    continue;
                }
                v_tmp.sort();
                v.push(v_tmp);
            }
            if ok(&a) {
                let now: Vec<usize> = make_seq(&a);
                v.push(now);
            }
            v.sort();
            let mut w = v[0].clone();
            ans.append(&mut w);
            let m: usize = v[0].len();
            for i in 0..m {
                a[v[0][i] - 1] -= 1;
            }
            num += m;
        }

        
        if all == num {
            break;
        }
    }

    for i in 0..(all - 1) {
        print!("{} ", ans[i]);
    }
    println!("{}", ans[all - 1]);
}