Problem p03128 (python)

Fixed submission:
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# MOD = 10 ** 9 + 7
# メモ化再起
from functools import lru_cache
import sys

sys.setrecursionlimit(100000000)

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]



@lru_cache(maxsize=1000000)
def f(n):
    maxval = -float('inf')
    for a in A:
        tmp = n - match[a]
        if tmp == 0:
            maxval = max(maxval, a)
        elif tmp > 0:
            # maxval = max(maxval, f2(f(n - match[a]) * 10 + a))
            maxval = max(maxval, f(n - match[a]) * 10 + a)
    return maxval

@lru_cache(maxsize=1000000)
def f2(n):
    if n < 0:
        return 0
    n = list(str(n))
    n.sort(reverse=True)
    return int("".join(n))

ans = f(N)
print(f2(ans))

Buggy submission (verdict: Runtime Error):
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# MOD = 10 ** 9 + 7
# メモ化再起
from functools import lru_cache
import sys

sys.setrecursionlimit(10000)

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]



@lru_cache(maxsize=1000000)
def f(n):
    maxval = -float('inf')
    for a in A:
        tmp = n - match[a]
        if tmp == 0:
            maxval = max(maxval, a)
        elif tmp > 0:
            maxval = max(maxval, f2(f(n - match[a]) * 10 + a))
    return maxval

@lru_cache(maxsize=1000000)
def f2(n):
    if n < 0:
        return 0
    n = list(str(n))
    n.sort(reverse=True)
    return int("".join(n))

ans = f(N)
print(ans)

Buggy submission (verdict: Runtime Error):
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# MOD = 10 ** 9 + 7
#

import sys

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

D = dict()
# 1から9の数値:必要なマッチの本数
for a in A:
    D[a] = min(D.get(a, 999), B[a])

L = []
# (必要なマッチの本数, 1～10の数値)
for k, v in D.items():
    L.append((v, k))
L.sort()


# 残りn本のマッチ、現在の値val
# 戻り値：その最大値
def f(n, val):
    if n < 0:
        return 0
    elif n == 0:
        ret = list(str(val))
        ret.sort(reverse=True)
        print("".join(ret))
        sys.exit(0)
        return 0

    ret = 0
    for l in L:
        ret = max(ret, f(n - l[0], val * 10 + l[1]))
    return ret


ans = f(N, 0)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(1000000)

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
Q = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

QA = [(Q[A[i]], A[i]) for i in range(M)]
QA.sort()

n2 = QA.count((5,2))
n3 = QA.count((5,3))
n5 = QA.count((5,5))
n6 = QA.count((6,6))
n9 = QA.count((6,9))

if n2 > 0 and n3 > 0:
    QA.remove((5,2))
if n3 > 0 and n5 > 0:
    QA.remove((5,3))
if n2> 0 and n5 > 0:
    QA.remove((5,2))
if n6 > 0 and n9 > 0:
    QA.remove((6,6))



def f(used_q, used_a):
    if used_q > N:
        return
    elif used_q == N:
        s = sorted(used_a, reverse=True)
        s = [str(i) for i in s]
        print("".join(s))
        sys.exit()

    for qa in QA:
        q, a = qa[0], qa[1]
        used_a.append(a)
        f(used_q + q, used_a)
        del used_a[-1]

l = []
f(0, l)

Buggy submission (verdict: Runtime Error):
import sys
N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
Q = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

QA = [(Q[A[i]], A[i]) for i in range(M)]
QA.sort()

def f(used_q, num):
    if used_q > N:
        return -float("inf")
    elif used_q == N:
        s = sorted(list(str(num)),reverse=True)
        print("".join(s))
        sys.exit()
        return num

    ret = 0
    for qa in QA:
        q = qa[0]
        a = qa[1]
        ret = max(ret,f(used_q + q, num * 10 + a))
    return ret

f(0,0)