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 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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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
        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)

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
    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)

Solution (Python):
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)

Solution (Python):
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


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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)

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

for i in range(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'))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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

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)

Solution (Python):
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)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
li = [[] for _ in range(K)]
for k in range(N):
  if T[k] == 'r':
    li[k%K].append([P, 'r'])
  elif T[k] == 's':
    li[k%K].append([R, 's'])
  else:
    li[k%K].append([S, 'p'])
ans = 0
for k in range(K):
  j = 0
  while j < len(li[k])-1:
    if li[k][j][1] == li[k][j+1][1]:
      count = 0
      while(li[k][j+count][1] == li[k][j+count+1][1]):
        count += 1
        if j + count > len(li[k])-2:
          break
      dp = [0, 0]
      for l in range(count+1):
        dp[l%2] += li[k][j+l][0]
        dp[l%2] = max(dp)
      ans += max(dp)
      j += count
    else:
      ans += li[k][j][0]
    j += 1
  if j == len(li[k]) - 1:
    ans += li[k][-1][0]
print(ans)

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

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):



    dpir=0
    dpis=0
    dpip=0
    for i2 in range(i,n,k):
        if t[i2]=="r":



            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif t[i2]=="s":



            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif t[i2]=="p":



            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2

    vsum+=max(dpir,dpis,dpip)
print(vsum)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
L=input()
T=[]
for i in range(N):
    T.append(L[i])
for i in range(K,N):
    if T[i]==T[i-K]:
        T[i]="0"
p=0
for i in T:
    if i=="r":
        p+=P
    elif i=="s":
        p+=R
    elif i=="p":
        p+=S
print(p)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
L=input()
T=[]
for i in range(N):
    T.append(L[i])
for i in range(K,N):
    if T[i]==T[i-K]:
        T[i]="0"
T=str(T)
p=0
for i in T:
    if i=="r":
        p+=P
    elif i=="s":
        p+=R
    elif i=="p":
        p+=S
print(p)

Solution (Python):

from collections import deque
from operator import itemgetter
from bisect import bisect_left, bisect
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)
input = sys.stdin.readline


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

    myhand = [None] * n
    point = 0

    for i in range(n):
        if t[i] == "r":
            if myhand[i] != "np":
                point += p
                if i+k < n:
                    myhand[i+k] = "np"
            else:
                if i+k < n:
                    myhand[i + k] = None
        elif t[i] == "s":
            if myhand[i] != "nr":
                point += r
                if i+k < n:
                    myhand[i+k] = "nr"
            else:
                if i+k < n:
                    myhand[i + k] = None
        elif t[i] == "p":
            if myhand[i] != "ns":
                point += s
                if i+k < n:
                    myhand[i+k] = "ns"
            else:
                if i+k < n:
                    myhand[i + k] = None

    print(point)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
A = []
count = 0

for i in range (0, N):
	if T[i] == 'r':
		A.append('p')
	elif T[i] == 's':
		A.append('r')
	else:
		A.append('s')

for i in range (K, N):
	if A[i] == A[i-K]:
		A[i] = 'd'

for i in range (0, N):
	if A[i] == 'r':
		count+=R
	if A[i] == 's':
		count+=S
	if A[i] == 'p':
		count+=P
        
print(count)

Solution (Python):
def f(x):
    if x=="r":
        return 2
    elif x=="s":
        return 0
    else:
        return 1

N,K=map(int,input().split())
L=list(map(int,input().split()))
T=["*"]+list(map(f,input()))


Z=[[0,0,0] for _ in range(N+1)]

for x in range(1,N+1):
    for a in range(3):
        if x<=K:
            Z[x][a]=L[a]*(T[x]==a)
        else:
            b=(a+1)%3
            c=(b+1)%3

            Z[x][a]=max(Z[x-K][b],Z[x-K][c])+L[a]*(T[x]==a)

X=0
for i in range(1,K+1):
    X+=max(Z[-i])
print(X)

Solution (Python):
n, k = map(int,input().split())
r,s,p = map(int, input().split())
t = list(input())
a = [1]*n
for i in range(k,n):
    if t[i] == t[i-k] and a[i-k] == 1:
        a[i] = 0
ans = 0
for j in range(n):
    if t[j] == 'r' and a[j] == 1:
        ans += p
    elif t[j] == 's' and a[j] == 1:
        ans += r
    elif t[j] == 'p' and a[j] == 1:
        ans += s
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
memo = [0 for i in range(N)]
for i in range(N):
    check = T[i]
    if i >= K and T[i-K] == check and memo[i-K] == 0:
        memo[i] = 1
        continue
    if check == "r":
        ans += P
    elif check == "p":
        ans += S
    elif check == "s":
        ans += R
print(ans)

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

scores = {'r': P, 's': R, 'p': S, 'default': 0}
win_flag = [False]*(N+1)
ans = 0


for i_k in range(K):
    prev = 'default'
    for i_n in range(i_k, N, K):
        
        
        if (T[i_n] != prev) or (not win_flag[i_n-K]):
            win_flag[i_n] = True
            ans += scores[T[i_n]]
        prev = T[i_n]

