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