n,m=map(int,input().split())
from collections import defaultdict
a=list(map(int,input().split()))
dic={i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)}
s={}
for i in a:
    if dic[i] in s:
        s[dic[i]]=max(s[dic[i]],i)
    else:
        s[dic[i]]=i
ok=list(s.items())#hon,digit
dp=[-float('inf')]*(n+1)
dp[0]=0
for i in range(1,n+1):
    for h,d in ok:
        if h<=i:
            dp[i]=max(dp[i],dp[i-h]+1)
from operator import itemgetter as it 
ok.sort(reverse=1,key=it(1))
ans=''
now=n
keta=dp[n]
while keta>1:
    for h,d in ok:
        if now>h and dp[now]==dp[now-h]+1:
            ans+=str(d);now-=h;keta-=1;break
print(ans+str(s[now])) n,m=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split m from collections import defaultdict a=list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10) for i in range(1,10) i range(1,10) range 1 10 for i in range(1,10) i [2,5,5,4,5,6,3,7,6][i-1] [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 i-1 i 1 dic={i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)} dic {i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)} s={} s {} for i in a:
    if dic[i] in s:
        s[dic[i]]=max(s[dic[i]],i)
    else:
        s[dic[i]]=i i a if dic[i] in s:
        s[dic[i]]=max(s[dic[i]],i)
    else:
        s[dic[i]]=i dic[i] in s dic[i] dic i s s[dic[i]]=max(s[dic[i]],i) s[dic[i]] s dic[i] dic i max(s[dic[i]],i) max s[dic[i]] s dic[i] dic i i s[dic[i]]=i s[dic[i]] s dic[i] dic i i ok=list(s.items()) ok list(s.items()) list s.items() s.items s items dp=[-float('inf')]*(n+1) dp [-float('inf')]*(n+1) [-float('inf')] -float('inf') float('inf') float 'inf' n+1 n 1 dp[0]=0 dp[0] dp 0 0 for i in range(1,n+1):
    for h,d in ok:
        if h<=i:
            dp[i]=max(dp[i],dp[i-h]+1) i range(1,n+1) range 1 n+1 n 1 for h,d in ok:
        if h<=i:
            dp[i]=max(dp[i],dp[i-h]+1) h d ok if h<=i:
            dp[i]=max(dp[i],dp[i-h]+1) h<=i h i dp[i]=max(dp[i],dp[i-h]+1) dp[i] dp i max(dp[i],dp[i-h]+1) max dp[i] dp i dp[i-h]+1 dp[i-h] dp i-h i h 1 from operator import itemgetter as it ok.sort(reverse=1,key=it(1)) ok.sort ok sort reverse=1 reverse 1 key=it(1) key it(1) it 1 ans='' ans '' now=n now n keta=dp[n] keta dp[n] dp n while keta>1:
    for h,d in ok:
        if now>h and dp[now]==dp[now-h]+1:
            ans+=str(d);now-=h;keta-=1;break keta>1 keta 1 for h,d in ok:
        if now>h and dp[now]==dp[now-h]+1:
            ans+=str(d);now-=h;keta-=1;break h d ok if now>h and dp[now]==dp[now-h]+1:
            ans+=str(d);now-=h;keta-=1;break now>h and dp[now]==dp[now-h]+1 now>h now h dp[now]==dp[now-h]+1 dp[now] dp now dp[now-h]+1 dp[now-h] dp now-h now h 1 ans+=str(d) ans str(d) str d now-=h now h keta-=1 keta 1 break print(ans+str(s[now])) print ans+str(s[now]) ans str(s[now]) str s[now] s now a=list(map(int,input().split())) list(map(int,input().split())) a m=map(int,input().split()) map(int,input().split()) m s={} {} s now=n n now ans+=str(d) str(d) ans dic={i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)} {i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)} dic ans='' '' ans n,m=map(int,input().split()) map(int,input().split()) n now-=h h now keta-=1 1 keta keta=dp[n] dp[n] keta ok=list(s.items()) list(s.items()) ok dp=[-float('inf')]*(n+1) [-float('inf')]*(n+1) dp
n,m=map(int,input().split())
from collections import defaultdict
a=list(map(int,input().split()))
dic={i:[2,5,5,4,5,6,3,7,6][i-1]for i in range(1,10)}
s={}
for i in a:
    if dic[i] in s:
        s[dic[i]]=max(s[dic[i]],i)
    else:
        s[dic[i]]=i
ok=list(s.items())#hon,digit
dp=[-float('inf')]*(n+1)
dp[0]=0
for i in range(1,n+1):
    for h,d in ok:
        if h<=i:
            dp[i]=max(dp[i],dp[i-h]+1)
from operator import itemgetter as it 
ok.sort(reverse=1,key=it(1))
ans=''
now=n
keta=dp[n]
while keta>1:
    for h,d in ok:
        if now>h and dp[now]==dp[now-h]+1:
            ans+=str(d);now-=h;keta-=1;break
print(ans+str(s[now]))