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):
from sys import stdin


n, k = map(int, stdin.readline().rstrip().split())

r,s,p = map(int, stdin.readline().rstrip().split())
ts = str(stdin.readline().rstrip())

points  = 0

win = []

def padd(t, points,win):
    if t == "r":
        win.append("r")
        return points+p, win
    elif t=="s":
        win.append("s")
        return points+r, win
    else:
        win.append("p")
        return points+s, win

for i in range(n):
    if i < k:
        points,win = padd(ts[i], points,win)
    else:
        if ts[i] == win[i-k]:
            win.append("n")
        else:
            points, win = padd(ts[i],points,win)

print(points)

Solution (Python):

url = "https://atcoder.jp//contests/abc149/tasks/abc149_d"

def main():
    n,k = list(map(int, input().split()))
    rsp = list(map(int, input().split()))
    j_machine = list(input())
    j_human = []
    score = 0
    for i, v in enumerate(j_machine):
        hand = ''
        points = 0
        hand, points = ret_win_hand_point(v, rsp)

        if i - k >= 0 and hand == j_human[i - k]:
            f_hand = [hand]
            if i + k < len(j_machine):
                h = ret_win_hand_point(j_machine[i + k], rsp)
                f_hand.append(h[0])
            for v in ['r','s','p']:
                if v not in f_hand:
                    hand = v
                    break
            points = 0
        score += points
        j_human.append(hand)
    print(score)


def ret_win_hand_point(machine, rsp):
    if machine == 'r':
        hand = 'p'
        points = rsp[2]
    elif machine == 'p':
        points = rsp[1]
        hand = 's'
    else:
        points = rsp[0]
        hand = 'r'
    return hand, points

if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
score_map = {'r':r, 's':s, 'p':p}
t = input()
history = [0] * n
score = 0
for i in range(n):
    opposite = t[i]
    if opposite == 'r':
        hand_win = 'p'
    elif opposite == 's':
        hand_win = 'r'
    else:
        hand_win = 's'

    if i + 1 <= k:
        score += score_map[hand_win]
        history[i] = hand_win
    else:
        if history[i-k] == hand_win:
            continue
        else:
            score += score_map[hand_win]
            history[i] = hand_win
print(score)

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

o = ''
for j in range(k):
    o += (''.join(t[j::k])).replace('rr', 'r').replace(
        'pp', 'p').replace('ss', 's')

x = 0
for i, h in enumerate(o):
    if h == 'r':
        x += p
    elif h == 's':
        x += r
    else:
        x += s

print(x)

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

