Problem:
 Score : 400 points    Problem Statement At an arcade, Takahashi is playing a game called RPS Battle , which is played as follows:   The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)  Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):  R points for winning with Rock;  S points for winning with Scissors;  P points for winning with Paper.    However, in the i -th round, the player cannot use the hand he/she used in the (i-K) -th round. (In the first K rounds, the player can use any hand.)   Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.  The information Takahashi obtained is given as a string T . If the i -th character of T  (1 \leq i \leq N) is r , the machine will play Rock in the i -th round. Similarly, p and s stand for Paper and Scissors, respectively.  What is the maximum total score earned in the game by adequately choosing the hand to play in each round?      Notes In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.   If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;  if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;  if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;  if both players play the same hand, it is a draw.       Constraints  2 \leq N \leq 10^5  1 \leq K \leq N-1  1 \leq R,S,P \leq 10^4  N,K,R,S, and P are all integers.  |T| = N  T consists of r , p , and s .         Input Input is given from Standard Input in the following format:  N  K  R  S  P  T       Output Print the maximum total score earned in the game.        Sample Input 1 5 2
8 7 6
rsrpr      Sample Output 1 27  The machine will play {Rock, Scissors, Rock, Paper, Rock}.  We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27 .       Sample Input 2 7 1
100 10 1
ssssppr      Sample Output 2 211       Sample Input 3 30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr      Sample Output 3 4996

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

c = [''] * n
ans = 0
for i in range(n):
    if t[i] == 'r':
        rsp = 'p'
        point = p
    elif t[i] == 's':
        rsp = 'r'
        point = r
    elif t[i] == 'p':
        rsp = 's'
        point = s
    
    if i >= k and c[i-k] == rsp:
        rsp = ''
        point = 0
    
    c[i] = rsp
    ans += point
print(ans)

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

ans = 0
check = [0] * N

for i in range(N):
  j = T[i]
  if i >= K:
    if j == check[i-K]: continue
  if j == "r": ans += P
  elif j == "s": ans += R
  else: ans += S
  check[i] = j

print(ans)

Solution (Python):
import sys
import itertools


def resolve(in_):
    N, K = map(int, next(in_).split())
    R, S, P = map(int, next(in_).split())
    T = next(in_).rstrip()
    r = ord(b'r')
    s = ord(b's')
    p = ord(b'p')

    temp = [0] * (N + 1)

    ans = 0
    for i, v in enumerate(T):
        j = i - K
        if j < 0:
            if v == r:
                ans += P
                temp[i] = p
            elif v == s:
                ans += R
                temp[i] = r
            elif v == p:
                ans += S
                temp[i] = s
        else:
            if v == r and temp[j] != p:
                ans += P
                temp[i] = p
            elif v == s and temp[j] != r:
                ans += R
                temp[i] = r
            elif v == p and temp[j] != s:
                ans += S
                temp[i] = s
    
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

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

for i in range(0,n):
    a.append(t[i])

for j in range(0,n):
    if j - k >= 0 and a[j - k] == a[j]:
           a[j] = "o"
    p
    if a[j] == "r":
        ans = ans + p
    if a[j] == "s":
        ans = ans + r
    if a[j] == "p":
        ans = ans + s


print(ans)

Solution (Python):


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

    for i in range(n):
        if t[i] == "r":
            if i >= k and c[i-k] == 'p':
                c += 'x'
            else:
                c += "p"
                ans += p
        elif t[i] == "s":
            if i >= k and c[i-k] == 'r':
                c += 'x'
            else:
                c += "r"
                ans += r
        else:
            if i >= k and c[i-k] == 's':
                c += 'x'
            else:
                c += "s"
                ans += s
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
  n,k=map(int,input().split())
  p=list(map(int,input().split()))
  t=input()
  s='rsp'
  dp=[[-10**10]*(n+k) for _ in range(3)]
  for i in range(k):
    for j in range(3):
      dp[j][i]=0
  for i in range(n):
    for j in range(3):
      for l in range(3):
        if j==l:
          continue
        res=((s.index(t[i])-l+3)%3==1)*p[l]
        if dp[l][i+k]<dp[j][i]+res:
          dp[l][i+k]=dp[j][i]+res
  ans=0
  for i in range(n,n+k):
    ans+=max(dp[0][i],dp[1][i],dp[2][i])
  print(ans)
