Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:
 - Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).
 - The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 10^4
 - 1 \leq M \leq 9
 - 1 \leq A_i \leq 9
 - A_i are all different.
 - There exists an integer that can be formed by exactly N matchsticks under the conditions.

-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_M

-----Output-----
Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

-----Sample Input-----
20 4
3 7 8 4

-----Sample Output-----
777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Difficulty: interview

Starter code:

Solutions:
n,m=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]+[-1]*(9*n)
l=[0,2,5,5,4,5,6,3,7,6]
for i in range(n):
  for j in a:
    if dp[i]>=0:
      dp[i+l[j]]=max(dp[i+l[j]],dp[i]*10+j)
print(dp[n])

def main():
    N, M = map(int, input().split())
    A = set(map(int, input().split()))


    cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    B = []
    for i in range(1, 10):
        if i in A:
            B.append((cost[i], i))

    B = B[::-1]
    dp = [[-1, -1] for i in range(N+1)]
    dp[0][0] = 0
    for i in range(1, N+1):
        for x, y in B:
            if i >= x and dp[i-x][0] >= 0:
                if dp[i][0] < dp[i-x][0] + 1:
                    dp[i][0] = dp[i-x][0] + 1
                    dp[i][1] = y
    id = N
    ans = []
    # print(dp)
    while(id > 0):
        ans.append(dp[id][1])
        id -= cost[dp[id][1]]
    ans.sort(reverse=True)
    print("".join(map(str, ans)))
main()

INF=float('inf')
cost=[-1,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))

dp=[-INF]*(N+1)
dp[0]=0

for i in range(1,N+1):
    for a in A:
        if i-cost[a]>=0:
            dp[i]=max(dp[i],dp[i-cost[a]]*10+a)
print(dp[N])

n, m = map(int, input().split())
A = list(map(int, input().split()))

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

dp = [-1] * (n + 1)
dp[0] = 0

for i in range(n):
  for j in A:
    if i + li[j - 1] <= n:
      dp[i + li[j - 1]] = max(dp[i + li[j - 1]], dp[i] * 10 + j)
print(dp[-1])

# dpに最大桁数を持たせる
N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

# dp[i]: マッチi本数で作れる最大の桁数(int)
dp = [-float('inf')]*(N+1)
dp[0] = 0
for i in range(N+1):
    nxt = dp[i]
    for aj in A:
        if i-f[aj]<0: continue
        nxt = max(nxt, 
                  dp[i-f[aj]] + 1)
    dp[i] = nxt
    #print(dp)
#print(dp[N])
keta = dp[N]
ans = ""
matchs = N
# 上位の桁から貪欲に
while keta>0:
    for aj in A:
        if matchs-f[aj] < 0: continue
        if dp[matchs-f[aj]] == keta-1:
            ans += str(aj)
            matchs -= f[aj]
            keta -= 1
            break
print(ans)


N,M = map(int,input().split())
A = list(map(int,input().split()))
weight = [0,2,5,5,4,5,6,3,7,6]
dp = [-1]*(N+1)
dp[0]=0
for i in range(N+1):
  for a in A:
    if i + weight[a]<N+1:
      dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a)
print(dp[N])

n,m=map(int,input().split())
A=reversed(sorted(map(int,input().split())))
M=[2,5,5,4,5,6,3,7,6]
D={}
E={}
for a in A:
    if M[a-1] in D:
        continue
    else:
        D[M[a-1]]=a
        E[a]=M[a-1]
S=list(reversed(sorted(E)))
        
dp=[0]*(n+2)
for i in range(1,n+2):
    cnt=-1
    for d in D:
        if i-d>=0:
            if dp[i-d]!=-1:
                cnt=max(dp[i-d]+1,cnt)
    dp[i]=cnt    
k=dp[n]
ans=""

while k>0:
    for s in S:
        x=E[s]
        if n-x<0:
            continue
        if dp[n-x]==k-1:
            ans+=str(s)
            k-=1
            n-=x
            break
print(ans)

# 解説放送の１つめ
N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

# dp[i]: マッチi本数で作れる最大の数(str)
dp = [None]*(N+1)
dp[0] = ""

# strの数の大きいほう
def max_str(s1:str, s2:str)->str:
    """return bigger one"""
    l1, l2 = len(s1), len(s2)
    if l1 != l2:
        return s1 if l1>l2 else s2
    if l1 == l2: 
        return s1 if s1>s2 else s2
    
for i in range(2, N+1):
    nxt = ""
    for aj in A:
        # 配列外参照と，i本ちょうどで作れないものはだめ
        if i-f[aj]<0 or dp[i-f[aj]]==None: continue
        nxt = max_str(nxt, 
                      dp[i-f[aj]]+str(aj))
    if nxt!="":
        dp[i] = nxt
    #print(dp)
print((dp[N]))


N,_ = map(int, input().split())
A = [int(i) for i in input().split()]

INF = "-"

def chmax(a:str,b:str):
    if a == INF: return b
    elif len(a) < len(b): return b
    elif len(a) == len(a):
        if a < b: return b
    return a

# 数字 : 必要なマッチの本数
cnt = dict(zip(range(1,10), [2,5,5,4,5,6,3,7,6]))

# dp[i]: マッチをi本使って作れる数値の最大値
dp = [INF] * (10 ** 5)

# 初期条件
dp[0] = ""

# 配るDP
for i in range(N):
    if dp[i] == INF: continue
    for a in A:
        dp[i + cnt[a]] = chmax(dp[i + cnt[a]], dp[i] + chr(ord("0") + a))

print(dp[N])

n,m=map(int,input().split())
a=list(map(int,input().split()))
table={
  1:2,
  2:5,
  3:5,
  4:4,
  5:5,
  6:6,
  7:3,
  8:7,
  9:6,
}
dp=[-1]*(n+1)
dp[0]=0
for i in range(1,n+1):
  mx=-1
  for j in a:
    if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
    if dp[i-table[j]]<0:
      continue
    if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
  dp[i]=mx
print(dp[n])

n, m = map(int, input().split())
a = list(map(int, input().split()))
l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n+1)
dp[0] =  0

for i in range(n+1):
    for j in a:
        if i + l[j] < n+1:
            dp[i + l[j]] = max(dp[i + l[j]], dp[i]*10 + j)
print(dp[-1])

n,m=list(map(int,input().split()))
A=sorted(list(map(int,input().split())),reverse=True)
L=[0,2,5,5,4,5,6,3,7,6]

inf=float('inf')
#dp[i]=i本で最大何桁作れるか
dp=[-inf]*(n+1)
dp[0]=0
for i in range(1,n+1):
    for j in range(m):
        num=L[A[j]]
        if i>=num:
            dp[i]=max(dp[i],dp[i-num]+1)

