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):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
win_hand = { "r": P, "s": R, "p": S }
prev_hand = {}
LOST,WON = -1,1

points = 0
for i in range(N):
  if i-K >= 0:
    if T[i] != T[i-K]:
      prev_hand[i] = WON
      points += win_hand[T[i]]
    else:
      if prev_hand[i-K] == WON:
        prev_hand[i] = LOST
      else:
        points += win_hand[T[i]]
        prev_hand[i] = WON
  else:
    prev_hand[i] = WON
    points += win_hand[T[i]]

print(points)

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

t = input()
lose_flag = []
ans = 0
for i in range(len(t)):
    if i >= k and t[i] == t[i-k] and not lose_flag[i-k]:
        lose_flag.append(True)
        continue
    lose_flag.append(False)
    ans += point[t[i]]
    
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
u = [False] * n

res = 0
for i in range(n):
    if t[i] == 'r':
        if i - k >= 0 and t[i-k] == 'r' and u[i-k]:
            continue
        u[i] = True
        res += p
    elif t[i] == 's':
        if i - k >= 0 and t[i-k] == 's' and u[i-k]:
            continue
        u[i] = True
        res += r
    else:
        if i - k >= 0 and t[i-k] == 'p' and u[i-k]:
            continue
        u[i] = True
        res += s

print(res)

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

dp = [[0] * 3 for _ in range(N+1)]

def f(i,r):
    return K*i - r
for r in range(0, K):
    i = 1
    while f(i,r) <= N:
        if T[f(i,r)] == "r":
            if i==1:
                dp[f(i, r)][0] = 0
                dp[f(i, r)][1] = 0
                dp[f(i, r)][2] = P
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2])
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2])
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1]) + P
        if T[f(i,r)] == "s":
            if i==1:
                dp[f(i, r)][0] = R
                dp[f(i, r)][1] = 0
                dp[f(i, r)][2] = 0
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2]) + R
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2])
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1])
        if T[f(i,r)] == "p":
            if i==1:
                dp[f(i, r)][0] = 0
                dp[f(i, r)][1] = S
                dp[f(i, r)][2] = 0
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2])
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2]) + S
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1])
        i += 1

ans = 0
for j in range(1, K+1):
    ans += max(dp[-j])
print(ans)

Solution (Python):


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


l = [[] for _ in range(k)]
for num, i in enumerate(t):
    q = num % k
    l[q].append(i)

points = []    
for num1, i in enumerate(l):
    points.append([])
    for num2, j in enumerate(i):
        if num2 == 0:
            if j == 'r':
                points[num1].append(p)
            elif j == 's':
                points[num1].append(r)
            else:
                points[num1].append(s)
        else:
            if j == i[num2-1]:
                if points[num1][num2-1] == 0:
                    if j == 'r':
                        points[num1].append(p)
                    elif j == 's':
                        points[num1].append(r)
                    else:
                        points[num1].append(s)
                else:
                    points[num1].append(0)
            else:
                if j == 'r':
                    points[num1].append(p)
                elif j == 's':
                    points[num1].append(r)
                else:
                    points[num1].append(s)
res = 0
for i in points:
    res += sum(i)

print(res)

Solution (Python):
import collections
N,K = list(map(int,input().split()))
R,S,P = list(map(int,input().split()))
T = list(input())
for i in range(N):
    if i+K < N and T[i] == T[i+K]:
        T[i+K] = 'x'
C = collections.Counter(T)
ans = 0
ans += C['r']*P
ans += C['s']*R
ans += C['p']*S
print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = read().rstrip().decode('utf-8')

cnt = 0
a = ""

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

cnt = a.count("r") * r + a.count("s") * s + a.count("p") * p

print(cnt)

Solution (Python):
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
      T[i]="rw"
    elif T[i]=="s":
      ans+=R
      T[i]="sw"
    elif T[i]=="p":
      ans+=S
      T[i]="pw"
  else:
    if T[i]=="r" and T[i-K]!="rw":
      ans+=P
      T[i]="rw"
    elif T[i]=="s" and T[i-K]!="sw":
      ans+=R
      T[i]="sw"
    elif T[i]=="p" and T[i-K]!="pw":
      ans+=S
      T[i]="pw"
      
print(ans)

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

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

for i in range(k,n):
    if t[i]=='r' and te[i-k]!='p':
        ans+=p
        te.append('p')
    elif t[i]=='p' and te[i-k]!='s':
        ans+=s
        te.append('s')
    elif t[i]=='s' and te[i-k]!='r':
        ans+=r
        te.append('r')
    else:
        te.append('*')
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
q=str(input())
mukou=[0]*n
ans=0

