N,M = map(int, input().split())
A = [int(a) for a in input().split()]

data = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
inf = -10**9
dp = [inf for i in range(N+1)]
dp[0] = 0
for i in range(1, N+1):
    for j in range(M):
        if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
            
ans = 0
A.sort(reverse=True)
n = dp[N]
i = n-1
temp = N
while i >= 0:
    j = 0
    while j < M:
        if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
        j += 1
    i -= 1
    
print(ans) N,M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M int(a) for a in input().split() for a in input().split() a input().split() ().split () input split for a in input().split() int(a) int a A = [int(a) for a in input().split()] A [int(a) for a in input().split()] data = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} data {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 inf = -10**9 inf -10**9 -10 9 inf 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) inf dp = [inf for i in range(N+1)] dp [inf for i in range(N+1)] dp[0] = 0 dp[0] dp 0 0 for i in range(1, N+1):
    for j in range(M):
        if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
             i range(1, N+1) range 1 N+1 N 1 for j in range(M):
        if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
             j range(M) range M if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
             i-data[A[j]] >= 0 i-data[A[j]] i data[A[j]] data A[j] A j 0 dp[i] = max(dp[i], dp[i-data[A[j]]]+1) dp[i] dp i max(dp[i], dp[i-data[A[j]]]+1) max dp[i] dp i dp[i-data[A[j]]]+1 dp[i-data[A[j]]] dp i-data[A[j]] i data[A[j]] data A[j] A j 1 ans = 0 ans 0 A.sort(reverse=True) A.sort A sort reverse=True reverse True n = dp[N] n dp[N] dp N i = n-1 i n-1 n 1 temp = N temp N while i >= 0:
    j = 0
    while j < M:
        if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
        j += 1
    i -= 1
     i >= 0 i 0 j = 0 j 0 while j < M:
        if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
        j += 1
     j < M j M if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
         temp-data[A[j]] >= 0 temp-data[A[j]] temp data[A[j]] data A[j] A j 0 if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
         dp[temp-data[A[j]]] == dp[temp]-1 dp[temp-data[A[j]]] dp temp-data[A[j]] temp data[A[j]] data A[j] A j dp[temp]-1 dp[temp] dp temp 1 temp -= data[A[j]] temp data[A[j]] data A[j] A j temp = max(temp, 0) temp max(temp, 0) max temp 0 ans += A[j]*10**i ans A[j]*10**i A[j] A j 10**i 10 i j = M j M j += 1 j 1 i -= 1 i 1 print(ans) print ans data = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} data temp -= data[A[j]] data[A[j]] temp temp = N N temp ans += A[j]*10**i A[j]*10**i ans n = dp[N] dp[N] n j = M M j temp = max(temp, 0) max(temp, 0) temp N,M = map(int, input().split()) map(int, input().split()) N dp = [inf for i in range(N+1)] [inf for i in range(N+1)] dp A = [int(a) for a in input().split()] [int(a) for a in input().split()] A j = 0 0 j inf = -10**9 -10**9 inf i -= 1 1 i i = n-1 n-1 i ans = 0 0 ans j += 1 1 j M = map(int, input().split()) map(int, input().split()) M
N,M = map(int, input().split())
A = [int(a) for a in input().split()]

data = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
inf = -10**9
dp = [inf for i in range(N+1)]
dp[0] = 0
for i in range(1, N+1):
    for j in range(M):
        if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
            
ans = 0
A.sort(reverse=True)
n = dp[N]
i = n-1
temp = N
while i >= 0:
    j = 0
    while j < M:
        if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
        j += 1
    i -= 1
    
print(ans)