sort_A=sorted(A,reverse=True)
ans=''
while True:
    if n==0:
        break
    for i in range(m):
        temp=sort_A[i]
        num=L[temp]
        if n>=num and dp[n-num]>=0:
            if dp[n-num]==dp[n]-1:
                ans+=str(temp)
                n-=num
                break
    

print((int(ans)))





def num_to_match(n):
    if n == 1:
        return 2
    elif n == 7:
        return 3
    elif n == 4:
        return 4
    elif n in [2, 3, 5]:
        return 5
    elif n in [6, 9]:
        return 6
    else:
        return 7


def max_length(N, match):
    match = set(match)
    INF = 10**15
    dp = [- INF for _ in range(N + 1)]
    dp[0] = 0
    for n in range(1, N + 1):
        max_l = dp[n]
        for m in match:
            if n - m >= 0 and dp[n - m] >= 0:
                max_l = max([max_l, 1 + dp[n - m]])
        dp[n] = max_l
    return dp


def main():
    N, _ = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    A.sort(reverse=True)
    match = list(map(num_to_match, A))
    max_l = max_length(N, match)
    digits = [0 for _ in range(max_l[N])]
    n_match = N
    for i in range(max_l[N]):
        for a in A:
            ma = num_to_match(a)
            if n_match - ma < 0:
                continue
            if max_l[n_match - ma] == max_l[n_match] - 1:
                digits[i] = a
                n_match -= ma
                break
    print(''.join(map(str, digits)))


def __starting_point():
    main()
__starting_point()

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
weight = [0,2,5,5,4,5,6,3,7,6]
dp = [-1] * (N+1)
dp[0] =  0

for i in range(N+1):
    for a in A:
        if i + weight[a] < N+1:
            dp[i + weight[a]] = max(dp[i + weight[a]], dp[i]*10 + a)
print((dp[-1]))


n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0,2,5,5,4,5,6,3,7,6]
wv=[]
for i in a:
  wv.append((b[i],i))
wv.sort()
dp=[-1 for i in range(n+1)]
dp[0]=0
for i in range(1,n+1):
  for w,v in wv:
    if i-w<0:
      continue
    if dp[i-w]==-1:
      continue
    dp[i]=max(dp[i],dp[i-w]*10+v)
print(dp[n])

N,M=map(int,input().split())
A=list(map(int,input().split()))

dp=[0]*(N+1)
cost=[0,2,5,5,4,5,6,3,7,6]
for i in range(1,N+1):
    for a in A:
        if i-cost[a]>=0:
            if dp[i-cost[a]]==0 and i-cost[a]>0:
                continue
            dp[i]=max(dp[i],dp[i-cost[a]]*10+a)
print(dp[-1])

N,M=map(int,input().split())
num={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
A=list(map(int,input().split()))
A.sort(reverse = True)
dp = [0]+[-1]*(N+1)
for i in range(N):
    for j in A:
        if i+num[j]<=N:
            dp[i+num[j]] = max(dp[i+num[j]],dp[i]+1)
ans = []
m=N
while m>0:
    for i in A:
        if m-num[i]>=0 and dp[m-num[i]]==dp[m]-1:
          m-=num[i]
          ans.append(str(i))
          break
print("".join(ans))

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
numk={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[0]+[-1]*(N+1)
for i in range(N):
    for j in A:
        if i+numk[j]<=N:
            dp[i+numk[j]]=max(dp[i+numk[j]],dp[i]+1)
m=N
ans=[]
while m>0:
    for i in A:
        if m-numk[i]>=0 and dp[m-numk[i]]==dp[m]-1:
          ans.append(str(i))
          m-=numk[i]
          break
print(''.join(ans))

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
INF = float('inf')
dp = [-INF]*(N + 5)
needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
can_use = {k: needs[k-1] for k in A}
dp[0] = 0
for i in range(2, N+1):
    for v in list(can_use.values()):
        if i - v < 0:
            continue
        dp[i] = max(dp[i], dp[i-v]+1)
ans = ''
i = N
while i != 0:
    for k, v in list(can_use.items()):
        if i - v < 0:
            continue
        while dp[i] == dp[i-v] + 1:
            ans += str(k)
            i -= v
print(ans)


N,M=map(int,input().split())
A=list(map(int,input().split()))

T=[0,2,5,5,4,5,6,3,7,6]
B=[]
for i in A:
    B.append(T[i])
B=sorted(list(set(B)))
dp=[0 for i in range(N+1)]
AB=[[i,T[i]] for i in A]
AB=sorted(AB,reverse=True)

for i in range(1,N+1):
    for j in B:
        if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
    if dp[i]==0:
        dp[i]=-1
#print(dp)
#print(AB)

c=N
U=[0 for i in range(8)]
while c>0:
    for i in range(len(AB)):
        if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U)

ans=""
if 9 in A:
    ans+='9'*U[6]
    U[6]=0
if 8 in A:
    ans+='8'*U[7]
if 7 in A:
    ans+='7'*U[3]
if 6 in A:
    ans+='6'*U[6]
if 5 in A:
    ans+='5'*U[5]
    U[5]=0
if 4 in A:
    ans+='4'*U[4]
if 3 in A:
    ans+='3'*U[5]
    U[5]=0
if 2 in A:
    ans+='2'*U[5]
if 1 in A:
    ans+='1'*U[2]

print(ans)

import sys
sys.setrecursionlimit(10 ** 4)
from fractions import gcd

num = [10**9, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    dp = [[""]*(n+1) for _ in range(len(a))]
    dp[0][num[a[0]]] = str(a[0])
    for i in range(num[a[0]], n+1):
        if len(dp[0][i-num[a[0]]]) > 0:
            dp[0][i] = dp[0][i-num[a[0]]] + str(a[0])
    for i in range(1, len(a)):
        for j in range(n+1):
            if j < num[a[i]]:
                dp[i][j] = dp[i-1][j]
            else:
                if j == num[a[i]] or dp[i][j-num[a[i]]] != "":
                    buf = dp[i][j-num[a[i]]] + str(a[i])
                    if len(buf) < len(dp[i-1][j]):
                        dp[i][j] = dp[i-1][j]
                    else:
                        dp[i][j] = buf
                else:
                    dp[i][j] = dp[i-1][j]
    ans = list(dp[len(a)-1][n])
    ans.sort(reverse=True)
    print("".join(ans))

def __starting_point():
    main()
__starting_point()

n,m=map(int,input().split())
a=list(map(int,input().split()))
temp=[0,2,5,5,4,5,6,3,7,6]
dp=[-1]*(n+1)
dp[0]=0
for i in range(n+1):
  for j in a:
    if i+temp[j]<n+1:
      dp[i+temp[j]]=max(dp[i+temp[j]],dp[i]*10+j)
print(dp[n])

from collections import defaultdict

def f(a, b):
    for i in range(9,0,-1):
        if a[i] == b[i]: continue
        elif a[i] > b[i]:
            return True
        else:
            return False
    return False

N, M = map(int, input().split())
se = set()
A = list(map(int, input().split()))
toNum = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[] for i in range(N+1)]
done = [False for i in range(N+10)]
done[N] = True
dics = [defaultdict(int) for i in range(N+1)]
for i in range(N-1, -1, -1):
    for a in A:
        num = toNum[a]
        if not done[i+num]: continue
        l = len(dp[i+num])+1
        d = dics[i+num].copy()
        d[a] += 1
        if l > len(dp[i]) or (l == len(dp[i]) and f(d, dics[i])):
            dp[i] = dp[i+num] + [a]
            done[i] = True
            dics[i] = d
dp[0].sort(reverse=True)
print(*dp[0], sep='')


#!/usr/bin/env python3
import sys
try:
    from typing import Dict, List
except ImportError:
    pass


def solve(N: int, M: int, A: "List[int]"):
    mn = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    A.sort(reverse=True)
    dp = [-1] * (N + 1)
    dp[0] = 0
    for i in range(1, N + 1):
        m = -1
        for Ai in A:
            if i - mn[Ai] >= 0 and dp[i - mn[Ai]] >= 0:
                m = max(m, dp[i - mn[Ai]] + 1)
        if m >= 0:
            dp[i] = m
    assert dp[N] >= 0
    k = N
    res = []  # type: List[int]
    while dp[k]:
        for Ai in A:
            if k - mn[Ai] >= 0 and dp[k - mn[Ai]] == dp[k] - 1:
                res.append(Ai)
                k -= mn[Ai]
                break

    print(("".join(str(d) for d in res)))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, A)


