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):
from sys import stdin
input = stdin.readline


def main():
  N, K = list(map(int, input().split()))
  R, S, P = list(map(int, input().split()))
  T = input()[:-1]

  mem = []
  hand_map = {'r': 'p', 's': 'r', 'p': 's'}
  score_map = {'r': P, 's': R, 'p': S}

  score = 0

  for i in range(K):
    mem.append(hand_map[T[i]])
    score += score_map[T[i]]

  for i in range(K, N):
    if not mem[i-K] == hand_map[T[i]]:
      mem.append(hand_map[T[i]])
      score += score_map[T[i]]
      continue
    cand = ['r', 's', 'p']
    
    cand.remove(mem[i-K])
    if i+K < N and not(mem[i-K] == hand_map[T[i+K]]):
      cand.remove(hand_map[T[i+K]])
    mem.append(cand[0])

  print(score)


if(__name__ == '__main__'):
  main()

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
'''
rsp -> 012
'''
dp = [[0] * 3 for i in range(N)]
for i in range(0, K):
    for j in range(i, N, K):
        if j == i:
            if T[j] == 'r':
                dp[j][0] += 0
                dp[j][1] += 0
                dp[j][2] += P
            if T[j] == 's':
                dp[j][0] += R
                dp[j][1] += 0
                dp[j][2] += 0
            if T[j] == 'p':
                dp[j][0] += 0
                dp[j][1] += S
                dp[j][2] += 0
        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])
            if T[j] == 'r':
                dp[j][0] += 0
                dp[j][1] += 0
                dp[j][2] += P
            if T[j] == 's':
                dp[j][0] += R
                dp[j][1] += 0
                dp[j][2] += 0
            if T[j] == 'p':
                dp[j][0] += 0
                dp[j][1] += S
                dp[j][2] += 0


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

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

point = 0
flg=[0]*n
for i in range(n):
  if t[i] == 'r' and flg[i] == 0:
    point += p
  elif t[i] == 's' and flg[i] == 0:
    point += r
  elif t[i] == 'p' and flg[i] == 0:
    point += s

  if i+k < n and t[i+k] == t[i]:
    if t[i] == 'r' and flg[i] == 0:
      flg[i+k] = 'r'
    elif t[i] == 's' and flg[i] == 0:
      flg[i+k] = 's'
    elif t[i] =='p' and flg[i] == 0:
      flg[i+k] = 'p'
print(point)

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


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

