Buggy python submission (verdict: Runtime Error):
from collections import deque

N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

win_point = {
    'r': r,
    's': s,
    'p': p,
}

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}

enemy_hands = input()


def can_win(enemy_hand, my_hands):
    # 勝てる場合にはTrueと勝てる手を教えてくれる
    if enemy_hand == 'r' and 'p' in my_hands:
        return True, 'p'
    if enemy_hand == 's' and 'r' in my_hands:
        return True, 'r'
    if enemy_hand == 'p' and 's' in my_hands:
        return True, 's'

    # 勝てる手がない場合
    return False, None


def choose_hand(enemy_hand, next_enemy_hand, now_hands):
    # 次の手に勝てるようにその手以外を選択する
    if next_enemy_hand == enemy_hand:
        # 次回も同じ手ならどっちでも良い
        return now_hands[0]

    win, hand = can_win(next_enemy_hand, enemy_hand)

    # 次の手と今の相手の手を戦わせる
    # 後の手が勝ったら今の手と同じものを出す
    # 今の手が勝ったら後の手と同じものと出す
    if win:
        return now_hands[enemy_hand]
    else:
        return now_hands[next_enemy_hand]


point = 0

for index in range(K):
    target = index
    now_hands = ['r', 'p', 's']
    for i in range(index, N, K):
        win, hand = can_win(enemy_hands[i], now_hands)

        if win:
            point += win_point[hand]
            now_hands = next_hands[hand]
        else:
            if i + K < N:
                next_enemy_hand = enemy_hands[i+K]
                now_hands = next_hands[choose_hand(
                    enemy_hands[i], next_enemy_hand, now_hands)]

print(point)


Fixed submission:
N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

win_point = {
    'r': r,
    's': s,
    'p': p,
}

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}

enemy_hands = input()


def can_win(enemy_hand, my_hands):
    # 勝てる場合にはTrueと勝てる手を教えてくれる
    if enemy_hand == 'r' and 'p' in my_hands:
        return True, 'p'
    if enemy_hand == 's' and 'r' in my_hands:
        return True, 'r'
    if enemy_hand == 'p' and 's' in my_hands:
        return True, 's'

    # 勝てる手がない場合
    return False, None


point = 0

for index in range(K):
    now_hands = ['r', 'p', 's']
    for i in range(index, N, K):
        win, hand = can_win(enemy_hands[i], now_hands)

        if win:
            point += win_point[hand]
            now_hands = next_hands[hand]
        else:
            # 勝てない場合次似邪魔しない手を選ぶ
            # 勝てない回の次は必ず勝てるため全手出せる前提とする
            now_hands = ['r', 'p', 's']

print(point)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

win_point = {
    'r': r,
    's': s,
    'p': p,
}

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}


def battle(enemy_hand, my_hand):
    point = 0

    if enemy_hand == 'r' and my_hand == 'p':
        point = win_point[my_hand]
    elif enemy_hand == 's' and my_hand == 'r':
        point = win_point[my_hand]
    elif enemy_hand == 'p' and my_hand == 's':
        point = win_point[my_hand]

    return point


enemy_hands = input()
search = deque()
max_point = 0
result = 0
debug = []

for index in range(K):
    hands = ['r', 's', 'p']
    max_point = 0

    # 初期化
    search.appendleft((index, 'r', 0))
    search.appendleft((index, 's', 0))
    search.appendleft((index, 'p', 0))

    while search:
        target = search.popleft()

        enemy_hand = enemy_hands[target[0]]

        # 累積ポイントを加算
        point = target[2] + battle(enemy_hand, target[1])
        next_hand = next_hands[target[1]]

        # 累積ポイントのうち、最も大きいものをmax_pointとして保持
        max_point = max(max_point, point)

        for hand in next_hand:
            if target[0] + K < N:
                search.appendleft((target[0] + K, hand, point))
    result += max_point

print(result)


Fixed submission:
N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

win_point = {
    'r': r,
    's': s,
    'p': p,
}

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}

enemy_hands = input()


def can_win(enemy_hand, my_hands):
    # 勝てる場合にはTrueと勝てる手を教えてくれる
    if enemy_hand == 'r' and 'p' in my_hands:
        return True, 'p'
    if enemy_hand == 's' and 'r' in my_hands:
        return True, 'r'
    if enemy_hand == 'p' and 's' in my_hands:
        return True, 's'

    # 勝てる手がない場合
    return False, None


point = 0

for index in range(K):
    now_hands = ['r', 'p', 's']
    for i in range(index, N, K):
        win, hand = can_win(enemy_hands[i], now_hands)

        if win:
            point += win_point[hand]
            now_hands = next_hands[hand]
        else:
            # 勝てない場合次似邪魔しない手を選ぶ
            # 勝てない回の次は必ず勝てるため全手出せる前提とする
            now_hands = ['r', 'p', 's']

print(point)