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