from math import isinf

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

m = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# 最大何桁作れるか

# dp[i] := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
# dp[i + 1] = max(dp[i - m[j]] + 1) for all j in A

dp = [float('-inf') for _ in range(N + 1)]
dp[0] = 0

for i in range(1, N + 1):
    for num in A:
        if i - m[num] >= 0:
            dp[i] = max(dp[i], dp[i - m[num]] + 1)

matches = N
ans = ''
for _ in range(dp[N]):
    for a in A:
        if matches - m[a] < 0:
            continue
        if dp[matches - m[a]] == dp[matches] - 1:
            matches -= m[a]
            ans += str(a)
            break

print(ans) from math import isinf 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 A.sort(reverse=True) A.sort A sort reverse=True reverse True m = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] m [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] None 2 5 5 4 5 6 3 7 6 float('-inf') for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) float('-inf') float '-inf' dp = [float('-inf') for _ in range(N + 1)] dp [float('-inf') for _ in range(N + 1)] dp[0] = 0 dp[0] dp 0 0 for i in range(1, N + 1):
    for num in A:
        if i - m[num] >= 0:
            dp[i] = max(dp[i], dp[i - m[num]] + 1) i range(1, N + 1) range 1 N + 1 N 1 for num in A:
        if i - m[num] >= 0:
            dp[i] = max(dp[i], dp[i - m[num]] + 1) num A if i - m[num] >= 0:
            dp[i] = max(dp[i], dp[i - m[num]] + 1) i - m[num] >= 0 i - m[num] i m[num] m num 0 dp[i] = max(dp[i], dp[i - m[num]] + 1) dp[i] dp i max(dp[i], dp[i - m[num]] + 1) max dp[i] dp i dp[i - m[num]] + 1 dp[i - m[num]] dp i - m[num] i m[num] m num 1 matches = N matches N ans = '' ans '' for _ in range(dp[N]):
    for a in A:
        if matches - m[a] < 0:
            continue
        if dp[matches - m[a]] == dp[matches] - 1:
            matches -= m[a]
            ans += str(a)
            break _ range(dp[N]) range dp[N] dp N for a in A:
        if matches - m[a] < 0:
            continue
        if dp[matches - m[a]] == dp[matches] - 1:
            matches -= m[a]
            ans += str(a)
            break a A if matches - m[a] < 0:
            continue
         matches - m[a] < 0 matches - m[a] matches m[a] m a 0 continue if dp[matches - m[a]] == dp[matches] - 1:
            matches -= m[a]
            ans += str(a)
            break dp[matches - m[a]] == dp[matches] - 1 dp[matches - m[a]] dp matches - m[a] matches m[a] m a dp[matches] - 1 dp[matches] dp matches 1 matches -= m[a] matches m[a] m a ans += str(a) ans str(a) str a break print(ans) print ans dp = [float('-inf') for _ in range(N + 1)] [float('-inf') for _ in range(N + 1)] dp N, M = map(int, input().split()) map(int, input().split()) N matches -= m[a] m[a] matches ans = '' '' ans M = map(int, input().split()) map(int, input().split()) M m = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] m A = list(map(int, input().split())) list(map(int, input().split())) A ans += str(a) str(a) ans matches = N N matches
from math import isinf

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

m = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# 最大何桁作れるか

# dp[i] := ちょうど i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
# dp[i + 1] = max(dp[i - m[j]] + 1) for all j in A

dp = [float('-inf') for _ in range(N + 1)]
dp[0] = 0

for i in range(1, N + 1):
    for num in A:
        if i - m[num] >= 0:
            dp[i] = max(dp[i], dp[i - m[num]] + 1)

matches = N
ans = ''
for _ in range(dp[N]):
    for a in A:
        if matches - m[a] < 0:
            continue
        if dp[matches - m[a]] == dp[matches] - 1:
            matches -= m[a]
            ans += str(a)
            break

print(ans)
