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