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

ans = 0

def janken(r: int, s: int, p: int, i: str):
    if i == 'p':
        return s, 's'
    elif i == 'r':
        return p, 'p'
    elif i == 's':
        return r, 'r'

for i in range(K):
    sans = janken(R, S, P, T[i])
    tmp = sans[1]
    ans += sans[0]
    
    
    for j in range(i + K, N, K):
        
        sans = janken(R, S, P, T[j])
        if tmp != sans[1]:
            ans += sans[0]
            tmp = sans[1]
        else:
            tmp = ''
        
        
print(ans)

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

TKlist=[]
for i in range(K):
  TKlist.append([])  

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


answer=0
for i in range(K):
  lastChar="
  for j in range(len(TKlist[i])):
    if TKlist[i][j]!=lastChar:
      lastChar=TKlist[i][j]
      if TKlist[i][j]=='r':        
        answer+=P
      elif TKlist[i][j]=='s':        
        answer+=R
      elif TKlist[i][j]=='p':        
        answer+=S
    else:
      lastChar="
      answer+=0

print(answer)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
W = [2]*N
list =(R,S,P)
list_2 = sorted(list)
count = 0
for i in range (0,3):
	if list_2[2-i] == P:
		for j in range (0,N):
			if T[j] == 'r':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 'r' :
							W[j+K] = 0
	if list_2[2-i]== R:
		for j in range (0,N):
			if T[j] == 's':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 's' :
							W[j+K] = 0
	if list_2[2-i]== S:
		for j in range (0,N):
			if T[j] == 'p':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 'p' :
							W[j+K] = 0

for i in range (N):
	if T[i] == 'r':
		count += P*W[i]
	elif  T[i] == 's':
		count += R*W[i]
	elif  T[i] == 'p':
		count += S*W[i]

print(count)

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

log = list()
ans = 0
for i in range(n):
  if i > k-1:
    if t[i] == log[i-k]:
      ans += 0
      log.append("null")
    else:
      log.append(t[i])
      if t[i] == "r":
        ans += p
      if t[i] == "s":
        ans += r
      if t[i] == "p":
        ans += s  
  else:
    log.append(t[i])
    if t[i] == "r":
      ans += p
    if t[i] == "s":
      ans += r
    if t[i] == "p":
      ans += s  
print(ans)

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

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

hands = sorted(
    hand.items(), key=lambda x: x[1],
    reverse=True
)
r = np.zeros(n)
ans = 0
for j in range(3):
    a = hands[j][0]
    b = hands[j][1]
    for i in range(n):
        if t[i] == a:
            if t[i-k] == a and i+1 > k and r[i-k] == 1:
                continue
            else:
                ans += b
                r[i] = 1
        else:
            continue
print(ans)

Solution (Python):


str1 = input().split(" ")
str2 = input().split(" ")
t = list(input())

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

result = 0
hoge = 0
for i in range(0,n):
    if i < k:
        continue

    j = i-k
    if t[i] == t[j]:
        t[i] = "_"

pres = p * t.count("r")
rres = r * t.count("s")
sres = s * t.count("p")

print(pres + rres + sres)

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
win_hand = {k:v for k, v in zip("r s p".split(), "p r s".split())}


def solve(pt_dict, pred, cur, prev, memo):
  if cur == len(pred):
    return 0
  elif (cur, prev) in memo:
    return memo[(cur, prev)]
  else:
    tmp = 0
    for hand in "r s p".split():
      if prev == hand:
        continue
      else:
        pt = 0
        if hand == win_hand[pred[cur]]:
          pt = pt_dict[hand]
        tmp = max(tmp, pt+solve(pt_dict, pred, cur+1, hand, memo))
    memo[(cur, prev)] = tmp
    return tmp
    
N, K = map(int, input().split())
R, S, P = map(int, input().split())
pt_dict = {}
pt_dict["r"] = R
pt_dict["s"] = S
pt_dict["p"] = P
T = list(input())
k = 0
ans = 0
while k < K:
  ans += solve(pt_dict, T[k::K], 0, "", {})
  k += 1
