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

dp = dict.fromkeys(range(n + 1))  # dp[n]: n本使うときの桁数
dp[0] = 0

num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
)  # 後の計算用にsortしておく

for i in range(n):
    dp[i + 1] = -float("inf")
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1)

ans = ""
while dp[n] > 1:
    for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定)
ans += str(list(filter(lambda e: e[1] == n, anum))[0][0])

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 dp = dict.fromkeys(range(n + 1)) dp dict.fromkeys(range(n + 1)) dict.fromkeys dict fromkeys range(n + 1) range n + 1 n 1 dp[0] = 0 dp[0] dp 0 0 num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} num {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
) anum sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
) sorted dict(filter(lambda e: e[0] in a, num.items())).items() (filter(lambda e: e[0] in a, num.items())).items (filter(lambda e: e[0] in a, num.items())) dict filter(lambda e: e[0] in a, num.items()) filter lambda e: e[0] in a e[0] in a e[0] e 0 a e num.items() num.items num items items reverse=True reverse True for i in range(n):
    dp[i + 1] = -float("inf")
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1) i range(n) range n dp[i + 1] = -float("inf") dp[i + 1] dp i + 1 i 1 -float("inf") float("inf") float "inf" for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1) e anum if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1) i + 1 - e[1] >= 0 i + 1 - e[1] i + 1 i 1 e[1] e 1 0 dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1) dp[i + 1] dp i + 1 i 1 max(dp[i + 1], dp[i + 1 - e[1]] + 1) max dp[i + 1] dp i + 1 i 1 dp[i + 1 - e[1]] + 1 dp[i + 1 - e[1]] dp i + 1 - e[1] i + 1 i 1 e[1] e 1 1 ans = "" ans "" while dp[n] > 1:
    for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定) dp[n] > 1 dp[n] dp n 1 for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定) e anum if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定) n - e[1] >= 0 n - e[1] n e[1] e 1 0 if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定) dp[n - e[1]] == dp[n] - 1 dp[n - e[1]] dp n - e[1] n e[1] e 1 dp[n] - 1 dp[n] dp n 1 ans += str(e[0]) ans str(e[0]) str e[0] e 0 n -= e[1] n e[1] e 1 break ans += str(list(filter(lambda e: e[1] == n, anum))[0][0]) ans str(list(filter(lambda e: e[1] == n, anum))[0][0]) str list(filter(lambda e: e[1] == n, anum))[0][0] [0] (filter(lambda e: e[1] == n, anum)) list filter(lambda e: e[1] == n, anum) filter lambda e: e[1] == n e[1] == n e[1] e 1 n e anum 0 0 print(ans) print ans anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
) sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
) anum n, m = map(int, input().split()) map(int, input().split()) n num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} num n -= e[1] e[1] n ans += str(list(filter(lambda e: e[1] == n, anum))[0][0]) str(list(filter(lambda e: e[1] == n, anum))[0][0]) ans m = map(int, input().split()) map(int, input().split()) m ans = "" "" ans a = list(map(int, input().split())) list(map(int, input().split())) a ans += str(e[0]) str(e[0]) ans dp = dict.fromkeys(range(n + 1)) dict.fromkeys(range(n + 1)) dp
n, m = map(int, input().split())
a = list(map(int, input().split()))

dp = dict.fromkeys(range(n + 1))  # dp[n]: n本使うときの桁数
dp[0] = 0

num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
)  # 後の計算用にsortしておく

for i in range(n):
    dp[i + 1] = -float("inf")
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1)

ans = ""
while dp[n] > 1:
    for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定)
ans += str(list(filter(lambda e: e[1] == n, anum))[0][0])

print(ans)
