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 = S().split() N = int(N) C = [0]+LI() digit_K = len(K) # Kの桁数 int_K = [0]+[int(s) for s in K] if N < digit_K: print(-1) exit() if N > digit_K: ANS = ['']*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 = int_K[i] S_count_K[i][k] += 1 for i in range(N,0,-1): # Kと上i-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 = int_K[i] for k in range(x+1,10): if C[k] > S_count_K[i-1][k]: C[k] -= 1 ANS = int_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) ans = ''.join([str(a) for a in ANS]) print(ans) exit() print(-1)