Solution (Python):
def calc_max_score(choices, R, S, P):
    score = {
        'r': P,  
        's': R,  
        'p': S   
    }
    before_choice = None
    seq_cnt = 1
    sum_score = 0
    for c in choices:
        if before_choice == c:
            seq_cnt += 1
            continue
        if before_choice is not None:
            sum_score += score[before_choice] * ((seq_cnt + 1) // 2)
        before_choice = c
        seq_cnt = 1
    if before_choice is not None:
        sum_score += score[before_choice] * ((seq_cnt + 1) // 2)
    return sum_score


def main():
    N, K = list(map(int, input().split(' ')))
    R, S, P = list(map(int, input().split(' ')))
    T = input()
    choice_list = [list() for _ in range(K)]
    for i, c in enumerate(T):
        i %= K
        choice_list[i].append(c)
    answer = 0
    for choices in choice_list:
        answer += calc_max_score(choices, R, S, P)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
n, k = [int(e) for e in input().split()]
r, s, p = [int(e) for e in input().split()]
t = input()
t1 = list()
for i in t:
    if i =='r':
        t1.append('p')
    elif i =='p':
        t1.append('s')
    else:
        t1.append('r')
t2=list()
for j,i in  enumerate(t1):
    if j < k:
        t2.append(i)
    else:
        if i == t2[j-k]:
            t2.append('x')
        else:
            t2.append(i)

num = 0

num += t2.count("r")*r
num += t2.count("s")*s
num += t2.count("p")*p

print(num)

Solution (Python):
n, k = map(int, input().split())
s, p, r = map(int, input().split())
t = input()
output = []
ans = 0
for i in range(n):
    if i < k:
        if t[i] == "r":
            ans += r
            output.append("r")
        if t[i] == "p":
            ans += p
            output.append("p")
        if t[i] == "s":
            ans += s
            output.append("s")
    else:
        if t[i] == output[i-k]:
            output.append(1)
        else:
            if t[i] == "r":
                ans += r
                output.append("r")
            if t[i] == "p":
                ans += p
                output.append("p")
            if t[i] == "s":
                ans += s
                output.append("s")

print(ans)

Solution (Python):
n, k = map(int, input().split())
r,s,p = map(int, input().split())
d = {"r":p, "s":r, "p":s}
t = input()
mode = [True]*n
score = 0
for i in range(n):
  score += d[t[i]]
  if i >= k and mode[i-k] and t[i] == t[i-k]:
    score -= d[t[i]]
    mode[i] = False
print(score)

Solution (Python):

import sys


def solve(N: int, K: int, R: int, S: int, P: int, T: str):

    winToR = P
    winToS = R
    winToP = S
    
    dp = []
    for i in range(K):
        dp.append([])
        for _ in range(N // K + 1):
            m = [0] * 3
            dp[i].append(m)

    for k in range(K):
        if T[k] == "r":
            dp[k][0][0] = winToR
        elif T[k] == "s":
            dp[k][0][1] = winToS
        else:
            dp[k][0][2] = winToP

    for n in range(K, N):
        if T[n] == "r":

            dp[n % K][n // K][0] = winToR + max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])
        elif T[n] == "s":
            dp[n % K][n // K][0] = max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = winToS + max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])
        else:
            dp[n % K][n // K][0] = max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = winToP + max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])

    sum = 0
    for k in range(N - K, N):
        imax = 0
        for i in range(3):
            imax = max(imax, dp[k % K][k // K][i])
        sum += imax

    print(sum)




    return



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

if __name__ == '__main__':
    main()

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

a = [0]*k
for i in range(k):
	a[i] = t[i::k]

ans = 0

for i in range(k):
	dp = [[0]*3 for l in range(len(a[i])+1)]
	for j in range(len(a[i])):
		if a[i][j] == 'r':
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2]))
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2]))
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1])+p)
		elif a[i][j] == 's':
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2])+r)
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2]))
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1]))
		else:
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2]))
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2])+s)
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1]))
	ans += max(dp[-1][0], max(dp[-1][1], dp[-1][2]))

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = 0
    for i in range(K):
        f = False
        for j in range(i, N, K):
            if j < K:
                if T[j] == 'r':
                    ans += P
                elif T[j] == 's':
                    ans += R
                else:
                    ans += S
            else:
                if T[j] == T[j - K] and not f:
                    f = True
                    continue
                else:
                    if T[j] == 'r':
                        ans += P
                    elif T[j] == 's':
                        ans += R
                    else:
                        ans += S
                    f = False
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def janken(val):
    if val == "r":
        return "p",p
    elif val == "p":
        return "s", s
    else:
        return "r",r
n, k=map(int, input().split())
r,s,p = map(int,input().split())
t = input()

jan = []
ans = 0
for i in range(n):
    hand, point = janken(t[i])
    if i < k :
        jan.append(hand)
        ans += point
    else:
        if jan[i-k] == hand:
            jan.append("all")
        else:
            jan.append(hand)
            ans+=point
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = [None]*N
for i in range(K):
    if T[i] == 'r':
        ans[i] = 'p'
    elif T[i] == 's':
        ans[i] = 'r'
    else:
        ans[i] = 's'
for i in range(K, N):
    if T[i] == 'r':
        if ans[i-K] == 'p':
            ans[i] = 'd'
        else:
            ans[i] = 'p'
    elif T[i] == 's':
        if ans[i-K] == 'r':
            ans[i] = 'd'
        else:
            ans[i] = 'r'
    else:
        if ans[i-K] == 's':
            ans[i] = 'd'
        else:
            ans[i] = 's'
r = R * ans.count('r')
s = S * ans.count('s')
p = P * ans.count('p')
print(r+s+p)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = [None]*N
for i in range(K):
    if T[i] == 'r':
        ans[i] = 'p'
    elif T[i] == 's':
        ans[i] = 'r'
    else:
        ans[i] = 's'