for i in range(n):
  if i>=k:
    if q[i]==q[i-k] and mukou[i-k]==0:
      mukou[i]=1

for i in range(n):
  if mukou[i]==0:
    if q[i]=="r": ans+=p
    elif q[i]=="s": ans+=r
    else:
      ans+=s
      
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
d = {"r":p, "s":r, "p":s}
ans = 0
for i in range(k):
    pa = ""
    while i < n:
        if t[i] == pa:
            pa = ""
        else:
            ans += d[t[i]]
            pa = t[i]
        i += k
print(ans)

Solution (Python):

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

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

ans = 0
for r in range(K):
    i = r
    p = '' 
    while i<N:
        if T[i] == p:
            p = ''
        else:
            ans += score[T[i]]
            p = T[i]
        i += K

print(ans)

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

t_k = [[] for i in range(K)]

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

hand = ['r', 's', 'p']
ans = 0
for i_k in t_k:
    n = len(i_k)
    dp = [[0,0,0] for i in range(n+1)]
    cnt = 1
    for s in i_k:
        a = hand.index(s) 
        if a==0:
            point = [0, 0, P]
        elif a==1:
            point = [R, 0, 0]
        else:
            point = [0, S, 0]
        dp[cnt][0] = max(dp[cnt-1][1]+point[0], dp[cnt-1][2]+point[0])
        dp[cnt][1] = max(dp[cnt-1][0]+point[1], dp[cnt-1][2]+point[1])
        dp[cnt][2] = max(dp[cnt-1][0]+point[2], dp[cnt-1][1]+point[2])
        cnt += 1
    ans += max(dp[n])


print(ans)

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

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

ans=0
result=[]

for i in range(k):
    ans+=point[t[i]]
    result.append(t[i])

for i in range(k,n):
    if t[i]==result[i-k]:
        result.append("n")
    else:
        ans+=point[t[i]]
        result.append(t[i])


print(ans)

Solution (Python):
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(n):
    if i<k:
        ans += score[t[i]]
    else :
        if t[i] != t[i-k] :
            ans += score[t[i]]
        else :
            t[i]="x"
print(ans)

Solution (Python):
p=0
N,K=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
L=list()
L_append = L.append
for i in range(N):
  if i<K:
    if s[i]=="r":
      p+=c
      L_append(i)
    elif s[i]=="s":
      p+=a
      L_append(i)
    else:
      p+=b
      L_append(i)
  else:
    if s[i]=="r" and s[i-K]!="r":
      p+=c
      L_append(i)
    elif s[i]=="s" and s[i-K]!="s":
      p+=a
      L_append(i)
    elif s[i]=="p" and s[i-K]!="p":
      p+=b
      L_append(i)
    if s[i]=="r" and s[i-K]=="r" and i-K not in L:
      p+=c
      L_append(i)
    elif s[i]=="s" and s[i-K]=="s" and i-K not in L:
      p+=a
      L_append(i)
    elif s[i]=="p" and s[i-K]=="p" and i-K not in L:
      p+=b
      L_append(i)
print(p)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
for i in range(K):
    t = T[i::K]
    dp = [[0] * 3 for _ in range(len(t) + 1)]
    for i in range(len(t)):
        if t[i] == "r":
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][1] = max(dp[i][0] + P, dp[i][2] + P)
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
        elif t[i] == "p":
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][2] = max(dp[i][0] + S, dp[i][1] + S)
        else:
            dp[i + 1][0] = max(dp[i][1] + R, dp[i][2] + R)
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
    ans += max(dp[-1])
print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline

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

P = {"r":P,"s":R,"p":S}
ans = 0
for i in range(len(T)):
  if i < K:
    ans += P[T[i]]
    continue
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += P[T[i]]
print(ans)

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


a=n%k
b=n//k

ans=[]

flg=0
for i in range(k):
  if i<=a-1:
    flg=0
    for j in range(b):
      if flg==1:
        flg=0
      else:
        if t[i+k*j]==t[i+k*j+k]:
          flg=1
          ans.append(t[i+k*j])
        else:
          ans.append(t[i+k*j])
    if flg==0:
      ans.append(t[i+b*k])
  else:
    flg=0
    for j in range(b-1):
      if flg==1:
        flg=0
      else:
        if t[i+k*j]==t[i+k*j+k]:
          flg=1
          ans.append(t[i+k*j])
        else:
          ans.append(t[i+k*j])
    if flg==0:
      ans.append(t[i+(b-1)*k])

      
from collections import Counter
zisyo=Counter(ans)
print(zisyo["r"]*p+zisyo["s"]*r+zisyo["p"]*s)

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

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

