import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [6,2,5,5,4,5,6,3,7,6] # B[i]がiを作るのに必要なマッチ棒の数

# dp[i]: i本マッチ使って作れるMAX桁数。。
dp = [-1 for _ in range(N+10)]
dp[0] = 0
A.sort()
visited = [False for _ in range(N+10)]
h = []
heapq.heappush(h, (0,0))

while h:
  now_n, keta = heapq.heappop(h)
  if visited[now_n]:
    continue
  visited[now_n] = True
  for a in A:
    nxt_n = now_n + B[a]
    if nxt_n >= N + 10:
      continue
    dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
    heapq.heappush(h, (nxt_n, dp[now_n] + 1))
    
#print(dp)

A.sort(reverse=True)
# 上の桁から決めていく。dp[N]
ANS = []
zan = N
for i in range(dp[N],0,-1):
  bugf = True
  # sorted(a) 大きい方から試す
  for a in A:
    if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
  if bugf:
    # Aが構成できない、と言うことなのでここは通らないはず
    print("bug...")
  
print("".join(ANS)) import heapq heapq N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split B = [6,2,5,5,4,5,6,3,7,6] B [6,2,5,5,4,5,6,3,7,6] 6 2 5 5 4 5 6 3 7 6 -1 for _ in range(N+10) for _ in range(N+10) _ range(N+10) range N+10 N 10 for _ in range(N+10) -1 dp = [-1 for _ in range(N+10)] dp [-1 for _ in range(N+10)] dp[0] = 0 dp[0] dp 0 0 A.sort() A.sort A sort False for _ in range(N+10) for _ in range(N+10) _ range(N+10) range N+10 N 10 for _ in range(N+10) False visited = [False for _ in range(N+10)] visited [False for _ in range(N+10)] h = [] h [] heapq.heappush(h, (0,0)) heapq.heappush heapq heappush h (0,0) 0 0 while h:
  now_n, keta = heapq.heappop(h)
  if visited[now_n]:
    continue
  visited[now_n] = True
  for a in A:
    nxt_n = now_n + B[a]
    if nxt_n >= N + 10:
      continue
    dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
    heapq.heappush(h, (nxt_n, dp[now_n] + 1))
    
#print(dp) h now_n, keta = heapq.heappop(h) now_n heapq.heappop(h) heapq.heappop heapq heappop h keta if visited[now_n]:
    continue
   visited[now_n] visited now_n continue visited[now_n] = True visited[now_n] visited now_n True for a in A:
    nxt_n = now_n + B[a]
    if nxt_n >= N + 10:
      continue
    dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
    heapq.heappush(h, (nxt_n, dp[now_n] + 1))
    
#print(dp) a A nxt_n = now_n + B[a] nxt_n now_n + B[a] now_n B[a] B a if nxt_n >= N + 10:
      continue
     nxt_n >= N + 10 nxt_n N + 10 N 10 continue dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1) dp[nxt_n] dp nxt_n max(dp[nxt_n], dp[now_n] + 1) max dp[nxt_n] dp nxt_n dp[now_n] + 1 dp[now_n] dp now_n 1 heapq.heappush(h, (nxt_n, dp[now_n] + 1)) heapq.heappush heapq heappush h (nxt_n, dp[now_n] + 1) nxt_n dp[now_n] + 1 dp[now_n] dp now_n 1 A.sort(reverse=True) A.sort A sort reverse=True reverse True ANS = [] ANS [] zan = N zan N for i in range(dp[N],0,-1):
  bugf = True
  # sorted(a) 大きい方から試す
  for a in A:
    if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
  if bugf:
    # Aが構成できない、と言うことなのでここは通らないはず
    print("bug...")
   i range(dp[N],0,-1) range dp[N] dp N 0 -1 bugf = True bugf True for a in A:
    if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
   a A if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
   dp[zan-B[a]] == dp[zan] - 1 dp[zan-B[a]] dp zan-B[a] zan B[a] B a dp[zan] - 1 dp[zan] dp zan 1 ANS.append(str(a)) ANS.append ANS append str(a) str a zan = zan - B[a] zan zan - B[a] zan B[a] B a bugf = False bugf False break if bugf:
    # Aが構成できない、と言うことなのでここは通らないはず
    print("bug...")
   bugf print("bug...") print "bug..." print("".join(ANS)) print "".join(ANS) "".join "" join ANS N, M = map(int, input().split()) map(int, input().split()) N dp = [-1 for _ in range(N+10)] [-1 for _ in range(N+10)] dp zan = N N zan h = [] [] h zan = zan - B[a] zan - B[a] zan bugf = False False bugf bugf = True True bugf keta = heapq.heappop(h) heapq.heappop(h) keta now_n, keta = heapq.heappop(h) heapq.heappop(h) now_n ANS = [] [] ANS A = list(map(int, input().split())) list(map(int, input().split())) A B = [6,2,5,5,4,5,6,3,7,6] [6,2,5,5,4,5,6,3,7,6] B M = map(int, input().split()) map(int, input().split()) M visited = [False for _ in range(N+10)] [False for _ in range(N+10)] visited nxt_n = now_n + B[a] now_n + B[a] nxt_n
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [6,2,5,5,4,5,6,3,7,6] # B[i]がiを作るのに必要なマッチ棒の数

# dp[i]: i本マッチ使って作れるMAX桁数。。
dp = [-1 for _ in range(N+10)]
dp[0] = 0
A.sort()
visited = [False for _ in range(N+10)]
h = []
heapq.heappush(h, (0,0))

while h:
  now_n, keta = heapq.heappop(h)
  if visited[now_n]:
    continue
  visited[now_n] = True
  for a in A:
    nxt_n = now_n + B[a]
    if nxt_n >= N + 10:
      continue
    dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
    heapq.heappush(h, (nxt_n, dp[now_n] + 1))
    
#print(dp)

A.sort(reverse=True)
# 上の桁から決めていく。dp[N]
ANS = []
zan = N
for i in range(dp[N],0,-1):
  bugf = True
  # sorted(a) 大きい方から試す
  for a in A:
    if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
  if bugf:
    # Aが構成できない、と言うことなのでここは通らないはず
    print("bug...")
  
print("".join(ANS))