for i in range(K, N):
    if T[i] == 'r':
        if ans[i-K] == 'p':
            ans[i] = 'd'
        else:
            ans[i] = 'p'
    elif T[i] == 's':
        if ans[i-K] == 'r':
            ans[i] = 'd'
        else:
            ans[i] = 'r'
    else:
        if ans[i-K] == 's':
            ans[i] = 'd'
        else:
            ans[i] = 's'
r = R * ans.count('r')
s = S * ans.count('s')
p = P * ans.count('p')
print(r+s+p)

Solution (Python):
if __name__ == "__main__":
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    point = {'r': p, 's': r, 'p': s}

    ans = 0

    for i in range(k):
        next_skip = False
        for j in range(i, n, k):
            if next_skip:
                next_skip = False
                continue
            if j + k < n and t[j] == t[j + k]:
                next_skip = True
            ans += point[t[j]]
    print(ans)

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


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

    n_win = {"r": 0, "s": 0, "p": 0}
    lock = [False] * N
    for i in range(K):
        n_win[T[i]] += 1
        lock[i] = True
    for i in range(K, N):
        if T[i-K] == T[i]:
            if not lock[i-K]:
                n_win[T[i]] += 1
                lock[i] = True
        else:
            n_win[T[i]] += 1
            lock[i] = True

    ans = R * n_win["s"] + S * n_win["p"] + P * n_win["r"]
    print(ans)


if __name__ == "__main__":
    main()

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



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

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

array=[True]*len(T)

for t in range(K,len(T)):
  if T[t-K] == T[t] and array[t-K]==True:
    array[t]=False
  

for t in range(len(T)):
  if T[t]=="r" and array[t]==True:
    ans += P
  elif T[t]=="s" and array[t]==True:
    ans += R
   
  elif T[t]=="p"and array[t]==True:
    ans += S

print(ans)

Solution (Python):
n,k=map(int, input().split())
R,S,P=map(int, input().split())
t=list(input())
aite=[]
tetuple=('r','s','p')
def kati(te,ans):
  if te=='r':
    ans+=P
  elif te=='s':
    ans+=R
  else:
    ans+=S
  return ans
for _ in range(k):
  aite.append([])
for i in range(n):
  aite[i%k].append(t[i])
ans=0
for i in range(k):
  ans=kati(aite[i][0],ans)
  for j in range(1,len(aite[i])):
    if aite[i][j]!=aite[i][j-1]:
      ans=kati(aite[i][j],ans)
    else:
      for m in range(3):
        if j!=len(aite[i])-1 and aite[i][j]==aite[i][j+1]==tetuple[m]:
          aite[i][j]=tetuple[(m+1)%3]
        elif j!=len(aite[i])-1 and aite[i][j]!=aite[i][j+1] and aite[i][j]!=tetuple[m] and aite[i][j+1]!=tetuple[m]:
          aite[i][j]=tetuple[m]
print(ans)

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

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

ans = 0
for mod in mods:
    L = len(mod)
    before = 'N'
    for i in range(L):
        if mod[i] != before:
            if mod[i] == 'r':
                ans += P
            elif mod[i] == 's':
                ans+= R
            else:
                ans+= S
            before = mod[i]
        else:
            before = 'N'


print(ans)

Solution (Python):
def check(list_num,test):
  dp = [[0] * 3 for i in range(len(list_num) + 1)]

  for i in range(len(list_num)):
    for j in range(3):
      if(int(list_num[i]) == int(j)):
        dp[i + 1][j] = max(dp[i][(j + 1) % 3], dp[i][(j + 2) % 3]) + test[j]
      else:
        dp[i + 1][j] = max(dp[i][(j + 1) % 3], dp[i][(j + 2) % 3])

  return max(dp[-1])

def main():
  n,k = map(int, input().split())
  r,s,p = map(int, input().split())
  j = [r,s,p]
  janken = list(input())
  t = []
  for i in janken:
    if(i == 'r'):
      t.append(2)
    elif(i == 's'):
      t.append(0)
    elif(i == 'p'):
      t.append(1)
      
  count = 0
  list_num = [[] for i in range(k)]

  for i in range(len(t)):
    list_num[i % k].append(t[i])


  for i in range(k):
    count += check(list_num[i], j)

  print(count)

