import copy def main(): N,K = map(int,input().split()) K = list(str(K)) K = [int(k) for k in K] if len(K) < N: t = N - len(K) K = [0 for _ in range(t)] + K #print(K) C = [0] + list(map(int,input().split())) #dp[i][j]:i桁目まで見て0/1完全に一致/より大の時の[値、残りのリスト] dp = [[],[]] val = -1 if C[K[0]] >= 1: NC = copy.copy(C) NC[K[0]] -= 1 dp[0] = [str(K[0]), NC] else: dp[0] = ["-1",[-1]*10] Flag = 0 for i in range(K[0]+1,10): if C[i] >= 1: Flag = 1 NC = copy.copy(C) NC[i] -= 1 dp[1] = [str(i), NC] break if Flag == 0: dp[1] = ["-1",[-1]*10] #print(dp) for i in range(1,N): #p = [[],[]] #p,dp = dp,p nxt = K[i] pre_val = dp[0][0] pre1 = copy.copy(dp[0][1]) if dp[0][1][nxt] >= 1: dp[0][0] = pre_val + str(nxt) dp[0][1][nxt] -= 1 #一個減らす else: dp[0] = ["-1",[-1]*10] Flag = 0 pre2_val = dp[1][0] pre2 = copy.copy(dp[1][1]) for j in range(nxt+1,10): if pre1[j] >= 1: Flag = 1 pre1[j] -= 1 dp[1][0] = pre_val + str(j) dp[1][1] = pre1 break if Flag == 0: #print("2",pre2) for j in range(1,10): if pre2[j] >= 1: Flag = 1 pre2[j] -= 1 dp[1][0] = pre2_val + str(j) dp[1][1] = pre2 break if Flag == 0: dp[1] = ["-1",[-1]*10] #print(dp) ans = dp[1][0] print(ans) if __name__ == '__main__': main()