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):
def main():
    
    
    
    
    N,K = map(int, input().split())
    R,S,P = map(int, input().split())
    T = input()
    hands = [[] for i in range(K)]
    for i in range(N):
        if T[i]=='r': hands[i%K].append(0)
        elif T[i]=='s': hands[i%K].append(1)
        else: hands[i%K].append(2)
    
    ans = 0
    points = [R,S,P]
    for i in range(K):
        
        dp = [[0]*3 for _ in range(len(hands[i])+1)]
        for j in range(len(hands[i])):
            for k in range(3):
                
                if hands[i][j]==(k+1)%3:
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+1)%3]+points[k])
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+2)%3]+points[k])
                else:
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+1)%3])
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+2)%3])
        ans += max(dp[-1])
    print(ans)

main()

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

ans = 0
for k in range(K):
    t = T[k::K]
    b = 'a'
    for i in range(len(t)):
        if t[i] == b:
            b = 'a'
        else:
            if t[i] == 'r':
                ans += P
            elif t[i] == 's':
                ans += R
            else:
                ans += S
            b = t[i]
print(ans)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
t=""
count=0
for i in range(n):
    if i>k-1:
        if T[i]=="r":
            if t[i-k]!="p":
                t=t+"p"
                count+=P
            else:
                t=t+" "
        if T[i]=="s":
            if t[i-k]!="r":
                t=t+"r"
                count+=R
            else:
                t=t+" "
        if T[i]=="p":
            if t[i-k]!="s":
                t=t+"s"
                count+=S
            else:
                t=t+" "
    else:
        if T[i]=="r":
            t=t+"p"
            count+=P
        if T[i]=="p":
            t=t+"s"
            count+=S
        if T[i]=="s":
            t=t+"r"
            count+=R
print(count)

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

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

t = input()
l = [[] for i in range(k)]

ans = 0

for i in range(n):
    mod = i%k
    l[mod].append(t[i])
        
for i in range(k):
    h = ""
    for j in l[i]:
        if j == "r" and h != "p":
            ans += p
            h = "p"
        elif j == "s" and h != "r":
            ans += r
            h = "r"
        elif j == "p" and h != "s":
            ans += s
            h = "s"
        else:
            h = ""
print(ans)

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

import numpy as np





ans = 0
L = [[] for _ in range(K)]
for i in range(K):
    L[i] = T[i:N:K]
for l in L:


    lenl = len(l)
    Dp = np.zeros([lenl+1, 3], dtype=int)
    Dp[:][:] = -1
    Dp[0][:] = 0

    ll = l[0]
    if ll == 'r':
        Dp[1][0] = 0
        Dp[1][1] = 0
        Dp[1][2] = P
    elif ll == 's':
        Dp[1][0] = R
        Dp[1][1] = 0
        Dp[1][2] = 0
    elif ll == 'p':
        Dp[1][0] = 0
        Dp[1][1] = S
        Dp[1][2] = 0

    for i in range(1, lenl):
        ll = l[i]
        if ll == 'r':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2])
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0])
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1]) + P
        elif ll == 's':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2]) + R
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0])
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1])
        elif ll == 'p':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2])
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0]) + S
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1])

    ans += max(Dp[-1][:])
print(ans)

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

use = [1]*n
for i in range(n-k):
    if not use[i]:
        continue
    if t[i] == t[i+k]:
        use[i+k] = 0

ans = 0
for i in range(n):
    if use[i]:
        x = t[i]
        if x == 'r':
            ans += p
        elif x == '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()

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

rsp_dic2 = {0:P,1:R,2:S}

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

rsp_lose = {"r":2,"s":0,"p":1}

anslist = [0] * K

