using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = ReadLine().Split(); var n = int.Parse(c[0]); var k = c[1]; var a = NList; if (k.Length > n) { WriteLine(-1); return; } var min = new List(n); for (var i = 0; i < 9; ++i) for (var j = 0; j < a[i]; ++j) min.Add((char)(i + '1')); if (IsLarge(min, k)) { WriteLine(string.Concat(min)); return; } var max = new List(n); for (var i = 8; i >= 0; --i) for (var j = 0; j < a[i]; ++j) max.Add((char)(i + '1')); if (!IsLarge(max, k)) { WriteLine(-1); return; } var ok = 0; var ng = n; while (ng - ok > 1) { var count = (int[]) a.Clone(); var mid = (ok + ng) / 2; var flg = true; for (var i = 0; i < mid; ++i) { if (k[i] == '0' || count[k[i] - '1'] == 0) { flg = false; break; } --count[k[i] - '1']; } if (flg) { flg = false; for (var j = k[mid] - '0'; j < 9; ++j) if (count[j] > 0) { flg = true; break; } } if (flg) ok = mid; else ng = mid; } var ans = new List(); for (var i = 0; i < ok; ++i) { ans.Add(k[i]); --a[k[i] - '1']; } for (var i = k[ok] - '0'; i < 9; ++i) if (a[i] > 0) { ans.Add((char)(i + '1')); --a[i]; break; } for (var i = 0; i < 9; ++i) for (var j = 0; j < a[i]; ++j) ans.Add((char)(i + '1')); WriteLine(string.Concat(ans)); } static bool IsLarge(List m, string k) { if (m.Count > k.Length) return true; for (var i = 0; i < m.Count; ++i) { if (m[i] > k[i]) return true; if (m[i] < k[i]) return false; } return false; } }