Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cost = [0,2,5,5,4,5,6,3,7,6]

dp = ["" for i in range(N+10)]

A = sorted(A, reverse=True)

for num in A:
    c = cost[num]
    if dp[c] == "":
        dp[c] = str(num)
    else:
        dp[c] = str(max(int(dp[c]), num))

for i in range(N+1):
    for num in A:
        c = cost[num]
        prev = dp[i]
        if prev != "":
            if int(prev[0]) > num:
                nxt = prev + str(num)
                nxt_i = int(nxt)
            else:
                nxt = str(num) + prev
                nxt_i = int(nxt)
            if dp[i + c] == "":
                dp[i + c] = nxt
            else:
                dp[i + c] = str(max(int(dp[i + c]), nxt_i))

print(dp[N])


Fixed submission:
import copy

N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cost = [0,2,5,5,4,5,6,3,7,6]

dp = [[0 for _ in range(10)] for i in range(N+10)]

A = sorted(A, reverse=True)

for num in A:
    c = cost[num]
    if dp[c][0] == 0:
        dp[c][0] = 1
        dp[c][num] = 1

for i in range(1, N+1):
    for num in A:
        c = cost[num]
        prev = copy.deepcopy(dp[i])
        if prev[0] != 0:
            if dp[i + c][0] > dp[i][0] + 1:
                continue
            elif dp[i + c][0] == dp[i][0] + 1:
                nxt = prev
                nxt[num] += 1
                nxt[0] += 1
                for number in range(9, 0, -1):
                    if dp[i + c][number] < nxt[number]:
                        dp[i + c] = copy.deepcopy(nxt)
                        break
                    if dp[i + c][number] > nxt[number]:
                        break
            else:
                nxt = prev
                nxt[num] += 1
                nxt[0] += 1
                dp[i + c] = copy.deepcopy(nxt)

# print(dp[N])
ans = ""
for number in range(9, 0, -1):
    for _ in range(dp[N][number]):
        ans = ans + str(number)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cost = [0,2,5,5,4,5,6,3,7,6]

dp = [0 for i in range(N+1)]

A = sorted(A, reverse=True)

for num in A:
    c = cost[num]
    dp[c] = max(dp[c], num)

for i in range(N+1):
    for num in A:
        c = cost[num]
        if i-c >= 0:
            prev = dp[i - c]
            cur = dp[i]
            if prev > 0:
                prev_str = str(prev)
                if int(prev_str[0]) > num:
                    nxt_str = prev_str + str(num)
                    nxt = int(nxt_str)
                else:
                    nxt_str = str(num) + prev_str
                    nxt = int(nxt_str)
                if nxt > cur:
                    dp[i] = nxt
                # if i == N:
                #     print(cur, prev, nxt)

print(dp[N])


Fixed submission:
import copy

N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cost = [0,2,5,5,4,5,6,3,7,6]

dp = [[0 for _ in range(10)] for i in range(N+10)]

A = sorted(A, reverse=True)

for num in A:
    c = cost[num]
    if dp[c][0] == 0:
        dp[c][0] = 1
        dp[c][num] = 1

for i in range(1, N+1):
    for num in A:
        c = cost[num]
        prev = copy.deepcopy(dp[i])
        if prev[0] != 0:
            if dp[i + c][0] > dp[i][0] + 1:
                continue
            elif dp[i + c][0] == dp[i][0] + 1:
                nxt = prev
                nxt[num] += 1
                nxt[0] += 1
                for number in range(9, 0, -1):
                    if dp[i + c][number] < nxt[number]:
                        dp[i + c] = copy.deepcopy(nxt)
                        break
                    if dp[i + c][number] > nxt[number]:
                        break
            else:
                nxt = prev
                nxt[num] += 1
                nxt[0] += 1
                dp[i + c] = copy.deepcopy(nxt)

# print(dp[N])
ans = ""
for number in range(9, 0, -1):
    for _ in range(dp[N][number]):
        ans = ans + str(number)

print(ans)