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):
def main():

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

  point=dict()
  point["p"]=s  
  point["s"]=r  
  point["r"]=p  
  point["checked"]=0

  ans=0
  for i in range(n):
    tmp=i-k
    if tmp>=0 and t[tmp]==t[i]:
      ans+=0
      t[i]="checked"
    else:
      ans+=point[t[i]]
  print(ans)

main()

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

point=dict()
point["p"]=s  
point["s"]=r  
point["r"]=p  
point["checked"]=0

ans=0
for i in range(n):
  tmp=i-k
  if tmp>=0 and t[tmp]==t[i]:
    ans+=0
    t[i]="checked"
  else:
    ans+=point[t[i]]
print(ans)

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

subGames = [[] for _ in range(k)]

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

subPts = [0 for _ in range(k)]
for i, hand in enumerate(t):
    subGame = subGames[i % k]
    winPt = pattern[hand]
    winHand = win[hand]
    if subGame and win[hand] == subGame[-1]:
        winPt = 0
        winHand = t[i+k] if i+k < n else hand

    subGame.append(winHand)
    subPts[i % k] += winPt

ans = sum(subPts)

print(ans)

Solution (Python):


n, k = map(int, input().split())
R, S, P = map(int, input().split())

dic = {'r': P, 's': R, 'p': S}
t = list(input())
tf_list = [0] * n
ans = 0

u = [[] for _ in range(k)]


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

for i in range(k, n):
    if u[i%k] != t[i]:
        ans += dic[t[i]]
        u[i%k] = t[i]
    elif u[i%k] == t[i] and tf_list[i-k] == 1:
        ans += dic[t[i]]
        tf_list[i] = 0
    elif u[i%k] == t[i] and tf_list[i-k] == 0:
        tf_list[i] = 1

print(ans)

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

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
pattern = {
    'r':P,
    's':R,
    'p':S
}
ret = 0
for i in range(N):
    tmp = i - K
    if tmp >= 0 and T[tmp] == T[i]:
        T[i] = ''
        continue
    ret += pattern[T[i]]
print(ret)

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

    ans = 0
    for k in range(K):
        slicedT = T[k::K]
        dp = [[0] * 3 for i in range(len(slicedT)+1)]

        for i in range(len(slicedT)):
            for frm in range(3):
                for to in range(3):
                    if frm == to:
                        continue
                    dp[i+1][to] = max(dp[i+1][to], dp[i][frm] + calcPoint(to,
                                                                        slicedT[i],
                                                                        R,
                                                                        S,
                                                                        P ))
                    
        ans += max(*dp[-1])
    
    print(ans)


def calcPoint(player, opponent, R, S, P):
    if player == 0 and opponent == 's':
        return R
    if player == 1 and opponent == 'p':
        return S
    if player == 2 and opponent == 'r':
        return P
        
    return 0

if __name__ == '__main__':
    solve()

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

count = 0

arr = [True] * n
for i in range(n):
    if i >= k:
        if t[i] == t[i-k]:
           if arr[i-k]:
               arr[i] = False
               
for i in range(n):
    if arr[i]:
        if t[i] == "r":
            count += p
        elif t[i] == "s":
            count += r
        else:
            count += s

print(count)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
res_list = {'r':'p','s':'r','p':'s'}
t=input()
res = [0]*n
ans = 0
for i,rsp in enumerate(t):
    res_rsp = res_list[rsp]
    if i<k or res[i-k] != res_rsp:
        ans += eval(res_rsp)
        res[i] = res_rsp
print(ans)

Solution (Python):
import sys

nk = list(input().split(" "))
n = int(nk[0])
k = int(nk[1])
rsp = list(input().split(" "))
r = int(rsp[0])
s = int(rsp[1])
p = int(rsp[2])
T = str(input())

my_choice = [""] * n
ans = 0

for i in range(0,k):
    if T[i] == "r":
        my_choice[i] = "p"
        ans += p
    elif T[i] == "s":
        my_choice[i] = "r"
        ans += r
    else:
        my_choice[i] = "s"
        ans += s

if k == n:
    print(ans)
    sys.exit()