print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    rsp = list(map(int, input().split()))
    ts = list(input())

    points = [0] * n
    for i, t in enumerate(ts):
        prev_idx = i - k
        if prev_idx >= 0:
            prev_t = ts[prev_idx]
            if prev_t == t:
                if points[prev_idx] == t:
                    pass
                else:
                    points[i] = t
                    continue
        if t == 'r':
            point = rsp[2]
        elif t == 's':
            point = rsp[0]
        else:
            point = rsp[1]
        points[i] = point

    ans = 0
    for p in points:
        if type(p) == int:
            ans += p
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
R,S,P  =  map(int,input().split())
t = input()
s = []
for i in range(N):
    s.append(t[i])
ans = 0
for i in range(K):
    if s[i] == 'r':
        ans += P
        s[i] = 'a'
    elif s[i] == 's':
        ans += R
        s[i] = 'b'
    else:
        ans += S
        s[i] = 'c'
T = []
for i in range(K,N):
    if s[i] == 'r':
        if s[i-K] != 'a':
            ans += P
            s[i] = 'a'
    elif s[i] == 's':
        if s[i-K] != 'b':
            ans += R
            s[i] = 'b'
    else:
        if s[i-K] != 'c':
            ans += S
            s[i] = 'c'
print(ans)

Solution (Python):
n, k = list(map(int,input().split()))
r, s, p = list(map(int,input().split()))
t = list(input())
i = 0
sum = 0
while i < n:
  if i < k:
    if t[i] == 'r':
      sum = sum + p
    elif t[i] == 's':
      sum = sum + r
    else:
      sum = sum + s
  else:
    if t[i] != t[i-k]:
      if t[i] == 'r':
        sum = sum + p
      elif t[i] == 's':
        sum = sum + r
      else:
        sum = sum + s 
    else:
      t[i] = 'U'
  i = i + 1

print(sum)

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

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

def score(s):
	if s=='r': return P
	if s=='s': return R
	if s=='p': return S
	return 0

X=[""]*K
for i in range(K):
	X[i]=T[i::K]

p(X)

def max_score(x):
	a=0
	c=''
	A=x
	for s in A:
		if c==s:
			c=''
		else:
			a+=score(s)
			c=s

	
	
	
	
	
	
	
	
	
	
	
	
	
	return a
	
	

ANS=[0]*K
for i,x in enumerate(X):
	ANS[i] = max_score(x)

ans=sum(ANS)
print(ans)

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

count=0
m=n//k

for i in range(k):
    flag=False
    for j in range(m+1):
        if j==0:
            if t[i] == 'r':
                count += P
            elif t[i] == 's':
                count += R
            else:
                count += S
        else:
            if i+k*j>=n:
                break

            if t[i+k*j]==t[i+k*(j-1)]:
                if flag==False:
                    flag=True
                    continue
                else:
                    flag=False
                    if t[i+k*j] == 'r':
                        count += P
                    elif t[i+k*j] == 's':
                        count += R
                    else:
                        count += S

            else:
                flag=False
                if t[i+k*j] == 'r':
                    count += P
                elif t[i+k*j] == 's':
                    count += R
                else:
                    count += S




print(count)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
data=[[] for i in range(K)]
for i in range(N):
    zzz=i%K
    u=T[i]
    data[zzz].append(u)
ans=0
for h in data:
    l=len(h)
    dp=[[0,0,0] for i in range(l)]
    u=h[0]
    if u=="r":
        dp[0][2]=P
    elif u=="s":
        dp[0][0]=R
    else:
        dp[0][1]=S
    for i in range(1,l):
        u=h[i]
        dp[i][0]=max(dp[i-1][1],dp[i-1][2])
        dp[i][1]=max(dp[i-1][0],dp[i-1][2])
        dp[i][2]=max(dp[i-1][1],dp[i-1][0])
        if u=="r":
            dp[i][2]+=P
        elif u=="s":
            dp[i][0]+=R
        else:
            dp[i][1]+=S
    ans+=max(dp[-1])
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
kr = 1
ks= 1
kp = 1
Is = 0
Ir=0
Ip=0
A =[]

