from collections import defaultdict

n, m = map(int, input().split())
A = list(map(int, input().split()))
dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
usable = defaultdict(int)

for num in A:
    use = dic[num]
    if usable[use] < num:
        usable[use] = num

DP = [-1]*(n+10)
DP[0] = 0

for i in range(n):
    digits = DP[i]
    if digits < 0:
        continue

    for use in usable.keys():
        if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue

digits = DP[n]
num_of_match = n
ans = ''

for _ in range(n):
    if digits == 0:
        break

    for num in sorted(usable.values(), reverse=True):
        use = dic[num]
        if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue

print(ans) from collections import defaultdict 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 dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} dic {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 usable = defaultdict(int) usable defaultdict(int) defaultdict int for num in A:
    use = dic[num]
    if usable[use] < num:
        usable[use] = num num A use = dic[num] use dic[num] dic num if usable[use] < num:
        usable[use] = num usable[use] < num usable[use] usable use num usable[use] = num usable[use] usable use num DP = [-1]*(n+10) DP [-1]*(n+10) [-1] -1 n+10 n 10 DP[0] = 0 DP[0] DP 0 0 for i in range(n):
    digits = DP[i]
    if digits < 0:
        continue

    for use in usable.keys():
        if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue i range(n) range n digits = DP[i] digits DP[i] DP i if digits < 0:
        continue

     digits < 0 digits 0 continue for use in usable.keys():
        if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue use usable.keys() usable.keys usable keys if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue i+use <= n and DP[i+use] < digits + 1 i+use <= n i+use i use n DP[i+use] < digits + 1 DP[i+use] DP i+use i use digits + 1 digits 1 DP[i+use] = digits + 1 DP[i+use] DP i+use i use digits + 1 digits 1 continue digits = DP[n] digits DP[n] DP n num_of_match = n num_of_match n ans = '' ans '' for _ in range(n):
    if digits == 0:
        break

    for num in sorted(usable.values(), reverse=True):
        use = dic[num]
        if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue _ range(n) range n if digits == 0:
        break

     digits == 0 digits 0 break for num in sorted(usable.values(), reverse=True):
        use = dic[num]
        if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue num sorted(usable.values(), reverse=True) sorted usable.values() usable.values usable values reverse=True reverse True use = dic[num] use dic[num] dic num if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue DP[num_of_match - use] == digits - 1 DP[num_of_match - use] DP num_of_match - use num_of_match use digits - 1 digits 1 digits -= 1 digits 1 num_of_match -= use num_of_match use ans += str(num) ans str(num) str num break continue print(ans) print ans usable = defaultdict(int) defaultdict(int) usable n, m = map(int, input().split()) map(int, input().split()) n digits = DP[n] DP[n] digits ans += str(num) str(num) ans num_of_match -= use use num_of_match ans = '' '' ans dic = {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} dic use = dic[num] dic[num] use m = map(int, input().split()) map(int, input().split()) m DP = [-1]*(n+10) [-1]*(n+10) DP use = dic[num] dic[num] use digits = DP[i] DP[i] digits num_of_match = n n num_of_match digits -= 1 1 digits A = list(map(int, input().split())) list(map(int, input().split())) A
from collections import defaultdict

n, m = map(int, input().split())
A = list(map(int, input().split()))
dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
usable = defaultdict(int)

for num in A:
    use = dic[num]
    if usable[use] < num:
        usable[use] = num

DP = [-1]*(n+10)
DP[0] = 0

for i in range(n):
    digits = DP[i]
    if digits < 0:
        continue

    for use in usable.keys():
        if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue

digits = DP[n]
num_of_match = n
ans = ''

for _ in range(n):
    if digits == 0:
        break

    for num in sorted(usable.values(), reverse=True):
        use = dic[num]
        if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue

print(ans)