all_match = [2,5,5,4,5,6,3,7,6]

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
match = []
for num in a:
    match.append(all_match[num-1])

dp = [-10**7 for i in range(n+1)] #桁数
dp[0] = 0

for i in range(1,n+1):
    possible = []
    for honsu in match:
        if i >= honsu:
            possible.append(dp[i-honsu] + 1)
    if len(possible)>0:
        dp[i] = max(possible)

length = dp[n]
ans = 0
now = n
for i in range(length):
    for j in range(m):
        if now >= match[j]:
            if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break

print(ans) all_match = [2,5,5,4,5,6,3,7,6] all_match [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 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 a.sort(reverse=True) a.sort a sort reverse=True reverse True match = [] match [] for num in a:
    match.append(all_match[num-1]) num a match.append(all_match[num-1]) match.append match append all_match[num-1] all_match num-1 num 1 -10**7 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) -10**7 -10 7 dp = [-10**7 for i in range(n+1)] dp [-10**7 for i in range(n+1)] dp[0] = 0 dp[0] dp 0 0 for i in range(1,n+1):
    possible = []
    for honsu in match:
        if i >= honsu:
            possible.append(dp[i-honsu] + 1)
    if len(possible)>0:
        dp[i] = max(possible) i range(1,n+1) range 1 n+1 n 1 possible = [] possible [] for honsu in match:
        if i >= honsu:
            possible.append(dp[i-honsu] + 1)
     honsu match if i >= honsu:
            possible.append(dp[i-honsu] + 1)
     i >= honsu i honsu possible.append(dp[i-honsu] + 1) possible.append possible append dp[i-honsu] + 1 dp[i-honsu] dp i-honsu i honsu 1 if len(possible)>0:
        dp[i] = max(possible) len(possible)>0 len(possible) len possible 0 dp[i] = max(possible) dp[i] dp i max(possible) max possible length = dp[n] length dp[n] dp n ans = 0 ans 0 now = n now n for i in range(length):
    for j in range(m):
        if now >= match[j]:
            if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break i range(length) range length for j in range(m):
        if now >= match[j]:
            if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break j range(m) range m if now >= match[j]:
            if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break now >= match[j] now match[j] match j if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break dp[now-match[j]] == dp[now]-1 dp[now-match[j]] dp now-match[j] now match[j] match j dp[now]-1 dp[now] dp now 1 ans += a[j]*10**(length-i-1) ans a[j]*10**(length-i-1) a[j] a j 10**(length-i-1) 10 length-i-1 length-i length i 1 now -= match[j] now match[j] match j break print(ans) print ans all_match = [2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] all_match n,m = map(int,input().split()) map(int,input().split()) n a = list(map(int,input().split())) list(map(int,input().split())) a length = dp[n] dp[n] length m = map(int,input().split()) map(int,input().split()) m match = [] [] match ans = 0 0 ans now = n n now ans += a[j]*10**(length-i-1) a[j]*10**(length-i-1) ans possible = [] [] possible now -= match[j] match[j] now dp = [-10**7 for i in range(n+1)] [-10**7 for i in range(n+1)] dp
all_match = [2,5,5,4,5,6,3,7,6]

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
match = []
for num in a:
    match.append(all_match[num-1])

dp = [-10**7 for i in range(n+1)] #桁数
dp[0] = 0

for i in range(1,n+1):
    possible = []
    for honsu in match:
        if i >= honsu:
            possible.append(dp[i-honsu] + 1)
    if len(possible)>0:
        dp[i] = max(possible)

length = dp[n]
ans = 0
now = n
for i in range(length):
    for j in range(m):
        if now >= match[j]:
            if dp[now-match[j]] == dp[now]-1:
                ans += a[j]*10**(length-i-1)
                now -= match[j]
                break

print(ans)