Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1e10] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
    # i 本で表せる最大の桁数を dp に求める
    l = [dp[i - V[j]] + 1 for j in A if i - V[j] >= 0]
    if l:
        dp[i] = max(l)
# 各桁の最大値を求めていく
n = N
while n > 0:
    for j in A:
        if dp[n] == dp[n - V[j]] + 1:
            print(j, end='')
            n = n - V[j]
            break
print()


Fixed submission:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1e10] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
    # i 本で表せる最大の桁数を dp に求める
    l = [dp[i - V[j]] + 1 for j in A if i - V[j] >= 0]
    if l:
        dp[i] = max(l)
# 各桁の最大値を求めていく
n = N
while n > 0:
    for j in A:
        if n - V[j] < 0: continue
        if dp[n] == dp[n - V[j]] + 1:
            print(j, end='')
            n = n - V[j]
            break
print()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

memo = [-1] * (N + 1)
memo[N] = 0
while 1:
    next_memo = [-1] * (N + 1)
    next_memo[0] = memo[0]
    fin = True
    for i in range(N, 0, -1):
        if memo[i] < 0: continue

        for j in range(1, 10):
            if not j in A: continue
            idx = i - V[j]
            if idx >= 0:
                next_memo[idx] = max(next_memo[idx], memo[i] * 10 + j)
                if idx >0:
                    fin = False
    memo = next_memo
    if fin:
        break
print(memo[0])


Fixed submission:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1e10] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
    # i 本で表せる最大の桁数を dp に求める
    l = [dp[i - V[j]] + 1 for j in A if i - V[j] >= 0]
    if l:
        dp[i] = max(l)
# 各桁の最大値を求めていく
n = N
while n > 0:
    for j in A:
        if n - V[j] < 0: continue
        if dp[n] == dp[n - V[j]] + 1:
            print(j, end='')
            n = n - V[j]
            break
print()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

memo = [-1] * (N + 1)
memo[N] = 0
while 1:
    next_memo = [-1] * (N + 1)
    next_memo[0] = memo[0]
    fin = True
    for i in range(N, 0, -1):
        if memo[i] < 0: continue

        for j in range(1, 10):
            if not j in A: continue
            idx = i - V[j]
            if idx >= 0:
                next_memo[idx] = max(next_memo[idx], memo[i] * 10 + j)
                if idx >0:
                    fin = False
    memo = next_memo
    if fin:
        break
print(memo[0])


Fixed submission:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1e10] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
    # i 本で表せる最大の桁数を dp に求める
    l = [dp[i - V[j]] + 1 for j in A if i - V[j] >= 0]
    if l:
        dp[i] = max(l)
# 各桁の最大値を求めていく
n = N
while n > 0:
    for j in A:
        if n - V[j] < 0: continue
        if dp[n] == dp[n - V[j]] + 1:
            print(j, end='')
            n = n - V[j]
            break
print()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

memo = [-1] * (N + 1)
memo[N] = 0
while 1:
    next_memo = [-1] * (N + 1)
    next_memo[0] = memo[0]
    for i in range(N, 0, -1):
        if memo[i] < 0: continue

        for j in range(1, 10):
            if not j in A: continue
            idx = i - V[j]
            if idx >= 0:
                next_memo[idx] = max(next_memo[idx], memo[i] * 10 + j)
    memo = next_memo
    if all((v == -1 for v in memo[1:])):
        break
print(memo[0])


Fixed submission:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

V = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1e10] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
    # i 本で表せる最大の桁数を dp に求める
    l = [dp[i - V[j]] + 1 for j in A if i - V[j] >= 0]
    if l:
        dp[i] = max(l)
# 各桁の最大値を求めていく
n = N
while n > 0:
    for j in A:
        if n - V[j] < 0: continue
        if dp[n] == dp[n - V[j]] + 1:
            print(j, end='')
            n = n - V[j]
            break
print()