from collections import defaultdict

N, M = map(int, input().split())
A = list(map(int, input().split()))
L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
D = defaultdict(int)

for i in A:
  if D[L[i]] < i:
    D[L[i]] = i

INF = float("inf")
dp = [-INF]*(N+1)
dp[0] = 0

for i in range(1, N+1):
  for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF)

ans = ""
temp = N

#なるべく大きい数字から使う
for i, x in sorted(D.items(), key=lambda x: x[1], reverse=True):
  while temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
    ans += str(x)
    temp -= i

print(ans) from collections import defaultdict 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 L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} L {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 D = defaultdict(int) D defaultdict(int) defaultdict int for i in A:
  if D[L[i]] < i:
    D[L[i]] = i i A if D[L[i]] < i:
    D[L[i]] = i D[L[i]] < i D[L[i]] D L[i] L i i D[L[i]] = i D[L[i]] D L[i] L i i INF = float("inf") INF float("inf") float "inf" dp = [-INF]*(N+1) dp [-INF]*(N+1) [-INF] -INF INF N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1, N+1):
  for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF) i range(1, N+1) range 1 N+1 N 1 for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF) j D.keys() D.keys D keys if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF) i-j >= 0 i-j i j 0 dp[i] = max(dp[i], dp[i-j] + 1) dp[i] dp i max(dp[i], dp[i-j] + 1) max dp[i] dp i dp[i-j] + 1 dp[i-j] dp i-j i j 1 dp[i] = max(dp[i], -INF) dp[i] dp i max(dp[i], -INF) max dp[i] dp i -INF INF ans = "" ans "" temp = N temp N for i, x in sorted(D.items(), key=lambda x: x[1], reverse=True):
  while temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
    ans += str(x)
    temp -= i i x sorted(D.items(), key=lambda x: x[1], reverse=True) sorted D.items() D.items D items key=lambda x: x[1] key lambda x: x[1] x[1] x 1 x reverse=True reverse True while temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
    ans += str(x)
    temp -= i temp - i >= 0 and dp[temp-i] == dp[temp] - 1 temp - i >= 0 temp - i temp i 0 dp[temp-i] == dp[temp] - 1 dp[temp-i] dp temp-i temp i dp[temp] - 1 dp[temp] dp temp 1 ans += str(x) ans str(x) str x temp -= i temp i print(ans) print ans M = map(int, input().split()) map(int, input().split()) M temp -= i i temp N, M = map(int, input().split()) map(int, input().split()) N A = list(map(int, input().split())) list(map(int, input().split())) A ans = "" "" ans temp = N N temp L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} L dp = [-INF]*(N+1) [-INF]*(N+1) dp D = defaultdict(int) defaultdict(int) D INF = float("inf") float("inf") INF ans += str(x) str(x) ans
from collections import defaultdict

N, M = map(int, input().split())
A = list(map(int, input().split()))
L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
D = defaultdict(int)

for i in A:
  if D[L[i]] < i:
    D[L[i]] = i

INF = float("inf")
dp = [-INF]*(N+1)
dp[0] = 0

for i in range(1, N+1):
  for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF)

ans = ""
temp = N

#なるべく大きい数字から使う
for i, x in sorted(D.items(), key=lambda x: x[1], reverse=True):
  while temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
    ans += str(x)
    temp -= i

print(ans)