Problem:
 Score : 400 points    Problem Statement At an arcade, Takahashi is playing a game called RPS Battle , which is played as follows:   The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)  Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):  R points for winning with Rock;  S points for winning with Scissors;  P points for winning with Paper.    However, in the i -th round, the player cannot use the hand he/she used in the (i-K) -th round. (In the first K rounds, the player can use any hand.)   Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.  The information Takahashi obtained is given as a string T . If the i -th character of T  (1 \leq i \leq N) is r , the machine will play Rock in the i -th round. Similarly, p and s stand for Paper and Scissors, respectively.  What is the maximum total score earned in the game by adequately choosing the hand to play in each round?      Notes In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.   If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;  if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;  if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;  if both players play the same hand, it is a draw.       Constraints  2 \leq N \leq 10^5  1 \leq K \leq N-1  1 \leq R,S,P \leq 10^4  N,K,R,S, and P are all integers.  |T| = N  T consists of r , p , and s .         Input Input is given from Standard Input in the following format:  N  K  R  S  P  T       Output Print the maximum total score earned in the game.        Sample Input 1 5 2
8 7 6
rsrpr      Sample Output 1 27  The machine will play {Rock, Scissors, Rock, Paper, Rock}.  We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27 .       Sample Input 2 7 1
100 10 1
ssssppr      Sample Output 2 211       Sample Input 3 30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr      Sample Output 3 4996

Solution (Python):
N, k = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
already = set()
for i in range(len(T)):
    t_now = T[i]
    point = 0
    if t_now == "r":
        point = P
    elif t_now == "p":
        point = S
    else:
        point = R

    if i >= k:
        t_before = T[i - k]
        if t_now == t_before and (i - k) not in already:
            point = 0
            already.add(i)
    ans += point
    
print(ans)

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

d = {'r': P, 's': R, 'p': S}
result = 0
wins = [False] * K
for i in range(len(T)):
    if not wins[i % K] or T[i] != T[i - K]:
        result += d[T[i]]
        wins[i % K] = True
    else:
        wins[i % K] = False
print(result)

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

draws = [True] * K
d = {'r': P, 's': R, 'p': S}
result = 0
for i in range(len(T)):
    if draws[i % K] or T[i] != T[i - K]:
        result += d[T[i]]
        draws[i % K] = False
    else:
        draws[i % K] = True
print(result)

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

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
hand = {"r" : p, "s" : r, "p" : s}
ans = 0
for n_ in range(n):
	temp = n_ - k
	if temp >= 0 and t[n_] == t[temp]:
		t[n_] = ""
	else:
		ans += hand[t[n_]]
print(ans)

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

dp = [[-10**9]*3for i in range(n+k)]
for i in range(k):
    dp[i][0],dp[i][1],dp[i][2] = 0,0,0
    
    
 
for i in range(k):
    for j in range(n//k+3):
        if i > n-1:continue
        if t[i] == "r":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0]+p,dp[i][1]+p)
        
        elif t[i] == "s":
            dp[i+k][0] = max(dp[i][1]+r,dp[i][2]+r)
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        
        elif t[i] == "p":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0]+s,dp[i][2]+s)
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        i += k
ans = 0    
for i in range(n,n+k):
    ans += max(dp[i])
print(ans)

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


def pon(computer, mine):
    if computer == 'r' and mine == 2: return P
    if computer == 's' and mine == 0: return R
    if computer == 'p' and mine == 1: return S
    return 0


dp = [[0 for _ in range(3)] for _ in range(N+10)]

ans = 0
for st in range(K):
    for i in range(st, N, K):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                if i+1-K >= 0:
                    dp[i+1][k] = max(dp[i+1][k], dp[i+1-K][j] + pon(T[i], k))
                else:
                    dp[i+1][k] = max(dp[i+1][k], pon(T[i], k))
    ans += max(dp[i+1])

print(ans)

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

subS = ""
for i in range(len(t)):
    if (t[i] == 'r'): subS += 'p'
    elif (t[i] == 'p'): subS += 's'
    else: subS += 'r'

sums = 0
already = [True]



for i in range(len(t)):
    if (i == 0):
        if(subS[i]== 'r'): sums += r
        elif(subS[i] == 'p'): sums += p
        else: sums += s
        continue   
    if (t[i] != t[i - k]):
        sums += (subS[i] == 'r')*r + (subS[i] == 'p')*p + (subS[i] == 's')*s
        already.append(True)
    else:
          if(i >= k and already[i - k]): already.append(False)
          else:
                already.append(True)
                sums += (subS[i] == 'r')*r + (subS[i] == 'p')*p + (subS[i] == 's')*s
