N, M = map(int, input().split())
A = list(map(int, input().split()))
count = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[0] * 10 for _ in range(N+1)]
for i in range(N+1):
    if i > 0 and all(dp[i][j] == 0 for j in range(10)):
        continue
    for a in A:
        if i + count[a] <= N:
            ndp = list(dp[i])
            ndp[a] += 1
            ca = sum(ndp)
            cb = sum(dp[i+count[a]])
            if ca > cb:
                dp[i+count[a]] = ndp
            if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break
ans = ''
for i in range(10)[::-1]:
    ans += str(i) * dp[N][i]
print(ans)
 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 count = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] count [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 [0] * 10 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [0] * 10 [0] 0 10 dp = [[0] * 10 for _ in range(N+1)] dp [[0] * 10 for _ in range(N+1)] for i in range(N+1):
    if i > 0 and all(dp[i][j] == 0 for j in range(10)):
        continue
    for a in A:
        if i + count[a] <= N:
            ndp = list(dp[i])
            ndp[a] += 1
            ca = sum(ndp)
            cb = sum(dp[i+count[a]])
            if ca > cb:
                dp[i+count[a]] = ndp
            if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break i range(N+1) range N+1 N 1 if i > 0 and all(dp[i][j] == 0 for j in range(10)):
        continue
     i > 0 and all(dp[i][j] == 0 for j in range(10)) i > 0 i 0 all(dp[i][j] == 0 for j in range(10)) all dp[i][j] == 0 dp[i][j] [i] dp i j 0 continue for a in A:
        if i + count[a] <= N:
            ndp = list(dp[i])
            ndp[a] += 1
            ca = sum(ndp)
            cb = sum(dp[i+count[a]])
            if ca > cb:
                dp[i+count[a]] = ndp
            if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break a A if i + count[a] <= N:
            ndp = list(dp[i])
            ndp[a] += 1
            ca = sum(ndp)
            cb = sum(dp[i+count[a]])
            if ca > cb:
                dp[i+count[a]] = ndp
            if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break i + count[a] <= N i + count[a] i count[a] count a N ndp = list(dp[i]) ndp list(dp[i]) list dp[i] dp i ndp[a] += 1 ndp[a] ndp a 1 ca = sum(ndp) ca sum(ndp) sum ndp cb = sum(dp[i+count[a]]) cb sum(dp[i+count[a]]) sum dp[i+count[a]] dp i+count[a] i count[a] count a if ca > cb:
                dp[i+count[a]] = ndp
             ca > cb ca cb dp[i+count[a]] = ndp dp[i+count[a]] dp i+count[a] i count[a] count a ndp if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break ca == cb ca cb for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break j range(10)[::-1] (10) range 10 ::-1 -1 if dp[i+count[a]][j] > ndp[j]:
                        break
                     dp[i+count[a]][j] > ndp[j] dp[i+count[a]][j] [i+count[a]] dp i+count[a] i count[a] count a j ndp[j] ndp j break if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break dp[i+count[a]][j] < ndp[j] dp[i+count[a]][j] [i+count[a]] dp i+count[a] i count[a] count a j ndp[j] ndp j dp[i+count[a]] = ndp dp[i+count[a]] dp i+count[a] i count[a] count a ndp break ans = '' ans '' for i in range(10)[::-1]:
    ans += str(i) * dp[N][i] i range(10)[::-1] (10) range 10 ::-1 -1 ans += str(i) * dp[N][i] ans str(i) * dp[N][i] str(i) str i dp[N][i] [N] dp N i print(ans) print ans count = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] count ndp = list(dp[i]) list(dp[i]) ndp A = list(map(int, input().split())) list(map(int, input().split())) A M = map(int, input().split()) map(int, input().split()) M cb = sum(dp[i+count[a]]) sum(dp[i+count[a]]) cb ca = sum(ndp) sum(ndp) ca ans += str(i) * dp[N][i] str(i) * dp[N][i] ans N, M = map(int, input().split()) map(int, input().split()) N ans = '' '' ans dp = [[0] * 10 for _ in range(N+1)] [[0] * 10 for _ in range(N+1)] dp
N, M = map(int, input().split())
A = list(map(int, input().split()))
count = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[0] * 10 for _ in range(N+1)]
for i in range(N+1):
    if i > 0 and all(dp[i][j] == 0 for j in range(10)):
        continue
    for a in A:
        if i + count[a] <= N:
            ndp = list(dp[i])
            ndp[a] += 1
            ca = sum(ndp)
            cb = sum(dp[i+count[a]])
            if ca > cb:
                dp[i+count[a]] = ndp
            if ca == cb:
                for j in range(10)[::-1]:
                    if dp[i+count[a]][j] > ndp[j]:
                        break
                    if dp[i+count[a]][j] < ndp[j]:
                        dp[i+count[a]] = ndp
                        break
ans = ''
for i in range(10)[::-1]:
    ans += str(i) * dp[N][i]
print(ans)