print(ans)

Solution (Python):
import os
import sys
import numpy as np

def solve(N, K, S, P, R, T):
    dp = np.zeros((N+1, 3), dtype=np.int64)
    for i in range(1, N+1):
        c = T[i]
        dp[i, 0] = max(dp[i-K, 1], dp[i-K, 2]) + (c == "r") * R
        dp[i, 1] = max(dp[i-K, 0], dp[i-K, 2]) + (c == "s") * S
        dp[i, 2] = max(dp[i-K, 0], dp[i-K, 1]) + (c == "p") * P
    
    d = np.zeros(N+1, dtype=np.int64)
    for i in range(N+1):
        d[i] = dp[i].max()
    ans = d[-K:].sum()
    return ans



numba_config = [
    [solve, "i8(i8,i8,i8,i8,i8,string)"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba import njit
    from numba.pycc import CC
    cc = CC("my_module")
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature)(func)
        cc.export(func.__name__, signature)(func)
    cc.compile()
    exit()
elif os.name == "posix":
    exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
    from numba import njit
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature, cache=True)(func)
    print("compiled!", file=sys.stderr)



def main():
    N, K = map(int, input().split())
    S, P, R = map(int, input().split())
    T = "_" + input()
    ans = solve(N, K, S, P, R, T)
    print(ans)

main()

Solution (Python):
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

n,k = MI()
R,S,P = MI()
T = input()
li = [[] for _ in range(k)]
for i in range(len(T)):
    j = i%k
    li[j].append(T[i])
score = 0
for i in range(k):
    prev = "-1"
    for j in range(len(li[i])):
        if li[i][j] == "r":
            if prev == "p":
                prev = "-1"
            else:
                score += P
                prev = "p"
        if li[i][j] == "s":
            if prev == "r":
                prev = "-1"
            else:
                score += R
                prev = "r"
        if li[i][j] == "p":
            if prev == "s":
                prev = "-1"
            else:
                score += S
                prev = "s"
print(score)

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
l = [1] * k + [0] * (n - k)

for i in range(k):
    ans += d[t[i]]

for i in range(k, n):
    if t[i] == t[i - k] and l[i - k]: continue
    ans += d[t[i]]
    l[i] = 1

print(ans)

Solution (Python):
N, K = [int(i) for i in input().split()]
R, S, P = [int(i) for i in input().split()]
d = {'r': P, 's': R, 'p': S}
T = input()
checked = [False for i in range(N)]

for i in range(N-K):
    if T[i] == T[i+K]:
        if checked[i] == False:
            checked[i+K] = True
result = 0
for i in range(N):
    if checked[i] == False:
        result += d[T[i]]
print(result)

Solution (Python):
N, K = [int(i) for i in input().split()]
R,S,P = [int(i) for i in input().split()]
T = input()

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

def score(card):
    if card == "r":
        return R
    elif card == "s":
        return S
    elif card == "p":
        return P

mycard = ""
score_sum = 0
for i in range(N):
    if i-K<0:
        mycard = mycard+wincard(T[i])
        score_sum += score(wincard(T[i]))
    else:
        if wincard(T[i]) != mycard[i-K]:
            mycard = mycard+wincard(T[i])
            score_sum += score(wincard(T[i]))
        else:
            mycard = mycard+"w"

