#2 5 5 4 5 6 3 7 6
#ナップザックのdpかな
#dp[i][v] = dp[i-A[i]][v-M[i]*(10*i)]

import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,M = na()
A = na()
cost = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

d = {i:cost[i] for i in A}

dp = [-1]*(N+1)
dp[0] = 0
ans = [0]*(N+1)
for i in range(N+1):
    for j in range(10)[::-1]:
        if j not in d:
            continue
        elif d[j] > i:
            continue
        else:
            #print(j)
            if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j
S = ""
pos = N
#print(dp,ans)
while pos!=0:
    #print(pos)
    S+=str(ans[pos])
    pos-=d[ans[pos]]


print(S) import sys sys import math math ni = lambda: int(ns()) ni lambda: int(ns()) int(ns()) int ns() ns na = lambda: list(map(int, input().split())) na lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ns = lambda: input() ns lambda: input() input() input N,M = na() N na() na M A = na() A na() na cost = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} cost {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 i:cost[i] for i in A for i in A i A for i in A i cost[i] cost i d = {i:cost[i] for i in A} d {i:cost[i] for i in A} dp = [-1]*(N+1) dp [-1]*(N+1) [-1] -1 N+1 N 1 dp[0] = 0 dp[0] dp 0 0 ans = [0]*(N+1) ans [0]*(N+1) [0] 0 N+1 N 1 for i in range(N+1):
    for j in range(10)[::-1]:
        if j not in d:
            continue
        elif d[j] > i:
            continue
        else:
            #print(j)
            if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j i range(N+1) range N+1 N 1 for j in range(10)[::-1]:
        if j not in d:
            continue
        elif d[j] > i:
            continue
        else:
            #print(j)
            if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j j range(10)[::-1] (10) range 10 ::-1 -1 if j not in d:
            continue
        elif d[j] > i:
            continue
        else:
            #print(j)
            if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j j not in d j d continue elif d[j] > i:
            continue
         d[j] > i d[j] d j i continue if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j dp[i] < dp[i-d[j]]+1 dp[i] dp i dp[i-d[j]]+1 dp[i-d[j]] dp i-d[j] i d[j] d j 1 dp[i] = dp[i-d[j]]+1 dp[i] dp i dp[i-d[j]]+1 dp[i-d[j]] dp i-d[j] i d[j] d j 1 ans[i] = j ans[i] ans i j S = "" S "" pos = N pos N while pos!=0:
    #print(pos)
    S+=str(ans[pos])
    pos-=d[ans[pos]] pos!=0 pos 0 S+=str(ans[pos]) S str(ans[pos]) str ans[pos] ans pos pos-=d[ans[pos]] pos d[ans[pos]] d ans[pos] ans pos print(S) print S cost = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} cost d = {i:cost[i] for i in A} {i:cost[i] for i in A} d dp = [-1]*(N+1) [-1]*(N+1) dp S = "" "" S pos = N N pos N,M = na() na() N ans = [0]*(N+1) [0]*(N+1) ans S+=str(ans[pos]) str(ans[pos]) S ni = lambda: int(ns()) lambda: int(ns()) ni ns = lambda: input() lambda: input() ns na = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) na M = na() na() M A = na() na() A pos-=d[ans[pos]] d[ans[pos]] pos
#2 5 5 4 5 6 3 7 6
#ナップザックのdpかな
#dp[i][v] = dp[i-A[i]][v-M[i]*(10*i)]

import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,M = na()
A = na()
cost = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

d = {i:cost[i] for i in A}

dp = [-1]*(N+1)
dp[0] = 0
ans = [0]*(N+1)
for i in range(N+1):
    for j in range(10)[::-1]:
        if j not in d:
            continue
        elif d[j] > i:
            continue
        else:
            #print(j)
            if dp[i] < dp[i-d[j]]+1:
                dp[i] = dp[i-d[j]]+1
                ans[i] = j
S = ""
pos = N
#print(dp,ans)
while pos!=0:
    #print(pos)
    S+=str(ans[pos])
    pos-=d[ans[pos]]


print(S)