n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1]) 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(reverse=True) a.sort a sort reverse=True reverse True lis = [2,5,5,4,5,6,3,7,6] lis [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret new_val if num1<0:
        return -(10**10)
     num1<0 num1 0 return -(10**10) -(10**10) 10**10 10 10 if num1==0:
        return num2
     num1==0 num1 0 return num2 num2 ret = (10**len(str(num2)))*num1 + num2 ret (10**len(str(num2)))*num1 + num2 (10**len(str(num2)))*num1 10**len(str(num2)) 10 len(str(num2)) len str(num2) str num2 num1 num2 return ret ret num1 num1 num2 num2 [-(10**10) for i in range(1+n)] for j in range(1+m) for j in range(1+m) j range(1+m) range 1+m 1 m for j in range(1+m) [-(10**10) for i in range(1+n)] dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)] dp [[-(10**10) for i in range(1+n)] for j in range(1+m)] for i in range(m):
    dp[i][0]=0 i range(m) range m dp[i][0]=0 dp[i][0] [i] dp i 0 0 for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j]) i range(m) range m num=a[i] num a[i] a i honsu=lis[a[i]-1] honsu lis[a[i]-1] lis a[i]-1 a[i] a i 1 for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j]) j range(1+n) range 1+n 1 n if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j]) j<honsu j honsu dp[i+1][j] = dp[i][j] dp[i+1][j] [i+1] dp i+1 i 1 j dp[i][j] [i] dp i j dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j]) dp[i+1][j] [i+1] dp i+1 i 1 j max(new_val(dp[i+1][j-honsu] , num),dp[i][j]) max new_val(dp[i+1][j-honsu] , num) new_val dp[i+1][j-honsu] [i+1] dp i+1 i 1 j-honsu j honsu num dp[i][j] [i] dp i j print(dp[-1][-1]) print dp[-1][-1] [-1] dp -1 -1 dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)] [[-(10**10) for i in range(1+n)] for j in range(1+m)] dp n,m = map(int, input().split()) map(int, input().split()) n lis = [2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] lis honsu=lis[a[i]-1] lis[a[i]-1] honsu a = list(map(int, input().split())) list(map(int, input().split())) a def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret new_val num=a[i] a[i] num m = map(int, input().split()) map(int, input().split()) m
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])