if __name__ == '__main__':
  main()

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

dp = [0] * n
commands = [""] * n

for i in range(n):
    if t[i] == "r":
        commands[i] = "p"
        dp[i] = p
    elif t[i] == "s":
        commands[i] = "r"
        dp[i] = r
    elif t[i] == "p":
        commands[i] = "s"
        dp[i] = s

    if k <= i:
        if commands[i] == commands[i-k]:
            commands[i] = ""
            dp[i] = 0

print(sum(dp))

Solution (Python):

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

answer = 0
for i, t in enumerate(T):
    if t == 's':
        if i - K >= 0:
            if T[i-K] != t:
                answer += R
            else:
                T[i]=0
        else:
            answer += R
    elif t == 'p':
        if i - K >= 0:
            if T[i-K] != t:
                answer += S
            else:
                T[i]= 0
        else:
            answer += S
    elif t == 'r':
        if i - K >= 0:
            if T[i-K] != t:
                answer += P
            else:
                T[i]=0
        else:
            answer += P
    
print(answer)

Solution (Python):
def add_point(string):
    global ans
    if string=="r":
        ans+=p
    elif string=="s":
        ans+=r
    else:
        ans+=s

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

ans=0

for i in range(n):
    if i<k:
        add_point(S[i])
    else:
        if S[i]==S[i-k]:
            if i+k<n and S[i]==S[i+k]:
                S[i]="a"
            continue
        add_point(S[i])
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
a=(t.count("r"))*p+(t.count("s"))*r+(t.count("p"))*s
for i in range(n-k):
    if t[i]==t[i+k]:
        if t[i]=="r":
            a-=p
        elif t[i]=="s":
            a-=r
        elif t[i]=="p":
            a-=s
        t[i+k]=i
print(a)

Solution (Python):


n,k=map(int,input().split())



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

point={}
point["r"]=p
point["p"]=s
point["s"]=r


t=input()
len_t=len(t)

tt=[[] for _ in range(k)]
for i in range(len_t):
    tt[i%k].append(t[i])