def __starting_point():
    main()

__starting_point()

def resolve():
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    required = [None]+[2,5,5,4,5,6,3,7,6]
    dp = [0 for _ in range(max(10, N+1))]
    candidates = {}
    for a in A:
        req = required[a]
        if dp[req] < a:
            dp[req] = a
            candidates[req] = a
    #print(dp)
    #print(candidates)
    for i in range(N+1):
        for n_match, num in candidates.items():
            if i - n_match >= 0 and dp[i-n_match] != 0:
                dp[i] = max(dp[i], dp[i-n_match]*10+num)
    #print(dp)
    print(dp[N])

if '__main__' == __name__:
    resolve()

import sys
input = sys.stdin.readline



def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    
    key = dict()
    if 1 in a:
        key[2] = 1
    for v in [5, 3, 2]:
        if v in a:
            key[5] = v
            break
    if 4 in a:
        key[4] = 4
    for v in [9, 6]:
        if v in a:
            key[6] = v
            break
    if 7 in a:
        key[3] = 7
    if 8 in a:
        key[7] = 8
 
    
    dp = [0]*(n+1)
   
    for i in range(n):
        if i != 0 and dp[i] == 0:
            continue
        for k, v in list(key.items()):
            if i+k <= n:
                dp[i+k] = max(dp[i+k], 10*dp[i]+v)
    print((dp[n]))
        
   
    
def __starting_point():
    main()

__starting_point()

N,M=map(int,input().split())
l=list(map(int,input().split()))
w=[2,5,5,4,5,6,3,7,6]
l.sort()
dp=[0]*(max(N+1,8))
for j in l:
   dp[w[j-1]]=j
for i in range(N):
   if dp[i]!=0:
      for j in l:
         if w[j-1]+i<=N:
            dp[i+w[j-1]]=max(dp[i+w[j-1]],dp[i]*10+j)
print(dp[N])

n, m = map(int, input().split())
D = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = list(map(int, input().split()))
C = [(a, D[a]) for a in A]
C.sort(reverse=True)
dp = [-1]*(n+1)
dp[0] = 0
for i in range(1, n+1):
  for a, c in C:
    if i >= c and dp[i-c] >= 0:
      dp[i] = max(dp[i-c]+1, dp[i])
now = n
ans = ""
while now:
  for a, c in C:
    if now-c >= 0 and dp[now] == dp[now-c]+1:
      ans += str(a)
      now -= c
      break
print(ans)

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = [10**5, 2,5,5,4,5,6,3,7,6]
dp = [(0, 0)] * (n + 1)
for i in range(n):
    if i > 0 and dp[i][0] == 0:
        continue
    k, _ = dp[i]
    for ai in a:
        if i + d[ai] <= n:
            dp[i+d[ai]] = max(dp[i+d[ai]], (k+1, ai))
ans = []
i = n
for _ in range(dp[n][0]):
    ans.append(str(dp[i][1]))
    i -= d[dp[i][1]]
print((''.join(ans)))


n, m = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [[-1] * (n+1) for _ in range(m+1)]
for i in range(m+1):
  dp[i][0] = 0
for i in range(m):
  for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i])
print(dp[m][n])

#!/usr/bin/env python3

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))


all_weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
weights = [all_weights[i] for i in a]

# print(weights)

dp = [-1 for _ in range(n+1)]
dp[0] = 0

for i in range(1, n+1):
    # print(dp)
    for j in range(len(weights)):
        if i-weights[j] >= 0:
            dp[i] = max(dp[i-weights[j]]*10 + a[j], dp[i])

print((dp[-1]))


N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
d = [0,2,5,5,4,5,6,3,7,6]
S = set()
for a in A:
    S.add(d[a])

INF = 10 ** 8
dp = [-INF for _ in range(N+1)]
dp[0] = 0

for i in range(N+1):
    for s in S:
        if i - s >= 0:
            dp[i] = max(dp[i], dp[i-s]+1)

ans = 0
digit = dp[N]

for i in range(digit-1, -1, -1):
    if i != 0:
        for a in A:
            if dp[N-d[a]] == i:
                ans = ans * 10 + a
                N -= d[a]
                break
    else:
        for a in A:
            if N == d[a]:
                ans = ans * 10 + a
                break
print(ans)

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
l = [0,2,5,5,4,5,6,3,7,6]

dp = [-1**10]*(n+1)
dp[0] = 0
for i in range(1,n+1):
    
    for j in range(m):
        if i >= l[a[j]] and dp[i] < dp[i-l[a[j]]]+1:
            dp[i] = dp[i-l[a[j]]]+1
            

ans = []
remain = dp[-1]
now = n
for i in a:
    x = l[i]
    while now -x >= 0 and dp[now] == dp[now-x]+1:
        ans.append(i)
        now -= x 
