fn main() { let mut nk = String::new(); std::io::stdin().read_line(&mut nk).ok(); let nk: Vec<&str> = nk.trim().split_whitespace().collect(); let n = nk[0].parse::().unwrap(); let k = nk[1].chars().map(|c| c as usize - '0' as usize).collect::>(); let mut c = String::new(); std::io::stdin().read_line(&mut c).ok(); let mut c: Vec = c.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); if k.len() < n { println!("{}", (0..9).map(|i| (i+1).to_string().repeat(c[i])).collect::>().join("")); return; } let maxval = (0..9).rev().map(|i| (i+1).to_string().repeat(c[i])).collect::>().join(""); if k.len() > n || maxval <= k.iter().map(|i| i.to_string()).collect::>().join("") { println!("-1"); return; } c.insert(0, 0); let mut result = vec![]; let mut idx = 0usize; while idx < n && c[k[idx]] > 0 { result.push(k[idx]); c[k[idx]] -= 1; idx += 1; } if idx == n { c[result.pop().unwrap()] += 1; idx -= 1; } loop { let mut flg = false; for i in k[idx]+1..10 { if c[i] > 0 { result.push(i); c[i] -= 1; idx += 1; flg = true; break; } } if flg { break; } else { c[result.pop().unwrap()] += 1; idx -= 1; } } (0..10).for_each(|i| (0..c[i]).for_each(|_| result.push(i)) ); println!("{}", result.iter().map(|i| i.to_string()).collect::>().join("")); }