for k in range(K):
    K_list[k] = K_list[k].replace("rr", "r")
    K_list[k] = K_list[k].replace("ss", "s")
    K_list[k] = K_list[k].replace("pp", "p")

ans = 0
for k_list in K_list:
    for s in k_list:
        ans += hands[s]

print(ans)

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

pr = input()
pool = []

def iWannaRemotework():
    for i, x in enumerate(pr):
        if i >= k:
            no = pool[i-k]
        else:
            no = ""

        if x == no:
            pool.append("")
            yield 0
        elif x == "r":
            pool.append("r")
            yield p
        elif x == "s":
            pool.append("s")
            yield r
        else:
            pool.append("p")
            yield s

print(sum(iWannaRemotework()))

Solution (Python):

import sys


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    
    
    
    
    
    
    
    
    

    m = {'r': 'p', 's': 'r', 'p': 's'}
    nt = list(''.join([m[i] for i in T]))
    pm = {'r': R, 's': S, 'p': P}
    flgs = [0] * N
    ans = 0
    for i in range(N):
        if flgs[i]:
            continue
        piece = nt[i::K]
        for j, jv in enumerate(piece):
            if j - 1 >= 0 and jv == piece[j - 1]:
                jv = piece[j] = m[jv]
                if j + 1 < len(piece) and jv == piece[j + 1]:
                    jv = piece[j] = m[jv]
            else:
                ans += pm[jv]
            idx = i + (j * K)
            nt[idx] = jv
            flgs[idx] = 1
    print(ans)
    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):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()

d = {"r": p, "s": r, "p": s}  
ans = 0
wins = [False] * k  

