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:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("## ",*x,file=sys.stderr) if LOCAL else 0
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    #N = int(input())
    N, K = map(int, input().split())
    R,S,P=map(int, input().split())
    T=input()
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    ans=0
    for k in range(K):
        i=0
        l=[]
        while i+k<N:
            l.append(i+k)
            i+=K
        if not l:
            continue
        dp=[[0]*3 for _ in range(len(l)+1)]
        
        for i in range(1,len(l)+1):
            si=T[l[i-1]]
            sc=[0]*3
            if si=="r":
                sc[2]=P
            elif si=="s":
                sc[0]=R
            elif si=="p":
                sc[1]=S

            for j in range(3):
                for j2 in range(3):
                    if j==j2:continue
                    dp[i][j]=max(dp[i][j],dp[i-1][j2]+sc[j])
            ans+=max(dp[-1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# K個の独立した問題として考える

N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

def is_win(me, other):
    if me == 0 and other == 1:
        return True
    elif me == 1 and other == 2:
        return True
    elif me == 2 and other == 0:
        return True
    return False

def char_to_num(s):
    if s == "r":
        return 0
    elif s == "s":
        return 1
    else:
        return 2

# dp[i][j] = i回目のじゃんけんでjを出して取れる最大の点数([i%K]ごとに独立)
dp = [[-1 for j in range(3)] for i in range(N+1)]
dp[0] = [0, 0, 0]
for i in range(1, N+1):
    t = char_to_num(T[i-1])
    if i <= K:
        for j in range(3):
            dp[i][j] = is_win(j, t)*RSP[j]
    else:    
        for j in range(3):
            dp[i][j] = max(
                [dp[i-K][k] + is_win(j, t)*RSP[j] for k in range(3) if j != k]
            )

# print(dp)

r = sum(map(max, dp[-K:]))
print(r)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
SC = list(map(int, input().split()))
T = input()
Q = []
for i in range(len(T)):
  if T[i] == "r":
    Q.append(0)
  elif T[i] == "s":
    Q.append(1)
  else:
    Q.append(2)
my = [0 for i in range(N)]
ans = 0
for i in range(N):
  if i < K:
    my[i] = (Q[i] - 1) % 3
    ans += SC[my[i]]
  else:
    n = (Q[i] - 1) % 3
    if my[i - K] != n:
      my[i] = n
      ans += SC[my[i]]
    else:
      if i + K < N:
        my[i] = Q[i+K]
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
    if i - K < 0:
        hands.append(wins[T[i]])
        ans += points[T[i]]
    else:
        if hands[i-K] == wins[T[i]]:
            #あいこにするしか無い時
            hands.append("-")
        else:
            #勝てる時
            hands.append(wins[T[i]])
            ans += points[T[i]]
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
dic = {}
dic['s'], dic['p'], dic['r'] = map(int, input().split())
T = input()
score = [0]*N
for i in range(N):
  if i>=K and T[i]==T[i-K] and score[i-K]>0:
    continue
  score[i] = dic[T[i]]
print(sum(score))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

def calc(l):
    dp = [[0]*3 for _ in range(len(l)+1)]
    
    for i in range(len(l)):
        dp[i+1][0] = max(dp[i][1], dp[i][2])+(R if l[i]=='s' else 0)
        dp[i+1][1] = max(dp[i][2], dp[i][0])+(S if l[i]=='p' else 0)
        dp[i+1][2] = max(dp[i][0], dp[i][1])+(P if l[i]=='r' else 0)

    return max(dp[-1])
    
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()[:-1]
d = defaultdict(list)

for i in range(N):
    d[i%K].append(T[i])

ans = 0

for i in range(K):
    ans += calc(d[i])

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
B=list(map(int,input().split()))
S=list(input())
A=[[] for i in range(k)]
for i in range(n):
  if S[i]=="s":
    A[i%k].append(0)
  elif S[i]=="p":
    A[i%k].append(1)
  else:
    A[i%k].append(2)
ans=0
for i in range(k):
  f=0
  for j in range(len(A[i])):
    if f==0:
      ans=ans+B[A[i][j]]
      if j!=len(A[i])-1:
        if A[i][j]==A[i][j+1]:
          f=1
    else:
      f=0
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
dp=[[0,0,0] for i in range(n)]
janken=list(map(int,input().split( )))
t=[0 if i=="r" else 1 if i=="s" else 2 for i in input()]
#直後の手を見て決める
for i in range(n):
    if i<k:
        for j in range(3):
            if (j+1)%3==t[i]:
                dp[i][j]=janken[j]
    else:
        #更新
        for j in range(3):
            #遷移の元
            for l in range(3):
                if j!=l:
                    if (j+1)%3==t[i]:
                        dp[i][j]=max(dp[i][j],janken[j]+dp[i-k][l])
                    else:
                        dp[i][j]=max(dp[i][j],dp[i-k][l])
ans=0
for i in range(n-k,n):
    ans+=max(dp[i])
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = [0] * k
for i in range(k):
    pre_cnt = ["", 0]
    for j in range(i, n, k):
        if j == i:
            if t[j] == "r":
                ans[i] += p
                pre_cnt = ["p", p]
            elif t[j] == "s":
                ans[i] += r
                pre_cnt = ["r", r]
            else:
                ans[i] += s
                pre_cnt = ["s", s]
            continue

        if t[j] == "r":
            if pre_cnt[0] == "p":
                pre_cnt[0] = ""
                continue
            ans[i] += p
            pre_cnt = ["p", p]
        elif t[j] == "s":
            if pre_cnt[0] == "r":
                pre_cnt[0] = ""
                continue
            ans[i] += r
            pre_cnt = ["r", r]
        else:
            if pre_cnt[0] == "s":
                pre_cnt[0] = ""
                continue
            ans[i] += s
            pre_cnt = ["s", s]
print(sum(ans))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = list(input())
rsp = ['r', 's', 'p']
for i in range(N-K):
    if T[i] == T[i+K]:
        T[i+K] = 0
print(sum(RSP[i]*T.count(rsp[(i+1)%3]) for i in range(3)))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = list(map(int, input().split()))
rps = ['r', 's', 'p']
T = input()
ans = 0
for i in range(3):
    for j in range(K):
        o = True
        for k in range(j, N, K):
            if o and (rps[i]+T[k] == 'rs' or rps[i]+T[k] == 'sp' or rps[i]+T[k] == 'pr'):
                ans += h[i]
                o = False
            else:
                o = True
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    RSP_score = LI()
    rsp_num = {'r': 0, 's': 1, 'p': 2}
    T = [rsp_num[i] for i in S()]

    # dp[i][j]: i回目にjを出したときの合計点数
    # Kのmod毎に独立
    dp = [[0] * 3 for _ in range(N)]
    for i in range(N):
        for j in range(3):
            # グーを出す場合、K回前までにグー以外出したときの最大得点に加えて、今回相手がチョキだったら勝点Rを足す
            if i - K >= 0:
                dp[i][j] += max([dp[i-K][k] for k in range(3) if k != j])
            if (j + 1) % 3 == T[i]:
                dp[i][j] += RSP_score[j]

    # for i in dp:
    #     print(i)
    ans = sum([max(i) for i in dp[N-K:]])
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

ans = 0
for i in range(0, K):
    rsp_K = T[i::K]
    rsp_before = ''
    for rsp in rsp_K:
        if rsp == 'r':
            if rsp_before != 'r':
                ans += P
                rsp_before = 'r'
            else:
                rsp_before = ''
        elif rsp == 's':
            if rsp_before != 's':
                ans += R
                rsp_before = 's'
            else:
                rsp_before = ''
        elif rsp == 'p':
            if rsp_before != 'p':
                ans += S
                rsp_before = 'p'
            else:
                rsp_before = ''

print(ans)

Submission status: Accepted

Solution code:
janken = {"r": "p",
          "s": "r",
          "p": "s"}


n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
T = list(input())
score = {"r": r,
         "s": s,
         "p": p}

ans = 0
for i in range(n):
    if i < k:
        ans += score[janken[T[i]]]
    else:
        if T[i] == T[i - k]:
            T[i] = "-"
        else:
            ans += score[janken[T[i]]]
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s, p, r = map(int, input().split()) 
dic = {"p":p, "r":r, "s":s}
t = input()
ans = 0
for i in range(k):
    bef = "a"
    for j in range(i, n, k):

        if bef == t[j]:
            bef = "a"
        else:
            ans += dic[t[j]]
            bef = t[j]
print(ans)

Submission status: Accepted

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

pc_hand = [0]*N
my_hand = [0]*N

for i in range(N):
    if T[i] == 'r':
        pc_hand[i] = 0
    elif T[i] == 's':
        pc_hand[i] = 1
    else:
        pc_hand[i] = 2

point = 0

for i in range(N):
    if i >= K and T[i] == T[i - K] and (my_hand[i - K] + 1) % 3 == pc_hand[i - K]:
        my_hand[i] = pc_hand[i]
    else:
        my_hand[i] = (pc_hand[i] - 1) % 3
        point += pt[my_hand[i]]

print(point)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
divided_T = [[] for _ in range(K)]
for i in range(N):
    divided_T[i%K].append(T[i])
ans = 0
for target in divided_T:
    dp = [[0]*3 for _ in range(len(target))]
    dp[0][0] = R if target[0] == 's' else 0
    dp[0][1] = P if target[0] == 'r' else 0
    dp[0][2] = S if target[0] == 'p' else 0
    for i in range(1,len(target)):
        dp[i][0] = max(dp[i-1][1],dp[i-1][2]) + (R if target[i] == 's' else 0)
        dp[i][1] = max(dp[i-1][0],dp[i-1][2]) + (P if target[i] == 'r' else 0)
        dp[i][2] = max(dp[i-1][1],dp[i-1][0]) + (S if target[i] == 'p' else 0)
    ans += max(dp[-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

rsp = {'r':0, 's':1, 'p':2}
t = [rsp[c] for c in input()]
point = [p, r, s]

dp = [[0] * n for _ in range(3)] # 0:r, 1:s, 2:p
if n <= k:
    ans = sum(point[i] for i in t)
else:
    for i in range(k):
        dp[t[i]][i] = point[t[i]]
    for i in range(k, n):
        for j in range(3):
            dp[j][i] = max(dp[(j+1) % 3][i-k], dp[(j+2) % 3][i-k])
            if j == t[i]:
                dp[j][i] += point[j]
    ans = 0
    for i in range(k):
        ans += max(dp[j][-1-i] for j in range(3))
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines

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

    dp = [[0] * 3 for _ in range(n + k)]
    for i1 in range(k):
        i2 = i1 + k
        while i2 - k < n:
            dp[i2][0] = max(dp[i2-k][1], dp[i2-k][2])
            dp[i2][0] += r if t[i2-k] == 's' else 0
            dp[i2][1] = max(dp[i2-k][0], dp[i2-k][2])
            dp[i2][1] += s if t[i2-k] == 'p' else 0
            dp[i2][2] = max(dp[i2-k][0], dp[i2-k][1])
            dp[i2][2] += p if t[i2-k] == 'r' else 0
            i2 += k
    r = 0
    for j1 in range(-k, 0, 1):
        r += max(dp[j1])
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(n):
    if t[i] == 'r':
        l[i] = p
    elif t[i] == 's':
        l[i] = r
    else:
        l[i] =s

ans = sum(l)
for i in range(k, n):
    if t[i] == t[i - k]:
        ans -= l[i]
        t[i] = 'o'

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline



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

    D = [[] for _ in range(K)]
    for i, t in enumerate(T):
        D[i%K].append(t)

    ans = 0
    for List in D:
        pre = -1
        for i, p in enumerate(List):
            if p == "r":
                s = P
            elif p == "s":
                s = R
            else:
                s = S
            if pre != p:
                ans += s
                pre = p
            else:
                pre = -1
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def decide_hand2(T, hand, i, K, N):
    if T[i] == "r":
        if i <= K - 1:
            hand[i] = "p"
        else:
            if hand[i - K] == "p":
                hand[i] = "-"
            else:
                hand[i] = "p"

    elif T[i] == "p":
        if i <= K - 1:
            hand[i] = "s"
        else:
            if hand[i - K] == "s":
                hand[i] = "-"
            else:
                hand[i] = "s"

    elif T[i] == "s":
        if i <= K - 1:
            hand[i] = "r"
        else:
            if hand[i - K] == "r":
                hand[i] = "-"
            else:
                hand[i] = "r"

def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    hand = ["" for _ in range(N)]

    for i in range(N):
        decide_hand2(T, hand, i, K, N)

    ans = 0
    for i in range(N):
        if hand[i] == "r":
            ans += R
        elif hand[i] == "s":
            ans += S
        elif hand[i] == "p":
            ans += P

    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

ten=0
te=[]

for i in range(k):
    if t[i]=="r":
        ten+=p
        te.append("p")
    elif t[i]=="s":
        ten+=r
        te.append("r")
    elif t[i]=="p":
        ten+=s
        te.append("s")

for i in range(k,len(t)):
    if t[i]=="r" and te[i-k]=="p":
        te.append("a")
    elif t[i]=="s" and te[i-k]=="r":
        te.append("a")
    elif t[i]=="p" and te[i-k]=="s":
        te.append("a")
    else:
        if t[i]=="r":
            ten+=p
            te.append("p")
        elif t[i]=="s":
            ten+=r
            te.append("r")
        elif t[i]=="p":
            ten+=s
            te.append("s")

print(ten)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
x=list(t)
ans=0
for i in range(n):
  if x[i]=="r":x[i]="p"
  elif x[i]=="p":x[i]="s"
  else:x[i]="r"
for i in range(k):
  if x[i]=="r":ans+=r
  elif x[i]=="p":ans+=p
  else:ans+=s
for i in range(k,n):
  if x[i]==x[i-k]:
    x[i]="-"
    continue
  if x[i]=="r":ans+=r
  elif x[i]=="p":ans+=p
  else:ans+=s
print(ans)

Submission status: Accepted

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

me = ""

ans = 0

for i in range(n):
    if t[i] == "r":
        if i < k:
            me += "p"
            ans += p
        elif me[i-k] != "p":
            me += "p"
            ans += p
        else:
            if t[i-k] == "p":
                me += "p"
                ans += p
            else:
                me += "r"

    elif t[i] == "s":
        if i < k:
            me += "r"
            ans += r
        elif me[i-k] != "r":
            me += "r"
            ans += r
        else:
            if t[i-k] == "r":
                me += "r"
                ans += r
            else:
                me += "s"

    else:
        if i < k:
            me += "s"
            ans += s
        elif me[i-k] != "s":
            me += "s"
            ans += s
        else:
            if t[i-k] == "s":
                me += "s"
                ans += s
            else:
                me += "p"

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
l=[]
for i in range(K):
    l.append([T[K-i-1]])
for j in range(K,N):
    if l[K-1-j%K][j//K-1]!=T[j]:
        l[K-1-j%K].append(T[j])
    else:
        l[K-1-j%K].append("t")
ans=0
for k in l:
    ans+=k.count("r")*P+k.count("s")*R+k.count("p")*S
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = [_ for _ in input()]
cnt = 0
for i in range(N):
    if i < K or T[i] != T[i-K]:
        if T[i] == "r":
            cnt += P
        elif T[i] == "s":
            cnt += R
        else:
            cnt += S
    else:
        T[i] = "z"
print(cnt)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = []
a,b,c = 0,0,0
for i in range(n):
    if i>=k:
        if t[i]==ans[i-k]:
            ans.append('X')
            continue
    if t[i] =='r':
        ans.append('r')
        c += 1
    elif t[i]=='s':
        ans.append('s')
        a += 1
    else:
        ans.append('p')
        b += 1
print(a*r+b*s+c*p)

Submission status: Accepted

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

def score(hand):
    if hand == 's':
        return r
    if hand == 'p':
        return s
    if hand == 'r':
        return p

ans = 0
changed = [0] * n

for i in range(n):
    if i >= k and t[i] == t[i - k] and changed[i - k] == 0:
        changed[i] = 1
        continue    
    ans += score(t[i])

print(ans)

Submission status: Accepted

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

ans = 0
player_hand = [None]*(n+3)
for i,a in enumerate(t):
    if a == "r":
        if player_hand[i-k] == "r":
            player_hand[i] == "kyomu"
        else:
            ans += p 
            player_hand[i] = "r"
    if a == "s":
        if player_hand[i-k] == "s":
            player_hand[i] == "kyomu"
        else:
            ans += r 
            player_hand[i] = "s"
    if a == "p":
        if player_hand[i-k] == "p":
            player_hand[i] == "kyomu"
        else:
            ans += s
            player_hand[i] = "p"
print(ans)

Submission status: Accepted

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

ans = 0
m = [0 for _ in range(n)]

for i in range(k):
    if t[i] == 'r':
        ans += p
    elif t[i] == 's':
        ans += r
    else:
        ans += s

for i in range(k, n):
    if t[i] != t[i-k] or m[i-k] == 1:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
    else:
        m[i] = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
dic = defaultdict(int)
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
h = []

r, s, p = 0, 0, 0
for i in range(N):
    if i>=K:
        if T[i]==h[i-K]:
            h.append("x")
            continue
    
    if T[i]=="r":
        h.append("r")
        p+=1
    elif T[i]=="p":
        h.append("p")
        s+=1
    elif T[i]=="s":
        h.append("s")
        r+=1

print(p*P + r*R + s*S)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())

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

t=input()

ans=0
l=[0 for _ in range(n)]

for i in range(k):
  if t[i]=="r":
    ans+=p
  elif t[i]=="s":
    ans+=r
  else:
    ans+=s

for i in range(k,n):
    if t[i]!=t[i-k] or l[i-k]==1:
        if t[i]=="r":
            ans+=p
        elif t[i]=="s":
            ans+=r
        else:
            ans+=s
    else:
        l[i]=1

print(ans)

Submission status: Accepted

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

for i in range(n):
    if i < k:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
    else:
        if t[i] != t[i - k]:
            if t[i] == "r":
                ans += p
            elif t[i] == "s":
                ans += r
            else:
                ans += s
        else:
            t[i] = ""

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
ans = 0
for i in range(k):
    if t[i] == "r":
        ans += p
    elif t[i] == "s":
        ans += r
    else:
        ans += s
for i in range(k, n):
    if t[i] != t[i - k]:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
    else:
        t[i] = ""

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = str(input())
num = p*t.count('r') + r*t.count('s') + s*t.count('p')
w = ""
for i in range(n):
    if t[i] == "r":
        x = "p"
        y = p
    elif t[i] == "s":
        x = "r"
        y = r
    else:
        x = "s"
        y = s
    if i < k:
        w += x
    else:
        if w[i-k] == x:
            num -= y
            w += "q"
        else:
            w += x
print(num)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

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

def score(dd):
    c = 0
    if dd[0] == "r": c += P
    if dd[0] == "s": c += R
    if dd[0] == "p": c += S

    if len(dd) >= 1:
        flag = 0
        for i in range(1,len(dd)):
            if dd[i] == dd[i-1] and flag == 1:
                if dd[i] == "r": c += P
                if dd[i] == "s": c += R
                if dd[i] == "p": c += S
                flag = 0
            elif dd[i] == dd[i-1] and flag == 0:
                flag = 1
            elif dd[i] != dd[i-1]:
                if dd[i] == "r": c += P
                if dd[i] == "s": c += R
                if dd[i] == "p": c += S
                flag = 0

    return c

total = 0
for dd in d:
    total += score(dd)

print(total)

Submission status: Accepted

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

for i in range(k):
    # print(t[i::k])
    t0 = t[i]
    cnt = 1
    for t1 in t[i+k::k]:
        if t0 == t1:
            t = t[:i+cnt*k] + 'n' + t[i+cnt*k+1:]
            t0 = 'n'
        else:
            t0 = t1
        cnt += 1

print(p*t.count('r')+r*t.count('s')+s*t.count('p'))

Submission status: Accepted

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

lis = []
for i in t:
    if i == 'r':
        lis.append('p')
    elif i == 's':
        lis.append('r')
    else:
        lis.append('s')

ans1 = 0
for i in range(k, len(lis)):
    if lis[i] == lis[i-k]:
        lis[i] = 'o'

rc = lis.count('r') * r
sc = lis.count('s') * s
pc = lis.count('p') * p

print(rc+sc+pc)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
a=[]
for i in range(k):
    a.append([])
for i in range(n):
    a[i%k].append(t[i])
for i in range(k):
    for j in range(len(a[i])):
        if j>=1:
            if a[i][j]==a[i][j-1]:
                a[i][j]=0
ans=0
for i in range(k):
    for j in range(len(a[i])):
        if a[i][j]=="r":
            ans+=p
        elif a[i][j]=="s":
            ans+=r
        elif a[i][j]=="p":
            ans+=s
print(ans)

Submission status: Accepted

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

ans=0
A=[]
for i in range(K):
    if T[i]=='r':
        ans+=P
        A.append('p')
    elif T[i]=='s':
        ans+=R
        A.append('r')
    else:
        ans+=S
        A.append('s')

for i in range(K,N):
    if T[i]=='r':
        if A[i-K]!='p':
            ans+=P
            A.append('p')
        else:
            A.append('x')
    elif T[i]=='s':
        if A[i-K]!='r':
            ans+=R
            A.append('r')
        else:
            A.append('x')
    else:
        if A[i-K]!='s':
            ans+=S
            A.append('s')
        else:
            A.append('x')

print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
r,s,p = inpm()
T = input()

cnt_r = T.count('r')
cnt_s = T.count('s')
cnt_p = T.count('p')
ans = cnt_r * p + cnt_s * r + cnt_p * s

tmp = [0]*n
for i in range(k,n):
    if tmp[i-k] == 1:
        continue
    if T[i] == T[i-k]:
        tmp[i] = 1
        if T[i] == 'r':
            ans -= p
        elif T[i] == 's':
            ans -= r
        elif T[i] == 'p':
            ans -= s
print(ans)

Submission status: Accepted

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

t = input()
last = ["" for _ in range(k)]
move = {"r": "p", "p": "s", "s": "r"}
pts = {"r": r, "p": p, "s": s}
score = 0

for i in range(n):
    if move[t[i]] != last[i%k]:
        score += pts[move[t[i]]]
        last[i%k] = move[t[i]]
    else:
        last[i%k] = ""

print(score)