print(score_sum)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for i in range(n):
    if((i - k) < 0):
        if(t[i] == 'r'):ans += p
        elif(t[i] == 's'):ans += r
        else:ans += s
        continue
    
    else:
        if(t[i-k] == t[i]):t = t[:i] + 'n' + t[i+1:]
        else:
            if(t[i] == 'r'):ans += p
            elif(t[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()
ans = 0
for i in range(n):
    if ((i - k) < 0):
        if (t[i] == 'r'):
            ans += p
        elif (t[i] == 's'):
            ans += r
        else:
            ans += s
        continue

    else:
        if (t[i - k] == t[i]):
            t = t[:i] + 'n' + t[i + 1:]
        else:
            if (t[i] == 'r'):
                ans += p
            elif (t[i] == 's'):
                ans += r
            else:
                ans += s
print(ans)

Solution (Python):
def janken(aite):
    tokuten = 0
    kachi = 1
    if aite[0]=='r':
        tokuten += pa_p
    elif aite[0]=='s':
        tokuten += gu_r
    else:
        tokuten += choki_s
    
    for i in range(1,len(aite)):
        if aite[i-1]!=aite[i] or kachi==0:
            kachi = 1
            if aite[i]=='r':
                tokuten += pa_p
            elif aite[i]=='s':
                tokuten += gu_r
            else:
                tokuten += choki_s
            
        else:
            kachi=0
    return tokuten

n,k = map(int,input().split())
gu_r,choki_s,pa_p = map(int,input().split())
t = str(input())
kaisu = {}
memo = [str('') for i in range(k)]
for i in range(n):
    kaisu.setdefault(t[i],0)
    kaisu[t[i]] += 1
    
    memo[i%k] += t[i]
ans = 0
for i in range(len(memo)):
    ans += janken(memo[i])

print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
point = [0]*K
flag = ["a"]*K
k = -1

for i in range(N):
  k = (k+1)%K  
  if T[i] == "r":
    if flag[k] == "r":
      flag[k] = "a"
    else:
      flag[k] = "r"
      point[k] += P
  elif T[i] == "s":
    if flag[k] == "s":
      flag[k] = "a"
    else:
      flag[k] = "s"
      point[k] += R
  if T[i] == "p":
    if flag[k] == "p":
      flag[k] = "a"
    else:
      flag[k] = "p"
      point[k] += S
print(sum(point))

Solution (Python):



import sys
sys.setrecursionlimit(10**8)



def solve(n,k,r,s,p,t):
  winHand = {"r":"p", "s":"b", "p":"s"}
  bestHandMemo = [""]*(n+1) 
  
  memo = [-1]*(n+1)
  def calcPoint(char):
    if char == "r":
      return p
    elif char == "s":
      return r
    elif char == "p":
      return s
    else:
      raise ValueError
  def dp(i):
    if i == 0:
      return 0
    elif memo[i] != -1:
      return memo[i]
    elif i <= k:
      bestHandMemo[i-1] = winHand[t[i-1]] 
      return dp(i-1) + calcPoint(t[i-1])
    elif winHand[t[i-1]] == bestHandMemo[i-k-1]:
      bestHandMemo[i-1] = "" 
      return dp(i-1)
    else: 
      bestHandMemo[i-1] = winHand[t[i-1]]
      return dp(i-1) + calcPoint(t[i-1])
  
  for i in range(n+1):
    memo[i] = dp(i)

  return memo[n]
    



def main():
  N,K = map(int,input().split())
  R,S,P = map(int,input().split())
  T = input()
  print(solve(N,K,R,S,P,T))
  return

if __name__ == '__main__':
  main()

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
POINTS = (R, P, S)
T = [("r", "p", "s").index(c) for c in input()]
WIN = [(t + 1) % 3 for t in T]

ans = 0
for i in range(K):
    p = [0] * 3
    p[WIN[i]] = POINTS[WIN[i]]
    j = i + K
    while j < N:
        n = [-1] * 3
        for px, pp in enumerate(p):
            if pp < 0:
                continue
            for nx in range(3):
                if nx == px:
                    continue
                np = pp
                if nx == WIN[j]:
                    np += POINTS[WIN[j]]
                n[nx] = max(n[nx], np)
        p = n
        j += K
    ans += max(p)
print(ans)

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

ans=0
for i in range(n):
  x=a[i]
  if i-k>=0:
    if x==a[i-k]:
      if ans1[i-k]==0:
        if x=='r':
          ans1.append(p)
        elif x=='p':
          ans1.append(s)
        else:
          ans1.append(r)
          
      else:
        ans1.append(0)
    else:
      if x=='r':
          ans1.append(p)
      elif x=='p':
          ans1.append(s)
      else:
          ans1.append(r)
  else:
    if x=='r':
          ans1.append(p)
     
    elif x=='p':
          ans1.append(s)
        
    else:
          ans1.append(r)

print(sum(ans1))

Solution (Python):
from itertools import groupby
N, K = map(int, input().split())
R, S, P = map(int, input().split())
D = {"r":P, "s":R, "p":S}
T = list(input())
c = 0
for i in range(K):
  gr = groupby(T[i::K])  
  for key, group in gr:
    l = len(list(group))
    d = D[key]
    c += d*((l+1)//2)
print(c)

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

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

safe = [False] * n

result = [True] * n

for i in range(n-k):
    if t[i] == t[i+k] and (not safe[i]):
        result[i] = False
        safe[i+k] = True


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

score = 0

for i in range(n):
    if result[i]:
        score += points[t[i]]

print(score)

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

ans=0
for k in range(K):
    dp=[0,0,0]
    for i in range(k,N,K):
        
        x=T[i]
        if x=='r':
            r,s,p=max(dp[1],dp[2]),max(dp[0],dp[2]),max(dp[0],dp[1])+P
        elif x=='s':
            r,s,p=max(dp[1],dp[2])+R,max(dp[0],dp[2]),max(dp[0],dp[1])
        else:
            r,s,p=max(dp[1],dp[2]),max(dp[0],dp[2])+S,max(dp[0],dp[1])
        dp=[r,s,p]
    ans+=max(dp)
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()

def addScore(hand):
    if hand == 'r':
        return P
    elif hand == 's':
        return R
    else :
        return S

score = 0
score_flag = [0 for i in range(N)]

for l in range(len(T)):
    if l < K:
        score += addScore(T[l])
        score_flag[l] = 1
    else :
        if T[l] == T[l-K] and score_flag[l-K] == 1:
            continue
        else :
            score += addScore(T[l])
            score_flag[l] = 1
            
print(score)