def solve(N: int, K: str, C: list[int]) -> str: if len(K) > N: return "-1" if len(K) < N: # 一番小さくなるように作る res: str = "" for i, c in enumerate(C, 1): res += str(i) * c return res # i桁目より上の判定 max_i = len(K) - 1 D = C.copy() for i in range(len(K) - 1): k = int(K[i]) if k == 0: max_i = i break if D[k - 1] <= 0: max_i = i break D[k - 1] -= 1 # print(max_i, D) for i in range(max_i, 0, -1): # i桁目はK[i]より大きく、i桁目より上はKと同じ、i桁目より下は最小 used = D.copy() # print(i, used) for n in range(int(K[i]) + 1, 10): # i桁目の判定 if used[n - 1] <= 0: continue used[n - 1] -= 1 res = K[:i] + str(n) # i桁目より下の処理 for i, c in enumerate(used, 1): res += str(i) * c return res assert int(K[i]) > 0 D[int(K[i - 1]) - 1] += 1 return "-1" N, K = input().split() C = list(map(int, input().split())) print(solve(int(N), K, C))