n,m=map(int,input().split())
a=list(map(int,input().split()))
mm=[0,2,5,5,4,5,6,3,7,6]

dp=[0]
m = [mm[i] for i in range(10) if i in a]
for i in range(1,n+1):
    tres=[-10**10]
    for sm in m:
        if i - sm >= 0:
            tres.append(dp[i-sm]+1)
    dp.append(max(tres))
k=dp[n]
res = []
a.sort(reverse=True)
while n > 0:
    for sa in a:
        if n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1:
            res.append(sa)
            n -= mm[sa]
            break

print("".join(map(str,res))) 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 mm=[0,2,5,5,4,5,6,3,7,6] mm [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 dp=[0] dp [0] 0 mm[i] for i in range(10) if i in a for i in range(10) if i in a i range(10) range 10 i in a i a if i in a mm[i] mm i m = [mm[i] for i in range(10) if i in a] m [mm[i] for i in range(10) if i in a] for i in range(1,n+1):
    tres=[-10**10]
    for sm in m:
        if i - sm >= 0:
            tres.append(dp[i-sm]+1)
    dp.append(max(tres)) i range(1,n+1) range 1 n+1 n 1 tres=[-10**10] tres [-10**10] -10**10 -10 10 for sm in m:
        if i - sm >= 0:
            tres.append(dp[i-sm]+1)
     sm m if i - sm >= 0:
            tres.append(dp[i-sm]+1)
     i - sm >= 0 i - sm i sm 0 tres.append(dp[i-sm]+1) tres.append tres append dp[i-sm]+1 dp[i-sm] dp i-sm i sm 1 dp.append(max(tres)) dp.append dp append max(tres) max tres k=dp[n] k dp[n] dp n res = [] res [] a.sort(reverse=True) a.sort a sort reverse=True reverse True while n > 0:
    for sa in a:
        if n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1:
            res.append(sa)
            n -= mm[sa]
            break n > 0 n 0 for sa in a:
        if n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1:
            res.append(sa)
            n -= mm[sa]
            break sa a if n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1:
            res.append(sa)
            n -= mm[sa]
            break n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1 n-mm[sa] >= 0 n-mm[sa] n mm[sa] mm sa 0 dp[n-mm[sa]]==dp[n]-1 dp[n-mm[sa]] dp n-mm[sa] n mm[sa] mm sa dp[n]-1 dp[n] dp n 1 res.append(sa) res.append res append sa n -= mm[sa] n mm[sa] mm sa break print("".join(map(str,res))) print "".join(map(str,res)) "".join "" join map(str,res) map str res dp=[0] [0] dp res = [] [] res m=map(int,input().split()) map(int,input().split()) m k=dp[n] dp[n] k n -= mm[sa] mm[sa] n mm=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] mm a=list(map(int,input().split())) list(map(int,input().split())) a n,m=map(int,input().split()) map(int,input().split()) n tres=[-10**10] [-10**10] tres m = [mm[i] for i in range(10) if i in a] [mm[i] for i in range(10) if i in a] m
n,m=map(int,input().split())
a=list(map(int,input().split()))
mm=[0,2,5,5,4,5,6,3,7,6]

dp=[0]
m = [mm[i] for i in range(10) if i in a]
for i in range(1,n+1):
    tres=[-10**10]
    for sm in m:
        if i - sm >= 0:
            tres.append(dp[i-sm]+1)
    dp.append(max(tres))
k=dp[n]
res = []
a.sort(reverse=True)
while n > 0:
    for sa in a:
        if n-mm[sa] >= 0 and dp[n-mm[sa]]==dp[n]-1:
            res.append(sa)
            n -= mm[sa]
            break

print("".join(map(str,res)))