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=list(input())


d={}
ans=0
c=0
for i in t:

    if c >= k:
        x =d[c-k]
        y = c+k
        if i =='r':
            if x == 'p':
                if  y < n and t[y] == 's':
                    d[c]='s'
                else:
                    d[c]='r'
            else:
                ans += p ; d[c]='p'
        elif i == 's': 
            if x == 'r':
               if y < n and t[y] == 'p' :
                    d[c]='p'
               else:
                    d[c]='s'
            else:
                ans += r;d[c]='r'
        elif i == 'p': 
            if x == 's':
               if  y < n and t[y] == 'r':
                    d[c]='r'
               else:
                    d[c]='p'
            else:
                ans += s;d[c]='s'

    else:
        if i =='r':
            ans += p ; d[c]='p'
        elif i == 's': 
            ans += r;d[c]='r'
        elif i == 'p': 
            ans += s;d[c]='s'

    c+=1


print(ans)

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

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

score = 0

last_lst = [""]*K

for i in range(N):
    if T[i] == last_lst[i%K]:
        last_lst[i%K] = ""
        continue
    else:
        if T[i] == "r":
            score += P
        elif T[i] == "p":
            score += S
        else:
            score += R
        last_lst[i%K] = T[i]
print(score)

Solution (Python):

import sys

def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    scoreDict = {}
    scoreDict["r"]=P
    scoreDict["s"]=R
    scoreDict["p"]=S
    def toScore(c):
        return scoreDict[c]
    sum = 0
    record = []
    for i in range(N):
        if i-K<0 or record[i-K]!=T[i]:
            sum += toScore(T[i])
            
            record.append(T[i])
        else:
            record.append("*")
    
    print(sum)



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    R = int(next(tokens))  
    S = int(next(tokens))  
    P = int(next(tokens))  
    T = next(tokens)  
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Solution (Python):

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


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

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

bef_val = ''
rsp_str = ['r','s','p']
all_sum = 0
for i in range(0,k):
    max_count = 0
    for ss in rsp_str:
        count = 0
        if t[i] == 'r' and ss == 'p':
            count = count + p
        elif t[i] == 's' and ss == 'r':
            count = count + r
        elif t[i] == 'p' and ss == 's':
            count = count + s
        bef_val = ss
        for j in range(i+k,n,k):
            if t[j] == 'r':
                if bef_val != 'p':
                    count = count + p
                    bef_val = 'p'
                else:
                    bef_val = ''
            elif t[j] == 's':
                if bef_val != 'r':
                    count = count + r
                    bef_val = 'r'
                else:
                    bef_val = ''
            else:
                if bef_val != 's':
                    count = count + s
                    bef_val = 's'
                else:
                    bef_val = ''
        if(max_count < count):
            max_count = count
    all_sum = all_sum + max_count
print(all_sum)

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
R,S,P=map(int,input().split())
s=input()
c=Counter(s)
ans=c["r"]*P+c["s"]*R+c["p"]*S
for _ in range(k):
  i=0
  while (_+(i+1)*k)<n:
    if s[_+i*k]==s[_+(i+1)*k]:
      if s[_+(i+1)*k]=="r":
        ans-=P
        i+=1
      elif s[_+(i+1)*k]=="s":
        ans-=R
        i+=1
      elif s[_+(i+1)*k]=="p":
        ans-=S
        i+=1
    i+=1     
print(ans)

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

ans=t.count('r')*d['r']+t.count('s')*d['s']+t.count('p')*d['p']

for i in range(k,n):
  if t[i]==t[i-k]:
    ans-=d[t[i]]
    t[i]='x'
print(ans)

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

total = 0
for i in range(N):
  t = T[i]
  if t == "r":
    h = "p"
    points = P
  elif t == "s":
    h = "r"
    points = R
  elif t == "p":
    h = "s"
    points = S

  if i >= K and hist[i-K] == h:
    continue
  else:
    hist[i] = h
    total += points

print(total)

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

T = T[::-1]
flags = [True for _ in range(N)]
for i in range(0, N - K):
    if flags[i] and T[i] == T[i + K]:
        flags[i + K] = False

res = 0
for i in range(0, N):
    if flags[i]:
        if T[i] == 'r':
            res += P
        elif T[i] == 's':
            res += R
        else:
            res += S
print(res)

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

scores = {'r': p, 's': r, 'p': s}
not_scored = set()
res = 0

for i in range(n):
    if i < k:
        res += scores[t[i]]
    else:
        if t[i] == t[i-k] and i-k in not_scored:
            res += scores[t[i]]
        elif t[i] == t[i-k]:
            not_scored.add(i)
        else:
            res += scores[t[i]]

