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 for i in range(n): if flag: if c[int(k[i])-1]==0: flag=False flag2=True for j in range(int(k[i]),9): if flag2 and c[j]>0: flag2=False tmp=k[:i]+str(j+1) for h in range(9): if j==h: tmp+=str(h+1)*(c[h]-1) else: tmp+=str(h+1)*c[h] if flag2==False: ans=tmp c[int(k[i])-1]-=1 #print(ans,i,c) if k[i]=='0': flag=False print(ans)