N, M = map(int, input().split())
A = list(map(int, input().split()))
num = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [0]*(N+1)
INF = -float('inf')

for i in range(1, N+1):
    dp[i] = max([dp[i-num[j]] if i - num[j] >= 0 else INF for j in A]) + 1

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

dp = [0]*(N+1)
INF = -float('inf')

for i in range(1, N+1):
    dp[i] = max([dp[i-num[j]] if i - num[j] >= 0 else INF for j in A]) + 1

A.sort(reverse = True)
ans = ''
tmp = N
while tmp > 0:
    for a in A:
        if tmp-num[a] >=0:
            if dp[tmp-num[a]] == dp[tmp] - 1:
                break
    ans += str(a)
    tmp -= num[a]
    
print(ans)    