print(sums)

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

ans = 0
pre = ""
for i in range(N):
    if i >= K and T[i - K]==T[i]:
        if T[i] == "r" and pre[i - K] == "p":
            pre += "x"
            continue
        if T[i] == "s" and pre[i - K] == "r":
            pre += "x"
            continue
        if T[i] == "p" and pre[i - K] == "s":
            pre += "x"
            continue

    if T[i] == "r":
        ans += P
        pre += "p"
    if T[i] == "s":
        ans += R
        pre += "r"
    if T[i] == "p":
        ans += S
        pre += "s"
print(ans)

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

result = 0

if len(T) > K:
    for i in range(K):
        if T[i] == 'r':
            result += P;
        elif T[i] == 's':
            result += R
        else:
            result += S
        M.append('win')
    for i in range(K, len(T)):
        if T[i - K] != T[i] or (T[i - K] == T[i] and M[i - K] == 'lose'):
            if T[i] == 'r':
                result += P
            elif T[i] == 's':
                result += R
            else:
                result += S
            M.append('win')
        else:
            M.append('lose')
            continue
else:
    for i in range(len(T)):
        if T[i] == 'r':
            result += P
        elif T[i] == 's':
            result += R
        else:
            result += S

print(result)

Solution (Python):




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


score = 0


string = ['' for i in range(K)]

for i in range(N) : 
    string[i%K] += T[i]
    

for h in range(K) : 
    n = len(string[h])
    
    dp = [[0 for j in range(3)] for i in range(n)] 
    
    for i in range(n) : 
        if i==0 : 
            if string[h][i] == 'r' : 
                dp[i][2] += P
            elif string[h][i] == 's' : 
                dp[i][0] += R
            elif string[h][i] == 'p' : 
                dp[i][1] += S
        else : 
            if string[h][i] == 'r' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2]))
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2]))
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1])+P)
            elif string[h][i] == 's' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2])+R)
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2]))
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1]))
            elif string[h][i] == 'p' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2]))
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2])+S)
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1]))
                    
    score += max(dp[i][0], dp[i][1], dp[i][2])

print(score)

Solution (Python):
n = input()    
m = n.split()
p = [int(i) for i in m]
n = input()    
m = n.split()
P = [int(i) for i in m]
n = input()
list = [""]*p[1]
for i, value in enumerate(n):
    list[i%p[1]] =list[i%p[1]] + value
A = 0
for i in list:
    able = [True,True,True]
    for j in i:
        if j == "r":
            if able[2]:
                A = A + P[2]
                able = [True,True,False]
            else:
                able = [True,True,True]
        if j == "s":
            if able[0]:
                A = A + P[0]
                able = [False,True,True]
            else:
                able = [True,True,True]
        if j == "p":
            if able[1]:
                A = A + P[1]
                able = [True,False,True]
            else:
                able = [True,True,True]

print(A)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
T=list(input())
for i in range(len(T)):
    if T[i] == "r":
        T[i] = "p"
    elif T[i] == "s":
        T[i] = "r"
    else:
        T[i] = "s"
for i in range(k): 
    j = i+k
    while j <= n-1: 
        if T[j-k] == T[j]:
            T[j] = "n"
        j = j+k
points = 0
for t in T:
    if t == "r":
        points += r
    elif t == "s":
        points += s
    elif t == "p":
        points += p
print(points)

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

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)

Solution (Python):


import copy
N,K=map(int,input().split())
R,S,P=map(int,input().split())
point=score={'r':R,'s':S,'p':P}
T=input()
l=[[]for i in range(K)]
for i in range(N):
  l[i%K].append(T[i])
def get_point(s1,s2):
    num={'r':0,'s':1,'p':2}
    if num[s1]==(num[s2]+1)%3:
        return point[s2]
    else:return 0
def rest(hand):
    if hand=='r':return['s','p']
    elif hand=='s':return['r','p']
    elif hand=='p':return['r','s']
def cal(mini_l):
    size=len(mini_l)
    kind=['r','s','p']
    score={'r':[],'s':[],'p':[]}
    for hand in kind:
        score[hand].append(get_point(mini_l[0],hand))
    for i in range(1,size):
        for hand in kind:
            choice=''
            tmp_score=-1
            for pre_hand in rest(hand):
                if score[pre_hand][i-1]+get_point(mini_l[i],hand)>tmp_score:
                    choice=pre_hand
                    tmp_score=score[pre_hand][i-1]+get_point(mini_l[i],hand)
            score[hand].append(tmp_score)
    tmp_score=-1
    for hand in kind:
        if score[hand][-1]>tmp_score:
            ans=hand
            tmp_score=score[hand][-1]
    return tmp_score
