import sys input = sys.stdin.readline def check(i, d): s = 0 for x in range(10)[::-1]: s += c[x] if x == d: s -= 1 if cnt[i+1][x] > s: return False if cnt[i+1][x] < s: return True return False N, K = input().rstrip().split() N = int(N) K = '0' * (N - len(K)) + K c = [0] + list(map(int, input().split())) ans = [] cnt = [[0] * 10 for _ in range(N+1)] for i in range(N)[::-1]: for d in range(10): if int(K[i]) >= d: cnt[i][d] = cnt[i+1][d] + 1 else: cnt[i][d] = 0 for i in range(N): d = int(K[i]) ok = False if c[d] > 0 and check(i, d): ans.append(d) c[d] -= 1 else: d += 1 while d < 10 and c[d] == 0: d += 1 if d == 10: print(-1) exit() ans.append(d) c[d] -= 1 break for d in range(10): for _ in range(c[d]): ans.append(d) print(''.join(map(str, ans)))