Problem:


Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996



Sample input:
5 2
8 7 6
rsrpr


Sample output:
27

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ans = 0
get_point = {}
get_point["r"] = p
get_point["s"] = r
get_point["p"] = s
string = [[] for i in range(k)]
for i in range(n):
    string[i%k].append(t[i])
for i in range(len(string)):
    dp = [[0 for q in range(3)] for z in range(len(string[i]))]
    if string[i][0] == "r":
        dp[0][2] = get_point[string[i][0]]
    elif string[i][0] == "s":
        dp[0][0] = get_point[string[i][0]]
    else:
        dp[0][1] = get_point[string[i][0]]
    for j in range(1,len(string[i])):
        if string[i][j] == "r":
            dp[j][0] = max(dp[j-1][1], dp[j-1][2])
            dp[j][1] = max(dp[j-1][0], dp[j-1][2]) 
            dp[j][2] = max(dp[j-1][0], dp[j-1][1]) + get_point[string[i][j]]
        elif string[i][j] == "s":
            dp[j][0] = max(dp[j-1][1], dp[j-1][2]) + get_point[string[i][j]]
            dp[j][1] = max(dp[j-1][0], dp[j-1][2]) 
            dp[j][2] = max(dp[j-1][0], dp[j-1][1]) 
        else:
            dp[j][0] = max(dp[j-1][1], dp[j-1][2])
            dp[j][1] = max(dp[j-1][0], dp[j-1][2]) + get_point[string[i][j]]
            dp[j][2] = max(dp[j-1][0], dp[j-1][1])
    ans += max(dp[-1])
print(ans)

Submission status: Accepted

Solution code:
N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T=input()
ans=0

jlist=[]
for i in range(K):
    jlist.append(T[i:N+1:K])
    
jlist2=[]
for i in range(len(jlist)):
    ind=0
    for j in range(len(jlist[i])-1):
        if jlist[i][j]==jlist[i][j+1]:
            continue
        else:
            jlist2.append(jlist[i][ind:j+1])
            ind=j+1
    jlist2.append(jlist[i][ind:])

