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 = list(map(int,input().split()))
R,S,P = list(map(int,input().split()))
T = input()
from collections import defaultdict
dp = defaultdict(int)

score = {"R":R,"P":P,"S":S}
def battle(user, box):
  d ={"S":0,"R":1,"P":2,
      "s":-1,"r":-2,"p":0}
  if (d[user] + d[box])%3 == 0:
    return score[user]
  else:
    return 0

for idx, teki in enumerate(T):
  for usr in "RPS":
  	dp[(idx,usr)] = max([
      dp[(idx-K,prev)]+battle(usr,teki) for prev in ("RPS".replace(usr,"")) 
    ])
    
print(sum ([max([dp[(N-r-1,te)] for te in "RPS"]) for r in range(K)]))

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

win={'r':'p','s':'r','p':'s'}
point={'r':R,'s':S,'p':P}
hand=['']*N
ans=0

for i in range(K):
    w=win[T[i]]
    ans+=point[w]
    hand[i]=w

for i in range(K,N):
    w=win[T[i]]
    if w==hand[i-K]:
        continue
    ans+=point[w]
    hand[i]=w
    
print(ans)

Solution (Python):
import sys

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

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

ans = 0

wins = [False] * K

for i in range(N):
    if not wins[i % K] or T[i] != T[i - K]:
        ans += v_hand[T[i]]
        wins[i % K] = True
    else:
        wins[i % K] = False
        
print(ans)

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

score = [0] * n
for i in range(n):
    if i-k < 0:
        score[i] = table[t[i]]
        continue
    if t[i-k] == t[i] and score[i-k] != 0:
        score[i] = 0
    else:
        score[i] = table[t[i]]

print(sum(score))

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

Solution (Python):
def iip():
    return [int(i) for i in input().split()]

N, K = iip()
R, S, P = iip()
T = input()

point = 0
win = []
wp = {
    "r":P,
    "s":R,
    "p":S
}

for i in range(N):
    if i >= K and win[i-K] == T[i]:
        win.append(False)
    else:
        win.append(T[i])
        point += wp[T[i]]


print(point)

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

table = [-1 for i in range(len(t) + k)]
count = 0
for i in range(len(t)):
    if table[i-k] != -1 and table[i-k] == t[i]:
        continue
    elif t[i] == "r":
            count += p
            table[i] = "r"
    elif t[i] == "s":
            count += r
            table[i] = "s"
    elif t[i] == "p":
            count += s
            table[i] = "p"

print(count)

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

get_point = []
score = 0

for i in range(n):
    if t[i] == 'r':
        if i - k < 0 or get_point[i-k] != 'p':
            score += p
            get_point.append('p')
        else:
            get_point.append('e')
    elif t[i] == 's':
        if i - k < 0 or get_point[i-k] != 'r':
            score += r
            get_point.append('r')
        else:
            get_point.append('e')
    elif t[i] == 'p':
        if i - k < 0 or get_point[i-k] != 's':
            score += s
            get_point.append('s')
        else:
            get_point.append('e')

print(score)

Solution (Python):
n, k = map(int, input().split())
gu, choki, pa = map(int, input().split())
t = list(input())

total = 0
for i, v in enumerate(t):
    if i >= k and v == t[i-k]:
        t[i] = 'd'
    elif v == 'r':
        total += pa
    elif v == 's':
        total += gu
    else:
        total += choki
print(total)

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

points = 0
for i in range(N):
  if T[i]=="r":
    if i-K < 0 or T[i]!=T[i-K]:
      points+=P
    else:
      T[i]="x"
  elif T[i]=="s":
    if i-K < 0 or T[i]!=T[i-K]:
      points+=R
    else:
      T[i]="x"
  else:
    if i-K < 0 or T[i]!=T[i-K]:
      points+=S
    else:
      T[i]="x"
print(points)

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

