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(); c.insert(0, 0); if k.len() < n { println!("{}", (0..10).map(|i| i.to_string().repeat(c[i])).collect::>().join("")); return; } let maxval = (0..10).rev().map(|i| i.to_string().repeat(c[i])).collect::>().join(""); if k.len() > n || k.iter().map(|&c| c.to_string()).collect::>().join("") >= maxval { println!("-1"); return; } 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 { idx -= 1; c[result.pop().unwrap()] += 1; } loop { let mut flg = false; for i in k[idx]+1..10 { if c[i] > 0 { result.push(i); idx += 1; c[i] -= 1; flg = true; break; } } if flg { break; } else { c[result.pop().unwrap()] += 1; idx -= 1; } } for i in 0..10 { for _ in 0..c[i] { result.push(i); } } println!("{}", result.iter().map(|v| v.to_string()).collect::>().join("")); }