n,k=map(str,input().split()) n=int(n) c=list(map(int,input().split())) ans='-1' if len(k)>n: print(-1) exit() x='' for i in range(9): x+=str(i+1)*c[i] if len(k)int(x[i]): flag=False flag=True d=c.copy() for i in range(n): if flag: if k[i]=='0': flag=False elif c[int(k[i])-1]==0: flag=False flag2=True for j in range(int(k[i]),9): if flag2 and c[j]>0: memo=i memo2=j flag2=False c[int(k[i])-1]-=1 #print(ans,i,c) tmp=k[:memo]+str(memo2+1) for i in range(memo): d[int(k[i])-1]-=1 d[memo2]-=1 for h in range(9): if memo2==h: tmp+=str(h+1)*(d[h]-1) else: tmp+=str(h+1)*d[h] print(tmp)