modcount = [N//K]*K
for i in range(1,N%K+1):
    modcount[i] += 1

for k in range(K):
    count = modcount[k]
    dp = [[0,0,0] for _ in range(count+1)]
    for i in range(1, count+1):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
        if k != 0:
            dp[i][rsp_lose[T[(i-1) * K + k - 1]]] += rsp_dic[T[(i-1) * K + k - 1]]
        else:
            dp[i][rsp_lose[T[i * K + k - 1]]] += rsp_dic[T[i * K + k - 1]]

    anslist[k] = max(dp[-1])

print(sum(anslist))

Solution (Python):

"""
Created on Sun Jun 14 17:18:33 2020

@author: NEC-PCuser
"""

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

score = 0
your_hand = [-1] * N

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


for i in range(len(T)):
    if T[i] != your_hand[i - K]:
        score += score_dict[T[i]]
        your_hand[i] = T[i]

print(score)

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

for i in range(N-K):
    if T[i+K] == T[i]:
        T[i+K] = 0

print(T.count("r")*P+T.count("s")*R+T.count("p")*S)

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

conv_winhand = {'r':'p', 's':'r', 'p':'s'}
conv_score = {'r':R, 's':S, 'p':P}
poi = []

ans = 0
for i in range(N):
    if i < K:
        poi.append(conv_winhand[T[i]])
        ans += conv_score[conv_winhand[T[i]]]
    else:
        if conv_winhand[T[i]] == poi[i-K]:
            tmp = T[i]
            if i + K < N and tmp == conv_winhand[T[i+K]]: tmp = conv_winhand[poi[i-K]]
            poi.append(tmp)
        else:
            poi.append(conv_winhand[T[i]])
            ans += conv_score[conv_winhand[T[i]]]
print(ans)

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

point = 0
for i in range(n):
    if (i >= k) & (t[i] == t[i - k]):
        t[i] = 'a'
        continue
    else:
        if t[i] == 'r':
            point += p
        elif t[i] == '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()
win={"r":"p","s":"r","p":"s"}
score={"r":R,"s":S,"p":P}

ans=0
dp=[0 for i in range(N)]
for i in range(K):
    dp[i]=win[T[i]]
    ans+=score[dp[i]]
        
for i in range(K,N):
    if T[i-K]==T[i] and dp[i-K]==win[T[i]]:
        dp[i]=T[i]
    elif T[i-K]==T[i]:
        dp[i]=win[T[i]]
        ans+=score[dp[i]]
    else:
        dp[i]=win[T[i]]
        ans+=score[dp[i]]
print(ans)

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

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

win = {
    'r': 'p',
    's': 'r',
    'p': 's'
}

ans = [None] * N

sum_ = 0
for i, t in enumerate(T):
    if i < K or ans[i - K] != win[t]:
        ans[i] = win[t]
        sum_ += tensu[ans[i]]

print(sum_)

Solution (Python):
def f(c):
    """
    入力に対する勝ち手を出力
    """
    if c=="r":
        return "p"
    elif c=="s":
        return "r"
    else:
        return "s"

def main():
    """
    まずは全て勝つ組み合わせを作って、合計ポイントを求める。
    iを0からN-Kまで変化させ、T(i)=T(k+i)となる時、
    T(k+i)をT(i)、T(i+2K)と異なる手に変更する。
    ※T(i)を変更すると、T(i)=T(i+k)=T(i+2K)=...=と連続した時に、
    全て変更されてスコアが最大にならない。
    T(k+i)は勝ちから"あいこまたは負け"に変化するので、合計ポイントから差っ引く。
    """

    
    N,K=map(int,input().strip().split())
    R,S,P=map(int,input().strip().split())
    T=list(input())
    
    win_T=list(map(lambda x:f(x),T))
    point={"r":R,"s":S,"p":P}
    score=sum([point[win_T[n]] for n in range(N)])
    for i in range(N-K):
        l=["r","s","p"]
        if win_T[i]==win_T[i+K]:
            score-=point[win_T[i+K]]
            l.remove(win_T[i])
            if i<=N-2*K-1 and win_T[i]!=win_T[i+2*K]:
                l.remove(win_T[i+2*K])
            win_T[i+K]=l[0]
    return score

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

Solution (Python):
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=list(input())
cnt=0
u=[]
for i in range(n):
  if t[i]=='r':
    u.append("p")
  elif t[i]=='s':
    u.append("r")
  else:
    u.append("s")

for i in range(n):
  if u[i]=="r":
    if i<k:
      cnt+=r
    else:
      if u[i-k]=="r":
        cnt+=0
        u[i]="
      else:
        cnt+=r

  elif u[i]=="s":
    if i<k:
      cnt+=s
    else:
      if u[i-k]=="s":
        cnt+=0
        u[i]="
      else:
        cnt+=s

  elif u[i]=="p":
    if i<k:
      cnt+=p
    else:
      if u[i-k]=="p":
        cnt+=0
        u[i]="
      else:
        cnt+=p

print(cnt)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[[] for _ in range(K)]
for i in range(N):
    A[i%K].append(T[i])

count=0
for i in range(K):
  cnt=0
  for j in range(len(A[i])):
    if j==0:
      if A[i][j]=='p':
        count+=S
      if A[i][j]=='s':
        count+=R
      if A[i][j]=='r':
        count+=P
    else:
      if A[i][j]==A[i][j-1]:
        cnt+=1
      if A[i][j]!=A[i][j-1]:
        cnt=0
      if cnt%2==0 and A[i][j]=='p':
          count+=S
      if cnt%2==0 and A[i][j]=='r': 
          count+=P
      if cnt%2==0 and A[i][j]=='s':
          count+=R
print(count)

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n,k = readInt()
    r,s,p = readInt()
    rsp = {"s":r,"p":s,"r":p}
    t = input()
    ans = 0
    u = []
    for i in range(n):
        if i<k:
            ans += rsp[t[i]]
            u.append(t[i])
        else:
            if t[i]!=u[i-k]:
                ans += rsp[t[i]]
                u.append(t[i])
            else:
                u.append("f")
    print(ans)
    return

if __name__=='__main__':
    main()

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

for i in range(len(t)):
    if t[i] == 'r':
        if i - k >= 0 and hand[i - k] == 'p':
            hand.append(' ')
            continue
        else:
            ans += p
            hand.append('p')
    elif t[i] == 's':
        if i - k >= 0 and hand[i - k] == 'r':
            hand.append(' ')
            continue
        else:
            ans += r
            hand.append('r')
    else:
        if i - k >= 0 and hand[i - k] == 's':
            hand.append(' ')
            continue
        else:
            hand.append('s')
            ans += s

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(len(t)):
    tmp = t[i]
    if i >= k:
        if tmp == t[i-k]:
            t[i] = "c"
            continue
    if tmp == "r":
        ans += p
    elif tmp == "s":
        ans += r
    elif tmp == "p":
        ans += s
        
print(ans)

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

Solution (Python):
def mi():
    return map(int, input().split())

def main():
    N, K = mi()
    R, S, P = mi()
    T = input()
    pt = 0
    my_choices = ['']*N

    for i in range(K):
        tmp = T[i::K]
        for j in range(len(tmp)):
            if j == 0:
                if tmp[j] == 'r':
                    my_choices[i] = 'p'
                    pt += P
                if tmp[j] == 's':
                    my_choices[i] = 'r'
                    pt += R
                if tmp[j] == 'p':
                    my_choices[i] = 's'
                    pt += S
            else:
                if tmp[j] == 'r' and my_choices[i+(j-1)*K] != 'p':
                    my_choices[i+j*K] = 'p'
                    pt += P
                if tmp[j] == 's' and my_choices[i+(j-1)*K] != 'r':
                    my_choices[i+j*K] = 'r'
                    pt += R
                if tmp[j] == 'p' and my_choices[i+(j-1)*K] != 's':
                    my_choices[i+j*K] = 's'
                    pt += S

    print(pt)


if __name__ == '__main__':
    main()

Solution (Python):



def subtask(sseq, pt):
    n = len(sseq)

    dp = [[0]*3 for _ in range(n+1)]
    for i in range(n):
        e = sseq[i]
        w = (e+2)%3
        l = (e+1)%3

        dp[i+1][w] = max(dp[i][e]+pt[w] ,dp[i][l]+pt[w])
        dp[i+1][l] = max(dp[i][e] ,dp[i][w])
        dp[i+1][e] = max(dp[i][w] ,dp[i][l])

    return max(dp[n])

def main():
    N, K = map(int, input().split())
    pt = list(map(int, input().split()))
    Sr = [[] for _ in range(K)]
    S = input()
    S = S.translate(str.maketrans('rsp','012'))

    for i, a in enumerate(S):
        Sr[i%K].append(int(a))

    ret=0
    for sq in Sr:
        ret += subtask(sq, pt)

    print(ret)



if __name__ == '__main__':
    main()

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

for i in range(k):
    for j in range(i, n, k):
        if j + k < n:
            if t[j] == t[j + k]:
                t[j + k] = "0"

ans = t.count("r")*r + t.count("p")*p + t.count("s")*s
print(ans)

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

    ans = 0
    for i in range(K):
        p = 'z'
        for c in T[i::K]:
            if c == p:
                p = 'z'
                continue
            if c == 'r':
                ans += P
            elif c == 's':
                ans += R
            else:
                ans += S
            p = c
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split(' '))
R, S, P = map(int, input().split(' '))
T = input()
 
