# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
    MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)

# dp[i] := i本のマッチを使って作れる最大桁数
dp = [-1] * (N+1)
dp[0] = 0
for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1)

remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break

print(ans) N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(int(x) for x in input().split()) A list(int(x) for x in input().split()) list int(x) int x match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)] match_list [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)] ('0', 6) '0' 6 ('1', 2) '1' 2 ('2', 5) '2' 5 ('3', 5) '3' 5 ('4', 4) '4' 4 ('5', 5) '5' 5 ('6', 6) '6' 6 ('7', 3) '7' 3 ('8', 7) '8' 7 ('9', 6) '9' 6 MA = [] MA [] for a in A:
    MA.append(match_list[a]) a A MA.append(match_list[a]) MA.append MA append match_list[a] match_list a MA.sort(key=lambda x: x[0], reverse=True) MA.sort MA sort key=lambda x: x[0] key lambda x: x[0] x[0] x 0 x reverse=True reverse True dp = [-1] * (N+1) dp [-1] * (N+1) [-1] -1 N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1) i range(N+1) range N+1 N 1 for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1) j range(M) range M if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1) i-MA[j][1] >= 0 i-MA[j][1] i MA[j][1] [j] MA j 1 0 dp[i] = max(dp[i], dp[i-MA[j][1]] + 1) dp[i] dp i max(dp[i], dp[i-MA[j][1]] + 1) max dp[i] dp i dp[i-MA[j][1]] + 1 dp[i-MA[j][1]] dp i-MA[j][1] i MA[j][1] [j] MA j 1 1 remain_keta = dp[N] remain_keta dp[N] dp N remain_match = N remain_match N ans = '' ans '' for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break i range(dp[N]) range dp[N] dp N for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break ma MA if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1 remain_match >= ma[1] remain_match ma[1] ma 1 dp[remain_match - ma[1]] == remain_keta - 1 dp[remain_match - ma[1]] dp remain_match - ma[1] remain_match ma[1] ma 1 remain_keta - 1 remain_keta 1 ans +=  ma[0] ans ma[0] ma 0 remain_keta -= 1 remain_keta 1 remain_match -= ma[1] remain_match ma[1] ma 1 break print(ans) print ans dp = [-1] * (N+1) [-1] * (N+1) dp remain_match = N N remain_match remain_keta -= 1 1 remain_keta match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)] [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)] match_list MA = [] [] MA remain_match -= ma[1] ma[1] remain_match ans = '' '' ans M = map(int, input().split()) map(int, input().split()) M A = list(int(x) for x in input().split()) list(int(x) for x in input().split()) A ans +=  ma[0] ma[0] ans remain_keta = dp[N] dp[N] remain_keta N, M = map(int, input().split()) map(int, input().split()) N
# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
    MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)

# dp[i] := i本のマッチを使って作れる最大桁数
dp = [-1] * (N+1)
dp[0] = 0
for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1)

remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break

print(ans)