import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = [0 for i in range(N+1)]
ans = 0
tmp = set()
tmp.add(0)
for i in range(1, N+1):
    for n, k in num:
        if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break


num.sort(reverse=True)
tmp = N
ans = []
for i in reversed(range(1, dp[N])):
    for n, k in num:
        if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break
for n, k in num:
    if k == tmp:
        ans.append(n)
        break

ans.sort(reverse=True)
ans = "".join(map(str, ans))
print(ans) import bisect bisect 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 num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] num [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 (i, num[i]) for i in range(10) if i in A for i in range(10) if i in A i range(10) range 10 i in A i A if i in A (i, num[i]) i num[i] num i num = [(i, num[i]) for i in range(10) if i in A] num [(i, num[i]) for i in range(10) if i in A] num.sort(key=lambda x: x[0], reverse=True) num.sort num sort key=lambda x: x[0] key lambda x: x[0] x[0] x 0 x reverse=True reverse True num.sort(key=lambda x: x[1]) num.sort num sort key=lambda x: x[1] key lambda x: x[1] x[1] x 1 x 0 for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) 0 dp = [0 for i in range(N+1)] dp [0 for i in range(N+1)] ans = 0 ans 0 tmp = set() tmp set() set tmp.add(0) tmp.add tmp add 0 for i in range(1, N+1):
    for n, k in num:
        if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break i range(1, N+1) range 1 N+1 N 1 for n, k in num:
        if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break n k num if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break i - k in tmp i - k i k tmp dp[i] = 1 + dp[i-k] dp[i] dp i 1 + dp[i-k] 1 dp[i-k] dp i-k i k tmp.add(i) tmp.add tmp add i break num.sort(reverse=True) num.sort num sort reverse=True reverse True tmp = N tmp N ans = [] ans [] for i in reversed(range(1, dp[N])):
    for n, k in num:
        if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break i reversed(range(1, dp[N])) reversed range(1, dp[N]) range 1 dp[N] dp N for n, k in num:
        if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break n k num if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break dp[tmp - k] == i dp[tmp - k] dp tmp - k tmp k i ans.append(n) ans.append ans append n tmp -= k tmp k break for n, k in num:
    if k == tmp:
        ans.append(n)
        break n k num if k == tmp:
        ans.append(n)
        break k == tmp k tmp ans.append(n) ans.append ans append n break ans.sort(reverse=True) ans.sort ans sort reverse=True reverse True ans = "".join(map(str, ans)) ans "".join(map(str, ans)) "".join "" join map(str, ans) map str ans print(ans) print ans tmp -= k k tmp dp = [0 for i in range(N+1)] [0 for i in range(N+1)] dp ans = 0 0 ans M = map(int, input().split()) map(int, input().split()) M ans = "".join(map(str, ans)) "".join(map(str, ans)) ans N, M = map(int, input().split()) map(int, input().split()) N tmp = N N tmp num = [(i, num[i]) for i in range(10) if i in A] [(i, num[i]) for i in range(10) if i in A] num ans = [] [] ans tmp = set() set() tmp num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] num A = list(map(int, input().split())) list(map(int, input().split())) A
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = [0 for i in range(N+1)]
ans = 0
tmp = set()
tmp.add(0)
for i in range(1, N+1):
    for n, k in num:
        if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break


num.sort(reverse=True)
tmp = N
ans = []
for i in reversed(range(1, dp[N])):
    for n, k in num:
        if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break
for n, k in num:
    if k == tmp:
        ans.append(n)
        break

ans.sort(reverse=True)
ans = "".join(map(str, ans))
print(ans)
