Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Buggy python submission (verdict: Runtime Error):
mport sys
from collections import defaultdict
sys.setrecursionlimit(10**8)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
sys.setrecursionlimit(10**8)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

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

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


global memo
memo = []


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        if [li, v] in memo:
            return memo[[li, v]]

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        memo.append([li, score])

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(K):
    ans.append(solve(cir[i], R, S, P))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

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

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

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

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(deque)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    if len(cir[i]) == 0:
        continue
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

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

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(deque)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Fixed submission:
from collections import defaultdict

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(map(str, input().rstrip()))

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    # 初手
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    # 前回と違う手なら問答無用で勝てる、前回と同じ手なら奇数回目に勝てる
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))