Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K

for i in range(N):
    l[i % K] += T[i]


def okjudge(ss, p):
    lss = len(ss)
    if p == lss:
        return 0

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            return P
        elif c == 's':
            return R
        else:
            return S
    if ss[p + 1] == ss[p]:
        if c == 'r':
            return max(okjudge(ss, p + 1), P + okjudge(ss, p + 2))
        elif c == 's':
            return max(okjudge(ss, p + 1), R + okjudge(ss, p + 2))
        else:
            return max(okjudge(ss, p + 1), S + okjudge(ss, p + 2))
    elif c == 'r':
        return P + okjudge(ss, p + 1)
    elif c == 's':
        return R + okjudge(ss, p + 1)
    else:
        return S + okjudge(ss, p + 1)

print(sum([okjudge(v, 0) for v in l]))


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K
memo = [{} for _ in range(K)]

for i in range(N):
    l[i % K] += T[i]

def okjudge(m, ss, p):
    if p in memo[m]:
        return memo[m][p]

    lss = len(ss)
    if p == lss:
        ret =  0
        memo[m][p] = ret
        return ret

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            ret = P
        elif c == 's':
            ret =  R
        else:
            ret =  S
        memo[m][p] = ret
        return ret

    if ss[p + 1] == ss[p]:
        if c == 'r':
            ret =  max(okjudge(m, ss, p + 1), P + okjudge(m, ss, p + 2))
        elif c == 's':
            ret =  max(okjudge(m, ss, p + 1), R + okjudge(m, ss, p + 2))
        else:
            ret =  max(okjudge(m, ss, p + 1), S + okjudge(m, ss, p + 2))
    elif c == 'r':
        ret =  P + okjudge(m, ss, p + 1)
    elif c == 's':
        ret =  R + okjudge(m, ss, p + 1)
    else:
        ret =  S + okjudge(m, ss, p + 1)

    memo[m][p] = ret
    return ret

print(sum([okjudge(mi, v, 0) for mi,v in enumerate(l)]))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 5)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K

for i in range(N):
    l[i % K] += T[i]


def okjudge(rest):
    if len(rest) == 0:
        return 0

    c = rest[0]

    if len(rest) == 1:
        if c == 'r':
            return P
        elif c == 's':
            return R
        else:
            return S
    if rest[1] == rest[0]:
        if c == 'r':
            return max(okjudge(rest[1:]), P + okjudge(rest[2:]))
        elif c == 's':
            return max(okjudge(rest[1:]), R + okjudge(rest[2:]))
        else:
            return max(okjudge(rest[1:]), S + okjudge(rest[2:]))
    elif c == 'r':
        return P + okjudge(rest[1:])
    elif c == 's':
        return R + okjudge(rest[1:])
    else:
        return S + okjudge(rest[1:])

print(sum([okjudge(v) for v in l]))


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K
memo = [{} for _ in range(K)]

for i in range(N):
    l[i % K] += T[i]

def okjudge(m, ss, p):
    if p in memo[m]:
        return memo[m][p]

    lss = len(ss)
    if p == lss:
        ret =  0
        memo[m][p] = ret
        return ret

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            ret = P
        elif c == 's':
            ret =  R
        else:
            ret =  S
        memo[m][p] = ret
        return ret

    if ss[p + 1] == ss[p]:
        if c == 'r':
            ret =  max(okjudge(m, ss, p + 1), P + okjudge(m, ss, p + 2))
        elif c == 's':
            ret =  max(okjudge(m, ss, p + 1), R + okjudge(m, ss, p + 2))
        else:
            ret =  max(okjudge(m, ss, p + 1), S + okjudge(m, ss, p + 2))
    elif c == 'r':
        ret =  P + okjudge(m, ss, p + 1)
    elif c == 's':
        ret =  R + okjudge(m, ss, p + 1)
    else:
        ret =  S + okjudge(m, ss, p + 1)

    memo[m][p] = ret
    return ret

print(sum([okjudge(mi, v, 0) for mi,v in enumerate(l)]))