N,K=input().split() C=list(map(int,input().split())) ANS="" for i in range(9): ANS+=str(i+1)*C[i] if (ANS>K and len(ANS)==len(K)) or len(ANS)>len(K): print(ANS) exit() if (ANS[::-1]<=K and len(ANS)==len(K)) or len(ANS)0 and i!=len(S)-1: ANS[i]=x C[x-1]-=1 else: for j in range(x,9): if C[j]>0: C[j]-=1 ANS[i]=j+1 break else: for j in range(i-1,-1,-1): C[ANS[j]-1]+=1 ANS[j]=-1 x=int(S[j]) flag=0 for k in range(x,9): if C[k]>0: C[k]-=1 ANS[j]=k+1 flag=1 break if flag: break break #print(ANS,C) for i in range(len(S)): if ANS[i]==-1: for j in range(9): if C[j]>0: C[j]-=1 ANS[i]=j+1 break print("".join(map(str,ANS)))