main()

Solution (Python):
n,k=map(int,input().split())
p=list(map(int,input().split()))
t=input()
s='rsp'
dp=[[-10**10]*(n+k) for _ in range(3)]
for i in range(k):
  for j in range(3):
    dp[j][i]=0
for i in range(n):
  for j in range(3):
    for l in range(3):
      if j==l:
        continue
      res=((s.index(t[i])-l+3)%3==1)*p[l]
      if dp[l][i+k]<dp[j][i]+res:
        dp[l][i+k]=dp[j][i]+res
ans=0
for i in range(n,n+k):
  ans+=max(dp[0][i],dp[1][i],dp[2][i])
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
RSP = list(map(int, input().split()))
D = {'r': 0, 's': 1, 'p': 2}
T = input()

M = N//K
dp = [[[0]*3 for _ in range(M+(i < (N % K)))] for i in range(K)]



def wins(i, s):
    j = D[s]
    return i % 3 == (j-1) % 3


ans = 0
for i in range(K):
    L = dp[i]
    for k in range(3):
        L[0][k] = RSP[k]*wins(k, T[i])
    for j in range(1, len(L)):
        for k in range(3):
            L[j][k] = max(L[j-1][l] + RSP[k]*wins(k, T[i+j*K])
                          for l in range(3) if k != l)
    
    ans += max(L[-1][k] for k in range(3))
print(ans)

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

rsp = {"r":p, "s":r, "p":s}
ans = 0
for i in range(n):
  if i < k: ans += rsp[t[i]]
  else:
    if t[i] != t[i-k]: ans += rsp[t[i]]
    else: t[i] = "a"
print(ans)

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

rsp = {"r":p, "s":r, "p":s}
ans = 0
for i in range(n):
  if i < k: ans += rsp[t[i]]
  else:
    if t[i] != t[i-k]: ans += rsp[t[i]]
    else: t[i] = "a"
print(ans)

Solution (Python):

N,K = map(int,input().split())
RSP = tuple(map(int,input().split()))
T = input()


def judge(a,b):
    if a == 'r' and b == 2:
        return True
    elif a == 's' and b == 0:
        return True
    elif a == 'p' and b == 1:
        return True
    return False



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


for i in range(1,K+1):
    for j in range(3):
        if judge(T[i-1],j):
            dp[i][j] = RSP[j]


for v in range(1,K+1):
    
    for w in range(v+K,N+1,K):
        
        for l in range(3):
            
            for m in range(3):
                if l == m:
                    continue
                
                if judge(T[w-1],l):
                    if w-K>=0:
                        dp[w][l] = max(dp[w][l],dp[w-K][m] + RSP[l])
                
                dp[w][l] = max(dp[w][l],dp[w-K][m])
                     

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

Solution (Python):

N,K = map(int,input().split())
RSP = tuple(map(int,input().split()))
T = input()


def judge(a,b):
    if a == 'r' and b == 2:
        return True
    elif a == 's' and b == 0:
        return True
    elif a == 'p' and b == 1:
        return True
    return False



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


for v in range(1,N+1):
    
    for w in range(3):
        
        for k in range(3):
            if w == k:
                continue
            
            if judge(T[v-1],w):
                dp[v][w] = max(dp[v][w],dp[v-K][k] + RSP[w])
            
            else:
                dp[v][w] = max(dp[v][w],dp[v-K][k])         

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,k = LI()
    r,s,p = LI()
    t = list(S())
    ans = 0

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

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

def janken(n):
    if n == "r":
        return P
    elif n == "s":
        return R
    else:
        return S

ans = 0

