import collections N,K = input().split() N = int(N) if len(K)>N: print(-1) exit() K = str(K).zfill(N) A = [0] + list(map(int, input().split())) ANS = collections.deque() for i in range(N): v = int(K[i]) if A[v]>0: ANS.append(v) A[v]-=1 else: #より大きい値がある for j in range(int(K[i])+1,10): if A[j]>0: ANS.append(j) A[j]-=1 for k in range(len(ANS),N): for j in range(10): if A[j]>0: ANS.append(j) A[j]-=1 print(''.join(map(str, ANS))) exit() #ないから遡る while len(ANS)>0: v = ANS.pop() A[v]+=1 for j in range(v+1,10): if A[j]>0: ANS.append(j) A[j]-=1 for k in range(len(ANS),N): for j in range(10): if A[j]>0: ANS.append(j) A[j]-=1 break print(''.join(map(str, ANS))) exit() print(-1) exit() while len(ANS)>0: v = ANS.pop() A[v]+=1 for j in range(v+1,10): if A[j]>0: ANS.append(j) A[j]-=1 for k in range(len(ANS),N): for j in range(10): if A[j]>0: ANS.append(j) A[j]-=1 break print(''.join(map(str, ANS))) exit() print(-1) exit()