for i in range(n):
  if i-k < 0:
      A.append(1)
      if t[i] == "r":
        kr = 1
        ans += p
      elif t[i] == "s":
        ks=1
        ans += r
      else:
        kp=1
        ans += s
  else:
    if t[i] != t[i-k]:
      if t[i] == "r":
        kr=1
        ans += p
        Ir = i
        A[i%k]=1
      elif t[i] == "s":
        ans += r
        ks=1
        Is = i
        A[i%k]=1
      else:
        kp=1
        ans += s
        A[i%k]=1
    else:
      if t[i] == "r":
        if A[i%k] == 0:
          ans+=p
          A[i%k] = 1
        else:
            A[i%k]  = 0
      elif t[i] == "s":
        if A[i%k] ==0:
          ans += r
          A[i%k] = 1
        else:
            A[i%k] = 0
      else:
        if A[i%k]==0:
          ans+=s
          kp=1
          Ip = i
          A[i%k] = 1
        else: 
            A[i%k] = 0
      
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
points = {'r':R, 's':S, 'p':P}
win = {'r':'p', 's':'r', 'p':'s'}

batch = [T[i::K] for i in range(K)]

ans = 0
for b in batch:
    tmp = 0
    for p in 'rsp':
        t = 0
        before = p
        if p == win[b[0]]: t += points[win[b[0]]]
        for i in range(1,len(b)):
            if win[b[i]] != before:
                t += points[win[b[i]]]
                before = win[b[i]]
            else:
                if i+1 < len(b):
                    before = b[i+1]
        if t > tmp: tmp = t
    ans += tmp
print(ans)

Solution (Python):
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ans = t.count("r") * p + t.count("s") * r + t.count("p") * s
R,S,P = 0,0,0
ok = [0] * (n+1)
for key,(i,j) in enumerate(zip(t[:-k],t[k:])):
  if i == j:
    if i == "r":
      if ok[key] == 0:
        ans -= p
        ok[key+k] = 1
    elif i == "s":
      if ok[key] == 0:
        ans -= r
        ok[key+k] = 1
    else:
      if ok[key] == 0:
        ans -= s
        ok[key+k] = 1
print(ans)

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