for i in range(len(jlist2)):
    if len(jlist2[i])%2!=0:
        if jlist2[i][0]=='r':
            ans+=P*(len(jlist2[i])//2+1)
        elif jlist2[i][0]=='s':
            ans+=R*(len(jlist2[i])//2+1)
        elif jlist2[i][0]=='p':
            ans+=S*(len(jlist2[i])//2+1)
    else:
        if jlist2[i][0]=='r':
            ans+=P*(len(jlist2[i])//2)
        elif jlist2[i][0]=='s':
            ans+=R*(len(jlist2[i])//2)
        elif jlist2[i][0]=='p':
            ans+=S*(len(jlist2[i])//2)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p_ = list(map(int, input().split()))
p = [r, p_, s]
t = str(input())

dp = [[0 for _ in range(3)] for _ in range(n+1)]
# 0: r, 1: p, 2: s
d = {'r': 1, 'p': 2, 's': 0}
for i in range(1, n+1):
    v_num = d[t[i-1]]
    for j in range(3):
        # 勝てる数字
        if i < k + 1:
            if j == v_num:
                dp[i][j] = p[j]
            else:
                dp[i][j] = 0
        else:
            tmp = dp[i-k][:]
            tmp.pop(j)
            if j == v_num:
                dp[i][j] = max(tmp) + p[j]
            else:
                dp[i][j] = max(tmp)
ans = 0
for i in range(1, k+1):
    ans += max(dp[-i])
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc149/tasks/abc149_d
# r = グー, s=ちょき、p=パー


def main():
    (n, k) = map(int, input().split(" "))
    (point_goo, point_choki, point_par) = map(int, input().split(" "))
    pattern = input()
    # rのときパーで勝てる、sのときグーで勝てる,pのときちょきで勝てる
    # k手前と同じ手は使えないという制約がある
    # つまり一手目から最善手を使えば最大点になるとは限らないということ
    # 途中であえて負ける必要性も出てくる
    # また単純に得点の高い手のときは勝つようにするというのもうまく行かない可能性もある
    point = {"r": point_goo, "s": point_choki, "p": point_par, "-": 0}
    win = {"r": "p", "s": "r", "p": "s", "-": "-"}
    total = 0
    for i in range(n - k):
        total += point[win[pattern[i]]]
        if pattern[i] == pattern[i + k]:
            tmp = pattern[:i + k] + "-" + pattern[i + k + 1:]
            pattern = tmp
    for i in range(n - k, n):
        total += point[win[pattern[i]]]
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

score = {'p': P, 'r': R, 's': S}
hand = {'r': 'p', 's': 'r', 'p': 's'}
H = [' '] * N

ans = 0
for sub in range(K):
    i = sub
    while i < N:
        c = T[i]
        if i - K >= 0 and hand[c] == H[i - K]:
            s = 0
            if i + K < N and hand[T[i + K]] == c:
                H[i] = hand[hand[c]]
            else:
                H[i] = c
        else:
            H[i] = hand[c]
            s = score[hand[c]]
        i += K
        ans += s

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())

for j in range(N-K):
    if T[K+j] == T[j]:
        T[K+j] = 0
print(sum([T.count("r")*P, T.count("s")*R, T.count("p")*S]))

Submission status: Accepted

Solution code:
def dp(T, R, S, P):
    r, s, p = 0, 0, 0
    for t in T:
        r, s, p = max(s, p), max(r, p), max(r, s)
        if t == 'r':
            p += P
        elif t == 's':
            r += R
        else:
            s += S
    return max(r, p, s)

def main():
    N, K = map(int, input().split())
    R, S, P = list(map(int, input().split()))
    T = input()
    m = 0
    for i in range(K):
        m += dp(T[i::K], R, S, P)
    print(m)


main()

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    d = {'r':P, 's':R, 'p':S}
    T = input()
    a = 0
    for j in range(K):
        prev = None
        for i in range(j, N, K):
            if prev != T[i]:
                a += d[T[i]]
                prev = T[i]
            else:
                prev = None
    return a

print(main())

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

ret = 0
for i in range(1, K+1):
    prev = '?'
    for j in range(len(T) // K + 1):
        if i + K*j - 1 >= len(T):
            continue
        now = T[i + K*j - 1]
        
        if prev == now:
            prev = '?'
        else:
            prev = now
            if now == 'r':  
                ret += P
            elif now == 's':
                ret += R
            else:
                ret += S
            
print(ret)

Submission status: Accepted

Solution code:
d = {"r": "p", "s": "r", "p": "s"}
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
score = {"r": r, "s": s, "p": p}
d2 = {}
r = 0


def set_hand(l, k):
    if l + k < n:
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "s"}:
            return "p"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "p"}:
            return "s"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"p", "s"}:
            return "r"
        return t[l]
    else:
        return t[l]


for i in range(k):
    for l in range(i, n, k):
        if l - k >= 0:
            if d2[l - k] == d.get(t[l]):
                d2[l] = set_hand(l, k)
            else:
                d2[l] = d.get(t[l])
                r += score[d2[l]]
        if l - k < 0:
            d2[l] = d.get(t[l])
            r += score[d2[l]]
print(r)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
points = list(map(int, input().split()))
hand = {'r': 0, 's': 1, 'p': 2}
T = [hand[t] for t in input()]
win = [False] * N
ans = 0
for i in range(N):
    if i < K or T[i] != T[i-K] or not win[i-K]:
        win[i] = True
        ans += points[(T[i]+2) % 3]
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    cost = defaultdict(int)
    cost["r"] = p
    cost["s"] = r
    cost["p"] = s
    t = list(input())

    ans = 0
    for i in range(k):
        c = 0
        for j in range(i, n, k):
            if i == j:
                ans += cost[t[j]]
            else:
                if t[j] == t[j - k]:
                    t[j] = "ggg"
                else:
                    ans += cost[t[j]]
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r,s,p = map(int, input().split())
S = input()
ans = 0
for i in range(k):
    Ssep = S[i::k] 
    flg = False
    for j in range(len(Ssep)):
        if j >= 1 and Ssep[j] == Ssep[j-1] and not flg:
            flg = True
            continue
        if Ssep[j] == "r":
            ans += p
        if Ssep[j] == "s":
            ans += r
        if Ssep[j] == "p":
            ans += s
        flg = False
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
l=[]
pt=0
ns=t.replace('r', 'tr').replace('p', 'tp').replace('s', 'ts').replace('tr', 'p').replace('tp', 's').replace('ts', 'r')
pt+=(ns.count("r")*r)+(ns.count("s")*s)+(ns.count("p")*p)
for i in range(n):
    if i>=k and ns[i]==ns[i-k]:
        if ns[i]=="p":
            ns=ns[:i]+'X'+ns[i+1:]
            pt-=p
        if ns[i]=="r":
            ns=ns[:i]+'X'+ns[i+1:]
            pt-=r
        if ns[i]=="s":
            ns=ns[:i]+'X'+ns[i+1:]
            pt-=s
print(pt)

Submission status: Accepted

Solution code:
def score(c):
    if c == "r":
        return R
    if c == "s":
        return S
    if c == "p":
        return P
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

new_T = [""] * N
for i in range(N):
    if T[i] == "r":
        new_T[i] = "p"
    if T[i] == "s":
        new_T[i] = "r"
    if T[i] == "p":
        new_T[i] = "s"

ans = 0
for i in range(N):
    if i < K:
       ans += score(new_T[i]) 
    else:
        if new_T[i] == new_T[i-K]:
            new_T[i] = "x"
        else:
            ans += score(new_T[i])

print(ans)

Submission status: Accepted

Solution code:
# 1秒で着想したけど実装が一度ドハマリして死亡 死亡ってか殺意
# 考え直して、すべての状態をboolで管理することにした

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
rsp = [True for _ in range(n)]
ans = r if t[0] == "s" else s if t[0] == "p" else p

for i in range(1, n):
    if i + 1 <= k:
        ans += r if t[i] == "s" else s if t[i] == "p" else p
    else:
        if t[i - k] == t[i]:
            if rsp[i - k] == True:
                rsp[i] = False
            else:
                ans += r if t[i] == "s" else s if t[i] == "p" else p
                rsp[i] = True
        else:
            ans += r if t[i] == "s" else s if t[i] == "p" else p
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())

l = []
res = 0

for i in range(n):

    if t[i] == 'r':
        l.append('p')
    if t[i] == 's':
        l.append('r')
    if t[i] == 'p':
        l.append('s')
        
    if i >= k:
        if l[i] == l[i - k]:
            l[i] = 'x'
        
    if l[i] == 'r':
        res += r
    if l[i] == 's':
        res += s
    if l[i] == 'p':
        res += p
        
# print(l)
print(res)

Submission status: Accepted

Solution code:
def main():
    N, K = tuple(int(i) for i in input().split())
    R, S, P = tuple(int(i) for i in input().split())
    T = input()

    points = {
        'r': P,
        's': R,
        'p': S,
    }

    # print(N, K)
    # print(R, S, P)
    # print(T)

    history = []
    total = 0

    for i in range(N):
        v = T[i]
        if i < K:
            history.append(v)
            total += points[v]
        elif history[i - K] == v:
            history.append('x')
        else:
            history.append(v)
            total += points[v]
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding:utf-8

def calc_max_point(e_seq, scores):
    to_win = {'r': 'p', 's': 'r', 'p': 's'}

    if e_seq in ['r' * len(e_seq), 's' * len(e_seq), 'p' * len(e_seq)]:
        if len(e_seq) % 2 == 1:
            return scores[to_win[e_seq[0]]] * (len(e_seq) // 2 + 1)
        else:
            return scores[to_win[e_seq[0]]] * len(e_seq) // 2

    m_seq = ''
    should_wait = False
    ret_val = 0

    for i, e_choice in enumerate(e_seq):
        m_choice = to_win[e_choice]

        if m_seq == '':
            ret_val += scores[m_choice]
            m_seq += m_choice

        else:
            if m_seq[-1] != m_choice:
                ret_val += scores[m_choice]
                m_seq += m_choice
            else:
                try:
                    next_choice = to_win[e_seq[i+1]]
                    if next_choice == m_choice:
                        m_choice = to_win[m_choice]
                        m_seq += m_choice
                except IndexError:
                    m_choice = to_win[m_choice]
                    m_seq += m_choice

    return ret_val


N, K = map(int, input().split(' '))
R, S, P = map(int, input().split(' '))
T = input()

ans = 0

for i in range(K):
    val = calc_max_point(T[i::K], {'r': R, 's': S, 'p': P})
    ans += val

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P =  map(int,input().split())
T = input()
T2=[0]*N
rsp = ["r","s","p"]
for i in range(N):
    for j in range(3):
        if T[i] == rsp[j]:
            T2[i] = j

won = [P,R,S]

for i in range(K,N):
    if T2[i]==T2[i-K]:
        T2[i] = -1

ans=0

for i in range(N):
    if T2[i]==0:
        ans += P
    if T2[i]==1:
        ans+= R
    if T2[i]==2:
        ans+= S

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
pattern = []

point = {"r" : p, "s" : r, "p" : s}

for i in range(0, k):
    pattern.append(t[i::k])

ans = 0
tmp = ""

for i in pattern:
    tmp = ""
    count = 0
    for j in range(len(i)):
        if tmp == i[j]:
            if count == 1:
                ans += point[i[j]]
                count = 0

            else:
                count = 1

        else:
            count = 0
            tmp = i[j]
            ans += point[i[j]]

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

Dasute = []
pts = 0

pts_d = {"r": R, "s": S, "p": P}

selects = ["r", "s", "p"]

for i in range(len(T)):
    if T[i] == "r":
        Dasute.append("p")
        # pts += P
    elif T[i] == "s":
        Dasute.append("r")
        # pts += R
    else:
        Dasute.append("s")
        # pts += S

for i in range(N - K):
    if Dasute[i] == Dasute[i + K]:
        selects = ["r", "s", "p"]
        selects.remove(Dasute[i])
        for sel in selects:
            if i + K + K <= N - 1:
                if sel != Dasute[i + K + K]:
                    # pts -= pts_d[Dasute[i]]
                    Dasute[i + K] = sel
            else:
                if sel != Dasute[i - K]:
                    # pts -= pts_d[Dasute[i]]
                    Dasute[i] = sel

re_pts = 0

for i in range(len(Dasute)):
    if Dasute[i] == "r" and T[i] == "s":
        re_pts += pts_d[Dasute[i]]
    if Dasute[i] == "s" and T[i] == "p":
        re_pts += pts_d[Dasute[i]]
    if Dasute[i] == "p" and T[i] == "r":
        re_pts += pts_d[Dasute[i]]

print(re_pts)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
points = 0
a = True
b = True
c = True
for i in range(k):
    j = i
    cnt = 1
    while j < n:
        if j + k < n and t[j] == t[j + k]:
            cnt += 1
        else:
            if cnt % 2 == 0:
                cnt = cnt // 2
            else:
                cnt = cnt // 2 +1
            if t[j] == "r":
                points += cnt * p
            elif t[j] == "s":
                points += cnt * r
            elif t[j] == "p":
                points += cnt * s
            cnt = 1
        # print(i, j, cnt, points)
        j += k


print(points)

Submission status: Accepted

Solution code:
import math
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = 0
hand = {"r":P, "s":R, "p":S}
for i in range(K):
    tmp = T[i::K]
    #print(T[i::K])
    score += hand[tmp[0]]
    flag = True
    for j in range(1,len(tmp)):
        if tmp[j] == tmp[j - 1]:
            flag = not flag
        else:
            flag = True
        if flag:
            score += hand[tmp[j]]
print(score)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
T=input()

rsp={'r':p,'p':s,'s':r}
ans=[-1]*n
for i in range(n):
  if ans[i]==-1:
    p=rsp[T[i]]
    ans[i]=p
    if i+k<n:
      if T[i]==T[i+k]:
        ans[i+k]=0
print(sum(ans))

Submission status: Accepted

Solution code:
# ランレングス圧縮
def getRunLengthEncoding(Ss):
    anss = []
    Ss += '$'
    SPrev = Ss[0]
    num = 1
    for S in Ss[1:]:
        if S == SPrev:
            num += 1
        else:
            anss.append((SPrev, num))
            SPrev = S
            num = 1
    return anss

N, K = map(int, input().split())
R, S, P = map(int, input().split())
Ts = input()

ans = 0
for i in range(K):
    As = Ts[i::K]
    RLE = getRunLengthEncoding(As)
    for c, num in RLE:
        m = (num+1)//2
        if c == 'r':
            ans += P*m
        elif c == 's':
            ans += R*m
        else:
            ans += S*m

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[]
for x in T:
    if x == 's':
        A.append('r')
    if x == 'p':
        A.append('s')
    if x == 'r':
        A.append('p')
 
for i in range(K,N):
    if A[i-K] == A[i]:
        A[i] = 'x'
 
ans = 0
for x in A:
    if x == 's':
        ans += S
    if x == 'p':
        ans += P
    if x == 'r':
        ans += R
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split(" "))
R, S, P = map(int, input().split(" "))
T = input()

score_by_hand = {"r": P, "s": R, "p": S}
result = 0
score = 0
for k in range(K):
    column = T[k::K]
    before = None
    for hand in column:
        if hand == before:
            before = None
        else:
            score += score_by_hand[hand]
            before = hand

print(score)

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())

    score = {'r':  P, 's': R, 'p': S}

    ans = 0
    for i in range(K):
        ans += score[T[i]]

    for i in range(K, N):
        if T[i-K] == T[i]:
            T[i] = ' '
            continue
        ans += score[T[i]]

    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r, s, p = map(int, input().split())
scores = {"r":p,"s":r,"p":s}
T = input()
import numpy as np
subT = [""] * k
for i in range(n):
    ind = i % k 
    subT[ind] += T[i]

ans = 0
for S in subT:
    cnt = 0
    prev = "0"
    for i, s in enumerate(S):
        if i == 0:
            ans += scores[s]
        elif prev == s:
            cnt += 1
            if cnt == 2:
                ans += scores[s]
                cnt = 0
        else:
            ans += scores[s]
            cnt = 0
        prev = s
        
        

print(ans)

Submission status: Accepted

Solution code:
#############################
#
# じゃんけん説明
# r: グー
# s: チョキ
# p: パー
#
#############################
#
# 入力r -> 勝利時の グー の点数
# 入力s -> 勝利時の チョキ の点数
# 入力p -> 勝利時の パー の点数
#
#############################

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

MY_HAND = []
ANS = 0

def can_not_use_hand(after_k_hand):
  if after_k_hand == 'r':
    return 'p'
  if after_k_hand == 's':
    return 'r'
  if after_k_hand == 'p':
    return 's'

for battle_time in range(n):
  if battle_time < k:
    if t[battle_time] == "r":
      MY_HAND.append("p")
      ANS += p
    
    if t[battle_time] == "s":
      MY_HAND.append("r")
      ANS += r
    
    if t[battle_time] == "p":
      MY_HAND.append("s")
      ANS += s
  else:
    if t[battle_time] == "r": # 基本 -> [p]
      if MY_HAND[battle_time-k] == "p": # [p] を前に使ってる -> [r, s, -]
        try:
          if can_not_use_hand(t[battle_time+k]) == 'r':
            MY_HAND.append("s")
            ANS += 0
          else:
            MY_HAND.append("r")
            ANS += 0
        except IndexError:
          MY_HAND.append("r")
          ANS += 0
      else: # 基本通り
        MY_HAND.append("p")
        ANS += p

    if t[battle_time] == "s": # 基本 -> [r]
      if MY_HAND[battle_time-k] == "r": # [r] を前に使ってる -> [-, s, p]
        try:
          if can_not_use_hand(t[battle_time+k]) == 's':
            MY_HAND.append("p")
            ANS += 0
          else:
            MY_HAND.append("s")
            ANS += 0
        except IndexError:
          MY_HAND.append("s")
          ANS += 0
      else: # 基本通り
        MY_HAND.append("r")
        ANS += r
    
    if t[battle_time] == "p": # 基本 -> [s]
      if MY_HAND[battle_time-k] == "s": # [s] を前に使ってる -> [r, -, p]
        try:
          if can_not_use_hand(t[battle_time+k]) == 'p':
            MY_HAND.append("r")
            ANS += 0
          else:
            MY_HAND.append("p")
            ANS += 0
        except IndexError:
          MY_HAND.append("p")
          ANS += 0
      else: # 基本通り
        MY_HAND.append("s")
        ANS += s

print(ANS)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
f = [0] * n
point = []
 
for i in range(n):
    if t[i] == "r":
        point.append(p)
    elif t[i] == "s":
        point.append(r)
    else:
        point.append(s)
 
for i in range(k, n):
    if t[i-k] == t[i] and f[i-k] == 0:
        point[i] = 0
        f[i] = 1
 
print(sum(point))

Submission status: Accepted

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    R, S, P = (int(i) for i in input().split())
    T = input()
    dp = [[0 for _ in range(N+1)] for _ in range(3)]
    pre = []
    for i, t in enumerate(T):
        if t == "r":
            dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + P
        elif t == "s":
            dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + R
            dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
        elif t == "p":
            dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + S
            dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])

        if dp[0][i+1] == max(dp[0][i+1], dp[1][i+1], dp[2][i+1]):
            pre.append(0)
        elif dp[1][i+1] == max(dp[0][i+1], dp[1][i+1], dp[2][i+1]):
            pre.append(1)
        else:
            pre.append(2)

        if K <= i:
            if pre[-(K+1)] == pre[-1]:
                dp[pre[-1]][i+1] = 0
                pre[-1] = -1
    print(max(dp[0][N], dp[1][N], dp[2][N]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
s, p, r = map(int, input().split())
T = [t for t in 'a'*K + input() + 'a'*K]
h = {'r':'p', 's':'r', 'p':'s'}
G = []
for n in range(K, N+K):
    if T[n] == T[n-K]:
        T[n] = h[T[n]]
        if T[n] == T[n+K]:
            T[n] = h[T[n]]
        G.append(T[n])

print(r*T.count('r')+p*T.count('p')+s*T.count('s') - (r*G.count('r')+p*G.count('p')+s*G.count('s')))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
d = {"r": P, "s": R, "p": S}
D = {"r": "p", "s": "r", "p": "s"}
janken = []

score = 0
for i in range(N):
  if len(janken) < K:
    score += d[T[i]]
    janken.append(D[T[i]])

  elif D[T[i]] != janken[-K]:
    score += d[T[i]]
    janken.append(D[T[i]])
  
  else:
    janken.append("")

print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
d = []
for i in range(K):
  d.append(T[i])
  
p = {'r': P, 's': R, 'p': S, 'X': 0}
ans = 0

for i in range(0, N - K):
  if d[i] == T[i + K]:
    d.append('X')
  else:
    d.append(T[i + K])
    
for i in range(N):
  ans += p[d[i]]
  
print(ans)

Submission status: Accepted

Solution code:
# D
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
ans = 0
dct = {'r': R, 's': S, 'p': P}
win = {'r': 'p', 's': 'r', 'p': 's'}
aiko = {'r': 'r', 's': 's', 'p': 'p'}
lose = {'r': 's', 's': 'p', 'p': 'r'}

hands = []
for i, s in enumerate(T):
    hand = win[s]
    if (i+1 <= K):
        hands.append(hand)
        ans += dct[hand]
    else:
        if (hands[i-K] != hand):
            ans += dct[hand]
            hands.append(hand)
        else:
            if (i < N-K):
                if (win[T[i+K]] != aiko[s]):
                    hands.append(aiko[s])
                elif (win[T[i+K]] != lose[s]):
                    hands.append(lose[s])
            else:
                hands.append(lose[s])
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = deque(list(s for s in input()))

best_hand = {'r': 'p', 's': 'r', 'p': 's'}
ban = []
cand = ''

while t:
    cpu = t.popleft()
    
    if len(ban) < k:
        add_hand = best_hand[cpu]
    else:
        ban_hand = ban.pop(0)
        if best_hand[cpu] == ban_hand:
            add_hand = 'e'
        else:
            add_hand = best_hand[cpu]
    ban.append(add_hand)
    cand += add_hand
    
print(p*cand.count('p') + s*cand.count('s') + r*cand.count('r'))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
machine_hands = list(input())
my_hands = []
ans = 0
for idx, m in enumerate(machine_hands):
      if m == 'r':
        my_hands.append('p')
        ans += p
        tmp = p
      elif m == 's':
        my_hands.append('r')
        ans += r
        tmp = r
      elif m == 'p':
        my_hands.append('s')
        ans += s
        tmp = s
      if (idx >= k) and (my_hands[-1] == my_hands[idx-k]):
        my_hands[-1] = '-'
        ans -= tmp

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
L=[]

for i in range(K):
    L.append(T[i::K])
#print(L)

def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i])
                table.append(count)
                count = 1
        except:
            result += str(num[i])
            table.append(count)
    return(result,table)