for i in range(n):
    
    
    if not wins[i % k] or t[i] != t[i-k]:
        ans += d[t[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False

print(ans)

Solution (Python):
from itertools import groupby

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

point = 0

for i in range(k):
    li = ["x"]+t[i::k]
    for j in range(1,len(li)):
        if li[j-1] == li[j]:
            li[j] = "x"
            continue
        if li[j] == "r":
            point += p
        elif li[j] == "s":
            point += r
        else:
            point += s
    
print(point)

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

for i in range(n):
  if i-k>=0:
    if t[i]==t[i-k]:
      if t[i]=="r":
        if S[i-k]=="p":
          S+="r"
        elif S[i-k]=="r":
          S+="p"
          ans+=p
      elif t[i]=="s":
        if S[i-k]=="r":
          S+="s"
        elif S[i-k]=="s":
          S+="r"
          ans+=r
      elif t[i]=="p":
        if S[i-k]=="s":
          S+="p"
        elif S[i-k]=="p":
          S+="s"
          ans+=s
      continue
  if t[i]=="r":
    ans+=p
    S+="p"
  elif t[i]=="s":
    ans+=r
    S+="r"
  elif t[i]=="p":
    ans+=s
    S+="s"
print(ans)

Solution (Python):
n, k = map(int, input().split())
score = list(map(int, input().split()))
score = {"r": score[2], "s": score[0], "p": score[1]}


t = input()
l = len(t)
ans = 0
for i in range(k):
    cnt = 0
    pre = ""
    while True:
        if k * cnt + i >= l:
            break
        if t[k * cnt + i] != pre:
            ans += score[t[k * cnt + i]]
            
            pre = t[k * cnt + i]
        else:
            pre = ""
        cnt += 1
print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
l = input()
ans =0
for i in range(k):
    
    if l[i] =="r":
        ans += p
    elif l[i] =="s":
        ans += r
    elif l[i] =="p":
        ans += s
        
        
daida=[]
for o in range(len(l)):
    daida.append(l[o])
for j in range(k,n):
    if l[j]== l[j-k]:
        jj=["r","s","p"]
        jj.remove(l[j])
        if l[j] == daida[j-k]:
            daida[j]=jj[0]
            continue
        else:
            if l[j] =="r":
                ans += p
            elif l[j] =="s":
                ans += r
            elif l[j] =="p":
                ans += s
            continue
            
    else:
        if l[j] =="r":
            ans += p
        elif l[j] =="s":
            ans += r
        elif l[j] =="p":
            ans += s
print(ans)

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

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

    ret = 0
    for r in range(K):
        i = r
        p = ''
        while i < N:
            if T[i] == p:
                p = ''
            else:
                ret += score[T[i]]
                p = T[i]
            i += K
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,k=map(int, input().split())
    r,s,p=map(int, input().split())
    t=input()
    def kachip(te):
        if te=='r':
            return p
        elif te=='s':
            return r
        else:
            return s
    def kachite(te):
        if te == 'r':
            return 'p'
        elif te == 's':
            return 'r'
        else:
            return 's'

    check=[0]*k 
    ans=0
    for i in range(n):
        point=kachip(t[i])
        te=kachite(t[i])
        if check[i%k]==te:
            check[i%k]=0
        else:
            ans+=point
            check[i%k]=te
    print(ans)
resolve()

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
D = {"r":P,"s":R,"p":S}
T = input().strip()
hist = [0 for _ in range(N)]
ans = 0
for i in range(N):
    a = T[i]
    if hist[i]==0:
        hist[i] = 1
        cnt = 1
        j = i
        while j+K<N and T[j+K]==a:
            hist[j+K]=1
            cnt += 1
            j += K
        ans += D[a]*(cnt-cnt//2)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

    print(res)


if __name__ == '__main__':
    resolve()

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

L = [[] for _ in range(K)]
for i in range(N):
  temp = T[i]
  MOD = i%K
  L[MOD].append(temp)
ans = 0
for i in range(len(L)):
  for j in range(len(L[i])):
    if j == 0:
      Flag = True 
    elif not Flag: 
      Flag = True
    elif Flag and L[i][j] == L[i][j-1]:
      Flag = False
    if L[i][j] == "r" and Flag: 
      ans += P
    elif L[i][j] == "s" and Flag: 
      ans += R
    elif L[i][j] == "p" and Flag: 
        ans += S
print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = list(input())
    score = {"r": p, "s": r, "p": s}
    answer = 0
    for i in range(n):
        if i < k:
            answer += score[t[i]]
            if i + k < n and t[i] == t[i + k]:
                t[i + k] = t[i + k] + "*"
        elif i + k < n:
            if "*" not in t[i]:
                answer += score[t[i][0]]
                if t[i][0] == t[i + k][0]:
                    t[i + k] = t[i + k] + "*"
        else:
            if "*" not in t[i]:
                answer += score[t[i][0]]
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

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

dic = defaultdict(list)
for i in range(N):
    dic[i%K].append(T[i])
ans = 0
D = []
for t_s in dic.values():
    before = 'z'
    for t in t_s:
        if t == 's':
            if not before == 's':
                ans += R
                before = t
            else:
                before = 'z'
                
        elif t == 'r':
            if not before == 'r':
                ans += P
                before = t
            else:
                before = 'z'
        else:
            if not before == 'p':
                ans += S
                before = t
            else:
                before = 'z'

print(ans)

Solution (Python):
N,K=map(int, input().split())
R,S,P=map(int, input().split())
T=list(input())
D={"r":P, "s":R, "p":S}
result=["none"]*N
ans=0
for i in range(N):
    if i<K:
        ans+=D[T[i]]
        result[i]=T[i]
    else:
        if T[i]!=result[i-K]:
            ans+=D[T[i]]
            result[i]=T[i]
print(ans)

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

ans = 0
hand = [""] * n

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

Solution (Python):
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
    elif t[i]=="p":
      ans+=s
  else:
    if t[i]=="r" and t[i-k]!=t[i]:
      ans+=p
    elif t[i]=="s" and t[i-k]!=t[i]:
      ans+=r
    elif t[i]=="p" and t[i-k]!=t[i]:
      ans+=s
    else:
      t[i]="X"
    
print(ans)

Solution (Python):
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
   
  elif T[i] == 'p':
    ans += S
    

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

Solution (Python):

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
point = {"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 or d[t[i]]!=janken[-k]:
    score += point[t[i]]
    janken.append(d[t[i]])
  else:
    janken.append("")
print(score)

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

dp = [[0]*3 for _ in range(N)]
for i in range(K):
    for j in range(i,N,K):
        c = T[j]
        if j < K:
            if c=='r':
                dp[j][0] = P
            elif c=='s':
                dp[j][1] = R
            else:
                dp[j][2] = S
        else:
            if c=='r':
                dp[j][0] = max(dp[j-K][1], dp[j-K][2]) + P
                dp[j][1] = max(dp[j-K][0], dp[j-K][2])
                dp[j][2] = max(dp[j-K][0], dp[j-K][1])
            elif c=='s':
                dp[j][0] = max(dp[j-K][1], dp[j-K][2])
                dp[j][1] = max(dp[j-K][0], dp[j-K][2]) + R
                dp[j][2] = max(dp[j-K][0], dp[j-K][1])
            else:
                dp[j][0] = max(dp[j-K][1], dp[j-K][2])
                dp[j][1] = max(dp[j-K][0], dp[j-K][2])
                dp[j][2] = max(dp[j-K][0], dp[j-K][1]) + S
ans = 0
for i in range(N-1,N-1-K,-1):
    ans += max(dp[i])
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,copy
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
R,S,P = inpl()
t = list(input())
di = {'r':P, 's':R, 'p':S}
res = 0
ch = True
for i in range(k):
    for j in range(i,n,k):
        if j > i and t[j] == t[j-k] and ch:
            ch = False
            continue
        res += di[t[j]]
        ch = True
        
        
print(res)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
score = {'r': p, 's': r, 'p': s}
for i in range(k):
    j = i
    while j<n:
        if j>=k and t[j]==t[j-k]:
            if j+k<n:
                ans += score[t[j+k]]
            j += 2*k
        else:
            ans += score[t[j]]
            j += k
print(ans)

Solution (Python):
n, k = map(int, input().split())
d = list(map(int, input().split()))
t = input()
ans = 0
lis = [-1] * n
for i in range(n):
    if t[i] == 'r':
        ans += d[2]
        val = 2
    if t[i] == 's':
        ans += d[0]
        val = 0
    if t[i] == 'p':
        ans += d[1]
        val = 1
    lis[i] = val
    if i >= k:
        if lis[i-k] == val:
            ans -= d[val]
            lis[i] = -1
print(ans)

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

hand = {"r":p,"s":r,"p":s}
ans = 0
for i in range(n):
    if a[i-k] == a[i] and i-k >= 0:
        a[i] = "0"
    else:
        ans += hand[a[i]]
print(ans)

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

hand = {"r":p,"s":r,"p":s}
ans = 0
for i in range(n):
    if a[i-k] == a[i] and i-k >= 0:
        a[i] = "0"
    else:
        ans += hand[a[i]]
print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    ts = list(input())

    hands = {"r": p, "s": r, "p": s}
    ans = 0
    skip = [0]*n
    for i in range(n):
        if skip[i] == 1:
            continue

        if i+k > n-1:
            ans += hands[ts[i]]

        else:
            if ts[i] != ts[i+k]:
                ans += hands[ts[i]]
            else:
                ans += hands[ts[i]]
                skip[i+k] = 1

    print(ans)


if __name__ == "__main__":
    main()

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

hand={"r":p,"s":r,"p":s}
ans=0
for i in range(n):
    if a[i-k]==a[i] and i-k>=0:
        a[i]="0"
    else:
        ans+=hand[a[i]]
print(ans)

Solution (Python):
n,k=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
l=['']*k
for i in range(n):
  l[i%k]+=s[i]
ans=0
d={'s':a,'p':b,'r':c}
for i in range(k):
  x=l[i]
  now=x[0]
  memo=0
  for j in range(len(x)):
    if x[j]==now:
      memo+=1
    else:
      ans+=(max(1,(memo+1)//2))*d[now]
      now=x[j]
      memo=1
  ans+=(max(1,(memo+1)//2))*d[now]
print(ans)

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

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

lines = [""] * K
for ix in range(len(T)):
    lines[ix%K] += T[ix]

ans = 0
for l in lines:
    before = ""
    for c in l:
        if before == c:
            before = ""

        else:
            ans += mp_score[c]
            before = c

print(ans)

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=[int(x) for x in input().rstrip().split()]
r,s,p=[int(x) for x in input().rstrip().split()]
t=input()
dp=["" for x in range(n)]
ans=0
for i in range(k):
  if t[i]=="r":
    ans+=p
    dp[i]="p"
  elif t[i]=="p":
    ans+=s
    dp[i]="s"
  else:
    ans+=r
    dp[i]="r"


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

Solution (Python):
def main():
    ans = 0
    mt = ""
    for i in range(N):
        if i - K >= 0:
            if mt[i-K] == win[T[i]][0]:
                if i+K < N and win[T[i+K]][0] == T[i]:
                    mt += lose[T[i]]
                else:
                    mt += T[i]
            else:
                mt += win[T[i]][0]
                ans += win[T[i]][1]
        else:
            mt += win[T[i]][0]
            ans += win[T[i]][1]
    print(ans)


if __name__ == "__main__":
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    win = {"r": ("p", P), "s": ("r", R), "p": ("s", S)}
    lose = {"r": "s", "s": "r", "p": "r"}
    T = input()
    main()

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

ans=0
prev=['']*k
for i in range(n):
    if t[i]!=prev[i%k]:
        prev[i%k]=t[i]
        if t[i]=='r':
            ans+=p
        elif t[i]=='p':
            ans+=s
        elif t[i]=='s':
            ans+=r
    else:
        prev[i%k]=''
print(ans)