for i in range(K):
    h = T[i::K]
    l = len(h)
    f = 1
    a = ""
    for j in range(l):
        if a != h[j]:
            a = h[j]
            f = 0
            ans += janken(h[j])
        else:
            ans += f * janken(h[j])
            f = - (f - 1)

print(ans)

Solution (Python):
from collections import deque
N,K = map(int,input().split())
R,S,P = map(int,input().split())
Tlist = input()
Answer = 0
def po(x):
    if x == 's':
        return R
    elif x == 'r':
        return P
    else:
        return S
for i in range(K):
    TK = list(Tlist[i::K]).copy()
    TK.append('')
    T2 = TK[0]
    sig = 2
    for i in range(1,len(TK)):
        T1 = T2
        T2 = TK[i]
        if T1 == T2 and i != len(TK)-1:
            sig += 1
        else:
            Answer += po(T1)*(sig//2)
            sig = 2
print(Answer)

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 t[i]=='r':
      dp[i+1][2]=max(dp[i+1-k][0],dp[i+1-k][1])+p
      dp[i+1][1]=max(dp[i+1-k][0],dp[i+1-k][2])
      dp[i+1][0]=max(dp[i+1-k][1],dp[i+1-k][2])
    

    elif t[i]=='s':
      dp[i+1][0]=max(dp[i+1-k][1],dp[i+1-k][2])+r
      dp[i+1][1]=max(dp[i+1-k][0],dp[i+1-k][2])
      dp[i+1][2]=max(dp[i+1-k][0],dp[i+1-k][1])
      
      
    else:
      dp[i+1][1]=max(dp[i+1-k][0],dp[i+1-k][2])+s
      dp[i+1][0]=max(dp[i+1-k][1],dp[i+1-k][2])
      dp[i+1][2]=max(dp[i+1-k][0],dp[i+1-k][1])
      


ans=0

for i in range(1,k+1):
  ans+=max(dp[-i])


print(ans)

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


dp = [[0 for j in range(3)] for i in range(n)]
for i in range(0,k):
    if T[i] == 'r':
        dp[i][2] = p 
    elif T[i] == 's':
        dp[i][0] = r 
    else:
        dp[i][1] = s 
    for j in range(k,n-i,k):
        if T[i+j] == 'r':
            dp[i+j][2] = max(dp[i+j-k][0]+p,dp[i+j-k][1]+p)
            dp[i+j][1] = max(dp[i+j-k][0],dp[i+j-k][2])
            dp[i+j][0] = max(dp[i+j-k][1],dp[i+j-k][2])
        elif T[i+j] == 's':
            dp[i+j][0] = max(dp[i+j-k][1]+r,dp[i+j-k][2]+r)
            dp[i+j][1] = max(dp[i+j-k][0],dp[i+j-k][2])
            dp[i+j][2] = max(dp[i+j-k][0],dp[i+j-k][1])
        else:
            dp[i+j][1] = max(dp[i+j-k][2]+s,dp[i+j-k][0]+s)
            dp[i+j][0] = max(dp[i+j-k][1],dp[i+j-k][2])
            dp[i+j][2] = max(dp[i+j-k][0],dp[i+j-k][1])
    
total = 0

for i in range(n-1,max(0,n-1-k),-1):
    total += max(dp[i])
print(total)

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

for i in range(k):
    for j in range(i,n,k):
        if j-k>=0 and a[j-k]!=a[j]:
            ans+=count[a[j]]
        if j-k>=0 and a[j-k]==a[j]:
            a[j]="q"
        elif j-k<0:
            ans+=count[a[j]]
        
print(ans)

Solution (Python):

N, K = list(map(int, input().split()))
point= list(map(int, input().split()))
T = list(input())
d = dict()
d['s'] = point[0]
d['p'] = point[1]
d['r'] = point[2]
res = 0
ans = []
flag = [0,0,0]

for i in range(K):
  res += d[T[i]]
  ans.append(T[i])
for i in range(K,N):
  if ans[i-K] == T[i]:
    ans.append("n")
  else:
    res += d[T[i]]
    ans.append(T[i])
print(res)

Solution (Python):
from collections import deque

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = dict(r=P, s=R, p=S)
ans = 0
q = deque()
for i in range(K):
    s = T[i]
    ans += score[s]
    q.append(s)
for i in range(K, N):
    s = T[i]
    s_pre_k = q.popleft()
    if s == s_pre_k:
        q.append('n')
    else:
        ans += score[s]
        q.append(s)
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
d={'r':p,'s':r,'p':s}
t=input()
ans=0
for i in range(k):
  x=t[i]
  ans+=d[x]
  for j in range(i+k,n,k):
    if t[j]!=x:
      x=t[j]
      ans+=d[x]
    else:
      x='z'
print(ans)

Solution (Python):









N, K = map(int, input().split())






































R, S, P = map(int, input().split())
T = input()
win = {"r": ["p", P], "s": ["r", R], "p": ["s", S]}

hands = []
tot = 0
for i, c in enumerate(T):
    if i >= K and win[c][0] == hands[i - K]:
        hands.append("anything")
        continue
    hands.append(win[c][0])
    tot += win[c][1]
        
print(tot)

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

ans=0
chk=[0]*N
for n in range(N):
  c=T[n]
  if n>=K and T[n-K]==c and chk[n-K]==0:
    chk[n]=1 
    continue
  if c=='r':
    ans+=P
  elif c=='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()
G=[[] for n in range(K)]

for n in range(N):
  G[n%K].append(T[n])

cnt=0
for g in G:
  r,s,p=True,True,True
  for g2 in g:
    if g2=='r':
      if p:
        p=False
        r,s=True,True
        cnt+=P
      else:
        r,s,p=True,True,True
    if g2=='s':
      if r:
        r=False
        p,s=True,True
        cnt+=R
      else:
        r,s,p=True,True,True
    if g2=='p':
      if s:
        s=False
        r,p=True,True
        cnt+=S
      else:
        r,s,p=True,True,True

print(cnt)

Solution (Python):









import os
import sys


from collections import Counter


def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    takahashi = ''
    for t in T:
        if t == 's':
            takahashi += 'r'
        elif t == 'p':
            takahashi += 's'
        else:
            takahashi += 'p'

    for i in range(K, N):
        if takahashi[i-K] == takahashi[i]:
            takahashi = takahashi[:i] + '_' + takahashi[i+1:]
    ans = takahashi.count('r')*R + takahashi.count('s')*S + takahashi.count('p')*P
    print(ans)

if __name__ == "__main__":
    main()

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


point = {'r':P, 's':R, 'p':S}
win = [False] * N
total = 0
for i in range(N):
    if i < K or T[i] != T[i - K] or win[i - K] == False:
        total += point[T[i]]
        win[i] = True

print(total)

Solution (Python):
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
T = input()

bestmove = {'r':2, 's':0, 'p':1}

ans = 0
for k in range(K):
    t = T[k::K]
    prev = -1
    for opp in t:
        me = bestmove[opp]
        if prev != me:
            prev = me
            ans += A[me]
        else:
            prev = -1

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(K):
    tt=T[i:N:K]
    l=len(tt)
    dp = [[0]*l for _ in range(3)]
    if tt[0]=="r":
        dp[2][0]=P
    if tt[0]=="s":
        dp[0][0]=R
    if tt[0]=="p":
        dp[1][0]=S
    
    for i in range(1,l):
        if tt[i]=="r":
            dp[0][i] = max(dp[1][i-1],dp[2][i-1])
            dp[1][i] = max(dp[2][i-1],dp[0][i-1])
            dp[2][i] = max(dp[0][i-1],dp[1][i-1])+P
        if tt[i]=="s":
            dp[0][i] = max(dp[1][i-1],dp[2][i-1])+R
            dp[1][i] = max(dp[2][i-1],dp[0][i-1])
            dp[2][i] = max(dp[0][i-1],dp[1][i-1])
        if tt[i]=="p":
            dp[0][i] = max(dp[1][i-1],dp[2][i-1])
            dp[1][i] = max(dp[2][i-1],dp[0][i-1])+S
            dp[2][i] = max(dp[0][i-1],dp[1][i-1])
    ans+= max(dp[h][-1] for h in range(3))
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

    
    N, K = map(int, input().split())
    R,S,P=map(int, input().split())
    T=input()
    
    
    
    ans=0
    for k in range(K):
        i=0
        l=[]
        while i+k<N:
            l.append(i+k)
            i+=K
        if not l:
            continue
        dp=[[0]*3 for _ in range(len(l)+1)]
        
        for i in range(1,len(l)+1):
            si=T[l[i-1]]
            sc=[0]*3
            if si=="r":
                sc[2]=P
            elif si=="s":
                sc[0]=R
            elif si=="p":
                sc[1]=S

            for j in range(3):
                for j2 in range(3):
                    if j==j2:continue
                    dp[i][j]=max(dp[i][j],dp[i-1][j2]+sc[j])
            ans+=max(dp[-1])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

def is_win(me, other):
    if me == 0 and other == 1:
        return True
    elif me == 1 and other == 2:
        return True
    elif me == 2 and other == 0:
        return True
    return False

def char_to_num(s):
    if s == "r":
        return 0
    elif s == "s":
        return 1
    else:
        return 2


dp = [[-1 for j in range(3)] for i in range(N+1)]
dp[0] = [0, 0, 0]
for i in range(1, N+1):
    t = char_to_num(T[i-1])
    if i <= K:
        for j in range(3):
            dp[i][j] = is_win(j, t)*RSP[j]
    else:    
        for j in range(3):
            dp[i][j] = max(
                [dp[i-K][k] + is_win(j, t)*RSP[j] for k in range(3) if j != k]
            )



r = sum(map(max, dp[-K:]))
print(r)

Solution (Python):
N, K = map(int, input().split())
SC = list(map(int, input().split()))
T = input()
Q = []
for i in range(len(T)):
  if T[i] == "r":
    Q.append(0)
  elif T[i] == "s":
    Q.append(1)
  else:
    Q.append(2)
my = [0 for i in range(N)]
ans = 0
for i in range(N):
  if i < K:
    my[i] = (Q[i] - 1) % 3
    ans += SC[my[i]]
  else:
    n = (Q[i] - 1) % 3
    if my[i - K] != n:
      my[i] = n
      ans += SC[my[i]]
    else:
      if i + K < N:
        my[i] = Q[i+K]
print(ans)

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

ans = 0
for i in range(N):
    if i - K < 0:
        hands.append(wins[T[i]])
        ans += points[T[i]]
    else:
        if hands[i-K] == wins[T[i]]:
            
            hands.append("-")
        else:
            
            hands.append(wins[T[i]])
            ans += points[T[i]]
print(ans)

Solution (Python):
N, K = map(int, input().split())
dic = {}
dic['s'], dic['p'], dic['r'] = map(int, input().split())
T = input()
score = [0]*N
for i in range(N):
  if i>=K and T[i]==T[i-K] and score[i-K]>0:
    continue
  score[i] = dic[T[i]]
print(sum(score))

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

def calc(l):
    dp = [[0]*3 for _ in range(len(l)+1)]
    
    for i in range(len(l)):
        dp[i+1][0] = max(dp[i][1], dp[i][2])+(R if l[i]=='s' else 0)
        dp[i+1][1] = max(dp[i][2], dp[i][0])+(S if l[i]=='p' else 0)
        dp[i+1][2] = max(dp[i][0], dp[i][1])+(P if l[i]=='r' else 0)

    return max(dp[-1])
    
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()[:-1]
d = defaultdict(list)

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

ans = 0

for i in range(K):
    ans += calc(d[i])

print(ans)

Solution (Python):
n,k=map(int,input().split())
B=list(map(int,input().split()))
S=list(input())
A=[[] for i in range(k)]
for i in range(n):
  if S[i]=="s":
    A[i%k].append(0)
  elif S[i]=="p":
    A[i%k].append(1)
  else:
    A[i%k].append(2)
ans=0
for i in range(k):
  f=0
  for j in range(len(A[i])):
    if f==0:
      ans=ans+B[A[i][j]]
      if j!=len(A[i])-1:
        if A[i][j]==A[i][j+1]:
          f=1
    else:
      f=0
print(ans)

Solution (Python):
n,k=map(int,input().split())
dp=[[0,0,0] for i in range(n)]
janken=list(map(int,input().split( )))
t=[0 if i=="r" else 1 if i=="s" else 2 for i in input()]

for i in range(n):
    if i<k:
        for j in range(3):
            if (j+1)%3==t[i]:
                dp[i][j]=janken[j]
    else:
        
        for j in range(3):
            
            for l in range(3):
                if j!=l:
                    if (j+1)%3==t[i]:
                        dp[i][j]=max(dp[i][j],janken[j]+dp[i-k][l])
                    else:
                        dp[i][j]=max(dp[i][j],dp[i-k][l])
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()
ans = [0] * k
for i in range(k):
    pre_cnt = ["", 0]
    for j in range(i, n, k):
        if j == i:
            if t[j] == "r":
                ans[i] += p
                pre_cnt = ["p", p]
            elif t[j] == "s":
                ans[i] += r
                pre_cnt = ["r", r]
            else:
                ans[i] += s
                pre_cnt = ["s", s]
            continue

        if t[j] == "r":
            if pre_cnt[0] == "p":
                pre_cnt[0] = ""
                continue
            ans[i] += p
            pre_cnt = ["p", p]
        elif t[j] == "s":
            if pre_cnt[0] == "r":
                pre_cnt[0] = ""
                continue
            ans[i] += r
            pre_cnt = ["r", r]
        else:
            if pre_cnt[0] == "s":
                pre_cnt[0] = ""
                continue
            ans[i] += s
            pre_cnt = ["s", s]
print(sum(ans))

Solution (Python):
N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = list(input())
rsp = ['r', 's', 'p']
for i in range(N-K):
    if T[i] == T[i+K]:
        T[i+K] = 0
print(sum(RSP[i]*T.count(rsp[(i+1)%3]) for i in range(3)))

Solution (Python):
N, K = map(int, input().split())
h = list(map(int, input().split()))
rps = ['r', 's', 'p']
T = input()
ans = 0
for i in range(3):
    for j in range(K):
        o = True
        for k in range(j, N, K):
            if o and (rps[i]+T[k] == 'rs' or rps[i]+T[k] == 'sp' or rps[i]+T[k] == 'pr'):
                ans += h[i]
                o = False
            else:
                o = True
print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    RSP_score = LI()
    rsp_num = {'r': 0, 's': 1, 'p': 2}
    T = [rsp_num[i] for i in S()]

    
    
    dp = [[0] * 3 for _ in range(N)]
    for i in range(N):
        for j in range(3):
            
            if i - K >= 0:
                dp[i][j] += max([dp[i-K][k] for k in range(3) if k != j])
            if (j + 1) % 3 == T[i]:
                dp[i][j] += RSP_score[j]

    
    
    ans = sum([max(i) for i in dp[N-K:]])
    print(ans)

if __name__ == '__main__':
    resolve()

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

ans = 0
for i in range(0, K):
    rsp_K = T[i::K]
    rsp_before = ''
    for rsp in rsp_K:
        if rsp == 'r':
            if rsp_before != 'r':
                ans += P
                rsp_before = 'r'
            else:
                rsp_before = ''
        elif rsp == 's':
            if rsp_before != 's':
                ans += R
                rsp_before = 's'
            else:
                rsp_before = ''
        elif rsp == 'p':
            if rsp_before != 'p':
                ans += S
                rsp_before = 'p'
            else:
                rsp_before = ''

print(ans)

Solution (Python):
janken = {"r": "p",
          "s": "r",
          "p": "s"}


n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
T = list(input())
score = {"r": r,
         "s": s,
         "p": p}

ans = 0
for i in range(n):
    if i < k:
        ans += score[janken[T[i]]]
    else:
        if T[i] == T[i - k]:
            T[i] = "-"
        else:
            ans += score[janken[T[i]]]
print(ans)

Solution (Python):
n, k = map(int, input().split())
s, p, r = map(int, input().split()) 
dic = {"p":p, "r":r, "s":s}
t = input()
ans = 0
for i in range(k):
    bef = "a"
    for j in range(i, n, k):

        if bef == t[j]:
            bef = "a"
        else:
            ans += dic[t[j]]
            bef = t[j]
print(ans)

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

pc_hand = [0]*N
my_hand = [0]*N

for i in range(N):
    if T[i] == 'r':
        pc_hand[i] = 0
    elif T[i] == 's':
        pc_hand[i] = 1
    else:
        pc_hand[i] = 2

point = 0

for i in range(N):
    if i >= K and T[i] == T[i - K] and (my_hand[i - K] + 1) % 3 == pc_hand[i - K]:
        my_hand[i] = pc_hand[i]
    else:
        my_hand[i] = (pc_hand[i] - 1) % 3
        point += pt[my_hand[i]]

print(point)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
divided_T = [[] for _ in range(K)]
for i in range(N):
    divided_T[i%K].append(T[i])
ans = 0
for target in divided_T:
    dp = [[0]*3 for _ in range(len(target))]
    dp[0][0] = R if target[0] == 's' else 0
    dp[0][1] = P if target[0] == 'r' else 0
    dp[0][2] = S if target[0] == 'p' else 0
    for i in range(1,len(target)):
        dp[i][0] = max(dp[i-1][1],dp[i-1][2]) + (R if target[i] == 's' else 0)
        dp[i][1] = max(dp[i-1][0],dp[i-1][2]) + (P if target[i] == 'r' else 0)
        dp[i][2] = max(dp[i-1][1],dp[i-1][0]) + (S if target[i] == 'p' else 0)
    ans += max(dp[-1])
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):
    if i >= K and T[i-K] == T[i]:
        T[i] = 'a'
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):
n, k = map(int, input().split())
r, s, p = map(int, input().split())