flags = [0] * len(T)
 
point_map = {'r': P, 's': R, 'p': S}
 
point_sum = 0
 
for i, t in enumerate(T):
    if i < K:
        point_sum += point_map[t]
        continue
    if t == T[i-K] and not flags[i-K]:
        flags[i] = 1
        continue
    point_sum += point_map[t]

print(point_sum)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
lis = ['' for _ in range(len(t))]
ans = 0

for i in range(k):
  if t[i] == 'r':
    ans += p
    lis[i] = 'p'
  elif t[i] == 'p':
    ans += s
    lis[i] = 's'
  else:
    ans += r
    lis[i] ='r'
    
for j in range(k,len(t)):
  if t[j] == 'p' and lis[j-k] != 's':
    ans+=s
    lis[j] = 's'
  elif t[j] == 'p' and lis[j-k] == 's':
    lis[j] = 'katenai'

    
  elif t[j] == 's' and lis[j-k] != 'r':
    ans+=r
    lis[j] = 'r'

  elif t[j] == 's' and lis[j-k] == 'r':
    lis[j] = 'katenai'

    
  elif t[j] == 'r' and lis[j-k] != 'p':
    ans+=p
    lis[j] = 'p'

  elif t[j] == 'r' and lis[j-k] == 'p':
    lis[j] = 'katenai'
      