ts=[t[i:len(t):k] for i in range(k)]
def score_str(l):
  res=0
  stt=0
  for i,c1,c2 in zip(range(len(l)),l,l[1:]+' '):
    if c1!=c2:
      res+=scores[c1]*((i-stt+2)//2)
      stt=i+1
  return res
print(sum(score_str(l) for l in ts))

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

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
d = {'p': S, 's': R, 'r': P}
flag = [0] * K
for n in range(N):
    q = n % K
    ans += d.get(T[n])
    if n >= K:
        if T[n] == T[n - K] and n < N - K:
            flag[q] += 1
        elif (flag[q] != 0 and T[n] != T[n - K]) or n >= N - K:
            if n >= N - K and T[n] == T[n - K]:
                flag[q] += 1
            ans -= d.get(T[n - K]) * ((flag[q] + 1) // 2)
            flag[q] = 0
print(ans)

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

Solution (Python):

n,k = (int(_) for _ in input().split())
R,S,P = (int(_) for _ in input().split())
t = list(input())
a ={"r":P,"s":R,"p":S,"f":0}
Q = n//k
q = n%k
ans = 0
for i in range(n):
	ans+=a[t[i]]

for i in range(k,n):
	if t[i]==t[i-k]:
		ans-=a[t[i-k]]
		if t[i-k] is not "f":
			t[i]="f"
print(ans)

Solution (Python):
a,b=map(int,input().split())
c,d,e=map(int,input().split())
g=input()
l = a-b
for i in range(l):
   if g[i] == g[i+b]:
      g = g[:i+b] + 'a' + g[i+1+b:]
      
else:
  R = g.count('r')
  S = g.count('s')
  P = g.count('p')
  answer = c*S+d*P+e*R
  print(answer)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
u = []
a = 0
for i in range(n):
    if i <= k-1:
        if t[i] == "r":
            u.append("p")
            a += p
        elif t[i] == "s":
            u.append("r")
            a += r
        else:
            u.append("s")
            a += s
    else:
        if t[i] == "r" and u[i-k] != "p":
            u.append("p")
            a += p
        elif t[i] == "s" and u[i-k] != "r":
            u.append("r")
            a += r
        elif t[i] == "p" and u[i-k] != "s":
            u.append("s")
            a += s
        elif t[i] == "r" and u[i-k] == "p":
                u.append("_p")
        elif t[i] == "s" and u[i-k] == "r":
            u.append("_r")
        elif t[i] == "p" and u[i-k] == "s":
            u.append("_s")
print(a)

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

def sc(S):
  if S=='r':
    return p
  elif S=='s':
    return r
  else:
    return s

def bchk(i):
  score=0
  stnum=int(i//k)
  sta=int(i%k)
  if stnum==0:
    return sc(t[i])
  else:
    score=bnum=sc(t[sta])
    for j in range(1,stnum+1):
      if t[sta+j*k]==t[sta+(j-1)*k]:
        if bnum==0:
          bnum=sc(t[sta+j*k])
          score+=bnum
        else:
          bnum=0
          continue
      else:
        bnum=sc(t[sta+j*k])
        score+=bnum
    return score

def achk(i):
  score=0
  stnum=int(i//k)
  if stnum==0:
    return sc(t[i])
  else:
    score=bnum=sc(t[i])
    for j in range(1,stnum+1):
      if t[i-j*k]==t[i-(j-1)*k]:
        if bnum==0:
          bnum=sc(t[i-j*k])
          score+=bnum
        else:
          bnum=0
          continue
      else:
        bnum=sc(t[i-j*k])
        score+=bnum
    return score

for i in range(n):
  if sco[n-1-i]==0:
    ans+=max(achk(n-1-i),bchk(n-1-i))
    j=0
    while n-1-i-j*k>=0:
      sco[n-1-i-j*k]=1
      j+=1
print(ans)

Solution (Python):
n,k= map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
takahashi = []
Sum = 0
for i in range(k):
    if t[i] == "r":
        takahashi.append("p")
    if t[i] == "p":
        takahashi.append("s")
    if t[i] == "s":
        takahashi.append("r")
for i in range(k,len(t)):
    if t[i] == "r":
        if takahashi[i-k] != "p":
            takahashi.append("p")
        else:
            takahashi.append("0")
    if t[i] == "p":
        if takahashi[i-k] != "s":
            takahashi.append("s")
        else:
            takahashi.append("0")
    if t[i] == "s":
        if takahashi[i-k] != "r":
            takahashi.append("r")
        else:
            takahashi.append("0")
for i in range(len(takahashi)):
    if takahashi[i] == "0":
        Sum += 0
    if takahashi[i] == "r":
        Sum += r
    if takahashi[i] == "p":
        Sum += p
    if takahashi[i] == "s":
        Sum += s
  
print(Sum)

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

for i in range(N):
    if i >= K and T[i] == T[i-K]:
        T[i] = 'zero'

for l in range(N):
    if T[l] == 'r':
        ans += P
    elif T[l] == 's':
        ans += R
    elif T[l] == 'p':
        ans += S

print(ans)

Solution (Python):
def get_point(machine_hand, R, S, P):
    """ 筐体がmachine_handを出した場合，勝った時に得られるポイント
    """
    if machine_hand == 'r':
        return P
    elif machine_hand == 's':
        return R
    elif machine_hand == 'p':
        return S



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

    points = 0
    used_trials = set()
    for i in range(0, len(T)):
        if i < K:
            
            points += get_point(T[i], R, S, P)
            used_trials.add(i)
        else:
            if T[i] == T[i - K] and i - K in used_trials:
                continue
            else:
                
                points += get_point(T[i], R, S, P)
                used_trials.add(i)
        
    print(points)


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = list()
for i in range(k):
    for j in range(int((n - i - 1) / k) + 1):
        if t[j * k + i] == "r":
            b = "p"
        elif t[j * k + i] == "s":
            b = "R"
        else:
            b = "S"
        a.append(b)
    a.append("/")
    
c = 2
ans = 0
e = 0
for i in range(len(a) - 1):
    if a[i] == a[i + 1]:
        c += 1
        if a[i] == "R":
            d = r
        elif a[i] == "S":
            d = s
        else:
            d = p
        e = 1
    elif a[i] != a[i + 1] and e == 1:
        ans += int(c / 2) * d
        c = 2
        d = 0
        e = 0
    elif a[i] != a[i + 1] and a[i] != "/":
        if a[i] == "R":
            f = r
        elif a[i] == "S":
            f = s
        else:
            f = p
        ans += f
        
print(ans)

Solution (Python):
def main():
    n , k = map(int,input().split())
    r, s, p = map(int,input().split())
    t = list(input())
    score = 0
    te = []

    for count, i in enumerate(t):
        if count < k :
            if i == 'r' :
                score += p
                te.append('p')
            elif i == 's' :
                score += r
                te.append('r')
            elif i == 'p' :
                score += s
                te.append('s')
        else:
            if i == 'r' and  te[-k] != 'p':
                score += p
                te.append('p')
            elif i == 's' and te[-k] != 'r':
                score += r
                te.append('r')
            elif i == 'p' and te[-k] != 's':
                score += s
                te.append('s')
            else:
                te.append('0')
    print(score)

if __name__ == '__main__':
    main()

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

ans = []
point = 0
for t in T[:K]:
    if t == 'r':
        ans.append(('p', True))
        point += P
    elif t == 's':
        ans.append(('r', True))
        point += R
    else:
        ans.append(('s', True))
        point += S

for i, t in enumerate(T[K:]):
    if t == 'r':
        if ans[i][0] == 'p' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('p', True))
            point += P
    elif t == 's':
        if ans[i][0] == 'r' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('r', True))
            point += R
    else:
        if ans[i][0] == 's' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('s', True))
            point += S

print(point)

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

from itertools import groupby
from math import ceil

answer = 0
for i in range(K):
    for key,group in groupby(T[i::K]):
        if key == "r":
            answer += P * ceil(len(list(group))/2)
        elif key == "s":
            answer += R * ceil(len(list(group))/2)
        elif key == "p":
            answer += S * ceil(len(list(group))/2)

print(answer)

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

for i in range(n):
  if t[i]=="r":
    mine.append("p")
  elif t[i]=="s":
    mine.append("r")
  elif t[i]=="p":
    mine.append("s")
  
for i in range(len(mine)-k):
  if mine[i]==mine[i+k]:
    mine[i+k] = "x"
    
ans = mine.count("r") * r + mine.count("s") * s + mine.count("p") * p
print(ans)

Solution (Python):
N,K = map(int, input().split(' '))
R,S,P = map(int, input().split(' '))
T = input()
win={'r':'p', 's':'r', 'p':'s'}
progress=''
for i,v in enumerate(T):
    if i<K:   progress+=win[v]
    else:
        if win[v]!=progress[i-K]:
            progress+=win[v]
        else:
            progress+='x'
print(progress.count('r')*R + progress.count('s')*S + progress.count('p')*P)

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

tmp = 1
mod = 1
score =0

while mod <= K:
    i = 0
    while i * K + mod - 1 < len(T):
        if (i + 1) * K + mod - 1 >= len(T):
            if T[i * K + mod - 1] == 'r':
                score += P
            elif T[i * K + mod - 1] == 's':
                score += R
            else:
                score += S
            i += 1
        else:
            if T[i * K + mod - 1] != T[(i + 1) * K + mod - 1]:
                if T[i * K + mod - 1] == 'r':
                    score += P
                elif T[i * K + mod - 1] == 's':
                    score += R
                else:
                    score += S
                i += 1
            else:
                if T[i * K + mod - 1] == 'r':
                    score += P
                elif T[i * K + mod - 1] == 's':
                    score += R
                else:
                    score += S
                i += 2
    mod += 1

print(score)

Solution (Python):

n,k = [int(i) for i in input().split()]
r,s,p = [int(i) for i in input().split()]
t = input()
res = 0
for i in range(0,k):
    pre = True
    for j in range(0,n//k+1):
        index = j*k + i
        if index < n:
            if pre:
                if t[index] == 'r':
                    res += p
                elif t[index] == 's':
                    res += r
                else:
                    res += s
                pre = False
            else:
                if t[index] == t[index-k]:
                    pre = True
                else:
                    if t[index] == 'r':
                        res += p
                    elif t[index] == 's':
                        res += r
                    else:
                        res += s
print(res)

Solution (Python):
N, K = map(int, input().strip().split())
R, S, P = map(int, input().strip().split())
score = {}
score['r'] = P
score['s'] = R
score['p'] = S
T = input().strip()

i = 0
adptl = []
res = 0
while i < N:
    if i-K >= 0 and T[i-K] == T[i] and adptl[i-K] == 1:
        i += 1
        adptl.append(0)
        continue
    res += score[T[i]]
    adptl.append(1)
    i += 1

print(res)

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    S, T = input().split()

    print(T+S)

    return




def B():
    A, B, K = LI()
    temp = A-K
    if temp >= 0:
        print(temp, B)
        return
    B = B+temp
    if B <= 0:
        print(0, 0)
        return
    else:
        print(0, B)

    return




def true():
    S = input()
    S = S[:-1]

    tmp = 0
    for x in range(1 << (len(S)-1)):
        func = S[-1]
        for i in range(len(S)-1):
            if(x & (1 << i)):
                tmp += int(func)
                func = S[-2-i]
            else:
                func = S[-2-i]+func
        tmp += int(func)
    print(tmp)

    return


def flagcounter(x):
    if (x == 0):
        return 0
    return flagcounter(x >> 1)+(x & 1)


def C():
    X = II()
    if X == 2:
        print(2)
        return
    while(1):
        if sosu(X):
            print(X)
            return
        X += 1

    return



def D():
    N, K = LI()
    R, S, P = LI()
    T=list(input().rstrip())
    ans = 0
    for j in range(K):
        if T[j] == 'r':
            ans += P
        if T[j] == 'p':
            ans += S
        if T[j] == 's':
            ans += R
    j = 0
    rps=['r','p','s']

    for i in range(K, N):
        if T[j] == T[i]:
            if i+K>=N:
                j += 1
                continue
            T[i]=rps[rps.index(T[i+K])-1]
            j += 1
            continue
        if T[i] == 'r':
            ans += P
        if T[i] == 'p':
            ans += S
        if T[i] == 's':
            ans += R
        j += 1
    print(ans)
    return


def Dsyakutori():

    N, K = LI()
    A = LI()
    MAX = sum(A)
    if MAX < K:
        print(0)
        return
    Temp = MAX
    ans = 1
    for i in range(N):
        Temp = Temp-A[i]
        if (Temp >= K):
            ans += 1
        else:
            T = Temp+A[i]
            break
    Temp = T
    ckeep = N
    for k in range(N):
        c = N-1
        i -= 1
        Temp = T

        for j in range(N):
            Temp = Temp-A[c]
            if (Temp >= K):
                ans += 1

            elif(c < ckeep and i >= 0):
                ans += i
                ckeep = c
                break
            else:
                ckeep = c
                break
            c -= 1
        if i < 0:
            break
        T = T+A[i]
    print(ans)

    return




def E():
    N, K = LI()
    A = LI()
    F = LI()
    A.sort(reverse=True)
    F.sort()
    cost = []
    for i in range(len(N)):
        cost.append(A[i]*F[i])

    a = F[cost.index(max(cost))]

    return




def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    



def solution(S):
    
    N = len(S)

    pass



if __name__ == '__main__':

    D()

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

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

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

win_hand = ''
for ti in t:
  win_hand += win[ti]

ans = 0
same_flag = [False]*n

for i in range(k):
  ans += point[win_hand[i]]

i=k
while i<n:

  if win_hand[i] == win_hand[i-k]:
    if not same_flag[i-k]:
      same_flag[i]=True
    else:
      ans += point[win_hand[i]]
  else:
    ans += point[win_hand[i]]
  i+=1

print(ans)

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

t1 = t.replace("r","P")
t2 = t1.replace("s","R")
t3 = t2.replace("p","S")


T = list(t3)

minus = 0
ls = ["R","S","P"]
for i in range(k,n):
    if i < n-k:
        tmp = T[i]
        before = T[i-k]
        after = T[i+k]
        if tmp == before:
            ls.remove(tmp)
            if tmp == after:
                T[i] = ls[0]
            else:
                ls.remove(after)
                T[i] = ls[0] 
                ls.append(after)
            ls.append(tmp)
            if T[i] == "R":
                minus += r
            elif T[i] == "S":
                minus += s
            elif T[i] == "P":
                minus += p
        else:
            pass
    elif i >= n-k:
        tmp = T[i]
        before = T[i-k]
        
        if tmp == before:
            ls.remove(tmp)
            T[i] = ls[0]
            ls.append(tmp)
            if T[i] == "R":
                minus += r
            elif T[i] == "S":
                minus += s
            elif T[i] == "P":
                minus += p
        else:
            pass


p1 = r * T.count("R")

p2 = s * T.count("S")

p3 = p * T.count("P")



print(p1+p2+p3-minus)