Problem p03128 (python)

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

costs = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]

# 優先度：桁数, [要素(降順)]
dp = [[0, []] for _ in range(N + 1)]
for a in A:
	cost = costs[a]
	for n in range(N):
		if n != 0 and dp[n][0] == 0:
			continue
		ni = n + cost
		if N < ni:
			break
		np = [dp[n][0] + 1, sorted(dp[n][1] + [a], reverse=True)]
		if dp[ni] < np:
			dp[ni] = np

print(*sorted(dp[N][1], reverse=True), sep="")

Buggy submission (verdict: Runtime Error):
import sys

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    print(*v, file=sys.stderr)

# エントリーポイント
def main(args):
    N, M = parse(args, int, int)
    A = map(int, next(args).split())

    #       1  2  3  4  5  6  7  8  9
    nums = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    dp = [[]] + [[] for _ in range(N)]
    for a in A:
        na = nums[a - 1]
        def chmax(c):
            nl = sorted(dp[c - na] + [a], reverse=True)
            if len(nl) < len(dp[c]):
                return dp[c]
            if len(dp[c]) < len(nl):
                return nl
            return max(nl, dp[c])
        dp[na] = chmax(na)
        for c in range(na + 1, N + 1):
            if dp[c - na]:
                dp[c] = chmax(c)
    # for i, v in enumerate(dp, start=0):
    #     debug(i, v)
    print(*dp[N], sep="")

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Buggy submission (verdict: Runtime Error):
import sys

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    print(*v, file=sys.stderr)

# エントリーポイント
def main(args):
    N, M = parse(args, int, int)
    A = map(int, next(args).split())

    #       1  2  3  4  5  6  7  8  9
    nums = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    dp = [[]] + [[] for _ in range(N)]
    for a in A:
        na = nums[a - 1]
        def chmax(c):
            nl = sorted(dp[c - na] + [a], reverse=True)
            if len(nl) < len(dp[c]):
                return dp[c]
            if len(dp[c]) < len(nl):
                return nl
            return max(nl, dp[c])
        dp[na] = chmax(na)
        for c in range(na + 1, N + 1):
            if dp[c - na]:
                dp[c] = chmax(c)
    # for i, v in enumerate(dp, start=0):
    #     debug(i, v)
    print(*dp[N], sep="")

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Buggy submission (verdict: Runtime Error):
import sys

# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    print(*v, file=sys.stderr)

# エントリーポイント
def main(args):
    N, M = parse(args, int, int)
    A = map(int, next(args).split())

    #       1  2  3  4  5  6  7  8  9
    nums = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    dp = [[]] + [[] for _ in range(N)]
    for a in A:
        na = nums[a - 1]
        def chmax(c):
            nl = sorted(dp[c - na] + [a], reverse=True)
            if len(nl) < len(dp[c]):
                return dp[c]
            if len(dp[c]) < len(nl):
                return nl
            return max(nl, dp[c])
        dp[na] = chmax(na)
        for c in range(na + 1, N + 1):
            if dp[c - na]:
                dp[c] = chmax(c)
    for i, v in enumerate(dp, start=0):
        debug(i, v)
    print(*dp[N], sep="")

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))