ans=0
win=[0]*n
for i in range(len(t)):
    if i-k>=0 and win[i-k] == 1 and t[i]==t[i-k]:
        pass
    else:
        if t[i] == "r":
          ans+=p
        if t[i] == "s":
            ans+=r
        if t[i] == "p":
            ans+=s
        win[i]=1

print(ans)

Solution (Python):
def sol(score,hand):
    if hand=="r":
        return score["p"]
    elif hand=="p":
        return score["s"]
    elif hand=="s":
        return score["r"]
    return 0
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
win=""
for num,val in enumerate(t):
    if num<k or win[num-k]!=val:
        win+=val
    else:
        win+="n"
score={"r":r,"p":p,"s":s}
ans=0
for val in win:
    ans+=sol(score,val)

print(ans)

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

score = 0
hand_score = {'r': R, 's': S, 'p': P}
my_hand_dict = {'r': 'p', 's': 'r', 'p': 's'}
my_hand_list = list()
for i, hand in enumerate(T):
    if i < K:
        my_hand = my_hand_dict[hand]
        score += hand_score[my_hand]
        my_hand_list.append(my_hand)
    else:
        my_hand = my_hand_dict[hand]
        if my_hand != my_hand_list[i-K]:
            score += hand_score[my_hand]
            my_hand_list.append(my_hand)
        else:
            my_hand_list.append('0')

print(score)

Solution (Python):
n, k = map(int, input().split())
score = list(map(int, input().split()))
t_str = input()
hand = 'rsp'
t = []
for x in t_str:
    t.append(hand.index(x))

ans = 0
for r in range(k):
    d = [0] * 3
    for h in t[r::k]:
        dd = [0] * 3
        for i in range(3):
            for j in range(3):
                if i != j:
                    point = 0
                    if (j - h) % 3 == 2:
                        point = score[j]
                    dd[j] = max(dd[j], d[i] + point)
        d = dd
    ans += max(d)
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"}
point = {"r": R, "s": S, "p": P}
taka = []
ans = 0
for i in range(N):
    if i >= K:
        if win[T[i]] == taka[i - K]:
            taka.append("*")
        else:
            taka.append(win[T[i]])
            ans += point[win[T[i]]]
    else:
        taka.append(win[T[i]])
        ans += point[win[T[i]]]
print(ans)

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



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

Solution (Python):






import sys
input = sys.stdin.readline

import math
from fractions import gcd
from functools import lru_cache





MOD = 10**9 +7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





N,K = inputIL()
R,S,P = inputIL()
T = list(inputS())

t = []
for i in range(K):
  t.append(T[i::K])
  
  
def get(s):
  if s=="r": return P
  if s=="s": return R
  if s=="p": return S
ans = 0

for l in t:
  skipped = 0
  for i in range(len(l)):
    if i == 0:
      ans += get(l[i])
    else:
      if l[i]!=l[i-1] or skipped == 1:
        ans += get(l[i])
        skipped = 0
      else:
        skipped = 1 

print(ans)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,k = mi()
ten = li()
t = input()
lis = ["a"]*n
for i in range(k,n):
    lis[i] = t[i-k]
ans = 0

for i in range(n):
    if t[i] == lis[i]:
        if i+k<n:
            lis[i+k] = "a"
        continue
    if t[i] == "s":
        ans += ten[0]
    elif t[i] == "p":
        ans += ten[1]
    else:
        ans += ten[2]
print(ans)

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

win = {"r":"p","s":"r","p":"s"}
point = {"r":r,"s":s,"p":p}

ans = []
sum = 0

for i in range(k):
    ans.append(win[t[i]])
    sum+=point[win[t[i]]]

for i in range(k,n):
    if win[t[i]] == ans[i - k]:
        ans.append(-1)
    else:
        ans.append(win[t[i]])
        sum+=point[win[t[i]]]
print(sum)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
T=[a for a in T]
answer=0
takahashi=[]
if N%K==0:
    number=(N/K-1)*K
