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

Solution (Python):
import math

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

Rc = T.count("r")
Sc = T.count("s")
Pc = len(T) - (Rc + Sc)



def check(x):
  global Rc, Sc, Pc
  if x == "r":
    Rc -= 1
  elif x == "s":
    Sc -= 1
  else:
    Pc -= 1

a, b = divmod(N, K)
T = list(T + "x"*(K - b))



for i in range(K):
  Flag = False
  for j in range(1, a+1):
    if T[i + K*j] == T[i + K*(j-1)]:
      if Flag:
        Flag = False
      else:
        check(T[i + K*j])
        Flag = True
    else:
      Flag = False
  

print(Rc * P + Sc * R + Pc * S)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    ret = 0
    a = []
    for i, c in enumerate(T):
        if c == 'r' and not (i >= K and a[i - K] == c):
            ret += P
            a.append('r')
        elif c == 's' and not (i >= K and a[i - K] == c):
            ret += R
            a.append('s')
        elif c == 'p' and not (i >= K and a[i - K] == c):
            ret += S
            a.append('p')
        else:
            a.append('g')
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    R = int(next(tokens))  
    S = int(next(tokens))  
    P = int(next(tokens))  
    T = next(tokens)  
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Solution (Python):

def myAnswer(N:int,K:int,R:int,S:int,P:int,T:str) -> int:
   dic = {"r":R,"s":S,"p":P}
   dic2 = {"r":["p","-"],"s":["r","-"],"p":["s","-"]}
   total = 0
   ans = []
   for n in range(N):
      target = T[n]
      if(n  < K):
         ans.append(dic2[target][0])
         total += dic[dic2[target][0]]
      else:
         if(ans[n - K] == dic2[target][0]):
            ans.append(dic2[target][1])
         else:
            ans.append(dic2[target][0])
            total += dic[dic2[target][0]]
   return total

def modelAnswer():
   return
def main():
   N,K = map(int,input().split())
   R,S,P = map(int,input().split())
   T = input()
   print(myAnswer(N,K,R,S,P,T))
if __name__ == '__main__':
   main()

Solution (Python):
from collections import defaultdict
n,k=map(int,input().split())
r,s,p=map(int,input().split())
S=input()
d=defaultdict(int)
d['r']=p
d['s']=r
d['p']=s
total=0
f=[True]*n

for i in range(n):
    
    
    if i<k or f[i-k] == False:
        total+=d[S[i]]
    else:
        b=S[i]
        a=S[i-k]
        if b==a:
            f[i]=False
        else :
            total+=d[b]
    

print(total)

Solution (Python):
import sys
import  math
from heapq import *
import fractions
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))


N,K=nm()
R,S,P=nm()
d={'r':P,'s':R,'p':S}
T=list(ns())