rsp = {'r':0, 's':1, 'p':2}
t = [rsp[c] for c in input()]
point = [p, r, s]

dp = [[0] * n for _ in range(3)] 
if n <= k:
    ans = sum(point[i] for i in t)
else:
    for i in range(k):
        dp[t[i]][i] = point[t[i]]
    for i in range(k, n):
        for j in range(3):
            dp[j][i] = max(dp[(j+1) % 3][i-k], dp[(j+2) % 3][i-k])
            if j == t[i]:
                dp[j][i] += point[j]
    ans = 0
    for i in range(k):
        ans += max(dp[j][-1-i] for j in range(3))
print(ans)

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

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

    dp = [[0] * 3 for _ in range(n + k)]
    for i1 in range(k):
        i2 = i1 + k
        while i2 - k < n:
            dp[i2][0] = max(dp[i2-k][1], dp[i2-k][2])
            dp[i2][0] += r if t[i2-k] == 's' else 0
            dp[i2][1] = max(dp[i2-k][0], dp[i2-k][2])
            dp[i2][1] += s if t[i2-k] == 'p' else 0
            dp[i2][2] = max(dp[i2-k][0], dp[i2-k][1])
            dp[i2][2] += p if t[i2-k] == 'r' else 0
            i2 += k
    r = 0
    for j1 in range(-k, 0, 1):
        r += max(dp[j1])
    print(r)

if __name__ == '__main__':
    main()

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

for i in range(n):
    if t[i] == 'r':
        l[i] = p
    elif t[i] == 's':
        l[i] = r
    else:
        l[i] =s

ans = sum(l)
for i in range(k, n):
    if t[i] == t[i - k]:
        ans -= l[i]
        t[i] = 'o'

print(ans)