Problem p03128 (python)

Fixed submission:
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

items = {
    1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6
}

dp = [-float("inf")] * (N + 1)
b_list = []
for a in a_list:
    need = items[a]
    if need <= N:
        dp[need] = 1
        b_list.append(a)

M = len(b_list)
dp[0] = 0
for i in range(N + 1):
    for b in b_list:
        if i - items[b] >= 0:
            if dp[i - items[b]] != -float("inf"):
                dp[i] = max(dp[i], dp[i - items[b]] + 1)

b_list.sort(reverse=True)
ans = ""

for i in range(dp[N]):
    for b in b_list:
        if dp[N - items[b]] == dp[N] - 1:
            ans += str(b)
            N -= items[b]
            break
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

items = {
    1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6
}
dp = [0] * (N + 1)

b_list = []
for a in a_list:
    need = items[a]
    if need <= N:
        dp[need] = a
        b_list.append(a)

M = len(b_list)
b_list.sort()
for i in range(N + 1):
    for b in b_list:
        if i - items[b] >= 0:
            if dp[i - items[b]] != 0:
                if dp[i - items[b]][0] < b:
                    cnt = 0
                    num = dp[i - items[b]]
                    while num != 0:
                        cnt += 1
                        num //= 10
                    tmp = max(b * (10 ** cnt) + dp[i - items[b]], dp[i - items[b]] * 10 + b)
                    dp[i] = max(tmp, dp[i])
                else:
                    dp[i] = max(dp[i], dp[i - items[b]] * 10 + b)


print(dp[N])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

items = {
    1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6
}
dp = [0] * (N + 1)

b_list = []
for a in a_list:
    need = items[a]
    if need <= N:
        dp[need] = a
        b_list.append(a)

M = len(b_list)
b_list.sort()
for i in range(N + 1):
    for b in b_list:
        if i - items[b] >= 0:
            if dp[i - items[b]] != 0:
                cnt = 0
                num = dp[i - items[b]]
                while num > 1:
                    cnt += 1
                    num /= 10
                tmp = max(b * (10 ** cnt) + dp[i - items[b]], dp[i - items[b]] * 10 + b)
                dp[i] = max(tmp, dp[i])
print(dp[N])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

items = {
    1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6
}
dp = [0] * (N + 1)

for a in a_list:
    need = items[a]
    dp[need] = a

for i in range(2, N+1):
    for j in range(M):
        if i - items[a_list[j]] >= 0:
                if dp[i - items[a_list[j]]] != 0:
                    dp[i] = max(dp[i - items[a_list[j]]] * 10 + a_list[j], dp[i])

print(dp[N])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

items = {
    1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6
}
dp = [0] * (N + 1)

for a in a_list:
    need = items[a]
    dp[need] = a

for i in range(2, N+1):
    for j in range(M):
        if i - items[a_list[j]] + 1 >= 0:
                if dp[i - items[a_list[j]]] != 0:
                    dp[i] = max(dp[i - items[a_list[j]]] * 10 + a_list[j], dp[i])

print(dp[N])