Problem p03128 (python)

Fixed submission:
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = [0 for i in range(N+1)]
ans = 0
tmp = set()
tmp.add(0)
for i in range(1, N+1):
    for n, k in num:
        if i - k in tmp:
            dp[i] = 1 + dp[i-k]
            tmp.add(i)
            break


num.sort(reverse=True)
tmp = N
ans = []
for i in reversed(range(1, dp[N])):
    for n, k in num:
        if dp[tmp - k] == i:
            ans.append(n)
            tmp -= k
            break
for n, k in num:
    if k == tmp:
        ans.append(n)
        break

ans.sort(reverse=True)
ans = "".join(map(str, ans))
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = ["" for i in range(N+1)]
ans = 0
tmp = set()
for x in num:
    if x > N:
        continue
    for i in range(1, N+1-x[1]):
        for n, k in num:
            if i - k in tmp:
                dp[i] = dp[i-k]+str(n)
                tmp.add(i)
                break
            else:
                if i == k:
                    dp[i] = str(n)
                    tmp.add(k)
                    break
    tmp2 = list(dp[N-x[1]])
    tmp2.append(str(x[0]))
    tmp2.sort(reverse=True)
    ans = max(int("".join(map(str, tmp2))), ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = ["" for i in range(N+1)]
ans = 0
for x in num:
    tmp = set()

    for i in range(1, N+1-x[1]):
        for n, k in num:
            if i - k in tmp:
                dp[i] = dp[i-k]+str(n)
                tmp.add(i)
                break
            else:
                if i == k:
                    dp[i] = str(n)
                    tmp.add(k)
                    break
    tmp2 = list(dp[N-x[1]])
    tmp2.append(str(x[0]))
    tmp2.sort(reverse=True)
    ans = max(int("".join(map(str, tmp2))), ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
num = [(i, num[i]) for i in range(10) if i in A]
num.sort(key=lambda x: x[0], reverse=True)
num.sort(key=lambda x: x[1])
dp = ["" for i in range(N+1)]
ans = 0
for x in num:
    tmp = set()

    for i in range(1, N+1-x[1]):
        for n, k in num:
            if i - k in tmp:
                dp[i] = dp[i-k]+str(n)
                tmp.add(i)
                break
            else:
                if i == k:
                    dp[i] = str(n)
                    tmp.add(k)
                    break
    tmp2 = list(dp[N-x[1]])
    tmp2.append(str(x[0]))
    tmp2.sort(reverse=True)
    ans = max(int("".join(map(str, tmp2))), ans)
print(ans)