print(res)

Solution (Python):


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


ans = 0
for i in range(n):  
    
    if i >= k:  
        
        if T[i] == T[i-k]:
            
            T = T[:i] + 'f' + T[i+1:]
        else:
            pass
    else:   
        pass
    
    if T[i] == 'r': 
        ans += p
    elif T[i] == 's':
        ans += r
    elif T[i] == 'p':
        ans += s
    elif T[i] == 'f':   
        pass
    else:
        pass    


print(ans)

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

for i in range(n):
    if t[i]=='r':
        if i>=k and t[i-k]=='r':
            t[i]='m'
        else:
            tot+=p
    elif t[i]=='s':
        if i>=k and t[i-k]=='s':
            t[i]='m'
        else:
            tot+=r

    else:
        if i>=k and t[i-k]=='p':
            t[i]='m'
        else:
            tot+=s

print(tot)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[0]*(len(T))
for i in range(len(T)):
 A[i]=T[i]
BR=[0]*K
BS=[0]*K
BP=[0]*K
for j in range(N):
 s=j%K
 f=int(j/K)
 if f==0:
  if A[j]=="r":
   BP[s]=P
  elif A[j]=="s":
   BR[s]=R
  else:
   BS[s]=S 
 else:
  if A[j]=="r":
   a=max(BS[s],BP[s])
   b=max(BR[s],BP[s])
   c=max(BR[s],BS[s])+P
   BR[s]=a
   BS[s]=b
   BP[s]=c
  elif A[j]=="s":
   a=max(BS[s],BP[s])+R
   b=max(BR[s],BP[s])
   c=max(BR[s],BS[s])
   BR[s]=a
   BS[s]=b
   BP[s]=c
  else:
   a=max(BS[s],BP[s])
   b=max(BR[s],BP[s])+S
   c=max(BR[s],BS[s])
   BR[s]=a
   BS[s]=b
   BP[s]=c
d=0
for k in range(K):
 d+=max(BR[k],BS[k],BP[k])
print(d)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input ().split())
t=input()
t2=""
for i in range(n):
    if t[i]=="r":
        t2=t2+"p"
    elif t[i]=="s":
        t2=t2+"r"
    else:
        t2=t2+"s"
sum=0
for j in range(n):
    if t2[j]=="r":
        sum+=r
    elif t2[j]=="s":
        sum+=s
    else:
        sum+=p