print(*ans,sep="")

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
numk={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[0]+[-1]*(N+1)
for i in range(N):
    for j in A:
        if i+numk[j]<=N:
            dp[i+numk[j]]=max(dp[i+numk[j]],dp[i]+1)
m=N
ans=[]
while m>0:
    for i in A:
        if m-numk[i]>=0 and dp[m-numk[i]]==dp[m]-1:
          m-=numk[i]
          ans.append(str(i))
          break
print("".join(ans))

N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()

if 1 in A:
    if N%2==0:
        print(("1"*(N//2)))
    else:
        if 7 in A:
            print(("7"+"1"*(N//2-1)))
        elif 5 in A:
            print(("5"+"1"*(N//2-2)))
        elif 3 in A:
            print(("3"+"1"*(N//2-2)))
        elif 2 in A:
            print(("2"+"1"*(N//2-2)))
        else:
            print(("8"+"1"*(N//2-3)))
else:
    B=[[] for i in range(0,10)]
    for i in range(0,M):
        if 2 in A:
            B[5].append(2)
        if 3 in A:
            B[5].append(3)
        if 4 in A:
            B[4].append(4)
        if 5 in A:
            B[5].append(5)
        if 6 in A:
            B[6].append(6)
        if 7 in A:
            B[3].append(7)
        if 8 in A:
            B[7].append(8)
        if 9 in A:
            B[6].append(9)


    dp=['n' for i in range(0,max(10,N+1))]
    for i in range(0,min(N+1,10)):
        if i==2:
            if 2 in A:
                dp[5]='2'
        if i==3:
            if 3 in A:
                dp[5]='3'
        if i==4:
            if 4 in A:
                dp[4]='4'
        if i==5:
            if 5 in A:
                dp[5]='5'
        if i==6:
            if 6 in A:
                dp[6]='6'
        if i==7:
            if 7 in A:
                dp[3]='7'
        if i==8:
            if 8 in A:
                dp[7]='8'
        if i==9:
            if 9 in A:
                dp[6]='9'

    if N>=6:
        a='0'
        b='0'
        c='0'
        if B[3]!=[]:
            b='77'
        if dp[6]=='n':
            a=int(a)
            b=int(b)
            c=int(c)
            x=max(a,b,c)
            if x!=0:
                dp[6]=str(x)
        else:
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(dp[6])
            dp[6]=str(max(a,b,c,d))

    if N>=7:
        a='0'
        b='0'
        c='0'
        d='0'
        if B[3]!=[] and dp[4]!='n':
            b='7'+dp[4]
        if B[4]!=[] and dp[3]!='n':
            c='47'
        if dp[7]=='n':
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(d)
            x=max(a,b,c,d)
            if x!=0:
                dp[7]=str(x)
        else:
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(d)
            e=int(dp[7])
            dp[7]=str(max(e,a,b,c,d))

    for i in range(8,N+1):
        a='0'
        b='0'
        c='0'
        d='0'
        e='0'
        f='0'
        if B[3] and dp[i-3]!='n':
            b='7'+dp[i-3]
        if B[4] and dp[i-4]!='n':
            c='4'+dp[i-4]
        if B[5] and dp[i-5]!='n':
            d=str(max(B[5]))+dp[i-5]
        if B[6] and dp[i-6]!='n':
            e=str(max(B[6]))+dp[i-6]
        if B[7] and dp[i-7]!='n':
            f='8'+dp[i-7]
        a=int(a)
        b=int(b)
        c=int(c)
        d=int(d)
        e=int(e)
        f=int(f)
        x=max(a,b,c,d,e,f)
        if x==0:
            dp[i]='n'
        else:
            dp[i]=str(x)
    print((int(dp[N])))


def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("")

def __starting_point():
    main()
__starting_point()

from collections import defaultdict
N,M,*A = map(int, open(0).read().split())
ls = [0]*10
ls[1] = 2
ls[2] = 5
ls[3] = 5
ls[4] = 4
ls[5] = 5
ls[6] = 6
ls[7] = 3
ls[8] = 7
ls[9] = 6
dic = defaultdict(int)
for a in A:
  k = ls[a]
  dic[k] = max(dic[k],a)
dp = [-1]*(N+1)
for k in dic.keys():
  if k>N:
    continue
  dp[k] = dic[k]
for i in range(N+1):
  for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m)
print(dp[N])

def max_for_str(a, b):
    if len(a) > len(b):
        return a
    elif len(a) < len(b):
        return b
    else:#This case is unlikely to happen.
        for c1, c2 in zip(a, b):
            if c1 > c2:
                return a
            elif c1 < c2:
                return b
        return a
            
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

N, M = map(int, input().split())
As = list(input().split())

dp = [None] * (N+1)
dp[0] = ""

for i in range(N+1):
    for A in As:
        if i - match[int(A)] >= 0 and not dp[i - match[int(A)]] is None:
            if dp[i] is None:
                dp[i] = A + dp[i - match[int(A)]]
            else:
                dp[i] = max_for_str(dp[i] , A + dp[i - match[int(A)]])

print(dp[N])

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
weight = [0,2,5,5,4,5,6,3,7,6]
inf = float('inf')
dp = [-inf] * (N+1)
dp[0] = 0
for i in range(N+1):
    for a in A:
        if i + weight[a] < N+1:
            dp[i + weight[a]] = max(dp[i + weight[a]], dp[i]*10 + a)
print((dp[-1]))


n,m = map(int,input().split())
a = [int(i) for i in input().split()]
match = [0,2,5,5,4,5,6,3,7,6]
dp = [-1]*(n+1)
dp[0] = 0
for i in range(1,n+1):
    for j in a:
        if i - match[j] < 0: continue
        if dp[i-match[j]] == -1: continue
        dp[i] = max(dp[i], dp[i-match[j]]*10+j)
# print(dp)
print(dp[n])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()

if 1 in A:
    if N%2==0:
        print("1"*(N//2))
    else:
        if 7 in A:
            print("7"+"1"*(N//2-1))
        elif 5 in A:
            print("5"+"1"*(N//2-2))
        elif 3 in A:
            print("3"+"1"*(N//2-2))
        elif 2 in A:
            print("2"+"1"*(N//2-2))
        else:
            print("8"+"1"*(N//2-3))
elif 7 in A:
    if N%3==0:
        print("7"*(N//3))
    elif N%3==1:
        if 4 in A:
            print("7"*(N//3-1)+"4")
        elif 8 in A:
            print("8"+"7"*(N//3-2))
        elif 5 in A:
            print("7"*(N//3-3)+"55")
        elif 3 in A:
            print("7"*(N//3-3)+"33")
        elif 2 in A:
            print("7"*(N//3-3)+"22")
    else:
        if 5 in A:
            print("7"*(N//3-1)+"5")
        elif 3 in A:
             print("7"*(N//3-1)+"3")
        elif 2 in A:
            print("7"*(N//3-1)+"2")
        elif 4 in A:
            print("7"*(N//3-2)+"44")
        elif 8 in A:
            print(print("7"*(N//3-4)+"88"))

else:
    B=[[] for i in range(0,10)]
    for i in range(0,M):
        if 2 in A:
            B[5].append(2)
        if 3 in A:
            B[5].append(3)
        if 4 in A:
            B[4].append(4)
        if 5 in A:
            B[5].append(5)
        if 6 in A:
            B[6].append(6)
        if 8 in A:
            B[7].append(8)
        if 9 in A:
            B[6].append(9)


    dp=['n' for i in range(0,max(10,N+1))]
    for i in range(0,min(N+1,10)):
        if i==2:
            if 2 in A:
                dp[5]='2'
        if i==3:
            if 3 in A:
                dp[5]='3'
        if i==4:
            if 4 in A:
                dp[4]='4'
        if i==5:
            if 5 in A:
                dp[5]='5'
        if i==6:
            if 6 in A:
                dp[6]='6'
        if i==8:
            if 8 in A:
                dp[7]='8'
        if i==9:
            if 9 in A:
                dp[6]='9'

    if N>=6:
        a='0'
        b='0'
        c='0'
        if dp[6]=='n':
            a=int(a)
            b=int(b)
            c=int(c)
            x=max(a,b,c)
            if x!=0:
                dp[6]=str(x)
        else:
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(dp[6])
            dp[6]=str(max(a,b,c,d))

    if N>=7:
        a='0'
        b='0'
        c='0'
        d='0'
        if dp[7]=='n':
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(d)
            x=max(a,b,c,d)
            if x!=0:
                dp[7]=str(x)
        else:
            a=int(a)
            b=int(b)
            c=int(c)
            d=int(d)
            e=int(dp[7])
            dp[7]=str(max(e,a,b,c,d))

    for i in range(8,N+1):
        a='0'
        b='0'
        c='0'
        d='0'
        e='0'
        f='0'
        if B[4] and dp[i-4]!='n':
            c='4'+dp[i-4]
        if B[5] and dp[i-5]!='n':
            d=str(max(B[5]))+dp[i-5]
        if B[6] and dp[i-6]!='n':
            e=str(max(B[6]))+dp[i-6]
        if B[7] and dp[i-7]!='n':
            f='8'+dp[i-7]
        a=int(a)
        b=int(b)
        c=int(c)
        d=int(d)
        e=int(e)
        f=int(f)
        x=max(a,b,c,d,e,f)
        if x==0:
            dp[i]='n'
        else:
            dp[i]=str(x)
    print(int(dp[N]))


import sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
d=dict()
need=[0,2,5,5,4,5,6,3,7,6]
for i in range(m):
    if need[a[i]] not in d:
        d[need[a[i]]]=a[i]
    else:
        if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i]
d=list(d.items())
d.sort()
m=len(d)
dp=[[-1]*10 for _ in range(n+1)]
dp[0]=[0]*10
for i in range(n+1):
    for j in range(m):
        if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(list(range(1,10))):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1
ans=str()
for i in reversed(list(range(1,10))):
    ans+=str(i)*dp[-1][i]
print(ans)
#print(d)
#print(dp[-1])


from collections import defaultdict

N, M = map(int, input().split())
A = list(map(int, input().split()))
L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
D = defaultdict(int)

for i in A:
  if D[L[i]] < i:
    D[L[i]] = i

INF = float("inf")
dp = [-INF]*(N+1)
dp[0] = 0

for i in range(1, N+1):
  for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF)

ans = ""
temp = N

#なるべく大きい数字から使う
for i, x in sorted(D.items(), key=lambda x: x[1], reverse=True):
  while temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
    ans += str(x)
    temp -= i

print(ans)

N, M = map(int, input().split())

A = list(map(int, input().split()))
B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

DP = [-1]*(N+1)
DP[0] = 0 

for i in range(N):
    for j in A:
        if i+B[j] <= N:
            DP[i+B[j]] = max(DP[i]*10+j, DP[i+B[j]])

print(DP[N]) 

import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
As = list(mapint())
num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6]
need_m = [num_lis[a-1] for a in As]
dp = [-1]*(N+1)
dp[0] = 0

for i in range(1, N+1):
    for a, need in zip(As, need_m):
        if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a)

print(dp[-1])

cost=[-1,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))

dp=[None]*(N+1)
dp[0]=''

for i in range(1,N+1):
    for a in A:
        if i-cost[a]>=0 and dp[i-cost[a]]!=None:
            s=dp[i-cost[a]]+str(a)
            if dp[i]==None or len(dp[i])<len(s):
                dp[i]=s
            elif len(dp[i])>len(s):
                continue
            else:
                dp[i]=max(dp[i],s)
            
            
print(dp[N])

#!/usr/bin/env python3

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))


all_weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
weights = [all_weights[i] for i in a]

# print(weights)

dp = [-1 for _ in range(n+1)]
dp[0] = 0

for i in range(1, n+1):
    # print(dp)
    for j in range(len(weights)):
        if i-weights[j] >= 0:
            dp[i] = max(dp[i-weights[j]]*10 + a[j], dp[i])

print((dp[-1]))


N, M = map(int, input().split())
A = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1]*(N+1)
dp[0] = 0
for i in range(N+1):
    for a in A:
        if i + weight[a]<=N:
            dp[i+weight[a]] = max(dp[i+weight[a]],dp[i]*10+a)
print(dp[N])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
numk={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[0]+[-1]*(N+1)
for i in range(N):
    for j in A:
        if i+numk[j]<=N:
            dp[i+numk[j]]=max(dp[i+numk[j]],dp[i]+1)
m=N
ans=[]
while m>0:
    for i in A:
        if m-numk[i]>=0 and dp[m-numk[i]]+1==dp[m]:
            ans.append(str(i))
            m-=numk[i]
            break
print(''.join(ans))

import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [6,2,5,5,4,5,6,3,7,6] # B[i]がiを作るのに必要なマッチ棒の数

# dp[i]: i本マッチ使って作れるMAX桁数。。
dp = [-1 for _ in range(N+10)]
dp[0] = 0
A.sort()
visited = [False for _ in range(N+10)]
h = []
heapq.heappush(h, (0,0))

while h:
  now_n, keta = heapq.heappop(h)
  if visited[now_n]:
    continue
  visited[now_n] = True
  for a in A:
    nxt_n = now_n + B[a]
    if nxt_n >= N + 10:
      continue
    dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
    heapq.heappush(h, (nxt_n, dp[now_n] + 1))
    
#print(dp)

A.sort(reverse=True)
# 上の桁から決めていく。dp[N]
ANS = []
zan = N
for i in range(dp[N],0,-1):
  bugf = True
  # sorted(a) 大きい方から試す
  for a in A:
    if dp[zan-B[a]] == dp[zan] - 1:
      ANS.append(str(a))
      zan = zan - B[a]
      bugf = False
      break
  if bugf:
    # Aが構成できない、と言うことなのでここは通らないはず
    print("bug...")
  
print("".join(ANS))

"""
- minでの桁数
- その後の余りでgreedy
- 
"""

num_dic = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

N, M = list(map(int, input().split()))
t_dic = {}
A = list(map(int, input().split()))
A = sorted(A)[::-1]
for a in A:
    t_dic[a] = num_dic[a]
min_v = min(t_dic.values())

dp = [0] * (N+8)
for i in range(N):
    for a in A:
        num = t_dic[a]
        if i !=0 and dp[i] == 0:
            continue
        dp[i+num] = max(10 * dp[i]+ a, dp[i+num])

print(dp[N])

def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n])

main()

import sys
readline = sys.stdin.readline

N,M = map(int,readline().split())
A = set(list(map(int,readline().split())))

dic = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

# ある本数で作れる数
candi = [[],[],[1],[7],[4],[2,3,5],[6,9],[8]]

# たとえば2と3を両方作れるときに、2を作るメリットはない。
if 2 in A and 3 in A:
  A.remove(2)
if 2 in A and 5 in A:
  A.remove(2)
if 3 in A and 5 in A:
  A.remove(3)
if 6 in A and 9 in A:
  A.remove(6)

for i in range(len(candi)):
  for j in range(len(candi[i])-1,-1,-1):
    if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較
dp = [-1] * (N + 1)
ans = [""] * (N + 1)
dp[0] = 0
def to_num(x):
  return int("".join(sorted(x, reverse = True)))
for i in range(len(candi)):
  if not candi[i]:
    continue
  num = str(candi[i][0]) # i文字使って作る文字
  # i文字使って作る
  for j in range(len(dp)):
    if dp[j] == -1:
      continue
    #print("j",j)
    if j + i > N:
      continue
    if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans)

print(to_num(ans[N]))

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

dp = [0] * (N+1)

t = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

for i in range(1, N+1):
    for a in A:
        if 0 <= i-t[a]:     
            if dp[i-t[a]] == 0 and i-t[a] > 0: continue
            dp[i] = max(dp[i], dp[i-t[a]]*10+a)
print(dp[N])

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))

num_of_matches = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = ['x'] * (n+8)
dp[0] = ''
for i in range(n+1):
    if dp[i] == 'x':
        continue
    for aa in a:
        x = dp[i + num_of_matches[aa]]
        y = dp[i] + str(aa)
        if x == 'x':
            dp[i + num_of_matches[aa]] = y
        elif len(x) < len(y):
            dp[i + num_of_matches[aa]] = y
        elif len(x) == len(y):
            if x < y:
                dp[i + num_of_matches[aa]] = y
print((dp[n]))


n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
L = [2,5,5,4,5,6,3,7,6]
dp = [-float('inf')]*(n+1)
dp[0] = 0
for i in range(1,n+1):
    for j in range(m):
        if i-L[A[j]-1] < 0:
            None
        else:
            dp[i] = max(dp[i-L[A[j]-1]]+1, dp[i])
A.sort()
A.reverse()
cur = n
ans = ''
while cur != 0:
    for i in range(m):
        temp = A[i]
        if cur-L[temp-1] >= 0:
            if dp[cur-L[temp-1]] == dp[cur]-1:
                ans = ans+str(temp)
                cur -= L[temp-1]
                break
print(ans)


N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")


n, m = map(int, input().split()) 
a = list(map(int, input().split()))
b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1] * (n + 1)
dp[n] = 0

for i in range(n, -1, -1):
    for j in range(m):
        x = b[a[j]]
        if dp[i] != -1 and i - x >= 0:
            dp[i - x] = max(dp[i - x], 10 * dp[i] + a[j])
            
print(dp[0])

# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,M = map(int,input().split())
A = list(map(int,input().split()))

# 各数字に必要なマッチの本数
num = [0,2,5,5,4,5,6,3,7,6]

# dp : i本のマッチで作ることが可能な最大の桁数
dp = [-float("inf")]*(N+1)
# dp初期化 
#   - 0本 -> 0桁
#   - 各数字を1つ作る場合は1桁
dp[0] = 0
for a in A:
    if num[a] <= N:
        dp[num[a]] = 1

# dp更新
for i in range(1,N+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元
A.sort(reverse=True)
ans = ""
for i in range(dp[N]):
    for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break

print(ans)

n,m=list(map(int,input().split()))
sz=[0,2,5,5,4,5,6,3,7,6]
l=[]
dp=[0]*(n+1)
l=list(map(int,input().split()))
l.sort(reverse=True)
dp[0]=1
for i in range(1,n+1):
    for j in l:
        if i-sz[j]>=0 and dp[i-sz[j]]>0:
            dp[i]=max(dp[i],1+dp[i-sz[j]])
#print(dp)
ans=[]
#using the dp table construct the number 
cur=n
while cur>0:#cur denotes count of available match sticks
 #   print(cur)
    for j in l:
        if cur-sz[j]>=0:
            if dp[cur]==1+dp[cur-sz[j]] and dp[cur-sz[j]]>0:
                ans.append(j)
                cur-=sz[j]
                break
ans.sort(reverse=True)
print((''.join(map(str,ans))))




def main():
    C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dp = [0] + [-1] * N
    K = {a: C[a - 1] for a in A}

    for i in range(min(K.values()), N + 1):
        try:
            dp[i] = max(dp[i - v] * 10 + k for k, v in list(K.items()) if i - v >= 0 and dp[i - v] >= 0)
        except ValueError:
            dp[i] = -1
    print((dp[N]))
main()


n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[0,2,5,5,4,5,6,3,7,6]
dp=[0]+[-1]*(9*n)
for i in range(n):
  for j in a:
    if dp[i]>=0:
      dp[i+l[j]]=max(dp[i+l[j]],dp[i]*10+j)
print(dp[n])

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))

a.sort(reverse=True)
num_of_matches = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n+1)
dp[0] = 0
for i in range(1, n+1):
    val = -1
    for aa in a:
        if i - num_of_matches[aa] < 0:
            continue
        if i - num_of_matches[aa] == 0:
            val = 1
        elif dp[i - num_of_matches[aa]] > 0:
            val = max(val, dp[i - num_of_matches[aa]] + 1)
    dp[i] = val

res = ''
remain = dp[n]
match = n
while match > 0:
    for i in range(m):
        if match - num_of_matches[a[i]] >= 0 and remain - 1 == dp[match-num_of_matches[a[i]]]:
            match -= num_of_matches[a[i]]
            remain -= 1
            res += str(a[i])
            break
print(res)


#!/usr/bin/env python3

def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))

    # dig: マッチ本数 -> 使える数字
    nm = [999, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    dig = [None for i in range(8)]
    for x in sorted(a):
        dig[nm[x]] = x

    # dp[j]: j本のマッチを使って作れる最大の数 tuple(桁数d, prevj, lastdig)
    dp = [None for j in range(n + 1)]
    dp[0] = (0, None, None)
    for j in range(1, n + 1):  # もらう
        for use in [2, 3, 4, 5, 6, 7]:
            jp = j - use
            if jp < 0:
                continue
            if dig[use] is None:
                continue
            if dp[jp] is None:
                continue
            d = dp[jp][0] + 1
            tup = (d, jp, dig[use])
            if (dp[j] is None) or (dp[j][0] < d) or (dp[j][0] == d and dp[j][2] < dig[use]):
                dp[j] = tup

    res = []
    j = n
    while True:
        d, jp, i = dp[j]
        if jp is None:
            break
        res.append(str(i))
        j = jp
    assert len(res) == dp[n][0]

    print(("".join(res)))

def __starting_point():
    main()

__starting_point()

def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0])
def __starting_point():
  main()
__starting_point()

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):  # dpで0~N本のマッチを探索
    for a in A:  # Aに入ってるもの（作れる数字）を探索
        if i + weight[a] < N + 1:  # i+(今回作る数字に必要なマッチ)本のマッチがN以下だったらOk
            dp[i + weight[a]] = max(dp[i + weight[a]], dp[i]
                                    * 10 + a)
print((dp[N]))


N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort(reverse=True)
honsuu=[0,2,5,5,4,5,6,3,7,6]

def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
  

ans=""
keta=ketadp(N,M,A)
remain=keta[N]
match=N
#print(keta)

while(match>0):
  for i in A:
    #print(i,match,remain,keta[match-honsuu[i]])
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break;
print(ans)

n, m = map(int, input().split())
a = list(map(int, input().split()))
dp = [-1] * (n * 18)
for i in range(n * 9):
    dp[i] = 0
lis = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(n):
    cnt = -float("inf")
    for j in a:
        cnt = max(cnt, dp[i + 1 - lis[j]] * 10 + j)
    dp[i + 1] = cnt
print(dp[n])

n, m = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [[0] * (n+1) for _ in range(m+1)]
for i in range(m):
  for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == 0:
        if j == cost[A[i]]:
          dp[i+1][j] = max(A[i], dp[i][j])
        else:
          dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i])
print(dp[m][n])

import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N])



def __starting_point():
    main()
__starting_point()

def main():
    n, m, *a = list(map(int, open(0).read().split()))
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)

    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        for j in a:
            dp[i] = max(dp[i], dp[i - t[j]] + 1)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans)


def __starting_point():
    main()

__starting_point()

import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = list(map(int, input().split()))
    A = list(map(int, input().split()))

    L = [(1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
    B = []
    for num, cost in L:
        if num in A:
            B.append([num, cost])
    B.sort(key=lambda x: -x[0])
    dp = [-f_inf] * (n + 1)
    dp[0] = 0
    for i in range(n + 1):
        for _, cost in B:
            if i - cost == 0:
                dp[i] = max(dp[i], dp[0] + 1)
            elif i - cost > 0 and dp[i - cost] != 0:
                dp[i] = max(dp[i], dp[i - cost] + 1)
    j = 0
    k = n
    res = ""
    while k > 0 and j < m:
        num, cost = B[j]
        if k - cost >= 0 and dp[k] - dp[k - cost] == 1:
            res += str(num)
            k -= cost
        else:
            j += 1
    print(res)


def __starting_point():
    resolve()

__starting_point()

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    #使う本数の種類
    b = set()
    matti = [2,5,5,4,5,6,3,7,6]
    for i in a:
        b.add(matti[i-1])

    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(n+1):
        for j in b:
            if i >= j:
                dp[i] = max(dp[i-j]+1, dp[i])

    keta = dp[-1]
    res = []
    a.sort(reverse=True)
    cnt = n
    while cnt>0:
        for i in a:
            if cnt >= matti[i-1]:
                if dp[cnt]-dp[cnt-matti[i-1]]==1:
                    res.append(str(i))
                    cnt -= matti[i-1]
                    break
    print(''.join(res))


def __starting_point():
    main()
__starting_point()

import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
def __starting_point():
    main()
__starting_point()

def main():
    n, m, *a = list(map(int, open(0).read().split()))
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)

    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        for j in a:
            dp[i] = max(dp[i], dp[i - t[j]] + 1)

    nums = []
    k = dp[n]
    while n:
        for x in a:
            if dp[n - t[x]] == k - 1:
                nums.append(x)
                n -= t[x]
                break
        k -= 1

    ans = ''.join(map(str, nums))
    print(ans)


def __starting_point():
    main()

__starting_point()

nim,mike = map(int,input().split())
array = list(map(int,input().split()))

dp = [-1 for i in range(nim+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(nim-1):
    for s in array:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[nim])

def main():
    n, m, *a = list(map(int, open(0).read().split()))
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans)


def __starting_point():
    main()

__starting_point()

import sys
input = sys.stdin.readline
INF = float("inf")
# 解説参照

def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort(reverse=True)  # 使用可能は数字は大きい順にソートしておく
    cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    # dp[i]:= マッチi本で作れる最大桁数
    dp = [-1] * (n + 1)
    dp[0] = 0
    for i in range(1, n + 1):
        for aj in a:
            if i - cost[aj] < 0:
                continue
            dp[i] = max(dp[i], dp[i - cost[aj]] + 1)
    digits = dp[n]
    ans = ""
    rest = n
    # 上から貪欲
    while digits:
        for aj in a:
            if rest - cost[aj] < 0:
                continue
            if dp[rest - cost[aj]] == digits - 1:
                ans += str(aj)
                rest -= cost[aj]
                digits -= 1
                break
    print(ans)


def __starting_point():
    main()

__starting_point()

n, m = map(int, input().split())
a = list(map(int, input().split()))

match_num = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = [-1] * (n+10)
dp[0] = 0

for i in range(n):
    for j in range(m):
        nxt = match_num[a[j]]
        if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j])
print(dp[n])

ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
cnt = [-1,2,5,5,4,5,6,3,7,6]
n,m = ma()
A = lma()
use=[(cnt[a],a) for a in A]
use.sort(key=lambda x:x[1],reverse=True)
INF=10**9
dp = [0]+[-INF]*(n+10)
for i in range(n):
    if dp[i]==-INF:continue
    for c,a in use:
        dp[i+c]=max(dp[i+c],dp[i]+1)
l = dp[n]
ans = ["0"]*l
num = n
for i in range(l):
    for c,a in use:
        if dp[num-c]==dp[num]-1:
            ans[i]=str(a)
            num-=c
            break
print("".join(ans))


n,m = map(int,input().split())
num = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
a = list(map(int,input().split()))
a.sort(reverse = True)
dp = [0]+[-1]*(n+1)
for i in range(n):
    for j in a:
        if i+num[j] <=n:
            dp[i+num[j]] = max(dp[i+num[j]],dp[i]+1)
ans = []
match = n
while match > 0:
    for i in a:
        if match-num[i] >= 0 and dp[match-num[i]] == dp[match]-1:
            match -= num[i]
            ans.append(str(i))
            break
print("".join(ans))

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(m):
    numberlist.append(matchlist[a[i]])
numberlist = set(numberlist)
#dp[i]:i本でのmax桁
dp = [-10000000]*(n+1)
dp[0] = 0
for i in range(n):
    for j in numberlist:
        if i+j <= n:
            dp[i+j] = max(dp[i+j], dp[i]+1)


ans = []
d = dp[n]
a.sort(reverse = True)
for i in range(d):
    for j in a:
        if n - matchlist[j] >= 0:
            if dp[n] == dp[n-matchlist[j]] + 1:
                n -= matchlist[j]
                ans.append(j)
                break
print(("".join(map(str,ans))))


n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
p=[2,5,5,4,5,6,3,7,6]
num=[]
for x in a:
	num.append(p[x-1])
inf=10**8
dp=[-inf]*(n+1)
dp[0]=0
for i in range(n):
	for j in range(m):
		if i+num[j]<=n and dp[i]!=-inf:
			dp[i+num[j]]=max(dp[i+num[j]],dp[i]+1)
ans=""
now=n
for i in range(dp[n]):
	for j in range(m-1,-1,-1):
		if now-num[j]<0:
			continue
		if dp[now-num[j]]==dp[now]-1:
			now-=num[j]
			ans+=str(a[j])
			break
print(ans)

n, m = map(int, input().split())
A = list(map(int, input().split()))

d = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

INF  =10**18
dp = [-INF]*(n+1)
dp[n] = 0
for i in reversed(range(1, n+1)):
    for a in A:
        j = i-d[a]
        if j >= 0:
            dp[j] = max(dp[j], dp[i]*10+a)
print(dp[0])

N, M = list(map(int, input().split()))
A = tuple(map(int, input().split()))

M_lst = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1] * (N + 10)
dp[0] = 0
for i in range(N + 1):
    if dp[i] == -1:
        continue
    for a in A:
        dp[i + M_lst[a]] = max(dp[i + M_lst[a]], dp[i] * 10 + a)
print((dp[N]))


n,m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
x=[0,2,5,5,4,5,6,3,7,6]
d=dict()
for j in l:
    d[x[j]]=j
INF=float('inf')
dp=[-INF for i in range(n+1)]
dp[0]=0
l=d.keys()
for i in range(2,n+1):
    for j in d.keys():
        if j<=i:
            dp[i]=max(dp[i],dp[i-j]*10+d[j])
print(dp[n])

def main():
    n, m, *a = list(map(int, open(0).read().split()))
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans)


def __starting_point():
    main()

__starting_point()

import sys

input = sys.stdin.readline


def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print((dp[-1]))



def __starting_point():
    main()

__starting_point()

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

match = [10**18, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# 作れる数字の個数
dp = [-10**18] * (N + 1)
dp[0] = 1

for i in range(N):
    for a in A:
        m = match[a]
        if i + m <= N and dp[i + m] < dp[i] + 1:
            dp[i + m] = dp[i] + 1

ans = []
while N > 0:
    for a in A:
        m = match[a]
        if N - m >= 0 and dp[N - m] + 1 == dp[N]:
            ans.append(a)
            N -= m
            break

ans.sort(reverse=True)

print((''.join(map(str, ans))))


N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
req = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

t = [-1]*(N+1)
t[0] = 0
for a in A:
    q = req[a]
    for i in range(q, N+1):
        t[i] = max(t[i], t[i-q] * 10 + a)
print((t[-1]))


# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
    MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)

# dp[i] := i本のマッチを使って作れる最大桁数
dp = [-1] * (N+1)
dp[0] = 0
for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1)

remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break

print(ans)

N,M = map(int,input().split())
A = list(map(int,input().split()))
C = [0,1,4,4,3,4,5,2,6,5]
dp = N*[0]+9*[-1]

for n in range(N):
  dp[n+1] = max(a+10*dp[n-C[a]] for a in A)

print(dp[N])

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

n,m=map(int, input().split())
lst=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6]
DP=[-1]*(n+1)
DP[0]=0
for i in range(n+1):
  for x in lst:
    if i+weight[x]<n+1:
      DP[i+weight[x]]=max(DP[i+weight[x]], DP[i]*10+x)
print(DP[n])

from collections import defaultdict

N, M = map(int, input().split())
A = list(map(int, input().split()))
L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
D = defaultdict(int)

for i in A:
  if D[L[i]] < i:
    D[L[i]] = i

INF = float("inf")
dp = [-INF]*(N+1)
dp[0] = 0

for i in range(1, N+1):
  for j in D.keys():
    if i-j >= 0:
      dp[i] = max(dp[i], dp[i-j] + 1)
    else:
      dp[i] = max(dp[i], -INF)

ans = ""
temp = N

while temp > 0:
  for i, x in sorted(D.items(), key=lambda x: x[1], reverse=True):
    if temp - i >= 0 and dp[temp-i] == dp[temp] - 1:
      ans += str(x)
      temp -= i
      break

print(ans)

import sys
N,M = map(int,sys.stdin.readline().rstrip().split())
A = list(map(int,sys.stdin.readline().rstrip().split()))

from collections import defaultdict

d = defaultdict(int)  # d[i] = (Aの中でi本用いて表せる最大の数)
for i in range(M):
    if A[i] == 1:
        d[2] = 1
    elif A[i] == 7:
        d[3] = 7
    elif A[i] == 4:
        d[4] = 4
    elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
    elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
    else:
        d[7] = 8

B = list(d.keys())
B.sort()

a = B[0]
B[0] = 0

if N % a == 0:
    print(''.join([str(d[a])]*(N//a)))
    return

from itertools import combinations_with_replacement

C = []

for i in range(N//a,max(-1,N//a-7),-1):  # aをi回用いる
    x = N-a*i
    for u in list(combinations_with_replacement(B,5)):
        if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s))

D = []
for i,j in C:
    if i + len(j) == C[0][0] + len(C[0][1]):
        D.append((i,j))

m = min(D[i][0] for i in range(len(D)))

q,r,s = 0,0,0
for i,j in D:
    X = [d[a]]*(i-m) + [d[k] for k in j]
    X.sort(reverse=True)
    M = ''
    for k in X:
        M += str(k)
    M = int(M)
    if q < M:
        q,r,s = M,i,j

E = [d[a]]*r + [d[i] for i in s]
E.sort(reverse=True)

ans = ''
for i in E:
    ans += str(i)
print(ans)