import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = [None,2,5,5,4,5,6,3,7,6]
ds = [(i, d[i]) for i in a] # (数、本数)
ds.sort(reverse=True)

dp = [list(0 for _ in range(m+1)) for _ in range(n+1)]
for i in range(1, n+1):
    for j,(ii,num) in enumerate(ds):
        if i-num==0 or (i-num>0 and dp[i-num][0]>0):
            item = dp[i-num][:]
            item[0] += 1
            item[j+1] += 1
#             print(item)
            dp[i] = max(dp[i], item)
ans = ""
for i,item in enumerate(dp[n][1:]):
    ans += str(ds[i][0])*item
print(ans) import sys sys input = lambda : sys.stdin.readline().rstrip() input lambda : sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip sys.setrecursionlimit(max(1000, 10**9)) sys.setrecursionlimit sys setrecursionlimit max(1000, 10**9) max 1000 10**9 10 9 write = lambda x: sys.stdout.write(x+"\n") write lambda x: sys.stdout.write(x+"\n") sys.stdout.write(x+"\n") sys.stdout.write sys.stdout sys stdout write x+"\n" x "\n" x n,m = list(map(int, input().split())) n list(map(int, input().split())) list 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 d = [None,2,5,5,4,5,6,3,7,6] d [None,2,5,5,4,5,6,3,7,6] None 2 5 5 4 5 6 3 7 6 (i, d[i]) for i in a for i in a i a for i in a (i, d[i]) i d[i] d i ds = [(i, d[i]) for i in a] ds [(i, d[i]) for i in a] ds.sort(reverse=True) ds.sort ds sort reverse=True reverse True list(0 for _ in range(m+1)) for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) list(0 for _ in range(m+1)) list 0 dp = [list(0 for _ in range(m+1)) for _ in range(n+1)] dp [list(0 for _ in range(m+1)) for _ in range(n+1)] for i in range(1, n+1):
    for j,(ii,num) in enumerate(ds):
        if i-num==0 or (i-num>0 and dp[i-num][0]>0):
            item = dp[i-num][:]
            item[0] += 1
            item[j+1] += 1
#             print(item)
            dp[i] = max(dp[i], item) i range(1, n+1) range 1 n+1 n 1 for j,(ii,num) in enumerate(ds):
        if i-num==0 or (i-num>0 and dp[i-num][0]>0):
            item = dp[i-num][:]
            item[0] += 1
            item[j+1] += 1
#             print(item)
            dp[i] = max(dp[i], item) j (ii,num) ii num enumerate(ds) enumerate ds if i-num==0 or (i-num>0 and dp[i-num][0]>0):
            item = dp[i-num][:]
            item[0] += 1
            item[j+1] += 1
#             print(item)
            dp[i] = max(dp[i], item) i-num==0 or (i-num>0 and dp[i-num][0]>0) i-num==0 i-num i num 0 i-num>0 and dp[i-num][0]>0 i-num>0 i-num i num 0 dp[i-num][0]>0 dp[i-num][0] [i-num] dp i-num i num 0 0 item = dp[i-num][:] item dp[i-num][:] [i-num] dp i-num i num : item[0] += 1 item[0] item 0 1 item[j+1] += 1 item[j+1] item j+1 j 1 1 dp[i] = max(dp[i], item) dp[i] dp i max(dp[i], item) max dp[i] dp i item ans = "" ans "" for i,item in enumerate(dp[n][1:]):
    ans += str(ds[i][0])*item i item enumerate(dp[n][1:]) enumerate dp[n][1:] [n] dp n 1: 1 ans += str(ds[i][0])*item ans str(ds[i][0])*item str(ds[i][0]) str ds[i][0] [i] ds i 0 item print(ans) print ans input = lambda : sys.stdin.readline().rstrip() lambda : sys.stdin.readline().rstrip() input item = dp[i-num][:] dp[i-num][:] item ans = "" "" ans dp = [list(0 for _ in range(m+1)) for _ in range(n+1)] [list(0 for _ in range(m+1)) for _ in range(n+1)] dp write = lambda x: sys.stdout.write(x+"\n") lambda x: sys.stdout.write(x+"\n") write n,m = list(map(int, input().split())) list(map(int, input().split())) n ds = [(i, d[i]) for i in a] [(i, d[i]) for i in a] ds m = list(map(int, input().split())) list(map(int, input().split())) m a = list(map(int, input().split())) list(map(int, input().split())) a d = [None,2,5,5,4,5,6,3,7,6] [None,2,5,5,4,5,6,3,7,6] d ans += str(ds[i][0])*item str(ds[i][0])*item ans
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = [None,2,5,5,4,5,6,3,7,6]
ds = [(i, d[i]) for i in a] # (数、本数)
ds.sort(reverse=True)

dp = [list(0 for _ in range(m+1)) for _ in range(n+1)]
for i in range(1, n+1):
    for j,(ii,num) in enumerate(ds):
        if i-num==0 or (i-num>0 and dp[i-num][0]>0):
            item = dp[i-num][:]
            item[0] += 1
            item[j+1] += 1
#             print(item)
            dp[i] = max(dp[i], item)
ans = ""
for i,item in enumerate(dp[n][1:]):
    ans += str(ds[i][0])*item
print(ans)