else:
    number=(N//K)*K
for a in range(K):
    if T[a]=='r':
        answer+=P
        takahashi.append('p')
    elif T[a]=='s':
        answer+=R
        takahashi.append('r')
    else:
        answer+=S
        takahashi.append('s')
for a in range(K,int(number)):
    if T[a]=='r':
        if takahashi[a-K]=='p':
            if a+K<N:
                if T[a + K] == 's':
                    takahashi.append('s')
                else:
                    takahashi.append('r')
            else:
                pass
        else:
            answer+=P
            takahashi.append('p')
    elif T[a]=='s':
        if takahashi[a-K]=='r':
            if a+K<N:
                if T[a + K] == 'p':
                    takahashi.append('p')
                else:
                    takahashi.append('s')
            else:
                pass
        else:
            answer+=R
            takahashi.append('r')
    else:
        if takahashi[a-K]=='s':
            if a+K<N:
                if T[a + K] == 'r':
                    takahashi.append('r')
                else:
                    takahashi.append('p')
            else:
                pass
        else:
            answer+=S
            takahashi.append('s')
for a in range(int(number),N):
    if T[a]=='r':
        if takahashi[a-K]=='p':
            pass
        else:
            answer+=P
    elif T[a]=='s':
        if takahashi[a-K]=='r':
           pass
        else:
            answer+=R
    else:
        if takahashi[a-K]=='s':
            pass
        else:
            answer+=S
print(answer)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t_list = list(input())
towin = {"r": p, "s": r, "p": s}
ans = 0
for i in range(n):
    if i < k:
        ans += towin[t_list[i]]
    else:
        if t_list[i - k] == t_list[i]:
            t_list[i] = "-1"
        else:
            ans += towin[t_list[i]]
print(ans)

Solution (Python):
n,k=map(int,input().split())
get_score=list(map(int,input().split()))
t=list(input())
dic={'r':0,'s':1,'p':2}

ans=0
for i in range(n):
    if t[i]=='D':continue
    if i+k<n and t[i]==t[i+k]:
        t[i+k]='D'
    ans+=get_score[(dic[t[i]]+2)%3]

print(ans)

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

ta = []
for i in range(n) :
  ta.append([t[i],1])

for i in range(n-k) :
  if ta[i][0] == ta[i+k][0] and ta[i][1] == ta[i+k][1]: 
    ta[i+k][1] = 0

ans = 0
for i in range(n) :
  if ta[i][1] == 1 :
    if ta[i][0] == 'r' :
      ans += p
    if ta[i][0] == 's' :
      ans += r
    if ta[i][0] == 'p' :
      ans += s

print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    r,s,p = inpm()
    t = list(input())
    past = [0 for _ in range(k)]
    ans = 0
    for i in range(n):
        if t[i] == 'r' and past[i%k] != 1:
            ans += p
            past[i%k] = 1
        elif t[i] == 's' and past[i%k] != 2:
            ans += r
            past[i%k] = 2
        elif t[i] == 'p' and past[i%k] != 3:
            ans += s
            past[i%k] = 3
        else:
            past[i%k] = 0
    print(ans)        

if __name__ == "__main__":
    main()

Solution (Python):
n, k = (int(x) for x in input().split())
score = [int(x) for x in input().split()]
translate = {'r':0, 's': 1, 'p': 2}
seq = [translate[x] for x in input()]
dp = [[float('-inf') for _ in range(3)] for _ in range(n+1)]
dp[0][0] = 0
dp[0][1] = 0
dp[0][2] = 0
for i in range(n):
    for j in range(3):
        if i < k:
            dp[i+1][j] =(score[j] if ((j+1) % 3) == seq[i] else 0)
        else:
            dp[i+1][j] = max(dp[i-k+1][(j+1) % 3], dp[i-k+1][(j+2) % 3]) + (score[j] if ((j+1) % 3) == seq[i] else 0) 
print(sum(max(dp[-1-i]) for i in range(k)))

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}
ans = 0

