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):
num, back = map(int, input().split(' '))
r_point, s_point, p_point = map(int, input().split(' '))
t = input()
j_dict = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': r_point, 's': s_point, 'p': p_point, 'e': 0}
result = [j_dict[i] for i in t]
for i in range(back, num):
    if result[i] == result[i - back]:
        result[i] = 'e'
print(sum(score[i] for i in result))

Solution (Python):
n, k = tuple(map(int, input().split(' ')))
r, s, p = tuple(map(int, input().split(' ')))
t = (input())
point = {'r':p, 's':r, 'p':s, 'any':0}
st = {i:t[i] for i in range(k)}
ans1 = sum(map(lambda x: point[x], t[:k]))
ans2 = 0
for i in range(k, n):
    h = t[i]
    m = i%k
    if st[m] != h:
        ans2 += point[h]
        st[m] = h
    else:
        st[m] = 'any' 
print(ans1 + ans2)

Solution (Python):
N, K = [int(_) for _ in input().split()]
points = [int(_) for _ in input().split()]
T = list(input())

PAT = ['r', 's', 'p']

ans = 0
for k in range(K):
    pre = -1
    for i in range(k, N, K):
        win_i = (PAT.index(T[i]) + 2) % 3
        if pre != win_i:
            ans += points[win_i]
            pre = win_i
        else:
            pre = -1

print(ans)

Solution (Python):
n, k = tuple(map(int, input().split(' ')))
r, s, p = tuple(map(int, input().split(' ')))
t = (input())
def char2int(c):
    if c == 'r':
        return p
    elif c == 's':
        return r
    else:
        return s
st = {i:t[i] for i in range(k)}
ans1 = sum(map(char2int, t[:k]))
ans2 = 0
for i in range(k, n):
    h = t[i]
    m = i%k
    if st[m] != h:
        ans2 += char2int(h)
        st[m] = h
    else:
        st[m] = 'any' 
print(ans1 + ans2)

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,N):
    if T[i] == T[i-K]:
        T[i] = 'n'
score = {'r':P,'s':R,'p':S,'n':0}
for i in range(N):
    ans += score[T[i]]
print(ans)

Solution (Python):
def calc(x):
    if x == 'r': n = P
    elif x == 's': n = R
    else: n = S
    
    return n

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

T = input()

ans = 0
prev = [False] * K

for i in range(N):
    if i < K: ans += calc(T[i])
    else:
        if T[i] == T[i-K]:
            if not prev[i%K]: prev[i%K] = True
            else:
                ans += calc(T[i])
                prev[i%K] = False
            
        else: 
            ans += calc(T[i])
            prev[i%K] = False
            
print(ans)

Solution (Python):
n1, n2 = [int(i) for i in input().split()]
n3, n4, n5= [int(i) for i in input().split()]
s6 = input()

r8 = 0
a9 = [0] * n1
for i7 in range(n1):
    if i7 >= n2:
        if s6[i7] == s6[i7 - n2]:
            if a9[i7 - n2] == 0:
                a9[i7] = 1
                continue
    if s6[i7] == "r":
        r8 += n5
    elif s6[i7] == "s":
        r8 += n3
    else:
        r8 += n4
print(r8)

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

d = {'r': p, 's': r, 'p': s}
ans = 0

for i in range(k):
    last = ''
    j = i
    while j < n:
        if t[j] != last:
            ans += d[t[j]]
            last = t[j]
        else:
            last = ''
        j += k

print(ans)

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

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

def win(enemy_hand):
  if enemy_hand == "r":
    return "p", P
  elif enemy_hand == "s":
    return "r", R
  elif enemy_hand == "p":
    return "s", S
  else:
    raise ValueError("HAND IS WRONG")

total_point = 0
my_hands = []
for enemy_hand in T:
  my_hand, point = win(enemy_hand)
  total_point += point
  my_hands.append(my_hand)

for i in range(K, N):
  my_hand = my_hands[i]
  if my_hand == my_hands[i-K]:
    total_point -= hand_points[my_hand]
    my_hands[i] = "any"

print(total_point)

Solution (Python):
N, K = input().split()
N, K = int(N), int(K)

R, S, P = input().split()
R, S, P = int(R), int(S), int(P)

T = input()


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