result=0
for item in tt:
    back_number=item[0]
    cnt=1
    for item2 in item[1:]:
        if item2 == back_number:
            cnt+=1
        else:
            result+=point[back_number]*((cnt*(-1))//2)*(-1)
            cnt=1
            back_number=item2
    result+=point[back_number]*((cnt*(-1))//2)*(-1)  

print(result)

Solution (Python):
def main():
    n,k = map(int,input().split(" "))
    r,s,p = map(int,input().split(" "))
    Ts = list(input())
    flags = [True]*(n)
    ks = {"r":0, "s":0, "p":0}
    for i in range(k,n):
        if Ts[i]==Ts[i-k] and flags[i-k]:
            ks[Ts[i]]+=1
            flags[i] = False

    sum = (Ts.count("r")-ks["r"])*p+(Ts.count("s")-ks["s"])*r+(Ts.count("p")-ks["p"])*s

    print(sum)
main()

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
T = input()
t = list(T)
h = 0
for i in range(len(t)):
    if t[i] == 'r':
        h += p
    elif t[i] == 's':
        h += r
    elif t[i] == 'p':
        h += s
for i in range(k,len(t)):
    if t[i] == t[i-k]:
        if t[i] == 'r':
            h -= p
            t[i] = 'g'
        elif t[i] == 's':
            h -= r
            t[i] = 'g'
        elif t[i] == 'p':
            h -= s
            t[i] = 'g'
    else:
        continue
print(h)

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

Pred = []
dp = []

for i in range(K,N):
    if(T[i] == T[i-K]):
        T[i] = 'x'
    else:
        continue

x = T.count('r')
y = T.count('s')
z = T.count('p')

print(P * x + R * y + S * z)

Solution (Python):

import numpy as np
from collections import deque
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
t = input()

win_hand = {'r': 'p', 's': 'r', 'p': 's'}
win_point = {'r': p, 's': r, 'p': s}
history = []
points = 0











for i, s in enumerate(t):
    if(i < k):
        history.append(win_hand[s])
        points += win_point[s]
    else:
        if (history[i - k] == win_hand[s]):
            history.append('other')
            continue
        else:
            points += win_point[s]
            history.append(win_hand[s])
print(points)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=input()
before=["a"]*k
dic={"r":a[2],"s":a[0],"p":a[1]}
ans=0
for i,s in enumerate(l):
    i=i%k
    if before[i]!=s:
        ans+=dic[s]
        before[i]=s
    else:
        before[i]="a"
print(ans)

Solution (Python):
N, K = map(int, input().split())
S, P, R = map(int, input().split())
T = input()
dic = {'s':S,'p':P,'r':R}
def solve(T):
    ans = 0
    for i in range(K):
        ans += dic[T[i]]
        serial = 1
        for j in range(i+K,N,K):
            if T[j]==T[j-K]:
                serial += 1
            else:
                serial = 1
            if serial%2==1:
                ans += dic[T[j]]
    return ans
print(solve(T))

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

C = ["s", "r", "p"]
Win = {"s": "r", "r": "p", "p": "s"}
Point = {"r": R, "p": P, "s": S}

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

res = 0
for g in Group:
    n = len(g)
    DP = [[0 for _ in range(3)] for _ in range(n + 1)]
    for index, tmp in enumerate(C):
        if Win[g[0]] == tmp:
            DP[0][index] += Point[tmp]
    for i in range(n - 1):
        for j, c in enumerate(C):
            p = 0
            if c == Win[g[i + 1]]:
                p = Point[c]
            if j == 0:
                DP[i + 1][j] = max(DP[i][1], DP[i][2]) + p
            elif j == 1:
                DP[i + 1][j] = max(DP[i][0], DP[i][2]) + p
            else:
                DP[i + 1][j] = max(DP[i][0], DP[i][1]) + p
    res += max(DP[n - 1][0], DP[n - 1][1], DP[n - 1][2])
    
    
    

print(res)

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

C = ["s", "r", "p"]
Win = {"s": "r", "r": "p", "p": "s"}
Point = {"r": R, "p": P, "s": S}

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

res = 0
for g in Group:
    n = len(g)
    DP = [[0 for _ in range(3)] for _ in range(n + 1)]
    for index, tmp in enumerate(C):
        if Win[g[0]] == tmp:
            DP[0][index] += Point[tmp]
    for i in range(n - 1):
        for j, c in enumerate(C):
            p = 0
            if c == Win[g[i + 1]]:
                p = Point[c]
            if j == 0:
                DP[i + 1][j] = max(DP[i][1], DP[i][2]) + p
            elif j == 1:
                DP[i + 1][j] = max(DP[i][0], DP[i][2]) + p
            else:
                DP[i + 1][j] = max(DP[i][0], DP[i][1]) + p
    res += max(DP[n - 1][0], DP[n - 1][1], DP[n - 1][2])
    
    
    

print(res)

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

t = input()
me = []
ans = 0
for i in range(len(t)):
    if i < k:
        if t[i] == "r":
            me.append("p")
            ans += p
        elif t[i] == "s":
            me.append("r")
            ans += r
        else:
            me.append("s")
            ans += s
    else:
        if t[i] == "r" and me[i-k] != "p":
            me.append("p")
            ans += p
        elif t[i] == "s" and me[i-k] != "r":
            me.append("r")
            ans += r
        elif t[i] == "p" and me[i-k] != "s":
            me.append("s")
            ans += s
        else:
            me.append("?")
print(ans)

Solution (Python):
n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = input()
te = ["" for i in range(k)]
ans = 0
for i in range(n):
    if not t[i] == te[i%k]:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
        te[i%k] = t[i]
    else:
        te[i%k] = ""

print(ans)

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



res = 0
p = 0
commands = []
for i, t in enumerate(T):
    if t == 'r':
        commands.append('p')
        p = P
    elif t == 's':
        commands.append('r')
        p = R
    else:
        commands.append('s')
        p = S

    if (i - K) >= 0 and commands[i - K] == commands[i]:
        p = 0
        commands[i] = 'x'

    res += p

print(res)

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

point = [0] * n
for i in range(n):
    if t[i] == 'r':
        point[i] = p
    elif t[i] == 's':
        point[i] = r
    elif t[i] == 'p':
        point[i] = s

for i in range(k, n):
    if (t[i-k] == t[i]) and point[i-k]>0:
        point[i] = 0
print(sum(point))

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

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

for i,t in enumerate(T):
    RSP[i%K].append(t)

ans = 0
road = []
for row in RSP:
    memo = 'baka'
    for a in row:
        if memo != a:
            if a == 'r':
                ans += P
                road.append('p')
            elif a == 's':
                ans+= R
                road.append('r')
            else:
                ans+= S
                road.append('s')
            memo = a
        else:
            memo = 'baka'

print(ans)

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

Solution (Python):


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

myhand=[]
for item in T:
    if item == "r":
        myhand.append("p")
    elif item == "s":
        myhand.append("r")
    else:
        myhand.append("s")

for i in range(N-K):
    if myhand[i]==myhand[i+K]:
        myhand[i+K]="-"
    
print(myhand.count("r")*R+myhand.count("s")*S+myhand.count("p")*P)

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

ans = {"r": 0, "s": 0, "p": 0}


def win(sr):
    if sr == "r":
        return "p"
    elif sr == "s":
        return "r"
    elif sr == "p":
        return "s"


for i in range(N):
    for j in range(K):
        if i * K + j >= N:
            break
        if T[i*K+j] != before[j]:
            ans[win(T[i*K+j])] += 1
            before[j] = T[i*K+j]
        else:
            before[j] = 0


print(ans["r"]*R + ans["s"]*S + ans["p"]*P)

Solution (Python):




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


d = dict()
d['r'] = r
d['s'] = s
d['p'] = p

d['wintr'] = 'p'
d['wints'] = 'r'
d['wintp'] = 's'

ngt = [''] * n


ans = 0
for i in range(n-1, -1, -1):
    wint = d['wint' + t[i]]
    if ngt[i] != wint:
        ans = ans + d[wint]
        if i - k >= 0:
            ngt[i-k] = wint


print(ans)

Solution (Python):
def solve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    dic = {"r": P, "s": R, "p": S} 
    T = list(input())
    before = [-1] * (K)
    ans = 0
    for i, j in enumerate(T):
        if not before[i % K] == j:
            before[i % K] = j
            ans += dic[j]
        else:
            before[i%K] = -1
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import math
import sys

n, k = map(int, sys.stdin.readline().rstrip().split())
r, s, p = map(int, sys.stdin.readline().rstrip().split())
t = sys.stdin.readline().rstrip()
ans = 0
sim = []
for i, ti in enumerate(t):
    if ti == 'r':
        if i >= k and sim[i - k] == 'p':
            sim.append('x')
        else:
            ans += p
            sim.append('p')
    elif ti == 's':
        if i >= k and sim[i - k] == 'r':
            sim.append('x')
        else:
            ans += r
            sim.append('r')
    else:
        if i >= k and sim[i - k] == 's':
            sim.append('x')
        else:
            ans += s
            sim.append('s')

print(ans)

Solution (Python):
if __name__ == '__main__':
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    elec = []
    point = 0
    for i in range(k):
        if t[i] == "r":
            point += p
            elec.append("r")
        if t[i] == "s":
            point += r
            elec.append("s")
        if t[i] == "p":
            point += s
            elec.append("p")

    for i in range(k, n):
        if elec[i - k] == t[i]:
            
            elec.append(" ")
        else:
            if t[i] == "r":
                point += p
                elec.append("r")
            if t[i] == "s":
                point += r
                elec.append("s")
            if t[i] == "p":
                point += s
                elec.append("p")
    print(point)

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
res = 0
k = ''
log = []
for i in range(N):
  t = T[i]
  if t == 'r':
    if K <= i and log[i-K] == 'p':
      log.append('x')
    else:
      res += P
      log.append('p')
  elif t == 's':
    if K <= i and log[i-K] == 'r':
      log.append('x')
    else:
      res += R
      log.append('r')
  else:
    if K <= i and log[i-K] == 's':
      log.append('x')
    else:
      res += S
      log.append('s')

print(res)