#!/usr/bin/env python



from collections import deque

import itertools as it

import sys

import math



sys.setrecursionlimit(1000000)



INF = 10 ** 18



N, M = list(map(int, input().split()))

cost = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

A = list(map(int, input().split()))



DP = [0] + [-1] * N

ans = [0] * N + [0]



for i in range(1, N + 1):

    for j in range(10)[::-1]:

        if not j in A:

            continue

        if cost[j] > i:

            continue

        if DP[i - cost[j]] < 0:

            continue

        if DP[i] < DP[i - cost[j]] + 1:

            DP[i] = DP[i - cost[j]] + 1

            ans[i] = j



S = ""

pos = N

while pos != 0:

    S += str(ans[pos])

    pos -= cost[ans[pos]]



print(S)
#!/usr/bin/env python



from collections import deque

import itertools as it

import sys

import math



sys.setrecursionlimit(1000000)



INF = 10 ** 18



N, M = list(map(int, input().split()))

cost = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

A = sorted(map(int, input().split()))



DP = [0] + [-1] * N

ans = [0, 0] * N



for i in range(1, N + 1):

    for j in A:

        pos = i - cost[j]

        if pos < 0 or DP[pos] < 0:

            continue

        if DP[i] <= DP[pos] + 1:

            DP[i] = DP[pos] + 1

            ans[i] = j



S = ""

pos = N

while pos:

    S += str(ans[pos])

    pos -= cost[ans[pos]]



print(S)
#!/usr/bin/env python
from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)
INF = 10**18
N, M = list(map(int, input().split()))
cost = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = list(map(int, input().split()))
DP = [0] + [-1] * N
ans = [0] * N + [0]
for i in range(1, N + 1):
    for j in range(10)[::-1]:
        if not j in A:
            continue
        if cost[j] > i:
            continue
        if DP[i - cost[j]] < 0:
            continue
        if DP[i] < DP[i - cost[j]] + 1:
            DP[i] = DP[i - cost[j]] + 1
            ans[i] = j
S = ""
pos = N
while pos != 0:
    S += str(ans[pos])
    pos -= cost[ans[pos]]
print(S)

#!/usr/bin/env python
from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)
INF = 10**18
N, M = list(map(int, input().split()))
cost = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = sorted(map(int, input().split()))
DP = [0] + [-1] * N
ans = [0, 0] * N
for i in range(1, N + 1):
    for j in A:
        pos = i - cost[j]
        if pos < 0 or DP[pos] < 0:
            continue
        if DP[i] <= DP[pos] + 1:
            DP[i] = DP[pos] + 1
            ans[i] = j
S = ""
pos = N
while pos:
    S += str(ans[pos])
    pos -= cost[ans[pos]]
print(S)

-A = list(map(int, input().split())) +A = sorted(map(int, input().split())) -ans = [0] * N + [0] +ans = [0, 0] * N -    for j in range(10)[::-1]: -        if not j in A: +    for j in A: +        pos = i - cost[j] +        if pos < 0 or DP[pos] < 0: -        if cost[j] > i: -            continue -        if DP[i - cost[j]] < 0: -            continue -        if DP[i] < DP[i - cost[j]] + 1: -            DP[i] = DP[i - cost[j]] + 1 +        if DP[i] <= DP[pos] + 1: +            DP[i] = DP[pos] + 1 -while pos != 0: +while pos: