n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
costs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
cs = {m: costs[m - 1] for m in a}
INF = 10 ** 9
dp = [0]

for i in range(1, n + 1):
    dp.append(max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) + 1)

digits = list(cs.keys())
digits.sort(reverse=True)
num = n
ans = []
for _ in range(dp[n]):
    for digit in digits:
        if num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1:
            ans.append(str(digit))
            num -= cs[digit]
            break
print("".join(ans)) n, m = list(map(int, input().split())) n list(map(int, input().split())) list 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 costs = [2, 5, 5, 4, 5, 6, 3, 7, 6] costs [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 m: costs[m - 1] for m in a for m in a m a for m in a m costs[m - 1] costs m - 1 m 1 cs = {m: costs[m - 1] for m in a} cs {m: costs[m - 1] for m in a} INF = 10 ** 9 INF 10 ** 9 10 9 dp = [0] dp [0] 0 for i in range(1, n + 1):
    dp.append(max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) + 1) i range(1, n + 1) range 1 n + 1 n 1 dp[i - c] if i - c >= 0 else -INF for c in cs.values() for c in cs.values() c cs.values() cs.values cs values for c in cs.values() dp[i - c] if i - c >= 0 else -INF i - c >= 0 i - c i c 0 dp[i - c] dp i - c i c -INF INF dp.append(max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) + 1) dp.append dp append max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) + 1 max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) max [dp[i - c] if i - c >= 0 else -INF for c in cs.values()] 1 digits = list(cs.keys()) digits list(cs.keys()) list cs.keys() cs.keys cs keys digits.sort(reverse=True) digits.sort digits sort reverse=True reverse True num = n num n ans = [] ans [] for _ in range(dp[n]):
    for digit in digits:
        if num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1:
            ans.append(str(digit))
            num -= cs[digit]
            break _ range(dp[n]) range dp[n] dp n for digit in digits:
        if num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1:
            ans.append(str(digit))
            num -= cs[digit]
            break digit digits if num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1:
            ans.append(str(digit))
            num -= cs[digit]
            break num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1 num >= cs[digit] and dp[num - cs[digit]] != -INF num >= cs[digit] num cs[digit] cs digit dp[num - cs[digit]] != -INF dp[num - cs[digit]] dp num - cs[digit] num cs[digit] cs digit -INF INF dp[num - cs[digit]] == dp[num] - 1 dp[num - cs[digit]] dp num - cs[digit] num cs[digit] cs digit dp[num] - 1 dp[num] dp num 1 ans.append(str(digit)) ans.append ans append str(digit) str digit num -= cs[digit] num cs[digit] cs digit break print("".join(ans)) print "".join(ans) "".join "" join ans digits = list(cs.keys()) list(cs.keys()) digits n, m = list(map(int, input().split())) list(map(int, input().split())) n num -= cs[digit] cs[digit] num INF = 10 ** 9 10 ** 9 INF dp = [0] [0] dp a = list(map(int, input().split())) list(map(int, input().split())) a num = n n num ans = [] [] ans m = list(map(int, input().split())) list(map(int, input().split())) m costs = [2, 5, 5, 4, 5, 6, 3, 7, 6] [2, 5, 5, 4, 5, 6, 3, 7, 6] costs cs = {m: costs[m - 1] for m in a} {m: costs[m - 1] for m in a} cs
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
costs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
cs = {m: costs[m - 1] for m in a}
INF = 10 ** 9
dp = [0]

for i in range(1, n + 1):
    dp.append(max(
        [dp[i - c] if i - c >= 0 else -INF for c in cs.values()]
    ) + 1)

digits = list(cs.keys())
digits.sort(reverse=True)
num = n
ans = []
for _ in range(dp[n]):
    for digit in digits:
        if num >= cs[digit] and dp[num - cs[digit]] != -INF and dp[num - cs[digit]] == dp[num] - 1:
            ans.append(str(digit))
            num -= cs[digit]
            break
print("".join(ans))
