Problem p02820 (python)

Fixed submission:
import math
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()

def didWin(idx, c):
    if c == 'r' and t[idx] == 's':
        return True
    if c == 's' and t[idx] == 'p':
        return True
    if c == 'p' and t[idx] == 'r':
        return True
    return False

def solve(mod):
    # print('mod:',mod)
    # tt = []
    #
    # for i in range(mod, n, k):
    #     tt.append(t[i])
    # print('tt:', tt)
    # print('n/k:', n / k)

    # tt[i] = t[i * k + mod]
    l = math.ceil((n - mod) / k)
    # print('l:', l)
    dp = [[0] * 4 for _ in range(l + 1)]

    for i in range(l):
        # Case rock
        if didWin(i * k + mod, 'r'):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        # Case scissors
        if didWin(i * k + mod, 's'):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        # Case paper
        if didWin(i * k + mod, 'p'):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    # print('dp:', dp)
    # print('max:', max(dp[l][0], dp[l][1], dp[l][2]))
    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = []
dp = [[None]*4 for _ in range(n+1)]

def solve(idx, score, move):
    if idx == n:
        return score

    # if dp[idx][0] is not None and dp[idx][1] is not None and dp[idx][2] is not None:
    #     return max(dp[idx][0], dp[idx][1], dp[idx][2])

    rock, sci, paper = 0, 0, 0

    if idx >= k:
        # print(idx, k)
        # print('move:', move)
        ng = move[idx-k]
        if ng == 'r':
            rock = -10**10
            if t[idx] == 'r':
                sci = solve(idx + 1, score, move + ['s'])
                paper = solve(idx + 1, score + p, move + ['p'])
            elif t[idx] == 's':
                sci = solve(idx + 1, score, move + ['s'])
                paper = solve(idx + 1, score, move + ['p'])
            elif t[idx] == 'p':
                sci = solve(idx + 1, score + s, move + ['s'])
                paper = solve(idx + 1, score, move + ['p'])
        elif ng == 's':
            sci = -10**10
            if t[idx] == 'r':
                rock = solve(idx + 1, score, move + ['r'])
                paper = solve(idx + 1, score + p, move + ['p'])
            elif t[idx] == 's':
                rock = solve(idx + 1, score + r, move + ['r'])
                paper = solve(idx + 1, score, move + ['p'])
            elif t[idx] == 'p':
                rock = solve(idx + 1, score, move + ['r'])
                paper = solve(idx + 1, score, move + ['p'])
        elif ng == 'p':
            paper = -10**10
            if t[idx] == 'r':
                rock = solve(idx + 1, score, move + ['r'])
                sci = solve(idx + 1, score, move + ['s'])
            elif t[idx] == 's':
                rock = solve(idx + 1, score + r, move + ['r'])
                sci = solve(idx + 1, score, move + ['s'])
            elif t[idx] == 'p':
                rock = solve(idx + 1, score, move + ['r'])
                sci = solve(idx + 1, score + s, move + ['s'])
    else:
        if t[idx] == 'r':
            rock = solve(idx+1, score, move + ['r'])
            sci = solve(idx+1, score, move + ['s'])
            paper = solve(idx+1, score + p, move + ['p'])
        elif t[idx] == 's':
            rock = solve(idx+1, score + r, move + ['r'])
            sci = solve(idx+1, score, move + ['s'])
            paper = solve(idx+1, score, move + ['p'])
        elif t[idx] == 'p':
            rock = solve(idx+1, score, move + ['r'])
            sci = solve(idx+1, score + s, move + ['s'])
            paper = solve(idx+1, score, move + ['p'])

    # if idx >= k:
    #     ng = move[idx-k]
    #     if ng == 'r':
    #         rock = -10**10
    #     elif ng == 'sr':
    #         sci = -10**10
    #     elif ng == 'p':
    #         paper = -10**10

    # dp[idx][0] = rock
    # dp[idx][1] = sci
    # dp[idx][2] = paper

    return max(rock, sci, paper)

print(solve(0, 0, []))