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