N,M=list(map(int,input().split()))
A=sorted(list(map(int,input().split())),reverse=True)
C=[-1,2,5,5,4,5,6,3,7,6]
DP=[[0 if j==0 else -1 for i in range(M)] for j in range(N+1)]
for i in range(N+1):
  for j in range(M):
    if(j>0):
      DP[i][j]=max(DP[i][j],DP[i][j-1]);
    if(i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]);
    if(j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]);
print(DP[N][M-1])   N,M=list(map(int,input().split())) N list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split M A=sorted(list(map(int,input().split())),reverse=True) A sorted(list(map(int,input().split())),reverse=True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse=True reverse True C=[-1,2,5,5,4,5,6,3,7,6] C [-1,2,5,5,4,5,6,3,7,6] -1 2 5 5 4 5 6 3 7 6 [0 if j==0 else -1 for i in range(M)] for j in range(N+1) for j in range(N+1) j range(N+1) range N+1 N 1 for j in range(N+1) [0 if j==0 else -1 for i in range(M)] DP=[[0 if j==0 else -1 for i in range(M)] for j in range(N+1)] DP [[0 if j==0 else -1 for i in range(M)] for j in range(N+1)] for i in range(N+1):
  for j in range(M):
    if(j>0):
      DP[i][j]=max(DP[i][j],DP[i][j-1]);
    if(i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]);
    if(j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]); i range(N+1) range N+1 N 1 for j in range(M):
    if(j>0):
      DP[i][j]=max(DP[i][j],DP[i][j-1]);
    if(i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]);
    if(j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]); j range(M) range M if(j>0):
      DP[i][j]=max(DP[i][j],DP[i][j-1]);
     j>0 j 0 DP[i][j]=max(DP[i][j],DP[i][j-1]) DP[i][j] [i] DP i j max(DP[i][j],DP[i][j-1]) max DP[i][j] [i] DP i j DP[i][j-1] [i] DP i j-1 j 1 if(i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]);
     i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1 i-C[A[j]]>=0 i-C[A[j]] i C[A[j]] C A[j] A j 0 DP[i-C[A[j]]][j]!=-1 DP[i-C[A[j]]][j] [i-C[A[j]]] DP i-C[A[j]] i C[A[j]] C A[j] A j j -1 DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]) DP[i][j] [i] DP i j max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]) max DP[i][j] [i] DP i j DP[i-C[A[j]]][j]*10+A[j] DP[i-C[A[j]]][j]*10 DP[i-C[A[j]]][j] [i-C[A[j]]] DP i-C[A[j]] i C[A[j]] C A[j] A j j 10 A[j] A j if(j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]); j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1 j>0 and i-C[A[j]]>=0 j>0 j 0 i-C[A[j]]>=0 i-C[A[j]] i C[A[j]] C A[j] A j 0 DP[i-C[A[j]]][j-1]!=-1 DP[i-C[A[j]]][j-1] [i-C[A[j]]] DP i-C[A[j]] i C[A[j]] C A[j] A j j-1 j 1 -1 DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]) DP[i][j] [i] DP i j max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]) max DP[i][j] [i] DP i j DP[i-C[A[j]]][j-1]*10+A[j] DP[i-C[A[j]]][j-1]*10 DP[i-C[A[j]]][j-1] [i-C[A[j]]] DP i-C[A[j]] i C[A[j]] C A[j] A j j-1 j 1 10 A[j] A j print(DP[N][M-1]) print DP[N][M-1] [N] DP N M-1 M 1 N,M=list(map(int,input().split())) list(map(int,input().split())) N DP=[[0 if j==0 else -1 for i in range(M)] for j in range(N+1)] [[0 if j==0 else -1 for i in range(M)] for j in range(N+1)] DP M=list(map(int,input().split())) list(map(int,input().split())) M C=[-1,2,5,5,4,5,6,3,7,6] [-1,2,5,5,4,5,6,3,7,6] C A=sorted(list(map(int,input().split())),reverse=True) sorted(list(map(int,input().split())),reverse=True) A
N,M=list(map(int,input().split()))
A=sorted(list(map(int,input().split())),reverse=True)
C=[-1,2,5,5,4,5,6,3,7,6]
DP=[[0 if j==0 else -1 for i in range(M)] for j in range(N+1)]
for i in range(N+1):
  for j in range(M):
    if(j>0):
      DP[i][j]=max(DP[i][j],DP[i][j-1]);
    if(i-C[A[j]]>=0 and DP[i-C[A[j]]][j]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j]*10+A[j]);
    if(j>0 and i-C[A[j]]>=0 and DP[i-C[A[j]]][j-1]!=-1):
      DP[i][j]=max(DP[i][j],DP[i-C[A[j]]][j-1]*10+A[j]);
print(DP[N][M-1])  