for i in range(k):
    ans += dic[t[i]]

for i in range(k,n):
    if t[i] != t[i-k]:
        ans += dic[t[i]]
    else:
        t[i] = 0
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 = 0
for i,c in enumerate(T):
    if i < K or c != T[i-K]:
        ans += d[c]
        continue
    else:
        T[i] = '_'
print(ans)

Solution (Python):

from itertools import groupby
n, k = map(int, input().split())
r, s, p = map(int, input().split())
point = {'s':r, 'p':s, 'r':p}
t = input()
ans = 0
for i in range(k):
    t_ = t[i::k]
    cnt = groupby(list(t_))
    for te, c in cnt:
        ans += point[te] * -(-len(list(c)) // 2)
print(ans)

Solution (Python):
game_num, k = map(int, input().split())
r_score, s_score, p_score = map(int, input().split())
game = input()
score = 0
waste_r, waste_s, waste_p = [], [], []
count_waste_r = count_waste_s = count_waste_p = 0

for i in range(game_num):
    if i < k:
        pass
    elif game[i] == 'r' and game[i - k] == 'r' and not (i - k) in waste_r:
        count_waste_r += 1
        if len(waste_r) > k:
            waste_r.pop(0)
        waste_r.append(i)
    elif game[i] == 's' and game[i - k] == 's' and not (i - k) in waste_s:
        count_waste_s += 1
        if len(waste_s) > k:
            waste_s.pop(0)
        waste_s.append(i)
    elif game[i] == 'p' and game[i - k] == 'p' and not (i - k) in waste_p:
        count_waste_p += 1
        if len(waste_p) >  k:
            waste_p.pop(0)
        waste_p.append(i)

r_count, s_count, p_count = map(lambda x: game.count(x), ['r', 's', 'p'])

score += r_count * p_score + s_count * r_score + p_count * s_score
score -= count_waste_r * p_score + count_waste_s * r_score + count_waste_p * s_score
print(score)

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

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

s = {"r": P, "s": R, "p": S}
res = 0
for i in range(N):
    if i >= K:
        if T[i] == T[i-K]:
            T[i] = 0
            continue
    res += s[T[i]]

print(res)

Solution (Python):

import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
R, S, P = MAP()
T = input()
point = {"r": R, "s": S, "p": P}
win = {"r": "p", "s": "r", "p":"s"}


win_T = [win[x] for x in T]

ans = 0
for i in range(N):
    if i < K:
        ans += point[win_T[i]]
    
    elif win_T[i-K] == win_T[i]:
        win_T[i] = None
    else:
        ans += point[win_T[i]]
print(ans)

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

Solution (Python):
import math

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


def Score(hand):
    if hand == "r":
        score = P
    elif hand == "s":
        score = R
    else:
        score = S
    return score


t = [[] for _ in range(K)]

for i in range(N):
    chk = i % K
    t[chk].append(T[i])

Ans = 0
for i in range(K):
    check = t[i]
    cnt = 1
    for j in range(len(check) - 1):
        if check[j] == check[j + 1]:
            cnt += 1
        else:
            Ans += Score(check[j]) * (math.ceil(cnt / 2))
            cnt = 1
    Ans += Score(check[-1]) * (math.ceil(cnt / 2))

print(Ans)

Solution (Python):
N,K = input().split(' ')
N = int(N)
K = int(K)
points = [int(x) for x in input().split(' ')]
line = input()
point = 0
W = {
  "r":"p",
  "s":"r",
  "p":"s"
}
P = {
  "r":points[0],
  "s":points[1],
  "p":points[2]
}
mem = [0]*K
for i in range(N):
  if i < K:
    mem[i] = W[line[i]]
    point += P[W[line[i]]]
  else:
    if mem[i%K] == W[line[i]]:
      mem[i%K] = 'N'
    else:
      mem[i%K] = W[line[i]]
      point += P[W[line[i]]]
print(point)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
for i in range(K):
    ans += P if T[i] == "r" else R if T[i] == "s" else S
    prev = T[i]
    for t in T[i::K][1:]:
        if t != prev:
            ans += P if t == "r" else R if t == "s" else S
            prev = t
        else:
            prev = "*"
print(ans)

Solution (Python):
from copy import deepcopy

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

ans = 0
for i in range(k):
  n_ = (n - i) // k + (1 if (n-i) % k > 0 else 0)
  rsp = [0] * 3
  for j in range(n_):
    bef = deepcopy(rsp)
    if l[j * k + i] == "r":
      rsp[2] = max(bef[0], bef[1]) + p
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2])
    elif l[j * k + i] == "s":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2]) + r
    elif l[j * k + i] == "p":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2]) + s
      rsp[0] = max(bef[1], bef[2])
  ans += max(rsp)
      