ans = 0
for i in range(K):
    Ti = T[i : : K]
    cnt = 0
    for j in range(len(Ti)):
        if j == 0 or Ti[j] == Ti[j - 1]:
            cnt += 1
        else:
            ans += rsp(Ti[j - 1]) * ((cnt + 1) // 2)
            cnt = 1
    ans += rsp(Ti[-1]) * ((cnt + 1) // 2)

print(ans)

Solution (Python):
"""
入力例1

5 2
8 7 6
rsrpr

このとき、r1回目から3回目に対して勝敗が
oxo
となるようにするとよさそう。
"""
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
memo = []   
for i in range(N):
    if i >= K and T[i] == T[i - K] and memo[i - K]:
        memo.append(False)
        continue
    if T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
    memo.append(True)
print(ans)

Solution (Python):
n,k = map(int,input().split())
R,S,P = map(int,input().split())
points = {'r':P,'s':R,'p':S}
l = list(input())
com = ['']*n
ans = 0
for i,j in enumerate(l):
  p = points[j]
  com[i] = j
  if (i-k>=0) and com[i] == com[i-k]:
    com[i] = ''
    p = 0
  ans += p
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
rsp = [set() for i in range(3)]
ans = 0
for i in range(N):
    if T[i] == 'r' and i - K not in rsp[0]:
        ans += P
        rsp[0].add(i)
    elif T[i] == 's' and i - K not in rsp[1]:
        ans += R
        rsp[1].add(i)
    elif T[i] == 'p' and i - K not in rsp[2]:
        ans += S
        rsp[2].add(i)
print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T1=input()
T2=T1.replace('r', str(P)+' ').replace('s',str(R)+' ').replace('p',str(S)+' ')[:-1]
T2=list(map(int, T2.split()))
for i in range(N):
  if i >=K and T1[i]==T1[i-K] and T2[i-K] != 0:T2[i]=0
print(sum(T2))

Solution (Python):
def solve(T, R, S, P, K):
    N = len(T)
    Z =[None for i in range(N)]
    for i in range(N):
        if T[i] == 'r':
            Z[i] = P
        elif T[i] == 's':
            Z[i] = R
        else:
            Z[i] = S
        if i - K >= 0:
            if T[i-K] == T[i] and Z[i-K] != 0:
                Z[i] = 0
    return sum(Z)
N, K = map(int,input().split(' '))
R, S, P = map(int,input().split(' '))
T = str(input())
print(solve(T, R, S, P, K))

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd





def solve():
    n, k = [int(x) for x in input().split()]
    R, S, P = [int(x) for x in input().split()]
    s = input()
    ans = 0
    b = []
    for i in range(n):
        e = s[i]
        if i < k or s[i] != s[i-k] or s[i-k] == b[i-k]:
            if e == 'r':
                ans += P
                b.append('p')
            elif e == 'p':
                ans += S
                b.append('s')
            elif e == 's':
                ans += R
                b.append('r')
        else:
            b.append(e)
    print(ans)
    
    

        
    
t = 1

for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = readline().decode().rstrip()

t = list(t)

for i in range(k, n):
    if t[i] == t[i - k]:
        t[i] = 0

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

Solution (Python):
import numpy as np

N,K=map(int,input().split())
R,S,P=map(int,input().split())
T1=input()
T2=T1.replace('r', str(P)+' ').replace('s',str(R)+' ').replace('p',str(S)+' ')[:-1]
T2=np.array(list(map(int, T2.split())))

for i in range(N):
  if i >=K and T1[i]==T1[i-K] and T2[i-K] != 0:
    T2[i]=0

print(T2.sum())

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = readline().decode().rstrip()
t = list(t)
for i in range(k, n):
    if t[i] == t[i - k]:
        t[i] = 0

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

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
dp = []
order = input()
def jcount(x):
  if x=='r':
    ans = P
  elif x=='s':
    ans = R
  else:
    ans = S
  return ans
counter = 0
for i in range(N):
  if i < K:
    counter += jcount(order[i])
    dp.append(order[i])
  elif order[i] != dp[i-K]:
    counter += jcount(order[i])
    dp.append(order[i])
  else:
    dp.append('x')
print(counter)

Solution (Python):

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


win = []
for t in T:
    if t == 'r':
        win.append('p')
    elif t == 'p':
        win.append('s')
    elif t == 's':
        win.append('r')


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

ans = 0
for i in range(n):
    if i - k < 0:
       ans += score(win[i])
    else:
        if win[i] == win[i-k]:
            win[i] = None
        else:
            ans += score(win[i])

print(ans)

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

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

ans = [0] * n

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

print(sum(ans))

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

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

U = []
for t in T:
    if t == 'r':
        u = 'p'
    elif t == 's':
        u = 'r'
    else:
        u = 's'
    U.append(u)

ans = 0
for i in range(N):
    if i >= K and U[i] == U[i - K]:
        U[i] = ''
    else:
        if U[i] == 'r':
            ans += R
        elif U[i] == 's':
            ans += S
        elif U[i] == 'p':
            ans += P

print(ans)

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


dic = {'r':P, 's':R, 'p':S}
flag = [0]*N
rlt = 0

T = input()

for i in range(N):
  if i < K or T[i] != T[i-K] or flag[i-K] == 1:
    rlt += dic[T[i]]
  else:
    flag[i] = 1
    
print(rlt)

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

for i in range(K):
  last = "D"
  wl = "l"
  for j in range(N//K+1):
    if  j*K + i >= N:
      break
    else:
      te = T[j*K+i]
      if te != last:
        last = te
        wl = "w"
        if te == "r":
          ans += P
        elif te == "s":
          ans += R
        else:
          ans += S
      else:
        if wl == "l":
          last = te
          wl = "w"
          if te == "r":
            ans += P
          elif te == "s":
            ans += R
          else:
            ans += S
        else:
          last = te
          wl = "l"
print(ans)

Solution (Python):
import sys,math,collections,itertools
input = sys.stdin.readline

N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T = input().rstrip()
s =T.replace('r','P') 
ss=s.replace('s','R') 
sss=ss.replace('p','S') 
sl = list(sss)

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

sc = collections.Counter(sl)

print(sc['R']*R + sc['P']*P + sc['S']*S)

Solution (Python):
from collections import Counter

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    T = list(input())
    l = list()
    ans = 0
    for i in range(k):
        l.append(T[i])
    for i in range(k, n):
        if l[i-k] == T[i]:
            l.append(None)
        else:
            l.append(T[i])
    res = Counter(l)
    ans = res['r'] * p + res['s'] * r + res['p'] * s
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
 
did = "-" * k
ans = 0
for i in t:
    if i == "r":
        if did[-k] == "p":
            did += "-"
        else:
            did += "p"
            ans += p
    elif i == "s":
        if did[-k] == "r":
            did += "-"
        else:
            did += "r"
            ans += r
    else:
        if did[-k] == "s":
            did += "-"
        else:
            did += "s"
            ans += s
 
print(ans)

Solution (Python):
n, k = map(int, input().split())
card = {x : i for x, i in zip (("s", "p", "r") , map(int, input().split()))}
t = input()
ans = [0]*k

for i in range(k):
    chk = 0
    for j in range(i, n, k):
        if j < k:
            tmp, chk = card[t[j]], 1
        elif t[j] != t[j-k]:
            tmp, chk = card[t[j]], 1
        elif chk == 0 and t[j] == t[j-k]:
            tmp, chk = card[t[j]], 1
        else:
            tmp, chk = 0, 0
        ans[i] += tmp
print(sum(ans))

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

N,K=(int(x) for x in input().rstrip('\n').split())
R,S,P=(int(x) for x in input().rstrip('\n').split())
T = list(input().rstrip('\n'))
s_d = {'r':P,'s':R,'p':S}
score = 0
for i in range(K):
  now=i
  now_h=''
  while now<N:
    next= T[now]
    if next!=now_h:
      score += s_d[next]
      now_h=next
    else:
      now_h=''
    now += K
print(score)

Solution (Python):

n,k=map(int,input().split())
s,p,r=map(int,input().split())
f={'s':s,'p':p,'r':r}
t=str(input())
ans=0
for i in range(k):
    a=i+k
    last=t[i]
    ans+=f[last]
    while a<n-k:
        if t[a]==last:
            if t[a+k]==last:
                if last=='s':
                    last='r'
                else:
                    last='s'
            else:
                if last=='s':
                    if t[a+k]=='r':
                        last='p'
                    else:
                        last='r'
                elif last=='r':
                    if t[a+k]=='s':
                        last='p'
                    else:
                        last='s'
                else:
                    if t[a+k]=='r':
                        last='s'
                    else:
                        last='r'
        else:
            last=t[a]
            ans+=f[last]
        a+=k
    
    if a<n:
        if t[a]!=last:
            ans+=f[t[a]]

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}
d_={'r':'p','s':'r','p':'s'}
s=0
for i in range(k):
  a=0
  z='0'
  t_=t[i::k]
  for a in range(len(t_)):
    if a==0:
      s+=d[t_[a]]
      z=d_[t_[a]]
      a+=1
    else:
      if z==d_[t_[a]]:
        z='0'
        a+=1
      else:
        s+=d[t_[a]]
        z=d_[t_[a]]
        a+=1
print(s)

Solution (Python):
import collections
n, k = [int(w) for w in input().split()]
r, s, p = [int(w) for w in input().split()]
score = {"s": r, "p": s, "r": p}

t = input()

cnt = collections.Counter(t)

ans = 0
ans += cnt["s"] * r
ans += cnt["p"] * s
ans += cnt["r"]*p

for i in range(k):
    sp_t = t[i:n: k]
    is_prev_lose = False
    for j in range(1, len(sp_t)):
        if sp_t[j - 1] == sp_t[j]:
            if is_prev_lose:
                is_prev_lose = False
            else:
                is_prev_lose = True
                ans -= score[sp_t[j]]
        else:
            is_prev_lose = False
print(ans)

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

(n, k), (r, s, p), t = map(int, input().split()), map(int, input().split()), input()[:-1]
d, res = {'r': p, 's': r, 'p': s}, 0
for i in range(k):
    b = False
    for j in range(i, n, k):
        if i == j: res += d[t[j]]; b = False; continue
        if (t[j] != t[j-k]) | b: res += d[t[j]]; b = False
        else: b = True
print(res)

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

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
ans=0
dic={'r':'p','s':'r','p':'s'}
score={'r':R,'s':S,'p':P}
past=[]
for i in range(len(T)):
    if i>=K:
        if dic[T[i]]==past[i-K]:
            past.append('0')
            continue
        else:
            ans+=score[dic[T[i]]]
            past.append(dic[T[i]])
    else:
        ans+=score[dic[T[i]]]
        past.append(dic[T[i]])
print(ans)

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

ans = 0
for d in dp[-K:]:
    ans += max(d)
print(ans)

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

def get_point(x):
    if x == 'r':
        return P
    if x == 's':
        return R
    if x == 'p':
        return S


ans = 0
for i in range(K):
    dp = [0] * 2
    pre = ''
    for j in range(i, N, K):
        dp2 = dp[:]
        dp2[0] = max(dp)
        if pre == T[j]:
            dp2[1] = dp[0] + get_point(T[j])
        else:
            dp2[1] = max(dp) + get_point(T[j])
        pre = T[j]
        dp = dp2
    ans += max(dp)

print(ans)

Solution (Python):
n,k = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
slist = ['']*k
for i in range(n):
    slist[i%k] += T[i]

ans = 0
for s in slist:
    dp = [[0,0,0]for i in range(len(s))]
    
    '''
    0..r
    1..s
    2..p
    '''
    dp[0][1] = S if s[0] == 'p' else 0
    dp[0][0] = R if s[0] == 's' else 0
    dp[0][2] = P if s[0] == 'r' else 0

    for i in range(1,len(s)):
        if s[i] == 'r':
            dp[i][2] = max(dp[i-1][0],dp[i-1][1]) + P
            dp[i][1] = max(dp[i-1][0],dp[i-1][2])
            dp[i][0] = max(dp[i-1][1],dp[i-1][2])
        elif s[i] == 's':
            dp[i][0] = max(dp[i-1][2],dp[i-1][1]) + R
            dp[i][1] = max(dp[i-1][0],dp[i-1][2])
            dp[i][2] = max(dp[i-1][1],dp[i-1][0])
        else:
            dp[i][1] = max(dp[i-1][2],dp[i-1][0]) + S
            dp[i][0] = max(dp[i-1][1],dp[i-1][2])
            dp[i][2] = max(dp[i-1][1],dp[i-1][0])
    
    ans += max(dp[len(s)-1][0],dp[len(s)-1][1],dp[len(s)-1][2])

print(ans)

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


ok = [True] * N
d = {"r": P, "s": R, "p": S}
ans = 0
for i in range(N):
    if i >= K and T[i - K] == T[i] and ok[i - K]:
        ok[i] = False
    if ok[i]:
        ans += d[T[i]]
print(ans)

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

  l = ["" for i in range(K)]
  s = 0
  for i in range(N):
    
    if l[i%K] == T[i]:
      l[i%K]=""
      continue

    if l[i%K] == "":
      if T[i] == "r":
        s = s+P
        l[i%K] = "r"
      elif T[i] == "s":
        s = s+R
        l[i%K] = "s"
      else:
        s = s+S
        l[i%K] = "p"

    elif T[i]=="r":
      s = s+P
      l[i%K] = "r"
    elif T[i]=="s":
      s = s+R
      l[i%K] = "s"
    else:
      s = s+S
      l[i%K] = "p"

  print(s)

if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n, k = LI()
r, s, p = LI()
d = {'r': p, 's': r, 'p': s}
t = S()
ans = 0









for j in range(k):
    is_win_last = False  
    i = j
    while i < n:
        
        if i >= k and t[i - k] == t[i] and is_win_last:
            is_win_last = False
        else:  
            ans += d[t[i]]
            is_win_last = True
        i += k


print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
total=0
M=[0]*n
for i in range(n):
  if t[i]=='r' and (i<k or M[i-k]!='p'):
    M[i]='p'
    total+=p
  elif t[i]=='s' and (i<k or M[i-k]!='r'):
    M[i]='r'
    total+=r
  elif t[i]=='p' and (i<k or M[i-k]!='s'):
    M[i]='s'
    total+=s
  
print(total)

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

def add(x):
    if x == 'r':
        return p
    elif x == 's':
        return r
    else:
        return s

nk =[0]*k
for i in range(n):
    key = i%k
    if nk[key]==0:
        nk[key] = [t[i]]
    else:
        nk[key].append(t[i])
ans = 0
for j in nk:
    if j ==0:
        continue
    ans += add(j[0])
    for k in range(1, len(j)):
        if j[k]== j[k-1]:
            j[k]='q'
        else:
            ans += add(j[k])
print(ans)

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

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

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

score = 0

for i in range(N):
    if i + K < N:
        if T[i] == T[i + K]:
            T[i + K] = 'N'
    
    score += P[T[i]]

print(score)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
ans = 0
commands = ['']*n
for i, t in enumerate(T):
  if t == 'r':
    command = 'p'
    point = p
  elif t == 's':
    command = 'r'
    point = r
  elif t == 'p':
    command = 's'
    point = s
  if (i - k >= 0) and (commands[i-k] == command):
    command = ''
    point = 0
  ans += point
  commands[i] = command
  
print(ans)

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

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
result = []
for i in range(K):
    result.append(T[i])
for j in range(K, N):
    if result[j-K] == T[j]:
        result.append("d")
    else:
        result.append(T[j])

result = Counter(result)
print(result["r"]*P+result["s"]*R+result["p"]*S)

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

win = ''
for i in range(len(T)-k):
    if T[i]==T[i+k]:
        T[i+k]='
ans = 0
for i in T:
    if i=='r':ans+=p
    elif i=='s':ans+=r
    elif i=='p':ans+=s
print(ans)

Solution (Python):
def game(word):
    if word=="r":return p,"p"
    elif word=="s":return r,"r"
    else:return s,"s"


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

point=0
for i in range(n):
    num,word=game(t[i])
    if i<k:
        point +=num
        ans +=word
    else:
        if (t[i-k]==t[i] and ans[-k]!=word) or (t[i-k]!=t[i]):
            point +=num
            ans +=word
        else:
            ans +=t[i]

print(point)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ok = [False] * n
score = {"r": p, "s": r, "p": s}
ans = 0
for i in range(n):
    if i>=k and t[i-k]==t[i] and not ok[i-k]:
        ok[i] = True
    else:
        ans += score[t[i]]

print(ans)

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    r, s, p = na()
    t = ns()
    ans = 0
    for j in range(k):
        m = (n-j-1)//k+1
        dp = [[0]*m for _ in range(3)]
        o = t[j]
        if o=="s":
            dp[0][0] = r
        if o=="r":
            dp[1][0] = p
        if o=="p":
            dp[2][0] = s
        for i in range(1, m):
            o = t[k*i+j]
            premax = max(dp[0][i-1], dp[1][i-1], dp[2][i-1])
            if o == "s":
                dp[0][i] = max(dp[1][i-1], dp[2][i-1]) + r
                dp[1][i] = premax
                dp[2][i] = premax
            if o == "r":
                dp[1][i] = max(dp[0][i-1], dp[2][i-1]) + p
                dp[0][i] = premax
                dp[2][i] = premax
            if o == "p":
                dp[2][i] = max(dp[0][i-1], dp[1][i-1]) + s
                dp[0][i] = premax
                dp[1][i] = premax
        ans += max([dp[0][-1], dp[1][-1], dp[2][-1]])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
point = list(map(int, input().split())) 
T = input()
te = ["r", "s", "p"]
past = []
ans = 0

for i,char in enumerate(T):
    index_win = (te.index(char) + 2) % 3
    te_win = te[index_win]
    if i >= K:
        if te_win == past[i - K]:
            past.append("$")
            continue
        else:
            ans += point[index_win]
    else:
        ans += point[index_win]
    past.append(te_win)

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):
    valian = t[i]
    if valian == 'r':
        ans += p
    elif valian == 's':
        ans += r
    else:
        ans += s
        
for i in range(k):
    check = ''
    for j in range(i, n, k):
        if check == '':
            check = t[j]
            continue
        
        if check == t[j]:
            if t[j] == 'r':
                ans -= p
            elif t[j] == 's':
                ans -= r
            else:
                ans -= s
            check = ''
        else:
            check = t[j]
        
print(ans)

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

hands = ["any"]*n

for i in range(n):
  if i-K < 0 or hands[i-K] != t[i]:
    hands[i] = t[i]
    
score = 0
for h in hands:
  if h == "r":
    score += P
  elif h == "s":
    score += R
  elif h == "p":
    score += S
print(score)

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}