ans=0
for ll in l:
    ans+=cal(ll)
print(ans)

Solution (Python):
n, k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
def janken(a):
    global ans
    if "r" == a:
        return p
    if "s" == a:
        return r
    if "p" == a:
        return s

for i in range(k):
    ti = t[i::k]
    odd = []
    even = []
    before = ""
    for j in range(len(ti)):
        if ti[j] == before:
            if j % 2 == 0:
                even.append(ti[j])
            else:
                odd.append(ti[j])
        else:
            if j != 0:
                ans += janken(before)*max(len(odd),len(even))
                even = []
                odd = []
            if j % 2 == 0:
                even.append(ti[j])
            else:
                odd.append(ti[j])
        if j == len(ti)-1:
            ans += janken(ti[j])*max(len(odd),len(even))
        before = ti[j]

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)


input = sys.stdin.readline


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = list(input())
    M = {'r': p, 's': r, 'p': s}

    ans = 0
    win = [False]*n
    for i in range(n):
        if win[i-k] and t[i-k] == t[i]:
            continue
        ans += M[t[i]]
        win[i] = True

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N,K=[int(i) for i in input().split()]
R,S,P=[int(i) for i in input().split()]
T=list(input())
M={"r":P,"s":R,"p":S}

done=[]
point=0
for i in range(K):
    temp=T[i]
    point+=M[temp]
    done.append(temp)




for j in range(K,N):
    temp=T[j]
    back=done[j-K]
    
    if temp!=back:
        point+=M[temp]
        done.append(temp)
    else:
        done.append('u')

print(point)

Solution (Python):
def ABC149D():
	N, K = map(int,input().split())
	RSP = list(map(int,input().split()))
	T = [0] + list(input())
	hand_dic ={'r' : 0, 's' : 1, 'p' : 2}
	ans=0
	mod_l = [ [] for _ in range(K)]
	for i in range(1,N+1):
		mod_l[i%K].append(T[i])
	for a in range(K):
		dp = [ [0 for i in range(3)] for j in range(len(mod_l[a])+5)]
		for b in range(len(mod_l[a])):
			for c in range(3):
				if hand_dic[mod_l[a][b]] == c:
					dp[b+1][c] = max(dp[b][(c+1)%3], dp[b][(c+2)%3])
					dp[b+1][(c+1)%3] = max(dp[b][c], dp[b][(c+2)%3])
					dp[b+1][(c+2)%3] = max(dp[b][c] + RSP[(c+2)%3], dp[b][(c+1)%3] + RSP[(c+2)%3])
		ans += max(dp[len(mod_l[a])])

	print(ans)

if __name__ == '__main__':
	ABC149D()

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

cnt={'r':0,'s':0,'p':0}
for i in range(n):
  if i<=k-1:
    cnt[t[i]]+=1
  elif t[i]==t[i-k]:
    t[i]='o'
    continue
  else:
    cnt[t[i]]+=1
ans=p*cnt['r']+r*cnt['s']+s*cnt['p']
print(ans)

Solution (Python):
def getChoice(pt):
    return 'rs' if pt == 'p' else 'rp' if  pt == 's' else 'sp'
n, k = map(int, input().split(' '))
r, s, p = map(int, input().split(' '))
t = input()
rsp = {'r': ('p', p), 's': ('r', r), 'p': ('s', s)}
total = 0
co = 0
last = []
for s in t:
    pt, score = rsp[s]
    l = getChoice(pt)
    if co - k >= 0:
        if pt not in last[co - k]:
            score = 0
            if co + k < n:
                fp, fs = rsp[t[co + k]]
                ca = l[0] if fp != l[0] else l[1]
                l = getChoice(ca)
          
    last.append(l)
    total += score
    co += 1
print(total)

Solution (Python):
N, K = map(int, input().split())
R,S,P = map(int,input().split())
T = input()
A = [0 for i in range(N)]
for i in range(K):
  if T[i] == "r":
    A[i] = P
  if T[i] == "s":
    A[i] = R
  if T[i] == "p":
    A[i] = S
