Problem p03128 (python)

Fixed submission:
def sub(N, M, As):

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    NINF = -10**6
    dp_table = [NINF for _ in range(N+1)]
    dp_table[0] = 0
    for i in range(N):
        if dp_table[i] < 0:
            continue
        for a in As:
            cost = i + d_cost[a]
            if cost <= N:
                dp_table[cost] = max(dp_table[cost], dp_table[i] + 1)

    Lmax = dp_table[N]

    life = N
    buf = []
    for i in range(Lmax):
        oks = []
        for a in As:
            if life - d_cost[a] < 0:
                continue
            if dp_table[life - d_cost[a]] == dp_table[life] - 1:
                oks.append(a)
        # oks = [a for a in As if dp_table[life - d_cost[a]] == dp_table[life] - 1 and life - d_cost[a] >= 0]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))
            

if __name__ == "__main__":
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]
    sub(N, M, As)

Buggy submission (verdict: Runtime Error):
def sub(N, M, As):

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    NINF = -10**6
    dp_table = [NINF for _ in range(N+1)]
    dp_table[0] = 0
    for i in range(N):
        if dp_table[i] < 0:
            continue
        for a in As:
            cost = i + d_cost[a]
            if cost <= N:
                dp_table[cost] = max(dp_table[cost], dp_table[i] + 1)

    Lmax = dp_table[N]

    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp_table[life - d_cost[a]] == dp_table[life] - 1 and life - d_cost[a] >= 0]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))
            

if __name__ == "__main__":
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]
    sub(N, M, As)

Buggy submission (verdict: Runtime Error):
def sub(N, M, As):

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    NINF = -10**6
    dp_table = [NINF for _ in range(N+1)]
    dp_table[0] = 0
    for i in range(N):
        for a in As:
            cost = i + d_cost[a]
            if cost <= N:
                dp_table[cost] = max(dp_table[cost], dp_table[i] + 1)

    Lmax = dp_table[N]

    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp_table[life - d_cost[a]] == dp_table[life] - 1 and life - d_cost[a] >= 0]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))
            

if __name__ == "__main__":
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]
    sub(N, M, As)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
def sub():
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    
    NINF = -10**9
    @lru_cache(maxsize=None)   
    def dp(a_life):
        if a_life == 0:
            return 0
        cc = [a_life - d_cost[a] for a in As]
        cc = [c for c in cc if c >= 0]
        if len(cc) == 0:
            return NINF
        buf = [dp(c) + 1 for c in cc]
        return max(buf)
    
    Lmax = dp(N)
    
    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp(life - d_cost[a]) == dp(life) - 1 and life - d_cost[a] >= 0]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))


sub()

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
def sub():
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    
    NINF = -10**12
    @lru_cache(maxsize=None)   
    def dp(a_life):
        if a_life == 0:
            return 0
        cc = [a_life - d_cost[a] for a in As]
        cc = [c for c in cc if c >= 0]
        if len(cc) == 0:
            return NINF
        buf = [dp(c) + 1 for c in cc]
        return max(buf)
    
    Lmax = dp(N)
    
    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp(life - d_cost[a]) == Lmax - (i+1)]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))


sub()

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
def sub():
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    
    NINF = -10**12
    @lru_cache(maxsize=None)   
    def dp(a_life):
        if a_life == 0:
            return 0
        cc = [a_life - d_cost[a] for a in As]
        cc = [c for c in cc if c >= 0]
        if len(cc) == 0:
            return NINF
        buf = [dp(c) + 1 for c in cc]
        return max(buf)
    
    Lmax = dp(N)
    
    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp(life - d_cost[a]) == Lmax - (i+1)]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))


sub()

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
def sub():
    N, M = [int(e) for e in input().split()]
    As = [e for e in input().split()]

    d_cost = {
            "1": 2,
            "2": 5,
            "3": 5,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 3,
            "8": 7,
            "9": 6,
            }
    
    
    NINF = -10**12
    @lru_cache(maxsize=None)   
    def dp(a_life):
        if a_life == 0:
            return 0
        cc = [a_life - d_cost[a] for a in As]
        cc = [c for c in cc if c >= 0]
        if len(cc) == 0:
            return NINF
        buf = [dp(c) + 1 for c in cc]
        return max(buf)
    
    Lmax = dp(N)
    print(Lmax)
    
    life = N
    buf = []
    for i in range(Lmax):
        oks = [a for a in As if dp(life - d_cost[a]) == Lmax - (i+1)]
        a_selected = max(oks)
        buf.append(a_selected)
        life = life - d_cost[a_selected]
    print("".join(buf))


sub()