def seq_score(T):
    score = 0
    cnt = 1
    c0 = T[0]
    for c in T[1:]:
        if c == c0:
            cnt += 1
        else:
            score += d[c0] * ((cnt+1) // 2)   
            c0 = c
            cnt = 1

    score += d[c0] * ((cnt+1) // 2)
    return score

score = 0
for k in range(K):
    score += seq_score(T[k::K])

print(score)

Solution (Python):
import math

if __name__=="__main__":
    M=0
    n,k=input().split()
    r,s,p=input().split()
    t=input()
    t=list(t)
    n,k,r,s,p=int(n),int(k),int(r),int(s),int(p)
    for i in range(0,n):
       if t[i]=='r':
           t[i]='p'
           M+=p
       elif t[i]=='s':
           t[i]='r'
           M+=r
       else:
           t[i]='s'
           M+=s
    for i in range(k,n):
        if t[i]==t[i-k]:
            if t[i]=='r':
                M-=r
            elif t[i]=='s':
                M-=s
            else:
                M-=p
            t[i]='u'
    print(M)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
point = 0
win = [0 for i in range(len(t))]

for i in range(len(t)):
  if t[i] == 'r':
    if (t[i-k] != 'r') or (win[i-k] != 1):
      point += p
      win[i] = 1

  if t[i] == 's':
    if (t[i-k] != 's') or (win[i-k] != 1):
      point += r
      win[i] = 1
    
  if t[i] == 'p':
    if (t[i-k] != 'p') or (win[i-k] != 1):
      point += s
      win[i] = 1

print(point)

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

score = 0
char_list = list(t)
v_list = []
janken = {0:r, 1:s, 2:p}
dp = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
  if char_list[i] == 'r':
    v_list.append(2)
  elif char_list[i] == 's':
    v_list.append(0)
  else:
    v_list.append(1)

for i in range(k):
  dp[i][v_list[i]] = janken[v_list[i]]
  for j in range(i+k, n, k):
    for l in range(3):
      if v_list[j] == l:
        dp[j][l] = max(dp[j-k][(l+1)%3], dp[j-k][(l+2)%3]) + janken[l]
      else:
        dp[j][l] = max(dp[j-k][(l+1)%3], dp[j-k][(l+2)%3])

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

print(score)

Solution (Python):
store_score = {}
win_hands = {"s": "r", "r": "p", "p": "s"}
scores = {}

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

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

    total_score = 0
    for i in range(K):
        
        target_text = T[i::K]
        
        target_text += "o"

        previous_select = "o"
        local_score = 0
        for j in range(len(target_text) - 1):
            previous_select, score = judge(previous_select, target_text[j], target_text[j+1])
            local_score += score    

        total_score += local_score

    print(total_score)
                        

def judge(previous_select, current_machine, next_machine):
    
    ideal_hand = win_hands[current_machine]
    if previous_select == ideal_hand:
        
        select = next_machine
        score = 0
    else:
        select = ideal_hand
        score = scores[select]
    
    return select, score


if __name__ == "__main__":
    resolve()

Solution (Python):
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=list(input())
hl=[]
ans=0
for i in range(n):
		if i<k:
			if t[i]=='r':
				ans+=p
				hl.append('r')
			elif t[i]=='p':
				ans+=s
				hl.append('p')
			elif t[i]=='s':
				ans+=r
				hl.append('s')
		elif i>=k:
			if t[i]==hl[i-k]:
				hl.append('a')
			else: 
				if t[i]==('r'):
					ans+=p
					hl.append('r')
				elif t[i]=='p':
					ans+=s
					hl.append('p')
				elif t[i]=='s':
					ans+=r
					hl.append('s')
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
win = [True] * len(T)

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

sum = 0
for i in range(K):
  sum += dic[T[i]]
for i in range(K, len(T)):
  if T[i] == T[i - K] and win[i - K]:
    win[i] = False
  else:
    sum += dic[T[i]]
print(sum)

Solution (Python):
"""
N K
R S P
T
"""

ln1 = input().split()
ln2 = input().split()
ln3 = input().split()

N, K = int(ln1[0]), int(ln1[1])
R, S, P = int(ln2[0]), int(ln2[1]), int(ln2[2])
T = ln3[0]

point = 0
result = []

def janken_result(te):
    if te == "r":
        return P
    elif te == "s":
        return R
    elif te == "p":
        return S
    else:
        exit(-1)

for i in range(N):
    if i < K:
        point += janken_result(T[i])
        result.append(1)
    elif T[i] != T[i-K]:
        point += janken_result(T[i])
        result.append(1)
    else:
        if result[i-K] == 0:
            point += janken_result(T[i])
            result.append(1)
        else:
            result.append(0)

print(point)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
win = [True] * len(T)

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

sum = 0
for i in range(K):
  sum += dic[T[i]]
for i in range(K, len(T)):
  if T[i] == T[i - K] and win[i - K]:
    win[i] = False
  else:
    sum += dic[T[i]]
print(sum)

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

def janken_win(x):
    if x == "r": 
        return "p"
    if x == "s": 
        return "r"
    if x == "p": 
        return "s"

def win_point(x):
    if x == "r": 
        return P
    if x == "s": 
        return R
    if x == "p": 
        return S


point = 0
janken = []

for text in range(len(T)):
    if text < K : 
        janken.append(janken_win(T[text]))
        point = point + win_point(T[text])

    elif T[text] == T[text-K]:
        if janken[text-K] == janken_win(T[text]):
            janken.append("a")
        
        else:
            janken.append(janken_win(T[text]))
            point = point + win_point(T[text])
    
    else:
        janken.append(janken_win(T[text]))
        point = point + win_point(T[text])



print(point)

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':
        ans += p
        if i+k < n and t[i+k] == 'r':
            t[i+k] = '0'
    elif t[i] == 's':
        ans += r
        if i+k < n and t[i+k] == 's':
            t[i+k] = '0'
    elif t[i] == 'p':
        ans += s
        if i+k < n and t[i+k] == 'p':
            t[i+k] = '0'
print(ans)

Solution (Python):

 
import math
import itertools
import sys
import copy
 





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

A = []
for i in range(N) :
  if T[i] == "r" :
    A.append("P")
  elif T[i] == "s" :
    A.append("R")
  else :
    A.append("S")


for i in range(N) :
  if i+K < N :
    if A[i] == A[i+K] :
      A[i+K] = "N"


sum = 0
for i in range(N) :
  if A[i] == "P" :
    sum += P
  elif A[i] == "R" :
    sum += R
  elif A[i] == "S" :
    sum += S    
print (sum)

Solution (Python):
n,k = (int(w) for w in input().split()[:2])
r,s,p = (int(w) for w in input().split()[:3])
t = input()

h = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': r, 's': s, 'p': p, 'e': 0}

z = [h[c] for c in t]
for i in range(k, n):
    if z[i]==z[i-k]: z[i] = 'e'

x = sum(score[v] for v in z)

print(x)

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

ans = 0
for i in range(n):
    if i-k >= 0:
        if t[i] == t[i-k]:
            t[i] = 'change'
        else:
            continue

for j in range(n):
    if t[j] == 'r':
        ans += s[2]
    elif t[j] == 's':
        ans += s[0]
    elif t[j] == 'p':
        ans += s[1]
    else:
        continue
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
dict = {"r":p, "s":r, "p":s}
val = 0

for i in range(n):
  if i < k:
    val += dict[t[i]]
  elif t[i-k] != t[i]:
    val += dict[t[i]]
  else:
    t[i] = " "

print(val)

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

Solution (Python):
from collections import defaultdict as dd
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
m = dd(int)
m1 = dd(int)

m['r']='p'
m['p']='s'
m['s']='r'

m1['r']=p
m1['p']=s
m1['s']=r
res=0
for i in range(k):
	res+=m1[t[i]]
	t = t[:i]+m[t[i]]+t[i+1:]
	
	

	
for i in range(k,n):
	
	
	if t[i-k] == m[t[i]]:
		t = t[:i]+'x'+t[i+1:]
		
	else:
		res+=m1[t[i]]
		t = t[:i]+m[t[i]]+t[i+1:]
	
print(res)

Solution (Python):
n_matches, k = map(int, input().split())
point_r, point_s, point_p = map(int, input().split())
ts = list(input())

total_point = 0
t_of_mine = []

for i in range(len(ts)):
    t = ts[i]

    if i < k:
        k_prev = '*'
    else:
        k_prev = t_of_mine[i - k]

    if t == 'r' and k_prev != 'p':
        total_point = total_point + point_p
        t_of_mine.append('p')
    elif t == 's' and k_prev != 'r':
        total_point = total_point + point_r
        t_of_mine.append('r')
    elif t == 'p' and k_prev != 's':
        total_point = total_point + point_s
        t_of_mine.append('s')
    else:
        t_of_mine.append('*')

print(total_point)

Solution (Python):

from collections import defaultdict


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


def main():
	N, K = getlist()
	R, S, P = getlist()
	T = input()
	ans = 0
	DPr = [0] * N
	DPs = [0] * N
	DPp = [0] * N
	
	for k in range(K):	
		if T[k] == "r":
			DPp[k] = P
		elif T[k] == "s":
			DPr[k] = R
		else:
			DPs[k] = S
	for i in range(K, N):
		now = i
		pre = i - K
	
		if T[now] == "r":
			DPr[now] = max(DPs[pre], DPp[pre])
			DPs[now] = max(DPr[pre], DPp[pre])
			DPp[now] = max(DPr[pre] + P, DPs[pre] + P)
		elif T[now] == "s":
			DPr[now] = max(DPs[pre] + R, DPp[pre] + R)
			DPs[now] = max(DPr[pre], DPp[pre])
			DPp[now] = max(DPr[pre], DPs[pre])
		else:
			DPr[now] = max(DPs[pre], DPp[pre])
			DPs[now] = max(DPr[pre] + S, DPp[pre] + S)
			DPp[now] = max(DPr[pre], DPs[pre])
	
	
	
	for i in range(K):
		ans += max(DPr[N - 1 - i], DPs[N - 1 - i], DPp[N - 1 - i])

	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):


def solve_sub(t, r, s, p):
    prev = "@"
    res = 0
    win = {
        "r": p,
        "s": r,
        "p": s,
    }
    for c in t:
        if c == prev:
            prev = "@"
            continue
        else:
            res += win[c]
            prev = c
    return res


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

    groups = [""] * K
    for i in range(len(T)):
        groups[i%K] += T[i]
    

    ans = 0
    for g in groups:
        ans += solve_sub(g, R, S, P)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def solve(string):
    *nkrsp, t = string.split()
    t = list(t)
    n, k, *rsp = map(int, nkrsp)
    b = {k: v for k, v in zip("spr", rsp)}
    ans = sum([b[k] for k in t[:k]])
    for i, _t in enumerate(t[k:]):
        if _t == t[i]:
            t[i + k] = "x"
        else:
            ans += b[_t]
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    *nkrsp, t = string.split()
    n, k, *rsp = map(int, nkrsp)
    table = {k: v for k, v in zip("spr", rsp)}
    ans = 0
    for s in range(k):
        u = t[s::k]
        i = 0
        while i < len(u):
            c = 1
            while i < len(u) - 1 and u[i] == u[i + 1]:
                i += 1
                c += 1
            ans += (c + 1) // 2 * table[u[i]]
            i += 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    *nkrsp, t = string.split()
    *t, = map(int, t.replace("r", "2").replace("s", "0").replace("p", "1"))
    n, k, *rsp = map(int, nkrsp)
    ans = 0
    for s in range(k):
        u = t[s::k]
        i = 0
        while i < len(u):
            c = 1
            while i < len(u) - 1 and u[i] == u[i + 1]:
                i += 1
                c += 1
            ans += (c + 1) // 2 * rsp[u[i]]
            i += 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

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

def win(c):
  if c=="r":
    return "p"
  elif c=="s":
    return 'r'
  else:
    return 's'
def rew(c):
  if c=='r':
    return r
  elif c=='p':
    return p
  else:
    return s
bef=[""]*k
ans=0
for i in range(n):
  mod=i%k
  hand=t[i]
  wi=win(hand)
  if not bef[mod]==wi:
    ans+=rew(wi)
    bef[mod]=wi
  else:
    bef[mod]=''
  
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
tmodk = []
score = 0
for i in range(k):
  tmodk.append(t[i::k])
for i in range(k):
  for j in range(len(tmodk[i])):
    if j > 0 and beforeEnemyHand == tmodk[i][j]:
      beforeEnemyHand = ""
      continue
    if tmodk[i][j] == "r":
      score += p
    if tmodk[i][j] == "s":
      score += r
    if tmodk[i][j] == "p":
      score += s
    beforeEnemyHand = tmodk[i][j]
    
print(score)

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

print ans

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

DP = [0 for i in range(N)]
pre = ["" for i in range(K)]
ans = 0

def Plus(s):
    if s =="s":
        return R
    elif s=="r":
        return P
    elif s=="p":
        return S
    else:
        return 0

for i in range(K):
    pre[i] = T[i]
    ans += Plus(pre[i])

for i in range(1,N//K+1):
    for j in range(K):
        if K*i+j >= N:
            break

        if pre[j] == T[K*i+j]:
            pre[j] = ""
        else:
            pre[j] = T[K*i+j]
            ans += Plus(pre[j])
print(ans)

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

ans = 0
hist = []
for _t in t:
    h = ""
    if len(hist) >= k:
        h = hist.pop(0)

    if _t == "r" and h != "p":
        ans += p
        hist.append("p")
    elif _t == "s" and h != "r":
        ans += r
        hist.append("r")
    elif _t == "p" and h != "s":
        ans += s
        hist.append("s")
    else:
        hist.append("")

print(ans)

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

n,k = (int(x) for x in input().split())
r,s,p = (int(x) for x in input().split())

mydict = {"s":r, "p":s, "r":p}
t = input()

ans = 0

for i in range(n):
	if i<k:
		ans += mydict[t[i]]
	elif t[i-k]!=t[i]:
		ans += mydict[t[i]]
	elif t[i-k]==t[i]:
		t = t[:i] + 'z' + t[i+1:]

print(ans)

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

ans = 0
dp = [[0 for _ in range(3)] for _ in range(N)]
for k in range(K):
  for i in range(k,N,K):
    if i < K:
      if T[i] == 'r':
        dp[i][2] = P
      elif T[i] == 's':
        dp[i][0] = R
      else:
        dp[i][1] = S
    else:
      if T[i] == 'r':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])+P
      elif T[i] == 's':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])+R
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])
      elif T[i] == 'p':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])+S
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])
  ans += max(dp[i][0],dp[i][1],dp[i][2])