print(ans)

Solution (Python):
from copy import deepcopy

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

ans = 0
for i in range(k):
  n_ = (n - i) // k + (1 if (n-i) % k > 0 else 0)
  rsp = [0] * 3
  for j in range(n_):
    bef = deepcopy(rsp)
    if l[j * k + i] == "r":
      rsp[2] = max(bef[0], bef[1]) + p
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2])
    elif l[j * k + i] == "s":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2]) + r
    elif l[j * k + i] == "p":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2]) + s
      rsp[0] = max(bef[1], bef[2])
  ans += max(rsp)
      
print(ans)

Solution (Python):
from copy import deepcopy

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

ans = 0
for i in range(k):
  n_ = (n - i) // k + (1 if (n-i) % k > 0 else 0)
  rsp = [0] * 3
  for j in range(n_):
    bef = deepcopy(rsp)
    if l[j * k + i] == "r":
      rsp[2] = max(bef[0], bef[1]) + p
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2])
    elif l[j * k + i] == "s":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2])
      rsp[0] = max(bef[1], bef[2]) + r
    elif l[j * k + i] == "p":
      rsp[2] = max(bef[0], bef[1])
      rsp[1] = max(bef[0], bef[2]) + s
      rsp[0] = max(bef[1], bef[2])
  ans += max(rsp)
      
print(ans)

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

score=0

for i in range(N):
    if i-K<0 or T[i-K]!=T[i]:
        if T[i]=="r":
            score+=P
        elif T[i]=="p":
            score+=S
        elif T[i]=="s":
            score+=R
    else:
        T[i]="a"

print(score)

Solution (Python):
n, k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
z = 0
kachi = dict({"r": "p", "s": "r", "p": "s"})
tt = []
for i in range(n):
    tt.append(kachi[t[i]])


for i in range(n - k):
    if tt[i] == tt[i + k]:
        tt[i+k] = "z"
kachi = dict({"r": r, "s": s, "p": p, "z": 0})
res = 0
for i in range(n):
    res += kachi[tt[i]]
print(res)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
T=input()
cnt=0
N=[0]*n
for i in range(n):
    if N[i]==1:
        continue
    if i+k<=n-1:
        if T[i]==T[i+k]:
            N[i+k]=1
    
    if T[i]=='r':
        cnt+=p
    elif T[i]=='s':
        cnt+=r
    else:
        cnt+=s

print(cnt)

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

def jk(n):
    if n == "r":
        return "p"
    elif n == "s":
        return "r"
    else:
        return "s"

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

ans = 0

hand = [[] for _ in range(K)]
for i in range(N):
    hand[i%K].append(T[i])

fight = [[] for _ in range(K)]
for i in range(K):
    for j in range(len(hand[i])):
        if j == 0:
            fight[i].append(jk(hand[i][j]))
            ans += sc(jk(hand[i][j]))
        elif hand[i][j] == hand[i][j-1]:
            if fight[i][j-1] == jk(hand[i][j]):
                fight[i].append(0)
            else:
                fight[i].append(jk(hand[i][j]))
                ans += sc(jk(hand[i][j]))
        else:
            fight[i].append(jk(hand[i][j]))
            ans += sc(jk(hand[i][j]))

