inf = 1 << 20
need = -1, 2, 5, 5, 4, 5, 6, 3, 7, 6

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

n, m = map(int, input().split())
a = map(int, input().split())
d = {need[x]: x for x in sorted(a)}  # sortedで昇順にすることで、同じ本数のとき最大値が残る
e = tuple(sorted(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)))
