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: Accepted)

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 mx: usize = vecmax(&v);
    let mn: usize = vecmin(&v);
    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 mx: usize = vecmax(&v);
    let mn: usize = vecmin(&v);
    if mx > 2 * mn {
        false
    } else {
        true
    }
}

fn vecmax(v: &Vec<usize>) -> usize {
    let n = v.len();
    let mut mx: usize = v[0];
    for i in 1..n {
        mx = cmp::max(mx, v[i]);
    }
    mx
}

fn vecmin(v: &Vec<usize>) -> usize {
    let n = v.len();
    let mut mn: usize = v[0];
    for i in 1..n {
        mn = cmp::min(mn, v[i]);
    }
    mn
}

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

    //可能かどうかの判定
    let mx: usize = vecmax(&a);
    let mn: usize = vecmin(&a);

    //不可能な場合を除外
    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(all);

    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::with_capacity(k + 1);
            let mut flag: bool;
            for i in 0..(k - 1) {
                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) {
                    v_tmp.sort();
                    v.push(v_tmp);
                } else {
                    //この場合もうまくいくかもしれない
                    let cmx: usize = vecmax(&c);
                    let cmn: usize = vecmin(&c);

                    if cmx == cmn * 2 + 1 {
                        let mut v_tmp2: Vec<usize> = Vec::with_capacity(k);
                        for j in (i + 1)..k {
                            v_tmp2.push(ans[n - k + j]);
                        }
                        let mut j: usize = 0;
                        for i in 1..=k {
                            if c[i - 1] == cmx {
                                j = i;
                            }
                        }
                        for i in 1..=k {
                            if c[i - 1] != cmn && v_tmp.contains(&i) {
                                v_tmp2.push(i);
                            }
                            if i == j {
                                break;
                            }
                        }
                        for i in 1..=k {
                            if c[i - 1] == cmn && v_tmp.contains(&i) {
                                v_tmp2.push(i);
                            }
                            if i == j {
                                break;
                            }
                        }
                        if j < k {
                            for i in (j + 1)..=k {
                                if v_tmp.contains(&i) {
                                    v_tmp2.push(i);
                                }
                            }
                        }
                        let mut lastcmx: usize = 0;
                        let mut firstcmn: usize = 0;
                        for i in 0..k {
                            if c[v_tmp2[i] - 1] == cmx {
                                lastcmx = i;
                            }
                        }
                        for i in 0..k {
                            if c[v_tmp2[i] - 1] == cmn {
                                firstcmn = i;
                                break;
                            }
                        }
                        if lastcmx < firstcmn {
                            for _ in (i + 1)..k {
                                v_tmp2.remove(0);
                            }
                            v.push(v_tmp2);
                        }
                    }
                }
            }
            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]);
}