print(ans)

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction


def main():
    num, k = map(int, input().split())
    R, S, P = map(int, input().split())
    data = list(input())

    ans = 0
    ans_dic = {"r":P, "s":R, "p":S}

    for i in range(num):
        hand = data[i]
        if i < k:
            ans += ans_dic[hand]
        else:
            if data[i - k] == hand:
                data[i] = -1
            else:
                ans += ans_dic[hand]
    print(ans)



if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):
n,k = map(int,input().split())
R,S,P = map(int,input().split())
po = {'r':P, 's':R, 'p':S}
t = list(input())
sub = [[] for _ in range(k)]

for i in range(n):
    sub[i%k].append(t[i])

point = 0
for j in range(k):
    m = len(sub[j])
    point += po[sub[j][0]]
    for l in range(1,m):
        if sub[j][l-1] != sub[j][l]:
            point += po[sub[j][l]]
        else:
            sub[j][l] = '

print(point)

Solution (Python):






N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
rsp = ['r','s','p']


def janken(my_s,op_s):
    if my_s == 'r' and op_s=='s':
        return R
    if my_s == 's' and op_s=='p':
        return S
    if my_s == 'p' and op_s=='r':
        return P
    return 0


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

for n in range(K):
    dp[n] = [janken(rsp[i],T[n]) for i in range(3)]

