from operator import itemgetter



inf = 10 ** 4



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

a = tuple(map(int, input().split()))



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

# use[i] : 数字iを作るのに必要なマッチの本数



use_num = {use[i]: i for i in range(1, 10) if i in a}

use_num = tuple(sorted(use_num.items()))

# 本数昇順

# print(use_num)



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

dp[0] = 0

# dp[j] : j本から構成できる桁数の最大値



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

    for use, number in use_num:

        k = j - use

        if k >= 0:

            dp[j] = max(dp[j], dp[k] + 1)

# print(dp)



use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True))

# 数字降順



rest = n

ans = ''

while rest > 0:

    for use, number in use_num:

        k = rest - use

        if (k >= 0) and (dp[k] == dp[rest] - 1):

            ans += str(number)

            rest -= use

            break

print(ans)
inf = 1 << 20

need = -1, 2, 5, 5, 4, 5, 6, 3, 7, 6



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

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

d = {need[x]: x for x in sorted(a)}  # sortedで昇順にすることで、同じ本数のとき最大値が残る

e = tuple(sorted(list(d.items()), key=lambda x: x[1], reverse=True))  # (必要本数,数字)



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

dp[0] = 0

# dp[k]:=k本で構成可能な最大桁数



for j in range(1, n + 1):

    dp[j] = max((dp[j - cnt] for cnt, _ in e if j - cnt >= 0), default=-inf) + 1



ret = []

r = dp[n]



curr = n

while r > 0:

    for cnt, num in e:

        if curr - cnt >= 0 and dp[curr - cnt] == r - 1:

            ret.append(num)

            r -= 1

            curr -= cnt

            break



ret.sort(reverse=True)

print((''.join(map(str, ret))))

from operator import itemgetter

inf = 10**4
n, m = list(map(int, input().split()))
a = tuple(map(int, input().split()))
use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# use[i] : 数字iを作るのに必要なマッチの本数
use_num = {use[i]: i for i in range(1, 10) if i in a}
use_num = tuple(sorted(use_num.items()))
# 本数昇順
# print(use_num)
dp = [-inf] * (n + 1)
dp[0] = 0
# dp[j] : j本から構成できる桁数の最大値
for j in range(2, n + 1):
    for use, number in use_num:
        k = j - use
        if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp)
use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True))
# 数字降順
rest = n
ans = ""
while rest > 0:
    for use, number in use_num:
        k = rest - use
        if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break
print(ans)

inf = 1 << 20
need = -1, 2, 5, 5, 4, 5, 6, 3, 7, 6
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {need[x]: x for x in sorted(a)}  # sortedで昇順にすることで、同じ本数のとき最大値が残る
e = tuple(sorted(list(d.items()), key=lambda x: x[1], reverse=True))  # (必要本数,数字)
dp = [-inf] * (n + 1)
dp[0] = 0
# dp[k]:=k本で構成可能な最大桁数
for j in range(1, n + 1):
    dp[j] = max((dp[j - cnt] for cnt, _ in e if j - cnt >= 0), default=-inf) + 1
ret = []
r = dp[n]
curr = n
while r > 0:
    for cnt, num in e:
        if curr - cnt >= 0 and dp[curr - cnt] == r - 1:
            ret.append(num)
            r -= 1
            curr -= cnt
            break
ret.sort(reverse=True)
print(("".join(map(str, ret))))

-from operator import itemgetter - -inf = 10**4 +inf = 1 << 20 +need = -1, 2, 5, 5, 4, 5, 6, 3, 7, 6 -a = tuple(map(int, input().split())) -use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] -# use[i] : 数字iを作るのに必要なマッチの本数 -use_num = {use[i]: i for i in range(1, 10) if i in a} -use_num = tuple(sorted(use_num.items())) -# 本数昇順 -# print(use_num) +a = list(map(int, input().split())) +d = {need[x]: x for x in sorted(a)}  # sortedで昇順にすることで、同じ本数のとき最大値が残る +e = tuple(sorted(list(d.items()), key=lambda x: x[1], reverse=True))  # (必要本数,数字) -# dp[j] : j本から構成できる桁数の最大値 -for j in range(2, n + 1): -    for use, number in use_num: -        k = j - use -        if k >= 0: -            dp[j] = max(dp[j], dp[k] + 1) -# print(dp) -use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True)) -# 数字降順 -rest = n -ans = "" -while rest > 0: -    for use, number in use_num: -        k = rest - use -        if (k >= 0) and (dp[k] == dp[rest] - 1): -            ans += str(number) -            rest -= use +# dp[k]:=k本で構成可能な最大桁数 +for j in range(1, n + 1): +    dp[j] = max((dp[j - cnt] for cnt, _ in e if j - cnt >= 0), default=-inf) + 1 +ret = [] +r = dp[n] +curr = n +while r > 0: +    for cnt, num in e: +        if curr - cnt >= 0 and dp[curr - cnt] == r - 1: +            ret.append(num) +            r -= 1 +            curr -= cnt -print(ans) +ret.sort(reverse=True) +print(("".join(map(str, ret))))