def Point(List):
    score=0
    RR=ranlength(List)
    word=RR[0]
    table=RR[1]
    for i in range(len(word)):
        if word[i]=='p':
            score+=(-(-table[i]//2))*S
        elif word[i]=='s':
            score+=(-(-table[i]//2))*R
        else:
            score+=(-(-table[i]//2))*P
#    print(word,table,score)
    return score

M=0
for i in L:
    M+=Point(i)

print(M)

Submission status: Accepted

Solution code:
def solve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())

    player = []
    ans = 0
    for i in range(N):
        winHand = ''
        point = 0
        if T[i] == 'r':
            winHand = 'p'
            point = P
        elif T[i] == 's':
            winHand = 'r'
            point = R
        elif T[i] == 'p':
            winHand = 's'
            point = S

        if i >= K and winHand == player[i-K]:
            point = 0
            winHand = 'other'
        
        ans += point
        player.append(winHand)

    print(ans)
                
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from math import ceil
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
st = t[0]
sc = {"r":p,"s":r,"p":s}
ans = 0

for i in range(k):
    cnt = 0
    for j in range(i,n,k):
        if t[j] != st:
            ans += sc[st]*ceil(cnt/2)
            st = t[j]
            cnt = 1
        else:
            cnt += 1
    else:
        ans += sc[st]*ceil(cnt/2)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    r,s,p=map(int,input().split())
    T=input()

    ans=0
    win=[0]*n
    for i in range(n):
        if(i>=k and T[i]==T[i-k] and win[i-k]): continue
        if(T[i]=='r'): ans+=p
        elif(T[i]=='s'): ans+=r
        else: ans+=s
        win[i]=1

    print(ans)
resolve()