for i in range(k,n):
    if T[i] == "r" and my_choice[i - k] != "p":
        my_choice[i] = "p"
        ans += p
    elif T[i] == "s" and my_choice[i - k] != "r":
        my_choice[i] = "r"
        ans += r
    elif T[i] == "p" and my_choice[i - k] != "s":
        my_choice[i] = "s"
        ans += s


print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

from bisect import *





INF = 500000
MOD = 10**9+7

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
for i in range(0, min(K, N)):
    if T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
    flg = True
    for j in range(i+K, N, K):
        if T[j-K] == T[j] and flg:
            flg = False
        else:
            if T[j] == 'r':
                ans += P
            elif T[j] == 's':
                ans += R
            else:
                ans += S
            flg = True
print(ans)

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

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

ans = 0
for t in T:
    ans += points[t]

for k in range(K):
    left = k
    while left < N:
        right = left
        while right < N and T[right] == T[left]:
            right += K
        cnt = (right - left) // K
        ans -= points[T[left]] * (cnt // 2)
        left = right
print(ans)

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

def win_hand(machine):
  if machine == "s":
    return "r"
  elif machine == "p":
    return "s"
  elif machine == "r":
    return "p"

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

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

ans = 0
for i in range(K):
  player = []
  for j in range(len(t_sorted[i])):
    if j == 0:
      player.append(win_hand(t_sorted[i][j]))
      ans += judge(t_sorted[i][j])
    else:
      if win_hand(t_sorted[i][j]) == player[j-1]:
        if j != len(t_sorted[i])-1:
          if "r" != win_hand(t_sorted[i][j]) and "r" != win_hand(t_sorted[i][j+1]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]) and "s" != win_hand(t_sorted[i][j+1]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]) and "p" != win_hand(t_sorted[i][j+1]):
            player.append("p")
        else:
          if "r" != win_hand(t_sorted[i][j]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]):
            player.append("p")
      else:
          player.append(win_hand(t_sorted[i][j]))
          ans += judge(t_sorted[i][j])

print(ans)

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

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

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
S = input()
ans = 0
hand = ""
dic = {"r":"p","p":"s","s":"r"}
for i in range(n):
    if i < k:
        if S[i] == "r":
            ans += p
            hand += "p"
        elif S[i] == "p":
            ans += s
            hand += "s"
        else:
            ans += r
            hand += "r"
   
    else:
        if dic[S[i]] == hand[i-k]:
            hand += "q"
            continue
        else:
            if S[i] == "r":
                ans += p
                hand += "p"
            elif S[i] == "p":
                ans += s
                hand += "s"
            else:
                ans += r
                hand += "r"
        
print(ans)

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

ans = ""
score = 0

for i, t in enumerate(T):
    if t == 'r':
        if i >= K and "p" == ans[-K]:
            ans += " "
        else:
            ans += 'p'
            score += P    

    elif t == 's':
        if i >= K and "r" == ans[-K]:
            ans += " "
        else:
            ans += 'r'
            score += R 
    else:
        if i >= K and 's' == ans[-K]:
            ans += " "
        else:
            ans += 's'
            score += S
print(score)

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

R,S,P=map(int,input().split())
total=0
I=input()
count=0
T=list(I)
for i in range(N):
    if T[i]=='r':
        total+=P
    if T[i]=='s':
        total+=R
    if T[i]=='p':
        total+=S
for k in range(K):
    for i in range(count,N,K):
        if T[i-K]==T[i] and i>=K :
            if T[i] == 'r':
                total -= P
            if T[i] == 's':
                total -= R
            if T[i] == 'p':
                total -= S
            T[i]='Y'
    count+=1

print(total)

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

pre = [[] for i in range(k)]

for i in range(n):
    
    pre[i%k].append(t[i])