for i in range(K, N):
  if (T[i] == "r") and (T[i-K] != "r"):
    A[i] = P
  if (T[i] == "r") and (T[i-K] == "r") and (A[i-K] == 0):
    A[i] = P
  if (T[i] == "s") and (T[i-K] != "s"):
    A[i] = R
  if (T[i] == "s") and (T[i-K] == "s") and (A[i-K] == 0):
    A[i] = R
  if (T[i] == "p") and (T[i-K] != "p"):
    A[i] = S
  if (T[i] == "p") and (T[i-K] == "p") and (A[i-K] == 0):
    A[i] = S
print(sum(A))

Solution (Python):

import numpy as np
from functools import lru_cache
import time

def main(): 
    if True:
        N,K = list(map(int, input().split()))
        R,S,P = list(map(int, input().split()))
        T = input()
        
    else:
        A = 6
        for i in range(0,A,4):
            print(i)
    rsp = {}
    rsp["s"] = R
    rsp["p"] = S
    rsp["r"] = P
    rsp["n"] = 0
    
    NN = N if N % K == 0 else (N // K + 1) * K
    score = 0
    T = np.asarray(list(T + "n"*(NN-N))).reshape((NN//K,K))
    for k in range(K):
        tmp = ""
        t = T[:,k]
        
        for tt in t:
            if tmp == tt:
                tmp = ""
                
            else:
                tmp = tt
                score += rsp[tt]
    print(score)
    



if __name__ =='__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=str(input())
V=[]
W=[]
A=[]
x=0
for i in T:
  if i=="r":
    V.append("p")
  elif i=="s":
    V.append("r")
  else:
    V.append("s")
A=V[:]
for j in range(K):
  l=j
  while l<len(V):
    W.append(V[l])
    l=l+K
  
  for i in range(1,len(W)):
    if W[i]==W[i-1]:
      if W[i]=="r":
        if i<len(W)-1 and W[i+1]=="s":
          W[i]="p"
        else:
          W[i]="s"
      elif W[i]=="s":
        if i<len(W)-1 and W[i+1]=="p":
          W[i]="r"
        else:
          W[i]="p"
      elif W[i]=="p":
        if i<len(W)-1 and W[i+1]=="r":
          W[i]="s"
        else:
          W[i]="r"
  
  for i in range(len(W)):
    V[i*K+j]=W[i]
  W.clear()
"""print(V)
print(W)
print(A)"""
for i in range(len(V)):
  if V[i]==A[i]=="r":
    x=x+R
  elif V[i]==A[i]=="s":
    x=x+S
  elif V[i]==A[i]=="p":
    x=x+P
print(x)

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

Solution (Python):
from collections import defaultdict as DD
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

D = DD(int)
D["s"] = R
D["p"] = S
D["r"] = P

ans = 0
C = [0 for i in range(N)]
for i in range(N):
  if i < K:
    ans +=  D[T[i]]
    C[i] = 1    
  elif T[i-K] == T[i]:
    if C[i-K] != 1:
      ans +=  D[T[i]]
      C[i] = 1
  else:
      ans +=  D[T[i]]
      C[i] = 1    
print (ans)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

N,K = LI()
R,S,P = LI()
T = list(input())
hand = []

def main():
    ans = 0
    for i in range(N):
        if T[i] == 's':
            if i<K or hand[i-K]!='r':
                ans += R
                hand.append('r')
            else:
                hand.append('x')
        if T[i] == 'p':
            if i<K or hand[i-K] != 's':
                ans += S
                hand.append('s')
            else:
                hand.append('x')
        if T[i] == 'r':
            if i<K or hand[i-K] != 'p':
                ans += P
                hand.append('p')
            else:
                hand.append('x')
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
R, S, P = MAP()
T = list(input())


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

ans = 0
for k in range(K):
    M = len(T2[k])
    
    dp = list2d(M+1, 3, 0)
    for i, t in enumerate(T2[k]):
        
        for j in range(3):
            
            for l in range(3):
                
                if i != 0 and j == l:
                    continue
                nxt = 0
                
                if t == 'r' and l == 2:
                    nxt = P
                
                elif t == 's' and l == 0:
                    nxt = R
                
                elif t == 'p' and l == 1:
                    nxt = S
                dp[i+1][l] = max(dp[i+1][l], dp[i][j] + nxt)
    
    ans += max(dp[M])
print(ans)

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):
def DP(s):
    N = len(s)
    dp = [[0] * (3) for i in range(N+1)]
    rsp = {'s':0, 'p':1, 'r':2}
    for i in range(N):
        for j in range(3):
            for k in range(3):
                win = int(rsp[s[i]] == k) * RSP[k]
                if j != k: dp[i+1][k] = max(dp[i+1][k], dp[i][j] + win)
    return max(dp[N])

N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

ans = 0
for i in range(K): ans += DP(T[i:N:K])
print(ans)

Solution (Python):
def DP(s):
    N = len(s)
    dp = [[0] * (3) for i in range(N+1)]
    rsp = {'s':0, 'p':1, 'r':2}
    for i in range(N):
        for j in range(3):
            for k in range(3):
                win = int(rsp[s[i]] == k) * RSP[k]
                if j != k: dp[i+1][k] = max(dp[i+1][k], dp[i][j] + win)
    return max(dp[N])

N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

ans = 0
for i in range(K):
    t = ''
    for j in range(i, N, K): t += T[j]
    ans += DP(t)
print(ans)

Solution (Python):



if __name__=='__main__':
  n, k = map(int, input().strip().split())
  r, s, p = map(int, input().strip().split())
  score = {'r':p, 's':r, 'p':s}
  t = list(input().strip())
  count = {'r':0, 's':0, 'p':0}
  for i in range(n-k):
    if t[i] == t[i+k]:
      t[i+k] = '*'
      count[t[i]] += 1
  ans = 0
  for x in t:
    if x!='*':
      ans += score[x]
  print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=str(input())
T=[z for z in T]
c=0
for i in range(K):
  M=[]
  t=0
  j=i
  while j <= N-1:
    M.append(T[j])
    j=j+K
  k=0
  while k <= len(M)-1:
    if M[k]=='r':
      t=t+P
    elif M[k]=='p':
      t=t+S
    else:
      t=t+R
    if k<len(M)-1 and M[k]==M[k+1]:
      k=k+2
    else:
      k=k+1
  c=c+t
print(c)

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

print(ans)

Solution (Python):
import sys
from collections import deque


   
def resolve():
    
    input = sys.stdin.readline
    N,K=map(int,input().rstrip().split())
    R,S,P=map(int,input().rstrip().split())
    T=list(input().rstrip())

    ans=0
    flag=[0 for _ in range(N)]
    for i in range(N):
        if T[i]==T[i-K] and i>=K and flag[i-K]==True: 
            continue
        if T[i]=="r":
            ans+=P
        if T[i]=="s":
            ans+=R
        if T[i]=="p":
            ans+=S
        flag[i]=True
    print(ans)

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5 2
8 7 6
rsrpr"""
        output = """27"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """7 1
100 10 1
ssssppr"""
        output = """211"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr"""
        output = """4996"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

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

dp = [None for _ in range(n)]

map_ = {
        's': 'r',
        'r': 'p',
        'p': 's'
    }
for play in range(n):
    c_play = t[play] 
    dp[play] = map_.get(c_play)

for play in range(k, n):
    if dp[play] == dp[play-k]:
        dp[play] = None

ans = 0
map_2 = {
    None: 0,
    'r': r,
    's': s,
    'p': p        
}
for play in dp:
    ans += map_2.get(play)

print(ans)

Solution (Python):
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    T_ = [True for _ in range(N)]

    def play(com, player):
        if com == 'r' and player == 'p':
            return P
        elif com == 's' and player == 'r':
            return R
        elif com == 'p' and player == 's':
            return S
        else:
            return 0

    score = 0
    for n in range(N):
        if n < K:
            score += max(play(T[n], 'r'), play(T[n], 's'), play(T[n], 'p'))
        else:
            if T[n - K] == T[n] and T_[n - K]:
                score += 0
                T_[n] = False
            else:
                score += max(play(T[n], 'r'), play(T[n], 's'), play(T[n], 'p'))

    print(score)


main()

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

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

ans=0

for i in range(k):
  if T[i]=="r":ans+=P
  elif T[i]=="s":ans+=R
  else:ans+=S

for i in range(k,n):
  if T[i]==T[i-k]:T[i]="a"
  else:
    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 = input()
array = [""]*k
for i in range(n):
    array[i % k]+=t[i]
a = 0

for l in array:
    l = l.replace("rr", "r0").replace("ss", "s0").replace("pp", "p0")
    a += p * l.count("r") + r * l.count("s") + s * l.count("p")
print(a)

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

Ts = [[] for _ in range(K)]
for i, x in enumerate(T):
    k = i % K
    Ts[k].append(x)


def solve(t):
    r, s, p = 0, 0, 0   

    for x in t[::-1]:   
        if x == 'r':
            r, s, p = max(s, p), max(p, r), max(r, s) + P
            
            
            
            
        elif x == 's':
            r, s, p = max(s, p) + R, max(p, r), max(r, s)
        elif x == 'p':
            r, s, p = max(s, p), max(p, r) + S, max(r, s)

    return max(r, s, p)


answer = sum(solve(t) for t in Ts)
print(answer)

Solution (Python):
n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
hand=input()
ans=0
for i in range(k):
  free=True
  j=i
  while j<n:
    if free==True or hand[j-k]!=hand[j]:
      if hand[j]=='r':
        ans+=p
      elif hand[j]=='s':
        ans+=r
      else:
        ans+=s
      free=False
    else:
      free=True
    j+=k
print(ans)

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

dp_mod_k = {}

score = 0
for i in range(n):
    if i % k in dp_mod_k:
        dp_r, dp_s, dp_p = dp_mod_k[i % k]
    else:
        dp_r, dp_s, dp_p = 0, 0, 0
    opponent_hand = t[i]
    if opponent_hand == "r":
        new_dp_r = max(dp_s, dp_p)
        new_dp_s = max(dp_r, dp_p)
        new_dp_p = max(dp_r, dp_s) + p
    elif opponent_hand == "s":
        new_dp_r = max(dp_s, dp_p) + r
        new_dp_s = max(dp_r, dp_p)
        new_dp_p = max(dp_r, dp_s)
    else:
        new_dp_r = max(dp_s, dp_p)
        new_dp_s = max(dp_r, dp_p) + s
        new_dp_p = max(dp_r, dp_s)
    dp_mod_k[i % k] = [new_dp_r, new_dp_s, new_dp_p]
    if i + k >= n:
        score += max(new_dp_r, new_dp_s, new_dp_p)

print(score)

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

ans = []
for i in range(n):
    c = t[i]
    if i - k >= 0 and t[i-k] == c and ans[i-k]:
        ans.append(0)
    else:
        ans.append(win[c])
print(sum(ans))

Solution (Python):

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

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

score = 0

for i in range(N):

    tmp = i - K
    if 0 <= tmp and T[i] == T[tmp]:
        score += 0
        T[i]=""

    else:
        score+= point[T[i]]
print(score)

Solution (Python):
*n,t=open(0).read().split()
n,k,r,s,p=map(int,n)
*t,=t
for i in range(n-k):
  if t[i+k]==t[i]:t[i+k]=5
a,b,c=map(t.count,'rsp')
print(a*p+b*r+c*s)

Solution (Python):
s1 = str(input())
s2 = str(input())
T = str(input())

N, K = list(map(int, s1.split(' ')))
R,S,P = list(map(int, s2.split(' ')))

win = {'r' : 'p', 's': 'r', 'p' : 's'}
point = {'r' : R, 's' : S, 'p' : P}
rsp = ['r', 's', 'p']
def calc_score(substr):

    my_prev = win[substr[0]]
    score = point[my_prev]
    for idx in range(1, len(substr) - 1):
        my_now = win[substr[idx]]
        if my_now != my_prev:
            score += point[my_now]
        else:
            my_next = win[substr[idx + 1]]
            for elem in rsp:
                if elem != my_prev and elem != my_next:
                    my_now = elem 
                    break

        my_prev = my_now

    my_now = win[substr[-1]]
    if my_now != my_prev:
        score += point[my_now]
    return score

all_score = 0
for offset in range(K):
    all_score += calc_score(T[offset::K])

print(all_score)

Solution (Python):


import math
import sys

NK = list(map(int, input().split()))
RSP = list(map(int, input().split()))
T = input()
ansrr =[0]*NK[0]
for i in range(NK[1]):
    if T[i]=="r":
        ansrr[i] += RSP[2]
    elif T[i] == "p":
        ansrr[i] += RSP[1]
    else:
        ansrr[i] += RSP[0]

for i in range(NK[1],NK[0]):
    if T[i] == T[i-NK[1]]:
        if ansrr[i-NK[1]] == 0:
            if T[i] == "r":
                ansrr[i] += RSP[2]
            elif T[i] == "p":
                ansrr[i] += RSP[1]
            else:
                ansrr[i] += RSP[0]
        else:
            ansrr[i]=0
    else:
        if T[i] == "r":
            ansrr[i] += RSP[2]
        elif T[i] == "p":
            ansrr[i] += RSP[1]
        else:
            ansrr[i] += RSP[0]

ans = 0
for i in range(NK[0]):
    ans += ansrr[i]
print(ans)