print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, k = na()
r, s, p = na()
t = input()
ans = 0
for i in range(k):
    
    li = t[0+i::k]
    li  = 'x' + li
    flg = False
    for j in range(1, len(li)):
        if li[j] != li[j-1] or flg:
            flg = False
            if li[j] == 'r':
                ans += p
            if li[j] == 's':
                ans += r
            if li[j] == 'p':
                ans += s
        else:
            flg = True
        
print(ans)

Solution (Python):
from itertools import groupby
from math import ceil
*A, T = open(0).read().split()
n, k, r, s, p = map(int, A)
T = T.strip()

d = {'r':p, 's':r, 'p':s}
X = [T[i::k] for i in range(k)]
s = 0
for x in X:
    for key, rep in groupby(x):
        s += d[key] * ceil(len(list(rep)) / 2)
print(s)

Solution (Python):
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
R,S,P = [int(x) for x in stdin.readline().rstrip().split()]
T = (stdin.readline().rstrip())
sub = [""]*K
for ind,i in enumerate(T):
    sub[(ind+1)%K] += i

ans = 0

for j in sub:
    former = ""
    for s in j:
        if s == "r":
            p = P
        elif s == "s":
            p = R
        else:
            p = S
        
        if s != former:
            ans += p
            former = s
        else:
            former = ""
            
print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
arr=[0]*N
d={'r':P,'s':R,'p':S}
for i,t in enumerate(T):
    if i-K>=0:
        if T[i-K]!=t or arr[i-K]==0:
            arr[i]=d[t]
    else:
        arr[i]=d[t]
print(sum(arr))

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
arr=[0]*N
d={'r':P,'s':R,'p':S}
for i,t in enumerate(T):
    if i-K>=0:
        if T[i-K]!=t:
            arr[i]=d[t]
        elif arr[i-K]==0:
            arr[i]=d[t]
    else:
        arr[i]=d[t]
print(sum(arr))

Solution (Python):
import math









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

score = 0
rev = [True] * k
for i in range(min(k,n)):
    if t[i] == 'r':
        score += p

    elif t[i] == 'p':
        score += s

    elif t[i] == 's':
        score += r

if n>k:

    for i in range(k,n):

        if t[i] == t[i-k] and rev[i%k] :
            rev[i % k] = False

        elif t[i] == t[i-k] and not rev[i%k] :
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

        elif t[i] != t[i-k]:
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

print(score)

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 i >= k and t[i] == t[i-k]:
        t[i] = "o"
        continue
    if t[i] == "r":
        ans += p
    elif t[i] == "s":
        ans += r
    else:
        ans += s
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = [i for i in input()]
ans = 0
rsp = {"r": 0, "s": 0, "p": 0}
for i in range(n):
    if t[i] == "r":
        ans += p
    elif t[i] == "s":
        ans += r
    else:
        ans += s
    if i >= k and t[i] == t[i-k]:
        rsp[t[i]] += 1
        t[i] = "o"
mi = int(rsp["r"] * p + rsp["s"] * r + rsp["p"] * s)
print(ans-mi)

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] = 'x'
for i in range(N):
    if T[i] == 'x':
        continue
    elif T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
R,S,P = inpl()
a = input()
res = 0
for rem in range(k):
    dp = [[0]*3 for _ in range(len(range(rem,n,k))+1)]
    for i,j in enumerate(range(rem,n,k)):
        for x in range(3):
            pls = 0
            if x == 0 and a[j] == 's':
                pls += R
            if x == 1 and a[j] == 'p':
                pls += S
            if x == 2 and a[j] == 'r':
                pls += P
            for y in range(3):
                if x == y: continue
                dp[i+1][x] = max(dp[i][y]+pls, dp[i+1][x])
    
    res += max(dp[-1])
print(res)