n, m = list(map(int,input().split()))

a = sorted(map(int,input().split()))[::-1]

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

dp = [-float('inf')]*(n+1)

dp[0] = 0

for i in range(2, n+1):

    for j in a:

        if i >= c[j]:

            dp[i] = max(dp[i], dp[i-c[j]]+1)

r = []

while n:

    i = max((i for i in a if n>=c[i]), key=lambda x: dp[n-c[x]])

    r.append(i)

    n -= c[i]

print((''.join(map(str, r))))
from fractions import *

from collections import *

n, m = list(map(int,input().split()))

a = sorted(map(int,input().split()))[::-1]

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

mi = min(a, key=lambda x: c[x])

x = sum(c[i]*c[mi]//gcd(c[i],c[mi]) for i in a if i!=mi)

q, r = divmod(max(0, n-x), c[mi])

n = min(n, x+r)

dp = [-float('inf')]*(n+1)

dp[0] = 0

for i in range(2, n+1):

    for j in a:

        if i >= c[j]:

            dp[i] = max(dp[i], dp[i-c[j]]+1)

d = Counter()

d[mi] += q

while n:

    i = max((i for i in a if n>=c[i]), key=lambda x: dp[n-c[x]])

    d[i] += 1

    n -= c[i]

print((''.join(str(k)*d[k] for k in sorted(d)[::-1])))
n, m = list(map(int, input().split()))
a = sorted(map(int, input().split()))[::-1]
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-float("inf")] * (n + 1)
dp[0] = 0
for i in range(2, n + 1):
    for j in a:
        if i >= c[j]:
            dp[i] = max(dp[i], dp[i - c[j]] + 1)
r = []
while n:
    i = max((i for i in a if n >= c[i]), key=lambda x: dp[n - c[x]])
    r.append(i)
    n -= c[i]
print(("".join(map(str, r))))

from fractions import *
from collections import *

n, m = list(map(int, input().split()))
a = sorted(map(int, input().split()))[::-1]
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
mi = min(a, key=lambda x: c[x])
x = sum(c[i] * c[mi] // gcd(c[i], c[mi]) for i in a if i != mi)
q, r = divmod(max(0, n - x), c[mi])
n = min(n, x + r)
dp = [-float("inf")] * (n + 1)
dp[0] = 0
for i in range(2, n + 1):
    for j in a:
        if i >= c[j]:
            dp[i] = max(dp[i], dp[i - c[j]] + 1)
d = Counter()
d[mi] += q
while n:
    i = max((i for i in a if n >= c[i]), key=lambda x: dp[n - c[x]])
    d[i] += 1
    n -= c[i]
print(("".join(str(k) * d[k] for k in sorted(d)[::-1])))

+from fractions import * +from collections import * + +mi = min(a, key=lambda x: c[x]) +x = sum(c[i] * c[mi] // gcd(c[i], c[mi]) for i in a if i != mi) +q, r = divmod(max(0, n - x), c[mi]) +n = min(n, x + r) -r = [] +d = Counter() +d[mi] += q -    r.append(i) +    d[i] += 1 -print(("".join(map(str, r)))) +print(("".join(str(k) * d[k] for k in sorted(d)[::-1])))