import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N,K = MI() C = [0]+LI() str_K = [0]+[int(s) for s in str(K)] digit_K = len(str_K)-1 if N < digit_K: print(-1) exit() if N > digit_K: ANS = [0]*N idx = 0 for i in range(1,10): c = C[i] s = str(i) for j in range(c): ANS[idx+j] = s idx += c ans = ''.join(ANS) print(ans) exit() S_count_K = [[0]*10 for _ in range(N+1)] for i in range(1,N+1): for j in range(10): S_count_K[i][j] += S_count_K[i-1][j] k = str_K[i] S_count_K[i][k] += 1 for i in range(N,0,-1): if S_count_K[i-1][0]: continue for j in range(1,10): if S_count_K[i-1][j] > C[j]: break else: x = str_K[i] for k in range(x+1,10): if C[k]-S_count_K[i-1][k]: C[k] -= 1 ANS = str_K[1:i]+[k] for l in range(1,10): c = C[l]-S_count_K[i-1][l] for _ in range(c): ANS.append(l) str_ANS = [str(a) for a in ANS] ans = ''.join(str_ANS) print(ans) exit() print(-1)