def score(h):
    c_r = 0
    c_s = 0
    c_p = 0
    num = "".join(h)
    count = 1
    result = ''

    
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
                count = count + 1
            else:
                result = result + str(num[i]) + str(count)
                ko = num[i]
                wa = count 
                if wa%2 == 0:
                    if ko == "r":
                        c_r += wa//2
                    elif ko == "s":
                        c_s += wa//2
                    else:
                        c_p += wa//2
                else:
                    if ko == "r":
                        c_r += 1 + (wa-1)//2
                    elif ko == "s":
                        c_s += 1 + (wa-1)//2
                    else:
                        c_p += 1 + (wa-1)//2
                count = 1
        except:
            
            result = result + str(num[i]) + str(count)
            ko = num[i]
            wa = count 
            if wa%2 == 0:
                if ko == "r":
                    c_r += wa//2
                elif ko == "s":
                    c_s += wa//2
                else:
                    c_p += wa//2
            else:
                if ko == "r":
                    c_r += 1 + (wa-1)//2
                elif ko == "s":
                    c_s += 1 + (wa-1)//2
                else:
                    c_p += 1 + (wa-1)//2

    
    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[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:
        if T[i]=="r":
            ans+=P
        elif T[i]=="s":
            ans+=R
        else:
            ans+=S
            
    else:
        if T[i]=="r":
            if T[i-K]!="r":
                ans+=P
            else:
                T[i]="-"
        elif T[i]=="s":
            if T[i-K]!="s":
                ans+=R
            else:
                T[i]="-"
        else:
            if T[i]=="p":
                if T[i-K]!="p":
                    ans+=S
                else:
                    T[i]="-"
print(ans)

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

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


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

for i in range(K, N):
  
  if T[i - K] == T[i]:
    
    T[i] = ""
    continue
  ans += best_score[T[i]]
print(ans)

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

count = [0] * 3
flag = [False] * N
for i in range(N):
    if i >= K and T[i - K] == T[i]:
        if flag[i - K]:
            pass
        else:
            flag[i] = True
            continue
    if T[i] == 'r':
        count[0] += 1
    elif T[i] == 's':
        count[1] += 1
    else:
        count[2] += 1

            

print(count[0] * P + count[1] * R + count[2] * S)

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

    can_win = {'r': 'p', 's': 'r', 'p': 's'}
    win_hand = [can_win[ch] for ch in T]
    points = {'r': R, 's': S, 'p': P}

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

    return sum(win_hand.count(ch) * points[ch] for ch in ('r', 's', 'p'))

print(d_prediction_and_restriction())

Solution (Python):



num,k = map(int,input().split(" "))
r,s,p = map(int,input().split(" "))
machine = list(input())

score = 0
score_list = []
for i in range(num):
    if i <= k - 1:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    elif machine[i] != machine[i - k]:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    elif score_list[i - k] == 0:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    else:
        score_list.append(0)
    
    

print(score)

Solution (Python):
def main():
    n,k = map(int,input().split())
    r,s,p = map(int,input().split())
    T = list(input()) 
    score = {"r": p, "s":r, "p":s} 
    ans = sum([score[T[i]] for i in range(k)]) 
    for i in range(k,n):
        if T[i]==T[i-k]:
            T[i]="." 
            continue
        ans += score[T[i]]
    print(ans)
main()

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

for i in range(n-k):
    if T[i] == T[i+k]:
        T[i+k] = "x"
cr = T.count("r")
cp = T.count("p")
cs = T.count("s")
print(cp*s+cr*p+cs*r)

Solution (Python):
from collections import deque

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

que = deque([], maxlen=k)

score = 0
for i, t in enumerate(ttt):
  if i >= k and ttt[i-k] == t:
    t_ = que.popleft()
    if t_ is not None:
      que.append(None)
      continue
      
  if t == 'r':
    score += p
  elif t == 's':
    score += r
  else:
    score += s
  
  que.append(True)
else:
  print(score)

Solution (Python):

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






ans = 0

for i in range(N):
    if T[i] == 'r':
        
        if i < K or T[i - K] != 'r':
            ans += P
        else:
            T = T[:i] + 'x' + T[i + 1:]

    elif T[i] == 's':
        
        if i < K or T[i - K] != 's':
            ans += R
        else:
            T = T[:i] + 'x' + T[i + 1:]

    elif T[i] == 'p':
        
        if i < K or T[i - K] != 'p':
            ans += S
        else:
            T = T[:i] + 'x' + T[i + 1:]

print(ans)

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

dict_sample = {
    "r":P,
    "s":R,
    "p":S
}
ans = 0

A = [[] for k in range(K)]
for i in range(N):
    A[i%K].append(T[i])
    ans += dict_sample[T[i]]

for k in range(K):
    cnt = 0
    tmp = A[k]
    for i in range(len(tmp)-1):
        if tmp[i]!=tmp[i+1]:
            if cnt > 0:
                cnt+=1
                ans -= (cnt // 2 ) * dict_sample[tmp[i]]
            cnt = 0
        else:
            cnt += 1

    if cnt>0:
        cnt+=1
        ans -= (cnt // 2 ) * dict_sample[tmp[-1]]
        
print(ans)

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

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

hands = [None]*N
ans = 0

for i in range(N):
    if T[i] == 'r':
        win = 'p'
    elif T[i] == 's':
        win = 'r'
    else:
        win = 's'
    
    if i < K:
        hands[i] = win
        ans += SCORE[win]
    else:
        if hands[i-K] == win:
            hands[i] = None
        else:
            hands[i] = win
            ans += SCORE[win]
            
            
print(ans)

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

mo = [[] for i in range(k)]

for i in range(n):
    num = i % k
    mo[num].append(T[i])
    
mo1 = copy.deepcopy(mo)
for i in range(k): 
    for j in range(len(mo[i])):
        if mo[i][j] == "r":
            mo1[i][j] = "p"
        elif mo[i][j] == "p":
            mo1[i][j] = "s"
        else:
            mo1[i][j] = "r"
        if j > 0 and mo1[i][j] == mo1[i][j-1]:
            mo1[i][j] = "x"

ans = 0
for i in range(k):
    for j in range(len(mo1[i])):
        if mo1[i][j] == "r":
            ans += r
        elif mo1[i][j] == "p":
            ans += p
        elif mo1[i][j] == "s":
            ans += s
print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=str(input())
c=int(0)
def get(x,c):
  if x=="r":
    c+=P
  elif x=="s":
    c+=R
  else:
    c+=S
  return c
n=len(T)
for i in range(n):
  if i<=K-1:
    c=get(T[i],c)
  else:
    if T[i-K]!=T[i]:
      c=get(T[i],c)
    if T[i-K]!="o" and T[i-K]==T[i]:
      T=T[:i]+"o"+T[i+1:]
print(c)

Solution (Python):
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
r, s, p = [int(i) for i in sys.stdin.readline().split()]
t = input()
score_dic = {"r":p, "s":r,"p":s}
f = [1 for i in range(n)]
score = 0
for i, (_t, flg) in enumerate(zip(t, f)):
    if i >= k and t[i-k] == _t and flg:
        if i < n - k:
            f[i+k] = 0
    else:
        score += score_dic[_t]
print(score)

Solution (Python):

def get_win_hand(com_hand):
    if com_hand == 'r':
        return 'p'
    elif com_hand == 's':
        return 'r'
    elif com_hand == 'p':
        return 's'

def get_score(my_hand, R, S, P):
    if my_hand == 'r':
        return R
    elif my_hand == 's':
        return S
    elif my_hand == 'p':
        return P
        
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())

score_list = [0] * K
for i in range(K):
    tmp_score = 0
    before_my_hand = '*'
    partialT = T[i::K]
    for com_hand in partialT:
        my_hand = get_win_hand(com_hand)
        if my_hand == before_my_hand:
            my_hand = '*'
        else:
            tmp_score += get_score(my_hand, R, S, P)
        before_my_hand = my_hand
    score_list[i] = tmp_score
    
print(sum(score_list))

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

rsp={"r":0, "s":1 , "p":2}
n,k=map(int,input().split())
rsp_points=list(map(int,input().split()))
T=input()
for i in range(k):
    dp=[[0]*3 for i in range(2)]
    odev=0
    for ti in range(i,n,k):
        now = (rsp[T[ti]]+2)%3
        rspp=rsp_points[now]
        dp[(odev+1)%2][now] = max(rspp+dp[odev][(now+1)%3],rspp+dp[odev][(now+2)%3])
        dp[(odev+1)%2][(now+1)%3] = max(dp[odev][now],dp[odev][(now+2)%3])
        dp[(odev+1)%2][(now+2)%3] = max(dp[odev][(now+1)%3],dp[odev][now])
        
        odev=(odev+1)%2
    ans+=max(dp[odev])
    
    
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()
ans=0
for i in range(k):
	bef=None
	for c in t[i::k]:
		if c==bef:
			bef = None
			pass
		else:
			ans+=d[c]
			bef=c
print(ans)

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

def didWin(idx, c):
    if c == 'r' and t[idx] == 's':
        return True
    if c == 's' and t[idx] == 'p':
        return True
    if c == 'p' and t[idx] == 'r':
        return True
    return False

def solve(mod):
    l = math.ceil((n - mod) / k)
    dp = [[0] * 4 for _ in range(l + 1)]

    for i in range(l):
        
        if didWin(i * k + mod, 'r'):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        
        if didWin(i * k + mod, 's'):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        
        if didWin(i * k + mod, 'p'):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

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

def didWin(idx, c):
    if c == 'r' and t[idx] == 's':
        return True
    if c == 's' and t[idx] == 'p':
        return True
    if c == 'p' and t[idx] == 'r':
        return True
    return False

def solve(mod):
    
    
    
    
    
    
    

    
    l = math.ceil((n - mod) / k)
    
    dp = [[0] * 4 for _ in range(l + 1)]

    for i in range(l):
        
        if didWin(i * k + mod, 'r'):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        
        if didWin(i * k + mod, 's'):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        
        if didWin(i * k + mod, 'p'):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    
    
    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N,K = II()
R,S,P = II()
T = str(input())

def calc(c):
    if c=='r':
        return P
    elif c=='s':
        return R
    else:
        return S

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

ans = 0
for i in range(K):
    for j,c in enumerate(d[i]):
        if j==0:
            ans += calc(c)
            before = c
        else:
            if c!=before:
                ans += calc(c)
                before = c
            else:
                before = 'z'

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}

ans = 0
for x in range(K):
    Tx = list(T[x::K])

    tmp = 0
    for i in range(len(Tx)):
        if i == 0:
            tmp += win[Tx[i]]
            continue
        if Tx[i] != Tx[i - 1]:
            tmp += win[Tx[i]]
        else:
            Tx[i] = "?"

    ans += tmp

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
lst = [False] * n
for i in range(n):
  if i < k or t[i] != t[i-k] or not lst[i-k]:
    ans += p if t[i] == 'r' else r if t[i] == 's' else s
    lst[i] = True
print(ans)

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

ans = 0
d = dict()
for i in range(n):
    if t[i] == "r" and d.get(i) != "r":
        ans += p
        d[i+k] = "r"
    elif t[i] == "s" and d.get(i) != "s":
        ans += r
        d[i+k] = "s"
    elif t[i] == "p" and d.get(i) != "p":
        ans += s
        d[i+k] = "p"

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t = input()
te = {"r":"p", "s":"r", "p":"s"}
win_hand = [te[t[i]] for i in range(len(t))]
ans = "".join(win_hand)
for i in range(k, len(t)):
    if ans[i] == ans[i-k]:
        ans = ans[:i] + "x" + ans[i+1:]

cnt = ans.count('r')*r + ans.count('s')*s + ans.count('p')*p
print(cnt)

Solution (Python):
import math
import bisect
import collections
import itertools
def gcd(a,b):return math.gcd 
def lcm(a,b):return (a*b) // math.gcd(a,b) 
def iin(): return int(input()) 
def imn(): return map(int, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def comb(l, n): return itertools.combinations(l, n) 
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True

N,K = imn()
RSP = iln()
T = input()

Point = {}
Point['r'] = RSP[2]
Point['s'] = RSP[0]
Point['p'] = RSP[1]

ans = 0
for i in range(len(T)):
    prev = i - K
    if prev >= 0 and T[prev] == T[i]:
        T = T[:i] + 'x' + T[i+1:]
        continue
    ans += Point[T[i]]
print(ans)

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


win_hand = {"r": "p", "s": "r", "p": "s"}
ANS = []


for i in range(N):
    ANS.append(win_hand[T[i]])


ans = "".join(ANS)


for i in range(K, N):
    
    if ans[i] == ans[i-K]:
        ans = ans[:i] + "x" + ans[i+1:]


point = ans.count("r")*R + ans.count("s")*S + ans.count("p")*P

print(point)