for i in range(N-K):
    dp[i+K][0] = max(dp[i][1]+janken(rsp[0],T[i+K]), dp[i][2]+janken(rsp[0],T[i+K]))
    dp[i+K][1] = max(dp[i][2]+janken(rsp[1],T[i+K]), dp[i][0]+janken(rsp[1],T[i+K]))
    dp[i+K][2] = max(dp[i][0]+janken(rsp[2],T[i+K]), dp[i][1]+janken(rsp[2],T[i+K]))
ans = 0
for i in range(K):
    ans += max(dp[N-1-i])
print(ans)

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

flags = [0] * len(T)

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

point_sum = 0

for i, t in enumerate(T):
    if i < K:
        point_sum += point_map[t]
        continue
    if t == T[i-K] and not flags[i-K]:
        flags[i] = 1
        continue
    point_sum += point_map[t]

print(point_sum)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
t=input()

v=[]
ans=0
for i in t:
  if i=='r':
    v.append('p')
    ans+=P
  if i=='s':
    v.append('r')
    ans+=R
  if i=='p':
    v.append('s')
    ans+=S

for i in range(k,n):
  if v[i]==v[i-k]:
    if v[i]=='r':
      ans-=R
    if v[i]=='s':
      ans-=S
    if v[i]=='p':
      ans-=P

    v[i]='?'

print(ans)

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


def R(i):
    if T[i-1]==("s"):
        return r
    else:
        return 0
def S(i):
    if T[i-1]==("p"):
        return s
    else:
        return 0

def P(i):
    if T[i-1]==("r"):
        return p
    else:
        return 0

dp=[[0]*3 for _ in range(n+1)]
dp[0][0],dp[0][1],dp[0][2]=0,0,0
for i in range(1,k+1):
    dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+R(i)
    dp[i][1]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+S(i)
    dp[i][2]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+P(i)
    

for i in range(0,n-k):
    if T[i]!="r":
        dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    else:
        if dp[i+1][2]==0:
            dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    if T[i]!="s":
        dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)
    else:
        if dp[i+1][0]==0:
            dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)

    if T[i]!="p":
        dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)
    else:
        if dp[i+1][1]==0:
            dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)

    
print(max(dp[n]))

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

def R(i):
    if T[i-1]==("s"):
        return r
    else:
        return 0
def S(i):
    if T[i-1]==("p"):
        return s
    else:
        return 0

def P(i):
    if T[i-1]==("r"):
        return p
    else:
        return 0

dp=[[0]*3 for _ in range(n+1)]
dp[0][0],dp[0][1],dp[0][2]=0,0,0
for i in range(1,k+1):
    dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+R(i)
    dp[i][1]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+S(i)
    dp[i][2]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+P(i)
    

for i in range(0,n-k):
    if T[i]!="r":
        dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    else:
        if dp[i+1][2]==0:
            dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    if T[i]!="s":
        dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)
    else:
        if dp[i+1][0]==0:
            dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)

    if T[i]!="p":
        dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)
    else:
        if dp[i+1][1]==0:
            dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)

    
print(max(dp[n]))

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

for i in t[:k]:
    if i == "r":
        point += p
    elif i == "s":
        point += r
    else:
        point += s

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

print(point)

Solution (Python):
n,k= map(int,input().split())
r,s,p= map(int,input().split())
pdic = {'r':p,'s':r,'p':s}
jan= input()
words=['']*k
junbite=[]
junbisu=[]
for i in range(n):words[i%k]+=jan[i]
for i in range(k):
    pre=''
    count=1
    tempte=[]
    tempsu=[]
    for j,c in enumerate(words[i]):
        if j==0:pre=c
        else:
            if pre==c:count+=1
            else:
                tempte.append(pre)
                tempsu.append(count)
                count=1
                pre=c
    else:
        tempte.append(pre)
        tempsu.append(count)
    junbite.append(tempte)
    junbisu.append(tempsu)
g=0
for i in range(k):
    for te,su in zip(junbite[i],junbisu[i]):
        point = su//2
        if su%2==1:point+=1
        g += pdic[te]*point
