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