print(ans)

Solution (Python):
N, K = map(int, input().split(' '))
R, S, P = map(int, input().split(' '))
T = list(input())
score = 0
A = T
for i in range(N-K):
  if T[i] == T[i+K]:
    A[i+K] = 'a'

for i in range(N):
  if A[i] == 'r':
    score += P
  elif A[i] == 's':
    score += R
  elif A[i] == 'p':
    score += S
print(score)

Solution (Python):
n,k = map(int,input().split())
score = list(map(int,input().split()))
t = list(map(int,input().replace('r','2').replace('s','0').replace('p','1')))
lt = [t[i::k] for i in range(k)]
ans = 0
for it in range(k):
    string = lt[it]
    dp = [[0 for _ in range(3)]for j in range(len(string)+1)]
    for i in range(len(string)):
        for j in range(3):
            for l in range(3):
                if l==j:
                    continue
                elif l==string[i] and dp[i+1][l]<dp[i][j]+score[l]:
                    dp[i+1][l] = dp[i][j]+score[l]
                elif dp[i+1][l]<dp[i][j]:
                    dp[i+1][l] = dp[i][j]
    ans += max((dp[len(string)][0], dp[len(string)][1], dp[len(string)][2]))
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = list(input())
J = {"r": P, "s": R, "p": S}