ans=0
for i in range(N):
    if i>=K and T[i-K]==T[i]:
            T[i]="o"
    else:
        ans+=d[T[i]]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9+7

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

memo = [None] * n
ans = 0

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

    if t[i] == 's':
        if i - k < 0 or (0 <= i - k and memo[i - k] != 'r'):
            ans += r
            memo[i] = 'r'
        elif n <= i + k or (i + k < n and t[i + k] != 'p'):
            memo[i] = 's'
        else:
            memo[i] = 'p'

    if t[i] == 'p':
        if i - k < 0 or (0 <= i - k and memo[i - k] != 's'):
            ans += s
            memo[i] = 's'
        elif n <= i + k or (i + k < n and t[i + k] != 'r'):
            memo[i] = 'p'
        else:
            memo[i] = 'r'

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9+7

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

memo = [None] * n
ans = 0

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

    if t[i] == 's':
        if i - k < 0 or (0 <= i - k and memo[i - k] != 'r'):
            ans += r
            memo[i] = 'r'
        elif n <= i + k or (i + k < n and t[i + k] != 'p'):
            memo[i] = 's'
        else:
            memo[i] = 'p'

    if t[i] == 'p':
        if i - k < 0 or (0 <= i - k and memo[i - k] != 's'):
            ans += s
            memo[i] = 's'
        elif n <= i + k or (i + k < n and t[i + k] != 'r'):
            memo[i] = 'p'
        else:
            memo[i] = 'r'

print(ans)

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

print(ans)