Problem p03128 (python)

Fixed submission:
def num_to_match(n):
    if n == 1:
        return 2
    elif n == 7:
        return 3
    elif n == 4:
        return 4
    elif n in [2, 3, 5]:
        return 5
    elif n in [6, 9]:
        return 6
    else:
        return 7


def max_length(N, match):
    match = set(match)
    INF = 10**15
    dp = [- INF for _ in range(N + 1)]
    dp[0] = 0
    for n in range(1, N + 1):
        max_l = dp[n]
        for m in match:
            if n - m >= 0 and dp[n - m] >= 0:
                max_l = max([max_l, 1 + dp[n - m]])
        dp[n] = max_l
    return dp


def main():
    N, _ = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    A.sort(reverse=True)
    match = list(map(num_to_match, A))
    max_l = max_length(N, match)
    digits = [0 for _ in range(max_l[N])]
    n_match = N
    for i in range(max_l[N]):
        for a in A:
            ma = num_to_match(a)
            if n_match - ma < 0:
                continue
            if max_l[n_match - ma] == max_l[n_match] - 1:
                digits[i] = a
                n_match -= ma
                break
    print(''.join(map(str, digits)))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def num_to_match(n):
    if n == 1:
        return 2
    elif n == 7:
        return 3
    elif n == 4:
        return 4
    elif n in [2, 3, 5]:
        return 5
    elif n in [6, 9]:
        return 6
    else:
        return 7


def max_length(N, match):
    match = set(match)
    INF = 10**15
    dp = [- INF for _ in range(N + 1)]
    dp[0] = 0
    for n in range(1, N + 1):
        max_l = dp[n]
        for m in match:
            if n - m >= 0:
                max_l = max([max_l, 1 + dp[n - m]])
        dp[n] = max_l
    return dp


def main():
    N, _ = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    A.sort(reverse=True)
    match = list(map(num_to_match, A))
    max_l = max_length(N, match)
    digits = [0 for _ in range(max_l[N])]
    n_match = N
    for i in range(max_l[N]):
        for a in A:
            ma = num_to_match(a)
            if max_l[n_match - ma] == max_l[n_match] - 1:
                digits[i] = a
                n_match -= ma
                break
    ans = 0
    for d in digits:
        ans *= 10
        ans += d
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def num_to_match(n):
    if n == 1:
        return 2
    elif n == 7:
        return 3
    elif n == 4:
        return 4
    elif n in [2, 3, 5]:
        return 5
    elif n in [6, 9]:
        return 6
    else:
        return 7


def match_to_max_num(m, A):
    if m == 2:
        return 1
    elif m == 3:
        return 7
    elif m == 4:
        return 4
    elif m == 5:
        return max(set({2, 3, 5}).intersection(set(A)))
    elif m == 6:
        return max(set({6, 9}).intersection(set(A)))
    else:
        return 8


def find_opt_match(N, match):
    match_list = [0 for _ in range(N)]
    def dfs(cur, rest, m=0):
        if rest < m:
            return False
        if rest == m:
            match_list[cur] = m
            return True
        for mm in match:
            if dfs(cur + 1, rest - m, mm):
                if cur >= 0:
                    match_list[cur] = m
                return True
        return False
    dfs(-1, N)
    return match_list[:match_list.index(0)]


def main():
    N, M = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    match = list(map(num_to_match, A))
    match.sort()
    opt_match = find_opt_match(N, match)
    opt_num = list(map(lambda x: match_to_max_num(x, A), opt_match))
    opt_num.sort()
    ans = 0
    pw = 1
    for n in opt_num:
        ans += n * pw
        pw *= 10
    print(ans)


if __name__ == '__main__':
    main()