ans = 0
for i in range(K):
  c = 1
  t = T[i]
  for j in range(i + K, N, K):
    if T[j] == t:
      c += 1
    else:
      ans += J[t] * ((c + 1) // 2)
      c = 1
      t = T[j]
  ans += J[t] * ((c + 1) // 2)

print(ans)

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

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


for i in range(0,K):
    s = [T[k + i] for k in range(0,N,K) if k + i < len(T)]
    prev_e = ''
    for e in s:
      if prev_e != e:
        if e == 'r':
          score += P
        if e == 's':
          score += R
        if e == 'p':
          score += S
        prev_e = e
      else:
        prev_e = ''

print(score)

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

def point(str):
    if str=="r":
        return P
    elif str=="s":
        return R
    elif str=="p":
        return S


ans=0
o=[False]*K
for i in range(N):
    if i<K:
        ans+=point(T[i])
        o[i%K]=True
    else:
        if T[i]==T[i-K]:
            if o[i%K]==False:
                ans+=point(T[i])
                o[i%K]=True
            else:
                o[i%K]=False
        else:
            ans+=point(T[i])
            o[i%K]=True
print(ans)

Solution (Python):
import math


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








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

def determine_R_P_S(val):
    if val == "r":
        
        return "p"
    elif val == "p":
        
        return "s"
    else:
        
        return "r"
    
def add_profit(machine_val, you_played, succeed, R, P, S):
    if machine_val == "r":
        if succeed == 1:
            return P
        else:
            return 0

    if machine_val == "p":
        if succeed == 1:
            return S
        else:
            return 0

    if machine_val == "s":
        if succeed == 1:
            return R
        else:
            return 0
    


def total_profit(T, R, P, S, K, N):
    profit = 0
    S_played = ""
    for i in range(0, K):
        I_played = determine_R_P_S(T[i])
        S_played += I_played
        
        
        
        
        
        profit += add_profit(T[i], I_played, 1, R, P, S)

    for i in range(K, N):
        I_played = determine_R_P_S(T[i])
        I_played_previous = S_played[i - K]
        
        
        
        
        if I_played != I_played_previous:
            S_played += I_played
            profit += add_profit(T[i], I_played, 1, R,  P, S)
        else:   
            if i + K < N:
                To_play_future = determine_R_P_S(T[i + K])
                if To_play_future != I_played_previous:
                    S_played += mapped_dict[3 - reverse_mapped_dict[To_play_future] - reverse_mapped_dict[I_played_previous]]
                else:
                    S_played += T[i]
                    
            else:
                S_played += T[i]
                

    
    return profit



print(total_profit(T, R, P, S, K, N))

Solution (Python):
a, b = input().split(" ")
c, d, e = input().split(" ")
str = input()

b = int(b)
c = int(c)
d = int(d)
e = int(e)
score = 0
last_data = []
count = 0
for char in str:
  if(count // b < 1):
    if char == "r":
      score += e
    if char == "s":
      score += c
    if char == "p":
      score += d
    last_data.append(char)
  else:
    mod = count % b
    if char == last_data[mod]:
      last_data[mod] = "a"
    else:
      if char == "r":
        score += e
      if char == "s":
        score += c
      if char == "p":
        score += d
      last_data[mod] = char
  count += 1
  
print(score)

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


def win(h):
    if h == 'r':
        return P
    elif h == 's':
        return R
    else:
        return S


score = []
for i in range(N):
    if i < K:
        score.append(win(T[i]))
    elif T[i] != T[i - K]:
        s = win(T[i])
        score.append(s)
    else:
        s = win(T[i])
        if s != score[i - K]:
            score.append(s)
        else:
            score.append(0)


print(sum(score))

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

def score(c):
	if c=="r":
		return p
	elif c=="s":
		return r
	else:
		return s

isUsed = [False]*n
ans = 0
for i in range(n):
	if i < k:
		ans+=score(t[i])
		isUsed[i]=True
	else:
		if t[i] == t[i-k] and isUsed[i-k]:
			continue
		ans+=score(t[i])
		isUsed[i]=True

print(ans)

Solution (Python):

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


dp = [True] * N
point = {"r": P, "s": R, "p": S}
answer = 0

for i in range(N):
    if i < K:
        answer += point[T[i]]
        dp[i] = False
    else:
        if T[i] == T[i-K]:
            if dp[i-K]:
                answer += point[T[i]]
                dp[i] = False
        else:
            answer += point[T[i]]
            dp[i] = False

print(answer)

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

def point(i):
	if T[i] == "r":
		return P
	elif T[i] == "s":
		return R
	elif T[i] == "p":
		return S

for i in range(N):
	if i < K:
		res.append(point(i))
	else:
		if T[i - K] != T[i]:
			res.append(point(i))
		elif res[-K] == 0:
			res.append(point(i))
		else:
			res.append(0)
			
print(sum(res))

Solution (Python):
import numpy as np

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

ans = 0
for k in range(K):
    orig_str = T[k::K]
    rep_str = orig_str.replace('rr', 'r').replace('ss', 's').replace('pp', 'p')
    point = rep_str.count('r') * P + rep_str.count('s') * R + rep_str.count('p') * S
    ans += point

print(ans)

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

scores = {'r':r,'p':p,'s':s}
def calc_score(my_hand, other_hand):
    if my_hand == 'r' and other_hand == 's':
        return r
    elif my_hand == 's' and other_hand == 'p':
        return s
    elif my_hand == 'p' and other_hand == 'r':
        return p
    else:
        return 0

def winnable_hand(other_hand):
    if other_hand == "r":
        return "p"
    elif other_hand == "p":
        return "s"
    else:
        return "r"
def losable_hand(other_hand):
    if other_hand == "r":
        return "s"
    elif other_hand == "p":
        return "r"
    else:
        return "p"

result = 0
for i in range(k):
    target = t[i::k]
    rps = {'r':0,'p':0,'s':0}
    for other_hand in target:
        wh = winnable_hand(other_hand)
        lh = losable_hand(other_hand)
        r_max = max(rps['p'], rps['s'])
        p_max = max(rps['r'], rps['s'])
        s_max = max(rps['p'], rps['r'])
        current_max = {hand: m for hand, m in zip(["r","s","p"], [r_max, s_max, p_max])}
        rps[wh] = current_max[wh] + scores[wh]
        rps[other_hand] = current_max[other_hand]
        rps[lh] = current_max[lh]
    
    result += max(rps.values())

print(result)