print(g)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
F = [0]*N
ans = 0
for i in range(N):
    t = T[i]
    if i < K:
        if t == 'r':
            ans += P
            F[i] = 1
        elif t == 's':
            ans += R
            F[i] = 1
        else:
            ans += S
            F[i] = 1
    elif t == 'r':
        if T[i-K] == 'r':
            if F[i-K] == 0:
                ans += P
                F[i] = 1
        else:
            ans += P
            F[i] = 1
    elif t == 's':
        if T[i-K] == 's':
            if F[i-K] == 0:
                ans += R
                F[i] = 1
        else:
            ans += R 
            F[i] = 1
    else:
        if T[i-K] == 'p':
            if F[i-K] == 0:
                ans += S
                F[i] = 1
        else:
            ans += S 
            F[i] = 1
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
c = {"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 += c[t[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False

print(ans)

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

ans = 0
counter = [{v:0 for v in "rsp"} for i in range(k)]

for i in range(n):
    if t[i] == 'r':
        counter[i%k]['r'] += 1
        counter[i%k]['s'] = 0
        counter[i%k]['p'] = 0
        if counter[i%k]['r'] %2 == 1:
            ans += p
    if t[i] == 's':
        counter[i%k]['s'] += 1
        counter[i%k]['r'] = 0
        counter[i%k]['p'] = 0
        if counter[i%k]['s'] %2 == 1:
            ans += r
    if t[i] == 'p':
        counter[i%k]['p'] += 1
        counter[i%k]['s'] = 0
        counter[i%k]['r'] = 0
        if counter[i%k]['p'] %2 == 1:
            ans += s
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
k = []
ans = 0
for i in range(K):
  if T[i] == 's':
    ans += R
  elif T[i] == 'p':
    ans += S
  elif T[i] == 'r':
    ans += P
  k.append(T[i])
for i in range(K, N):
  if T[i] == 's':
    if k[0] != 's':
      ans += R
      k.pop(0)
      k.append('s')
    else:
      k.pop(0)
      k.append('0')
  elif T[i] == 'p':
    if k[0] != 'p':
      ans += S
      k.pop(0)
      k.append('p')
    else:
      k.pop(0)
      k.append('0')
  elif T[i] == 'r':
    if k[0] != 'r':
      ans += P
      k.pop(0)
      k.append('r')
    else:
      k.pop(0)
      k.append('0')
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 = input()
scores = {'r':p, 's':r, 'p':s}
ans = 0
for i in range(k):
    ans += scores[t[i]]
    flg = False
    for j in range(i+k, n, k):
        if (not flg) and t[j]==t[j-k]:
            flg = True
            continue
        flg = False
        ans += scores[t[j]]
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}
do = {"r":"p", "s":"r", "p":"s"}
d = []

score = 0
for i in range(n):
    if i < k:
        score += point[t[i]]
        d.append(do[t[i]])
    else:
        if d[-k] == do[t[i]]:
            d.append("")
        else:
            score += point[t[i]]
            d.append(do[t[i]])
print(score)

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):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()

dp = [[0] * 3 for i in range(n + 1)]
for i in range(n):
    if i + 1 - k < 0:
        if t[i] == "r":
            dp[i + 1][0] = p
        if t[i] == "s":
            dp[i + 1][1] = r
        if t[i] == "p":
            dp[i + 1][2] = s
    else:
        if t[i] == "r":
            dp[i + 1][0] = p + max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = max(dp[i + 1 - k][0], dp[i + 1 - k][1])
        if t[i] == "s":
            dp[i + 1][0] = max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = r + max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = max(dp[i + 1 - k][0], dp[i + 1 - k][1])
        if t[i] == "p":
            dp[i + 1][0] = max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = s + max(dp[i + 1 - k][0], dp[i + 1 - k][1])
ans = 0
for i in range(k):
    ans += max(dp[-i - 1])
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 += R if t[i] == "s" else S if t[i] == "p" else P
        else:
            if t[i - k] != t[i]:
                res += R if t[i] == "s" else S if t[i] == "p" else P
            else:
                t[i] = "0"
    print(res)


if __name__ == '__main__':
    resolve()