# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# まずは桁数を増やす→数字の大きい順に並べる
N, M = lr()
A = lr()
matches = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = [(matches[a], a) for a in A]
A.sort(key = lambda x: x[1], reverse=True)
A.sort(key = lambda x: x[0])
top_match = A[0][0]
dp = [None] * (N+1)
dp[0] = []
used = set()
for match, num in A:
    if match in used:
        continue
    used.add(match)
    for x in range(N+1):
        if x - match < 0:
            continue
        if dp[x] == None and dp[x-match] != None:
            dp[x] = dp[x-match] + [num]
        elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num]

X = dp[N]
X.sort(reverse=True)
answer = ''.join(list(map(str, X)))
print(answer)
# 37 import sys sys sr = lambda: sys.stdin.readline().rstrip() sr 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 ir = lambda: int(sr()) ir lambda: int(sr()) int(sr()) int sr() sr lr = lambda: list(map(int, sr().split())) lr lambda: list(map(int, sr().split())) list(map(int, sr().split())) list map(int, sr().split()) map int sr().split() ().split () sr split N, M = lr() N lr() lr M A = lr() A lr() lr matches = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] matches [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 (matches[a], a) for a in A for a in A a A for a in A (matches[a], a) matches[a] matches a a A = [(matches[a], a) for a in A] A [(matches[a], a) for a in A] A.sort(key = lambda x: x[1], reverse=True) A.sort A sort key = lambda x: x[1] key lambda x: x[1] x[1] x 1 x reverse=True reverse True A.sort(key = lambda x: x[0]) A.sort A sort key = lambda x: x[0] key lambda x: x[0] x[0] x 0 x top_match = A[0][0] top_match A[0][0] [0] A 0 0 dp = [None] * (N+1) dp [None] * (N+1) [None] None N+1 N 1 dp[0] = [] dp[0] dp 0 [] used = set() used set() set for match, num in A:
    if match in used:
        continue
    used.add(match)
    for x in range(N+1):
        if x - match < 0:
            continue
        if dp[x] == None and dp[x-match] != None:
            dp[x] = dp[x-match] + [num]
        elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] match num A if match in used:
        continue
     match in used match used continue used.add(match) used.add used add match for x in range(N+1):
        if x - match < 0:
            continue
        if dp[x] == None and dp[x-match] != None:
            dp[x] = dp[x-match] + [num]
        elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] x range(N+1) range N+1 N 1 if x - match < 0:
            continue
         x - match < 0 x - match x match 0 continue if dp[x] == None and dp[x-match] != None:
            dp[x] = dp[x-match] + [num]
        elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] dp[x] == None and dp[x-match] != None dp[x] == None dp[x] dp x None dp[x-match] != None dp[x-match] dp x-match x match None dp[x] = dp[x-match] + [num] dp[x] dp x dp[x-match] + [num] dp[x-match] dp x-match x match [num] num elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] dp[x] != None and dp[x-match] != None dp[x] != None dp[x] dp x None dp[x-match] != None dp[x-match] dp x-match x match None if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] len(dp[x-match]) >= len(dp[x]) len(dp[x-match]) len dp[x-match] dp x-match x match len(dp[x]) len dp[x] dp x dp[x] = dp[x-match] + [num] dp[x] dp x dp[x-match] + [num] dp[x-match] dp x-match x match [num] num elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num] len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1 len(dp[x-match]) >= 1 len(dp[x-match]) len dp[x-match] dp x-match x match 1 len(dp[x-match]) == len(dp[x]) - 1 len(dp[x-match]) len dp[x-match] dp x-match x match len(dp[x]) - 1 len(dp[x]) len dp[x] dp x 1 y = list(map(str, dp[x][::-1])) y list(map(str, dp[x][::-1])) list map(str, dp[x][::-1]) map str dp[x][::-1] [x] dp x ::-1 -1 y.sort(reverse=True) y.sort y sort reverse=True reverse True z = [str(num)] + list(map(str, dp[x-match][::-1])) z [str(num)] + list(map(str, dp[x-match][::-1])) [str(num)] str(num) str num list(map(str, dp[x-match][::-1])) list map(str, dp[x-match][::-1]) map str dp[x-match][::-1] [x-match] dp x-match x match ::-1 -1 z.sort(reverse=True) z.sort z sort reverse=True reverse True y = int(''.join(y)) y int(''.join(y)) int ''.join(y) ''.join '' join y z = int(''.join(z)) z int(''.join(z)) int ''.join(z) ''.join '' join z if z > y:
                    dp[x] = dp[x-match] + [num] z > y z y dp[x] = dp[x-match] + [num] dp[x] dp x dp[x-match] + [num] dp[x-match] dp x-match x match [num] num X = dp[N] X dp[N] dp N X.sort(reverse=True) X.sort X sort reverse=True reverse True answer = ''.join(list(map(str, X))) answer ''.join(list(map(str, X))) ''.join '' join list(map(str, X)) list map(str, X) map str X print(answer) print answer ir = lambda: int(sr()) lambda: int(sr()) ir X = dp[N] dp[N] X A = lr() lr() A A = [(matches[a], a) for a in A] [(matches[a], a) for a in A] A used = set() set() used sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr z = [str(num)] + list(map(str, dp[x-match][::-1])) [str(num)] + list(map(str, dp[x-match][::-1])) z y = int(''.join(y)) int(''.join(y)) y top_match = A[0][0] A[0][0] top_match answer = ''.join(list(map(str, X))) ''.join(list(map(str, X))) answer lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr dp = [None] * (N+1) [None] * (N+1) dp M = lr() lr() M matches = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] matches z = int(''.join(z)) int(''.join(z)) z N, M = lr() lr() N y = list(map(str, dp[x][::-1])) list(map(str, dp[x][::-1])) y
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# まずは桁数を増やす→数字の大きい順に並べる
N, M = lr()
A = lr()
matches = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = [(matches[a], a) for a in A]
A.sort(key = lambda x: x[1], reverse=True)
A.sort(key = lambda x: x[0])
top_match = A[0][0]
dp = [None] * (N+1)
dp[0] = []
used = set()
for match, num in A:
    if match in used:
        continue
    used.add(match)
    for x in range(N+1):
        if x - match < 0:
            continue
        if dp[x] == None and dp[x-match] != None:
            dp[x] = dp[x-match] + [num]
        elif dp[x] != None and dp[x-match] != None:
            if len(dp[x-match]) >= len(dp[x]):
                dp[x] = dp[x-match] + [num]
            elif len(dp[x-match]) >= 1 and len(dp[x-match]) == len(dp[x]) - 1:
                y = list(map(str, dp[x][::-1])); y.sort(reverse=True)
                z = [str(num)] + list(map(str, dp[x-match][::-1])); z.sort(reverse=True)
                y = int(''.join(y))
                z = int(''.join(z))
                if z > y:
                    dp[x] = dp[x-match] + [num]

X = dp[N]
X.sort(reverse=True)
answer = ''.join(list(map(str, X)))
print(answer)
# 37
