import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, M = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))

requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# dp[i][0]: i 個のマッチ棒を使って作れる整数の最大の桁数
# dp[i][1][-j]: j を何個作るか
dp = [(0, [0] * 9) for _ in range(N + 10)]
for i in range(N):
    d, counts = dp[i]
    if i > 0 and d == 0:
        continue
    for a in A:
        cnts = counts[:]
        cnts[-a] += 1
        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts))
ans = ''
for i, cnt in enumerate(dp[N][1]):
    ans += str(9 - i) * cnt
print(ans) import os os import sys sys if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r") os.getenv("LOCAL") os.getenv os getenv "LOCAL" sys.stdin = open("_in.txt", "r") sys.stdin sys stdin open("_in.txt", "r") open "_in.txt" "r" sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float("inf") INF float("inf") float "inf" IINF = 10 ** 18 IINF 10 ** 18 10 18 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 N, M = list(map(int, sys.stdin.buffer.readline().split())) N list(map(int, sys.stdin.buffer.readline().split())) list map(int, sys.stdin.buffer.readline().split()) map int sys.stdin.buffer.readline().split() sys.stdin.buffer.readline().split sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline split M A = list(map(int, sys.stdin.buffer.readline().split())) A list(map(int, sys.stdin.buffer.readline().split())) list map(int, sys.stdin.buffer.readline().split()) map int sys.stdin.buffer.readline().split() sys.stdin.buffer.readline().split sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline split requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] requires [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 (0, [0] * 9) for _ in range(N + 10) for _ in range(N + 10) _ range(N + 10) range N + 10 N 10 for _ in range(N + 10) (0, [0] * 9) 0 [0] * 9 [0] 0 9 dp = [(0, [0] * 9) for _ in range(N + 10)] dp [(0, [0] * 9) for _ in range(N + 10)] for i in range(N):
    d, counts = dp[i]
    if i > 0 and d == 0:
        continue
    for a in A:
        cnts = counts[:]
        cnts[-a] += 1
        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts)) i range(N) range N d, counts = dp[i] d dp[i] dp i counts if i > 0 and d == 0:
        continue
     i > 0 and d == 0 i > 0 i 0 d == 0 d 0 continue for a in A:
        cnts = counts[:]
        cnts[-a] += 1
        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts)) a A cnts = counts[:] cnts counts[:] counts : cnts[-a] += 1 cnts[-a] cnts -a a 1 dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts)) dp[i + requires[a]] dp i + requires[a] i requires[a] requires a max(dp[i + requires[a]], (d + 1, cnts)) max dp[i + requires[a]] dp i + requires[a] i requires[a] requires a (d + 1, cnts) d + 1 d 1 cnts ans = '' ans '' for i, cnt in enumerate(dp[N][1]):
    ans += str(9 - i) * cnt i cnt enumerate(dp[N][1]) enumerate dp[N][1] [N] dp N 1 ans += str(9 - i) * cnt ans str(9 - i) * cnt str(9 - i) str 9 - i 9 i cnt print(ans) print ans MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD A = list(map(int, sys.stdin.buffer.readline().split())) list(map(int, sys.stdin.buffer.readline().split())) A counts = dp[i] dp[i] counts INF = float("inf") float("inf") INF ans = '' '' ans dp = [(0, [0] * 9) for _ in range(N + 10)] [(0, [0] * 9) for _ in range(N + 10)] dp N, M = list(map(int, sys.stdin.buffer.readline().split())) list(map(int, sys.stdin.buffer.readline().split())) N ans += str(9 - i) * cnt str(9 - i) * cnt ans M = list(map(int, sys.stdin.buffer.readline().split())) list(map(int, sys.stdin.buffer.readline().split())) M IINF = 10 ** 18 10 ** 18 IINF requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] requires cnts = counts[:] counts[:] cnts d, counts = dp[i] dp[i] d
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, M = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))

requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# dp[i][0]: i 個のマッチ棒を使って作れる整数の最大の桁数
# dp[i][1][-j]: j を何個作るか
dp = [(0, [0] * 9) for _ in range(N + 10)]
for i in range(N):
    d, counts = dp[i]
    if i > 0 and d == 0:
        continue
    for a in A:
        cnts = counts[:]
        cnts[-a] += 1
        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts))
ans = ''
for i, cnt in enumerate(dp[N][1]):
    ans += str(9 - i) * cnt
print(ans)