res=n%k
for x in range(k):
    if res>0:
        flag=1
    else:
        flag=0
    tx=""
    for i in range(n//k+flag):
        tx=tx+t2[x+i*k]
    res-=1
    bef=tx[0]
    cnt=0
    for y in range(len(tx)):
        if tx[y]==bef:
            cnt+=1
        else:
            if bef=="r":
                num=r
            elif bef=="s":
                num=s
            else:
                num=p
            if cnt!=1:
                sum=sum-num*(cnt//2)
            cnt=1
            bef=tx[y]
        if y==len(tx)-1:
            if bef=="r":
                num=r
            elif bef=="s":
                num=s
            else:
                num=p
            if cnt!=1:
                sum=sum-num*(cnt//2)
print(sum)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[0]*(len(T))
for i in range(len(T)):
 A[i]=T[i]
BR=[0]*K
BS=[0]*K
BP=[0]*K
for j in range(N):
 s=j%K
 f=int(j/K)
 if f==0:
  if A[j]=="r":
   BP[s]=P
  elif A[j]=="s":
   BR[s]=R
  else:
   BS[s]=S 
 else:
  if A[j]=="r":
   a=max(BS[s],BP[s])
   b=max(BR[s],BP[s])
   c=max(BR[s],BS[s])+P
   BR[s]=a
   BS[s]=b
   BP[s]=c
  elif A[j]=="s":
   a=max(BS[s],BP[s])+R
   b=max(BR[s],BP[s])
   c=max(BR[s],BS[s])
   BR[s]=a
   BS[s]=b
   BP[s]=c
  else:
   a=max(BS[s],BP[s])
   b=max(BR[s],BP[s])+S
   c=max(BR[s],BS[s])
   BR[s]=a
   BS[s]=b
   BP[s]=c
d=0
for k in range(K):
 d+=max(BR[k],BS[k],BP[k])
print(d)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
rslt = 0
def score(chr):
    if chr == 'r':
        return p
    elif chr == 's':
        return r
    else:
        return s
stack = ''
for i in range(n):
    if i < k:
        rslt += score(t[i])
        stack += t[i]
    else:
        if t[i] != stack[i-k]:
            rslt += score(t[i])
            stack += t[i]
        else:
            stack += 'a'
print(rslt)

Solution (Python):
from collections import deque
N,K = (int(x) for x in input().split())
R,S,P = (int(x) for x in input().split())
T = input()
count = 0
prog = 0
checker = ""
l = list(T)
d = deque(l[0:K])
for hand in l:
    if prog >= K:
        if hand == "r" and hand != checker:
            count += P
            d.append(hand)
            checker = d.popleft()
        elif hand == "s" and hand != checker:
            count += R
            d.append(hand)
            checker = d.popleft()
        elif hand == "p" and hand != checker:
            count += S
            d.append(hand)
            checker = d.popleft()
        else:
            d.append(hand+" ")
            checker = d.popleft()
    else:
        prog += 1
        if hand == "r":
            count += P
        elif hand == "s":
            count += R
        else:
            count += S
        if prog >= K:
            checker = d.popleft()
print(count)

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

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

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

for i in range(k):
    j = 0
    while j <= ((n-i-1)//k -1):        
        if (t[i+k*j]==t[i+k*(j+1)]):
            ans += point(t[i+k*j])
            j += 2
        else:
            ans += point(t[i+k*j])
            j += 1
    if j == (n-i-1)//k:
        ans += point(t[i+k*j])
print(ans)

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,k = map(int, input().split())
    r,s,p = map(int, input().split())
    t = list(input().rstrip())
    score = []
    for i in t:
        if i == 'r':
            score.append(p)
        if i == 's':
            score.append(r)
        if i == 'p':
            score.append(s)

    res = sum(score)
    for i in range(k, n):
        if t[i] == t[i-k]:
            res -= score[i]
            t[i] = 'u'
    print(res)

if __name__ == '__main__':
    main()

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

dic = {'r':p, 's':r, 'p':s}
score = 0
for i in range(n):
    if t[i] == 0:continue
    score += dic[t[i]]
    if i + k < n and t[i + k] == t[i]:
        t[i + k] = 0
print(score)

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

for i in range(K):
    a = i
    prev = ''
    while a < N:
        if prev == 'r' and T[a] == 's' or \
                prev == 's' and T[a] == 'p' or \
                prev == 'p' and T[a] == 'r':
            prev = ''
        else:
            if T[a] == 's':
                prev = 'r'
                res += R
            if T[a] == 'p':
                prev = 's'
                res += S
            if T[a] == 'r':
                prev = 'p'
                res += P
        a += K

print(res)

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

for i,x in enumerate(T):
    k = i % K
    k_to_word[k].append(x)
    
def solve(A):
    
    r,s,p = 0,0,0 
    for x in A:
        
        if x == 'r':
            
            
            r,s,p = max(s,p),max(r,p),max(r,s)+P
        elif x == 's':
            r,s,p = max(s,p)+R,max(r,p),max(r,s)
        elif x == 'p':
            r,s,p = max(s,p),max(r,p)+S,max(r,s)   
    
    return max(r,s,p)
 
answer = sum(solve(A) for A in k_to_word)
print(answer)

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

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

wT = []
p = {"p":P, "s":S, "r":R}
ans = 0

for t in range(N):
    for i, j in zip(["r", "s", "p"], ["p", "r", "s"]):
        if T[t] == i:
            wT.append(j)
            break
    if t - K >= 0:
        if wT[t - K] == wT[-1]:
            wT[-1] = "x"
            continue
        else:
            ans += p[wT[-1]]
    else:
        ans += p[wT[-1]]
print(ans)

Solution (Python):
import math
from functools import reduce
import itertools

def input1():
	return int(input())


def input2():
	return map(int,input().split())


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






n,k=input2()
r,s,p=input2()
t=str(input())
V = {'r':p, 's':r, 'p':s}

ans_list=[]
ans=0

for i in range(k):
	check=t[i]
	ans+=V[check]
	for _ in range(n):
		i+=k
		if i>=n:
			break
		elif check==t[i]:
			check="-1"
		else:
			check=t[i]
			ans+=V[t[i]]

print(ans)

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


print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = [_ for _ in input()]
d = {'r':p,"s":r,"p":s}
ans = 0
t_hand = []
for i,x in enumerate(t):
    if i-k<0:
        ans += d[x]
        t_hand.append(d[x])
        continue
    if (t[i-k] == x)and(t_hand[i-k]!=0):
        ans += 0
        t_hand.append(0)
    else:
        ans += d[x]
        t_hand.append(d[x])
        
print(ans)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
t = input()
r,s,p=t.count('s'),t.count('p'),t.count('r')
ans=r*R+s*S+p*P
count=[1]*k
for i in range(k,n):
  if t[i-k]==t[i]:
    count[i%k]+=1
  else:
    if t[i-k]=='r':
      ans-=(count[i%k]//2)*P
    elif t[i-k]=='s':
      ans-=(count[i%k]//2)*R
    elif t[i-k]=='p':
      ans-=(count[i%k]//2)*S
    count[i%k]=1
for i in range(n-k,n):
  if t[i-k]=='r':
    ans-=(count[(i)%k]//2)*P
  elif t[i-k]=='s':
    ans-=(count[(i)%k]//2)*R
  elif t[i-k]=='p':
    ans-=(count[(i)%k]//2)*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(k):
    A = []
    j = i
    l = 0
    while(k*l+j < n):
        A.append(t[k * l + j])
        l += 1
    
    begin = 0
    A.append('o')
    while begin < len(A)-1:
        if A[begin] == A[begin + 1]:
            if A[begin] == 'r':
                ans += p
            elif A[begin] == 's':
                ans += r
            else:
                ans += s
            begin += 2
        else:
            if A[begin] == 'r':
                ans += p
            elif A[begin] == 's':
                ans += r
            else:
                ans += s
            begin += 1
print(ans)

Solution (Python):
battle_times, restriction = [int(n) for n in input().rstrip().split()]

win_points = [int(n) for n in input().rstrip().split()]

opp_hand_my_point = {'s':win_points[0], 'p':win_points[1], 'r':win_points[2]}

opp_hand_patterns = input().rstrip()
total = 0

for i in range(restriction):
    pre_opp_hand = opp_hand_patterns[i]
    total+=opp_hand_my_point[pre_opp_hand]

    for _ in range(battle_times):
        i+=restriction
        if i>=battle_times:
            break
        if pre_opp_hand==opp_hand_patterns[i]:
            pre_opp_hand='-1'
        else:
            total+=opp_hand_my_point[opp_hand_patterns[i]]
            pre_opp_hand = opp_hand_patterns[i]
print(total)

Solution (Python):
N, K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
V = {'r':P, 's':R, 'p':S}
ans = 0
 
 
for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Solution (Python):


N,K=map(int,input().split())
R,S,P=map(int,input().split())
pnt={'r':R,'s':S,'p':P}
WIN={'s':'r','p':'s','r':'p'}
T=input()
ans=0
for k in range(K):
    t=list(T[i] for i in range(k,N,K))
    win=[WIN[x] for x in t]
    
    prev=win[0]
    cnt=1
    for i in range(1,len(t)):
        if prev==win[i]:
            cnt+=1
        else:
            ans+=((cnt+1)//2)*pnt[prev]
            
            cnt=1
            prev=win[i]
    
    ans+=((cnt+1)//2)*pnt[prev]
    
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
d = {'r':r, 's':s, 'p':p}
t = input()
def janken(x):
    if x == 'r':
        return 'p'
    elif x == 's':
        return 'r'
    else:
        return 's'
l = ['']*k
for i in range(n):
    l[i%k] += t[i]
ans = 0
for i in l:
    dp = [janken(i[0])] + ['-1']*(len(i)-1)
    ans += d[janken(i[0])]
    for j in range(1, len(i)):
        if janken(i[j]) != dp[j-1]:
            ans += d[janken(i[j])]
            dp[j] = janken(i[j])
        else:
            dp[j] = '0'
print(ans)

Solution (Python):
n,k = map(int,input().split())
R,S,P = map(int,input().split())
t = input()
mod = []
for i in range(k):
    mod.append(t[i::k])
ans = 0
for i in range(k):
    lis = mod[i]
    dp = [0]*(len(lis)+1)
    if lis[0]=='r':
        dp[1] = P
    elif lis[0]=='p':
        dp[1] = S
    elif lis[0]=='s':
        dp[1] = R
    for j in range(1,len(lis)):
        if lis[j]=='r':
            dp[j+1] = P+dp[j]
        elif lis[j]=='p':
            dp[j+1] = S+dp[j]
        elif lis[j]=='s':
            dp[j+1] = R+dp[j]
        if lis[j-1] == lis[j]:
            dp[j+1] = dp[j]
            if j != len(lis)-1:
                lis = lis[:j]+'o'+lis[j+1:]
    ans += dp[-1]
print(ans)

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

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
x = n // k+1
ans = 0
for i in range(k):
    ph = 0
    for j in range(x):
        if i + j * k >= n:
            break
        nh = t[i + j * k]
        if ph == nh:
            ph = 0
            continue
        elif nh == "r":
            ans += p
        elif nh == "s":
            ans += r
        elif nh == "p":
            ans += s
        ph = nh

print(ans)

Solution (Python):
import collections

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

convert_information=information[:K]
CNT_information = collections.Counter(information[:K])

rock = R * CNT_information['s']
sciss = S * CNT_information['p']
par = P * CNT_information['r']

total = rock + sciss + par

for i in range(N - K):
    index = i + K
    point = 0
    
    if (convert_information[i] == information[index]):
        convert_information.append('x')
    
    else:
        hand = information[index]

        if (hand == 's'):
            point = R
            convert_information.append('s')

        elif (hand == 'p'):
            point = S
            convert_information.append('p')

        else:
            point = P
            convert_information.append('r')
    
    total += point


print(total)

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

def p(a):
  if a == 'r':
    return P
  elif a == 's':
    return R
  else:
    return S

A = [0]*N
for i in range(K):
  A[i] = p(T[i])

for i in range(K):
  for j in range(1,((N-1-i)//K)+1):
    if T[K*j+i] != T[K*(j-1)+i] or A[K*(j-1)+i] == 0:
      A[K*j+i] = p(T[K*j+i])

print(sum(A))

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

def p(a):
  if a == 'r':
    return P
  elif a == 's':
    return R
  else:
    return S

A = [0]*N
for i in range(K):
  A[i] = p(T[i])

for i in range(K):
  for j in range(1,((N-1-i)//K)+1):
    if T[K*j+i] != T[K*(j-1)+i] or A[K*(j-1)+i] == 0:
      A[K*j+i] = p(T[K*j+i])

print(sum(A))

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




hand_points = {'r':P, 's':R, 'p':S}
ans = 0
for i in range(k):
    hands = t[i::k]
    tmp = ''
    for hand in hands:
        if hand == tmp:
            tmp = ''
        else:
            ans+= hand_points[hand]
            tmp = hand

print(ans)

Solution (Python):
n, k = (int(x) for x in input().split())
r, s, p = (int(x) for x in input().split())
t_ch = input()
t=[]
for i in range(n):
    t.append(t_ch[i])

for i in range(k, n):
    if t[i] == t[i-k]:
        t[i]=0
for i in range(0,n):
    if t[i]=="r":
        t[i]=p
    elif t[i]=="p":
        t[i]=s
    elif t[i]=="s":
        t[i]=r
print(sum(t))

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

def calc_dp(st):
    len_st = len(st)
    dp = [[0] * 3 for _ in range(len_st + 1)]
    for ind, i in enumerate(st, 1):
        dp[ind][0] = max(dp[ind-1][1],dp[ind-1][2])
        dp[ind][1] = max(dp[ind-1][0],dp[ind-1][2])
        dp[ind][2] = max(dp[ind-1][0],dp[ind-1][1])
        if(i=='r'):
            dp[ind][2] += p
        elif(i=='s'):
            dp[ind][0] += r
        else:
            dp[ind][1] += s

    return max(dp[-1])

ans = 0
for i in range( k ):
    if( i >= n ):
        break
    st = t[i::k]
    ans += calc_dp(st)

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
d = {'r':r, 's':s, 'p':p}
t = input()
def janken(x):
    if x == 'r':
        return 'p'
    elif x == 's':
        return 'r'
    else:
        return 's'
l = ['']*k
for i in range(n):
    l[i%k] += t[i]
ans = 0
for i in l:
    pre = i[0]
    ans += d[janken(i[0])]
    for j in range(1, len(i)):
        if i[j] != pre:
            ans += d[janken(i[j])]
            pre = i[j]
        else:
            pre = ''
print(ans)

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

dic = {"r":P, "s":R, "p":S}
points = 0

for st in range(K):
    series = T[st::K]
    prev = "x"
    for now in series:
        if now != prev:
            points += dic[now]
            prev = now
        else:
            prev = "x"
            
print(points)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  r,s,p=LI()
  t=S()

  dp=[[0]*3 for _ in range(n+1)]
  for i in range(n):
    a=t[i]
    b=max(dp[i])
    for j in range(3):
      dp[i+1][j]=b
    if i+1<=k:
      if a=='r':
        dp[i+1][2]+=p
      elif a=='s':
        dp[i+1][0]+=r
      else:
        dp[i+1][1]+=s

    else:
      _a,_b,_c=dp[i+1-k]
      if a=='r':
        if not (_c>_a and _c>_b):
          dp[i+1][2]+=p
      elif a=='s':
        if not (_a>_b and _a>_c):
          dp[i+1][0]+=r
      else:
        if not (_b>_a and _b>_c):
          dp[i+1][1]+=s

  
  return max(dp[n])


print(main())