ans=0
for i in range(K):
    cur=i
    pre=''
    while(cur<N):
        if(pre!=T[cur]):
            ans+=d[T[cur]]
            pre=T[cur]
        else:
            pre=''
        cur+=K

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
S = list(input())
ans = 0
rsp = ['']*n
for i, op in enumerate(S):
    if op=='r': me, pt = 'p', p
    elif op=='s': me, pt = 'r', r
    else: me, pt = 's', s
    if i-k>=0 and rsp[i-k]==me: me, pt = '', 0
    ans+=pt
    rsp[i] = me
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N, K = map(int, readline().split())
    R, S, P = map(int, readline().split())
    T = readline().rstrip().decode()
    ans = 0

    def getPoint(letter):
        if letter == 'r':
            return P
        if letter == 's':
            return R
        if letter == 'p':
            return S

    for i in range(K):
        seq = T[i::K] + 'o'
        
        cnt = 0
        nowLetter = seq[0] 
        for j in seq:
            if j != nowLetter:
                
                ans += ((cnt+1)//2)*getPoint(nowLetter)
                cnt = 0
            cnt += 1
            nowLetter = j
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N, K = map(int, readline().split())
    R, S, P = map(int, readline().split())
    T = readline().rstrip().decode()
    ans = 0

    def getPoint(letter):
        if letter == 'r':
            return P
        if letter == 's':
            return R
        if letter == 'p':
            return S

    for i in range(K):
        seq = T[i::K] + 'o'
        
        cnt = 0
        nowLetter = seq[0] 
        for j in seq:
            if j != nowLetter:
                
                ans += ((cnt+1)//2)*getPoint(nowLetter)
                cnt = 0
            cnt += 1
            nowLetter = j
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
score = {"r": P, "s": R, "p": S, "x": 0}
T = input()

ans = 0

for i in range(K):
    temp = []
    for j in range(i, N, K):
        if not temp:
            temp.append(T[j])
        else:
            if temp[-1] == T[j]:
                temp.append("x")
            else:
                temp.append(T[j])
        ans += score[temp[-1]]
print(ans)

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


ans = 0
commands = [''] * N
for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

    if (i - K >= 0) and (commands[i -  K] == command):
        command = ''
        point = 0

    ans += point
    commands[i] = command

print(ans)

Solution (Python):
import itertools
N,K = [int(hoge) for hoge in input().split()]
RSP = [int(hoge) for hoge in input().split()]
T = input()
Hand = [[] for i in range(K)]

for n,d in enumerate(T):
    if d == "r":
        t = 2
    elif d == "s":
        t = 0
    else:
        t = 1
    Hand[n%K].append(t)

ans = 0
for k in Hand:
    Hands = itertools.groupby(k)
    for h in Hands:
        l = len(list(h[1]))
        ans +=  RSP[h[0]] * ((l+1)//2)
    
print(ans)

Solution (Python):


def II(): return int(input())
def MII(): return map(int, input().split())
def LII(): return list(map(int, input().split()))

def main():
    N, K = MII()
    R, S, P = MII()
    T = list(input())

    ans = 0
    for i, t in enumerate(T):
        if i >= K and T[i-K] == t:
            T[i] = 'x'
        elif t == 'r':
            ans += P
        elif t == 's':
            ans += R
        elif t == 'p':
            ans += S
    
    print(ans)
        

if __name__ == '__main__':
    main()

Solution (Python):
import math
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
hand=[[] for _ in range(k)]
for i in range(len(t)):
    hand[i%k].append(t[i])

def janken(str):
    if str == "r":
        return p
    elif str == "s":
        return r
    else:
        return s

counter=0

for ha in hand:
    dp=[janken(ha[0])]
    if len(ha) == 1:
        counter += dp[0]
        continue

    if ha[0]==ha[1]:
        dp.append(dp[0])
    else:
        dp.append(janken(ha[0])+janken(ha[1]))
    
    for i in range(2,len(ha)):
        if ha[i-1] != ha[i]:
            dp.append(dp[i-1]+janken(ha[i]))
        elif ha[i] != ha[i-2]:
            dp.append(dp[i-1])
        elif dp[i-2] == dp[i-1]:
            dp.append(dp[i-1]+janken(ha[i]))
        else:
            dp.append(dp[i-1])
    counter += dp[-1]
            

print(counter)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
V = {'r':p, 's':r, 'p':s}
ans = 0

for i in range(k):
    check = t[i]
    ans += V[check]
    while True:
        i += k
        if i >= n:
            break
        if check == t[i]:
            check = '-1'
        else:
            ans += V[t[i]]
            check = t[i]
print(ans)

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

pre_T = [[None,False] for _ in range(K)]

ans = 0
for i in range(N):
    if pre_T[i%K][0] == T[i] and pre_T[i%K][1]:
        pre_T[i%K] = T[i], False
    else:
        pre_T[i%K] = T[i], True

        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        else:
            ans += S

print(ans)

Solution (Python):
import numpy as np

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

def num(x):
    if x=="r":
        return 0
    if x=="s":
        return 1
    if x=="p":
        return 2

def miss_count(T):
    miss = [0,0,0]
    for k in range(K):
        l = 1
        for i in range(k+K,N,K):
            if T[i] == T[i-K]:
                l += 1
            else:
                miss[num(T[i-K])] += l//2
                l = 1
        if l!=1:
            miss[num(T[i-K])] += l//2
            l = 1
    return miss
        
miss = np.array(miss_count(T))

scores = np.array([P,R,S])
T_count = np.array([T.count("r"),T.count("s"),T.count("p")]) - miss
score = (scores*T_count).sum()

print(score)

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


ans = 0
commands = [''] * N
for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

    if (i - K >= 0) and (commands[i -  K] == command):
        command = ''
        point = 0

    ans += point
    commands[i] = command

print(ans)

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


ans = 0
commands = [''] * N
for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

    if (i - K >= 0) and (commands[i -  K] == command):
        command = ''
        point = 0

    ans += point
    commands[i] = command

print(ans)

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

score = [0 for _ in range(N)]
for i in range(N):
    if i >= K and t[i-K] == t[i] and score[i-K] != 0:
        continue
    if t[i] == 'r':
        score[i] += P
    elif t[i] == 's':
        score[i] += R
    else:
        score[i] += S

print(sum(score))

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

hands = []
points = 0

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

for i in range(N):
    game_count = i+1
    
    if game_count <= K:
        hands.append(hand_for_win[T[i]])
        points += point_for_win[hand_for_win[T[i]]]
    else:
        if hand_for_win[T[i]] == hands[i-K]:
            hands.append('U') 
        else:
            hands.append(hand_for_win[T[i]])
            points += point_for_win[hand_for_win[T[i]]]

print(points)

Solution (Python):
n , k = map(int, input().split())
r , s , p = map(int, input().split())
t = input()
g = [['?']*(n//k+2) for i in range(k)]
for i in range(n):
    g[i%k][i//k]=t[i]
score = 0
for i in range(k):
    ch=1
    for j in range(n//k+1):
        if ch%2==0:
            if g[i][j]==g[i][j+1]:
                ch+=1
            else:
                ch=1
            continue
        if g[i][j]==g[i][j+1]:
            ch+=1
        else:
            ch=1
        if g[i][j]=='r':
            score+=p
        if g[i][j]=='s':
            score+=r
        if g[i][j]=='p':
            score+=s
print(score)

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

def ten(a):
  if a=="r":
    return p
  elif a=="s":
    return r
  else:
    return s

l = [0 for _ in range(n)]  
for i in range(k):
  l[i] += ten(t[i])
for i in range(k,n):
  if t[i]==t[i-k]:
    if l[i-k]==0:
      l[i] += ten(t[i])
  else:
    l[i] += ten(t[i])
print(sum(l))

Solution (Python):


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


st = [0 for _ in range(n)]
for i in range(n):
    if i < k: 
        if t[i] == 'r' :
            st[i] = 'p'
        elif t[i] == 's' :
            st[i] = 'r'
        elif t[i] == 'p':
            st[i] = 's'
    else:
        if t[i] == 'r' and st[i-k] != 'p':
            st[i] = 'p'
        elif t[i] == 's' and st[i-k] != 'r':
            st[i] = 'r'
        elif t[i] == 'p'and st[i-k] != 's':
            st[i] = 's'
        
score = 0
for i in st:
    if i == 'r' :
        score += r
    elif i == 's' :
        score += s
    elif i == 'p' :
        score += p

print(score)

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

def calculate(n,k,r,s,p,trr):
    points = []
    total = 0
    for i in range(n):
        t = trr[i]
        score = 0
        if t == 'r':
            point = "p"
            score = p
        if t == "s":
            point = "r"
            score = r
        if t == "p":
            point = "s"
            score = s

        if i - k >= 0:
            if point == points[i - k]:
                point = ""
                score = 0

        total = total + score

        points.append(point)

    print(total)

calculate(N,K,R,S,P,T)

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

ans = 0
tk = ['0'] * n
for i in range(len(t)):
    if i < k:
        if t[i] == 'r':
            tk[i] = 'p'
        elif t[i] == 's':
            tk[i] = 'r'
        else:
            tk[i] = 's'
    else:
        if t[i] == 'r' and tk[i-k] != 'p':
            tk[i] = 'p'
        elif t[i] == 's' and tk[i-k] != 'r':
            tk[i] = 'r'
        elif t[i] == 'p' and tk[i-k] != 's':
            tk[i] = 's'
        else:
            continue
c = collections.Counter(tk)
print(c['r']*r + c['s']*s + c['p']*p)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
score = [r,s,p]
def f():
    dp = [[0]*3 for j in range(len(win)+1)]
    for j in range(1,len(win)+1):
        w = win[j-1]
        
        for k in range(3):
            pl = 0
            if(k==0):
                if(w=='r'):
                    pl = r
                dp[j][k] = max(dp[j-1][1]+pl,dp[j-1][2]+pl) 
            if(k==1):
                if(w=='p'):
                    pl = p
                dp[j][k] = max(dp[j-1][0]+pl,dp[j-1][2]+pl)
            if(k==2):
                if(w=='s'):
                    pl = s
                dp[j][k] = max(dp[j-1][0]+pl,dp[j-1][1]+pl)
    return max(dp[-1])

ans = 0
for i in range(k):
    win = ''
    for j in range(i,n,k):
        enemy = t[j]
        if(enemy=='r'):
            win += 'p'
        if(enemy=='p'):
            win += 's'
        if(enemy=='s'):
            win += 'r'
    
    ans += f()

print(ans)

Solution (Python):
def resolve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    point = {"r": P, "s": R, "p": S}
    T = input()

    res = [0] * N
    for i, t in enumerate(T):
        if i - K < 0:
            res[i] = point[t]
        if i - K >= 0 and (res[i - K] == 0 or T[i - K] != T[i]):
            res[i] = point[t]

    print(sum(res))

resolve()

Solution (Python):
n,k=map(int,input().split())
s,p,r=map(int,input().split())
t=input()
u=[0]*k
tt=[[p,r,s][(x=='r')+2*(x=='s')] for x in t]
ans=0
for i in range(n):
  if u[i%k]==0:
    ans+=tt[i]
    u[i%k]=1
  else:
    if not t[i]==t[i-k]:
      ans+=tt[i]
    else:
      u[i%k]=0
print(ans)

Solution (Python):
N, K = 5, 2
R, S, P = 8, 7, 6
TRR = ['r', 's', 'r', 'p', 'r']

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


def calculate(n, k, r, s, p, trr):
    commands = []
    points = 0

    for i in range(n):
        command = ""
        point = 0

        if trr[i] == 'r':
            command = "p"
            point = p

        if trr[i] == 's':
            command = 'r'
            point = r

        if trr[i] == 'p':
            command = 's'
            point = s

        if i - k >= 0:
            if commands[i - k] == command:
                command = ""
                point = 0

        commands.append(command)
        points = points + point

    print(points)


calculate(N, K, R, S, P, TRR)

Solution (Python):
N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T=input()
D=dict()
D['s']=R; D['p']=S; D['r']=P

A=[[-1 for i in range(K)] for j in range(N//K+1)]
B=[[-1 for i in range(K)] for j in range(N//K+1)]
for i,value in enumerate(T):
    A[i//K][i%K]=value
    B[i//K][i%K]=D[value]
    
ans=0
for i in range(N):
    if B[i//K][i%K]!=-1 and i//K!=0 and A[i//K][i%K]!=A[i//K-1][i%K]:
        ans+=B[i//K][i%K]
    elif B[i//K][i%K]!=-1 and i//K!=0 and A[i//K][i%K]==A[i//K-1][i%K]:
        A[i//K][i%K]=-1
    elif B[i//K][i%K]!=-1 and i//K==0:
        ans+=B[i//K][i%K]
print(ans)

Solution (Python):

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

dic_p = {"r": r, "s": s, "p": p}
dic_win = {"r": "p", "s": "r", "p": "s"}
player = [dic_win[c] for c in com]

point = 0
for i in range(n):
    if i - k < 0 or (i - k >= 0 and player[i] != player[i - k]):
        point += dic_p[player[i]]
    else:
        if player[i] == player[i - k]:
            player[i] = ""

print(point)

Solution (Python):
n, k = map(int, input().split())
R, S, P = map(int, input().split())
t = input()
point = {'r':P, 's':R, 'p':S}

ans = 0
for i in range(k):
    j = i
    m = 0
    pre_t = 'na'
    counted = False
    while j < n:
        if not (t[j] == pre_t and counted):
            ans += point[t[j]]
            counted = True
        else:
            counted = False
        pre_t = t[j]
        m += 1
        j = i + m * k
print(ans)

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

def point(i, hand):
    if   T[i] == 'r' and hand == 'p':
        return P
    elif T[i] == 's' and hand == 'r':
        return R
    elif T[i] == 'p' and hand == 's':
        return S
    return 0

dp = [{'r': 0, 's': 0, 'p': 0} for _ in range(N)]

for i in range(N):
    if i - K < 0:
        dp[i]['r'] = point(i, 'r')
        dp[i]['s'] = point(i, 's')
        dp[i]['p'] = point(i, 'p')
    else:
        dp[i]['r'] = point(i, 'r') + max(dp[i-K]['s'], dp[i-K]['p'])
        dp[i]['s'] = point(i, 's') + max(dp[i-K]['r'], dp[i-K]['p'])
        dp[i]['p'] = point(i, 'p') + max(dp[i-K]['r'], dp[i-K]['s'])

ans = 0
for i in range(N - K, N):
    ans += max(dp[i]['r'], dp[i]['s'], dp[i]['p'])

print(ans)

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

ans=[0]*N
i=0
while i<N:
    if i<K:
        if T[i]=='r':
            ans[i]=P
        elif T[i]=='s':
            ans[i]=R
        else:
            ans[i]=S
    else:
        if T[i]=='r':
            if T[i-K]=='r'and ans[i-K]==P:
                ans[i]=0
            else:
                ans[i]=P
        elif T[i]=='s':
            if T[i-K]=='s'and ans[i-K]==R:
                ans[i]=0
            else:
                ans[i]=R
        else:
            if T[i-K]=='p'and ans[i-K]==S:
                ans[i]=0
            else:
                ans[i]=S
    i+=1

print(sum(ans))

Solution (Python):
N, K = map(int, input().split())
points = {c: p for c, p in zip("spr", map(int, input().split()))}
strings = [[] for _ in range(K)]
counts = [1] * K
ans = 0
for i, c in enumerate(list(input()) + [""] * K):
    idx = i % K
    if strings[idx]:
        last = strings[idx][-1]
        if c == last:
            counts[idx] += 1
        else:
            ans += ((counts[idx] + 1) >> 1) * points[last]
            counts[idx] = 1
    strings[idx].append(c)
print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()[:-1].decode()

    ans = 0
    result = [False] * n
    for i in range(n):
        if k <= i and t[i] == t[i - k] and result[i - k]:
            continue
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        elif t[i] == 'p':
            ans += s
        result[i] = True

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
N,K=map(int, input().split())
R,S,P=map(int, input().split())
T=input()
hand=[0]*(len(T))
for i in range(len(T)):
    if (i>K-1) and (T[i]==T[i-K]) and (hand[i-K]!=0):
            hand[i]=0
    elif (T[i]=='r'):
        hand[i]=P
    elif (T[i]=='s'):
        hand[i]=R
    elif (T[i]=='p'):
        hand[i]=S
print(sum(hand))

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

aite_to_rew = {'r': [0, 0, P], 's': [R, 0, 0,], 'p': [0, S, 0]}

total_score = 0
for i in range(K):
    tv = T[i::K]
    dp = aite_to_rew[tv[0]]

    for i in range(1, len(tv)):
        aite = tv[i]

        r_score = R if aite == 's' else 0
        s_score = S if aite == 'p' else 0
        p_score = P if aite == 'r' else 0

        dp_next = [
            max(dp[1] + r_score, dp[2] + r_score),
            max(dp[0] + s_score, dp[2] + s_score),
            max(dp[0] + p_score, dp[1] + p_score),
        ]
        dp = dp_next
    
    total_score += max(dp)

print(total_score)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()[:-1].decode()

    ans = 0
    result = [False] * n
    for i in range(n):
        if k <= i and t[i] == t[i - k] and result[i - k]:
            continue
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        elif t[i] == 'p':
            ans += s
        result[i] = True

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):


def get_score (taKa,cp,score):
    result = 0
    if taKa == "r" and cp == "s":
        result = score[taKa]
    if taKa == "s" and cp == "p":
        result = score[taKa]
    if taKa == "p" and cp == "r":
        result = score[taKa]
    return result
    

N , K = map(int,input().split())
scR,scS,scP = map(int,input().split())
score = {"r":scR,"s":scS,"p":scP}
word = input()
dp = [[0]*3  for i in range(N)]
hand = ["r","s","p"]
result  = [0]*3
for i in range(N):
    for j in range(3):
        result[j] = get_score(hand[j],word[i],score)

    if i < K:
        for j in range(3):
            dp[i][j] = result[j]
    else:
        dp[i][0] = max(dp[i-K][1]+result[0],dp[i-K][2]+result[0])
        dp[i][1] = max(dp[i-K][0]+result[1],dp[i-K][2]+result[1])
        dp[i][2] = max(dp[i-K][0]+result[2],dp[i-K][1]+result[2])
result = [0]*K
for i in range(N-K,N):
    result[N-K-i] = max(dp[i])

print(sum(result))

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

hands = {'r':p, 's':r, 'p':s}
ans = 0


for i in range(k):
    last_hand = ""
    j = i
    while j < n:
        opposite_hand = t[j]
        if opposite_hand != last_hand:
            ans += hands[opposite_hand]
            last_hand = opposite_hand
        else:
            last_hand = "" 
        j += k

print(ans)

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

rew = { 'r': P, 's': R, 'p': S, }
win = { 'r': 'p', 's': 'r', 'p': 's' }
te = {'r', 's', 'p'}

tot = 0
for i in range(K):
    tv = T[i::K] + 'k'
    
    mae = ''
    for j in range(len(tv)-1):
        s = tv[j]
        ns = tv[j+1]

        if mae != win[s]:
            tot += rew[s]
            mae = win[s]
        else:
            if ns == 'k':
                break
            mae = te - {win[ns], mae}
print(tot)

Solution (Python):
from collections import defaultdict
n, k = map(int, input().split())
r, s, p = map(int, input().split())
point = defaultdict(int)
point["r"] = p
point["p"] = s
point["s"] = r
t = input()
ans = 0
hand = [-1] * k
for i in range(n):
    if i < k:
        ans += point[t[i]]
        hand[i] = 1
    elif t[i] != t[i - k] or hand[i % k] != 1:
        ans += point[t[i]]
        hand[i % k] = 1
    elif hand[i % k] == 1:
        hand[i % k] = -1
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
l=list(input())
sumup=0
for i in range(k):
  if l[i]=="r":
    sumup+=p
  if l[i]=="s":
    sumup+=r
  if l[i]=="p":
    sumup+=s
for i in range(k,n):
  if l[i]!=l[i-k]:
    if l[i]=="r":
      sumup+=p
    if l[i]=="s":
      sumup+=r
    if l[i]=="p":
      sumup+=s
  else:
    l[i]="f"
print(sumup)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    RSP = list(map(int, input().split()))
    T = list(input().rsplit()[0])

    win_hand = ['s', 'p', 'r']

    ans = 0
    hand = [0]*N
    for i in range(N):
        for j in range(3):
            if T[i] == win_hand[j]:
                if i >= K:
                    if T[i] == T[i-K] and T[i] == hand[i-K]:
                        continue
                hand[i] = win_hand[j]
                ans += RSP[j]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left

import sys


mod = 10 ** 9 + 7

from collections import deque



N, K = getNM()
R, S, P = getNM()
T = input()

flag = [0] * N
ans = 0
for i in range(N):
    if i - K >= 0 and T[i - K] == T[i] and flag[i - K]:
        continue
    if T[i] == 'r':
        ans+=P
    elif T[i] == 's':
        ans+=R
    else:
        ans+=S
    flag[i] = True
print(ans)

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

X  = input()

res = 0
command = []
x  = 0
for i,t in enumerate(X):

    if t == "r":
        res += P
        x = P
        command.append("p")

    if t == "s":
        res += R
        x = R
        command.append("r")

    if t == "p":
        res += S
        x = S
        command.append("s")
    
    if i >= K and command[i] == command[i-K]:
        res -= x
        command.pop()
        command.append("")
    

print(res)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N, K = MAP()
R, S, P = MAP()
T = list(input())
 
t = []

for i in range(N):
	if T[i] == "r":
		t.append(P)
	if T[i] == "p":
		t.append(S)
	if T[i] == "s":
		t.append(R)
 
ans = sum(t)
 
for i in range(N-K):
	if T[i] == T[i+K]:
		ans -= t[i+K]
		T[i+K] = "O"
 
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
score_dic = {"r":R,"s":S,"p":P}
win_dic = {"r":"p","s":"r","p":"s"}
T = list(input())
score = 0
for i in range(0,N,1):
    if i >= K and T[i-K] == T[i]:
        T[i]="m"
    else:
        score+=score_dic[win_dic[T[i]]]

print(score)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for i in range(k):
    dp = [[0] * (n // k + 3) for _ in range(3)]
    j = 0
    while j * k + i < n:
        dp[0][j + 1] = max(dp[1][j], dp[2][j])
        dp[1][j + 1] = max(dp[0][j], dp[2][j])
        dp[2][j + 1] = max(dp[0][j], dp[1][j])
        if t[j * k + i] == 'r':
            dp[2][j + 1] += p
        elif t[j * k + i] == 's':
            dp[0][j + 1] += r
        else:
            dp[1][j + 1] += s
        j += 1
    ans += max(dp[0][j], dp[1][j], dp[2][j])
print(ans)

Solution (Python):
n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = input()

hand = {'r':p, 's':r, 'p':s}
ans = 0

for i in range(k):
    last = ''
    j = i
    while j < n:
        aite = t[j]
        if aite != last: 
            ans += hand[aite]
            last = aite
        else: 
            last = '' 
        j += k

print(ans)