#!/usr/bin/env python3
#ABC118 D

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

M = [0,2,5,5,4,5,6,3,7,6]
n,m = map(int,input().split())
a = list(map(int,input().split()))
A = [[i,M[i]] for i in a]
A.sort(key = itemgetter(1))

#dp[i] := ちょうどマッチi本を使ってできる最大桁数
dp = [-float('inf')]*(n+1)
dp[0] = 0
for i in range(n):
    for j,k in A:
        if i + k < n+1:
            if dp[i] < 0:
                continue
            dp[i+k] = dp[i] + 1
A.sort(key = itemgetter(0),reverse = True)
ans = ''
k = n
while 1:
    for i,j in A:
        if k - j < 0:
            continue
        if dp[k-j] == dp[k]-1:
            ans += str(i)
            k -= j
            break
    else:
        break
print(ans) import sys sys import math math import bisect bisect from heapq import heappush, heappop from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 M = [0,2,5,5,4,5,6,3,7,6] M [0,2,5,5,4,5,6,3,7,6] 0 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 [i,M[i]] for i in a for i in a i a for i in a [i,M[i]] i M[i] M i A = [[i,M[i]] for i in a] A [[i,M[i]] for i in a] A.sort(key = itemgetter(1)) A.sort A sort key = itemgetter(1) key itemgetter(1) itemgetter 1 dp = [-float('inf')]*(n+1) dp [-float('inf')]*(n+1) [-float('inf')] -float('inf') float('inf') float 'inf' n+1 n 1 dp[0] = 0 dp[0] dp 0 0 for i in range(n):
    for j,k in A:
        if i + k < n+1:
            if dp[i] < 0:
                continue
            dp[i+k] = dp[i] + 1 i range(n) range n for j,k in A:
        if i + k < n+1:
            if dp[i] < 0:
                continue
            dp[i+k] = dp[i] + 1 j k A if i + k < n+1:
            if dp[i] < 0:
                continue
            dp[i+k] = dp[i] + 1 i + k < n+1 i + k i k n+1 n 1 if dp[i] < 0:
                continue
             dp[i] < 0 dp[i] dp i 0 continue dp[i+k] = dp[i] + 1 dp[i+k] dp i+k i k dp[i] + 1 dp[i] dp i 1 A.sort(key = itemgetter(0),reverse = True) A.sort A sort key = itemgetter(0) key itemgetter(0) itemgetter 0 reverse = True reverse True ans = '' ans '' k = n k n while 1:
    for i,j in A:
        if k - j < 0:
            continue
        if dp[k-j] == dp[k]-1:
            ans += str(i)
            k -= j
            break
    else:
        break 1 for i,j in A:
        if k - j < 0:
            continue
        if dp[k-j] == dp[k]-1:
            ans += str(i)
            k -= j
            break
    else:
        break i j A if k - j < 0:
            continue
         k - j < 0 k - j k j 0 continue if dp[k-j] == dp[k]-1:
            ans += str(i)
            k -= j
            break
     dp[k-j] == dp[k]-1 dp[k-j] dp k-j k j dp[k]-1 dp[k] dp k 1 ans += str(i) ans str(i) str i k -= j k j break break print(ans) print ans n,m = map(int,input().split()) map(int,input().split()) n A = [[i,M[i]] for i in a] [[i,M[i]] for i in a] A dp = [-float('inf')]*(n+1) [-float('inf')]*(n+1) dp mod = 10**9 + 7 10**9 + 7 mod k -= j j k m = map(int,input().split()) map(int,input().split()) m a = list(map(int,input().split())) list(map(int,input().split())) a ans = '' '' ans k = n n k ans += str(i) str(i) ans M = [0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] M
#!/usr/bin/env python3
#ABC118 D

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

M = [0,2,5,5,4,5,6,3,7,6]
n,m = map(int,input().split())
a = list(map(int,input().split()))
A = [[i,M[i]] for i in a]
A.sort(key = itemgetter(1))

#dp[i] := ちょうどマッチi本を使ってできる最大桁数
dp = [-float('inf')]*(n+1)
dp[0] = 0
for i in range(n):
    for j,k in A:
        if i + k < n+1:
            if dp[i] < 0:
                continue
            dp[i+k] = dp[i] + 1
A.sort(key = itemgetter(0),reverse = True)
ans = ''
k = n
while 1:
    for i,j in A:
        if k - j < 0:
            continue
        if dp[k-j] == dp[k]-1:
            ans += str(i)
            k -= j
            break
    else:
        break
print(ans)
