N,M=map(int,input().split())
n=[0,1,2,3,4,5,6,7,8,9]
H=[0,2,5,5,4,5,6,3,7,6]

L=[]
A=list(map(int,input().split()))
A.sort(reverse=True)

for i in range(M):
    for j in range(10):
        if A[i]==n[j]:
            L.append([H[j],n[j]])
#print(L)
dp=["" for i in range(N+1)]
dp[0]="0"
for i in range(len(L)):
    for j in range(L[i][0],N+1):
        if dp[j-L[i][0]]!="":
            if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1])
print(dp[N][1:]) N,M=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split M n=[0,1,2,3,4,5,6,7,8,9] n [0,1,2,3,4,5,6,7,8,9] 0 1 2 3 4 5 6 7 8 9 H=[0,2,5,5,4,5,6,3,7,6] H [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 L=[] L [] A=list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split A.sort(reverse=True) A.sort A sort reverse=True reverse True for i in range(M):
    for j in range(10):
        if A[i]==n[j]:
            L.append([H[j],n[j]])
#print(L) i range(M) range M for j in range(10):
        if A[i]==n[j]:
            L.append([H[j],n[j]])
#print(L) j range(10) range 10 if A[i]==n[j]:
            L.append([H[j],n[j]])
#print(L) A[i]==n[j] A[i] A i n[j] n j L.append([H[j],n[j]]) L.append L append [H[j],n[j]] H[j] H j n[j] n j "" for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) "" dp=["" for i in range(N+1)] dp ["" for i in range(N+1)] dp[0]="0" dp[0] dp 0 "0" for i in range(len(L)):
    for j in range(L[i][0],N+1):
        if dp[j-L[i][0]]!="":
            if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1]) i range(len(L)) range len(L) len L for j in range(L[i][0],N+1):
        if dp[j-L[i][0]]!="":
            if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1]) j range(L[i][0],N+1) range L[i][0] [i] L i 0 N+1 N 1 if dp[j-L[i][0]]!="":
            if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]]!="" dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 "" if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1]) len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])) len(dp[j]) len dp[j] dp j len(dp[j-L[i][0]]+str(L[i][1])) len dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 str(L[i][1]) str L[i][1] [i] L i 1 continue elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
             len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])) len(dp[j]) len dp[j] dp j len(dp[j-L[i][0]]+str(L[i][1])) len dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 str(L[i][1]) str L[i][1] [i] L i 1 dp[j]=dp[j-L[i][0]]+str(L[i][1]) dp[j] dp j dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 str(L[i][1]) str L[i][1] [i] L i 1 if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1]) dp[j]<dp[j-L[i][0]]+str(L[i][1]) dp[j] dp j dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 str(L[i][1]) str L[i][1] [i] L i 1 dp[j]=dp[j-L[i][0]]+str(L[i][1]) dp[j] dp j dp[j-L[i][0]]+str(L[i][1]) dp[j-L[i][0]] dp j-L[i][0] j L[i][0] [i] L i 0 str(L[i][1]) str L[i][1] [i] L i 1 print(dp[N][1:]) print dp[N][1:] [N] dp N 1: 1 N,M=map(int,input().split()) map(int,input().split()) N n=[0,1,2,3,4,5,6,7,8,9] [0,1,2,3,4,5,6,7,8,9] n H=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] H L=[] [] L dp=["" for i in range(N+1)] ["" for i in range(N+1)] dp M=map(int,input().split()) map(int,input().split()) M A=list(map(int,input().split())) list(map(int,input().split())) A
N,M=map(int,input().split())
n=[0,1,2,3,4,5,6,7,8,9]
H=[0,2,5,5,4,5,6,3,7,6]

L=[]
A=list(map(int,input().split()))
A.sort(reverse=True)

for i in range(M):
    for j in range(10):
        if A[i]==n[j]:
            L.append([H[j],n[j]])
#print(L)
dp=["" for i in range(N+1)]
dp[0]="0"
for i in range(len(L)):
    for j in range(L[i][0],N+1):
        if dp[j-L[i][0]]!="":
            if len(dp[j])>len(dp[j-L[i][0]]+str(L[i][1])):
                continue
            elif len(dp[j])<len(dp[j-L[i][0]]+str(L[i][1])):
                dp[j]=dp[j-L[i][0]]+str(L[i][1])
            else:
                if dp[j]<dp[j-L[i][0]]+str(L[i][1]):
                    dp[j]=dp[j-L[i][0]]+str(L[i][1])
print(dp[N][1:])