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()
keys = [i for i in range(N)]
hands = {key: 0 for key in keys}  

for i in range(N):
    if i < K:
        if T[i] == 'r':
            hands[i] = P
        elif T[i] == 's':
            hands[i] = R
        else:
            hands[i] = S
    elif i >= K:
        if T[i] == T[i-K] == 'r':
            if hands[i-K] == 0:
                hands[i] = P
        elif T[i] == T[i-K] == 's':
            if hands[i-K] == 0:
                hands[i] = R
        elif T[i] == T[i-K] == 'p':
            if hands[i-K] == 0:
                hands[i] = S
        else:
            if T[i] == 'r':
                hands[i] = P
            elif T[i] == 's':
                hands[i] = R
            else:
                hands[i] = S

total = sum(hands.values())
print(total)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    T = [int(c) for c in T.translate(str.maketrans('rsp', '012'))]

    point = [R, S, P]
    hand = [0] * N

    ans = 0
    for i, com in enumerate(T):
        win = (com - 1) % 3
        if i >= K and win == hand[i - K]:
            hand[i] = -1
        else:
            hand[i] = win
            ans += point[win]

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    T = [int(c) for c in T.translate(str.maketrans('rsp', '012'))]
    H = [R, S, P]

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

    for i in range(N):
        com = T[i]
        for h in range(3):
            if i > K - 1:
                dp[i + 1][h] = max(dp[i + 1 - K][(h + 1) % 3], dp[i + 1 - K][(h + 2) % 3])
            if (h + 1) % 3 == com:
                dp[i + 1][h] += H[h]

    print(sum(max(p) for p in dp[-K:]))

    return


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
R, S, P = map(int, input().split())
t = input()
prev = {}

ans = 0
for i in range(k):
    if t[i] == 'r':
        ans += P
        prev[i] = 'p'
    elif t[i] == 's':
        ans += R
        prev[i] = 'r'
    elif t[i] == 'p':
        ans += S
        prev[i] = 's'

for i in range(k, n):
    a = i % k
    if t[i] == 'r':
        if prev[a] != 'p':
            ans += P
            prev[a] = 'p'
        else:
            if i + k < n and t[i + k] != 's':
                prev[a] = 'r'
            else:
                prev[a] = 's'
    elif t[i] == 's':
        if prev[a] != 'r':
            ans += R
            prev[a] = 'r'
        else:
            if i + k < n and t[i + k] != 'p':
                prev[a] = 's'
            else:
                prev[a] = 'p'
    elif t[i] == 'p':
        if prev[a] != 's':
            prev[a] = 's'
            ans += S
        else:
            if i + k < n and t[i + k] != 'r':
                prev[a] = 'p'
            else:
                prev[a] = 'r'
print(ans)

Solution (Python):
N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

win_point = {
    'r': r,
    's': s,
    'p': p,
}

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}

enemy_hands = input()


def can_win(enemy_hand, my_hands):
    
    if enemy_hand == 'r' and 'p' in my_hands:
        return True, 'p'
    if enemy_hand == 's' and 'r' in my_hands:
        return True, 'r'
    if enemy_hand == 'p' and 's' in my_hands:
        return True, 's'

    
    return False, None


point = 0

for index in range(K):
    now_hands = ['r', 'p', 's']
    for i in range(index, N, K):
        win, hand = can_win(enemy_hands[i], now_hands)

        if win:
            point += win_point[hand]
            now_hands = next_hands[hand]
        else:
            
            
            now_hands = ['r', 'p', 's']

print(point)

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

ans = 0
commands = [''] * N


for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

 
 
    if (i - K >= 0) and (commands[i - K] == command):
        command = '-'
        point = 0

    ans += point
    commands[i] = command

print(ans)

Solution (Python):
N, K = map(int, input().split())
ls1 = list(map(int, input().split()))
d = dict()
ls2 = ['r', 's', 'p']
for x, y in zip(ls1, ls2):
    d[y] = x
T = input()

S = T.translate(str.maketrans({'r': 'p', 's': 'r', 'p': 's'}))
ans = 0
for i in range(K):
    cur = ''
    for j in range(i, N, K):
        if cur != S[j]:
            ans += d[S[j]]
            cur = S[j]
        else:
            cur = ''
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
P = {'s':r, 'p':s, 'r':p}
 
mx = r*T.count('s')+s*T.count('p')+p*T.count('r')
if len(T)<k:
    print(mx)
else:
    for i in range(n-k):
        if T[i]==T[i+k]:
            mx -= P[T[i]]
            T[i+k] = '_'
print(mx)

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

t2 = [[] for _ in range(k)]
score = [0]*n
for i,c in enumerate(t):
  t2[i%k].append(c)
score = {'r':p,'s':r,'p':s}
total = 0
for t3 in t2:
  if len(t3)==0: continue
  if len(t3)==1:
    total += score[t3[0]]
    continue  
  for i in range(len(t3)-1):
    if t3[i] == 'e': continue
    total += score[t3[i]]
    if t3[i] == t3[i+1]:
      t3[i+1]='e'
  if t3[-1] != 'e':
    total += score[t3[-1]]
print(total)

Solution (Python):
import sys


import math





def main():
    N, K = map(int,input().split())
    R,S,P = map(int,input().split())
    T =  sys.stdin.readline().rstrip()
    tt =[0]*(N+ K)
    ans =0

    score = 0
    for i in range(N):
        current = T[i]
        if current == tt[i]:
            pass
        else:
            tt[i + K] = current
            if current == "r":
                ans +=P
            elif current =="s":
                ans +=R
            else:
                ans +=S
 
    print(ans)





if __name__ == "__main__":
    main()

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

ans = 0
prevs = [''] * k
for i in range(n//k+1):
  
  
  for j in range(i*k, (i+1)*k):
    if j >= n:
      break
    prevs_i = j%k
    prev = prevs[prevs_i]
    
    
    if t[j] == 'r':
      if prev == 'p':
        prevs[prevs_i] = ''
      else:
        prevs[prevs_i] = 'p'
        ans += p
    elif t[j] == 's':
      if prev == 'r':
        prevs[prevs_i] = ''
      else:
        prevs[prevs_i] = 'r'
        ans += r
    elif t[j] == 'p':
      if prev == 's':
        prevs[prevs_i] = ''
      else:
        prevs[prevs_i] = 's'
        ans += s
print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
points = {'r':r, 's':s, 'p':p}
hands = {'r':'p', 's':'r', 'p':'s'}
t = list(input())
rpsList = ['']*n
ans = 0

for i,hand in enumerate(t):
    rpsList[i] += hands[hand]
    if i>=k and rpsList[i]==rpsList[i-k]:
        last_hand= rpsList[i-k]
        next_hand = hands[t[i+k]] if i+k<n else ''
        for j in ['r','s','p']:
            if j!=last_hand and j!=next_hand:
                rpsList[i] = j
    else:
        ans += points[hands[hand]]

print(ans)

Solution (Python):
def choose_hand(last,next):
    for j in ['r','s','p']:
        if j!=last and j!=next:
            return j

n,k = map(int,input().split())
r,s,p = map(int,input().split())
points = {'r':r, 's':s, 'p':p}
hands = {'r':'p', 's':'r', 'p':'s'}
t = list(input())
rpsList = ['']*n
ans = 0

for i,hand in enumerate(t):
    rpsList[i] += hands[hand]
    if i>=k and rpsList[i]==rpsList[i-k]:
        lastHand = rpsList[i-k]
        nextHand = hands[t[i+k]] if i+k<n  else ''
        rpsList[i] = choose_hand(lastHand,nextHand)
    else:
        ans += points[hands[hand]]

print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def jan(a):
    if a == 'r':
        return P
    elif a == 's':
        return R
    else:
        return S
ans = 0
for i in range(K):
    check = 'n' + T[i::K]
    h = 0
    for i in range(len(check)-1):
        if check[i] != check[i+1]:
            ans += jan(check[i+1])
            h = 0
        else:
            if h == 0:
                h = 1
            else:
                ans += jan(check[i+1])
                h = 0
print(ans)

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

for i in range(N-K):
  if T[i] == T[i+K]:
    if len(T) > i+K:
      T = T[:i+K] + '*' + T[i+K+1:]
    else:
      T = T[:i+K] + '*'

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

Solution (Python):








def solve(N, K, R, S, P, T):
    k_list = [[] for _ in range(K)]
    for i in range(len(T)):
        k_list[i % K].append(T[i])

    ans = 0
    for kl in k_list:
        dp = [[0, 0, 0] for _ in range(len(kl) + 1)]
        for i in range(len(kl)):
            
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][0] += R if kl[i] == 's' else 0
            
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][1] += S if kl[i] == 'p' else 0
            
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
            dp[i + 1][2] += P if kl[i] == 'r' else 0
        ans += max(dp[-1])

    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()

    
    
    

    solve(N, K, R, S, P, T)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
P = {'s':r, 'p':s, 'r':p}
 
mx = r*T.count('s')+s*T.count('p')+p*T.count('r')
if len(T)<k:
    print(mx)
else:
    for i in range(n-k):
        if T[i]==T[i+k]:
            mx -= P[T[i]]
            T[i+k] = '_'
print(mx)

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

POINT = {"r":P,"s":R,"p":S}
GET = [False] * N

ANS = 0
for i in range(N):
  t = T[i]
  if i <= K-1:
    ANS += POINT[t]
    GET[i] = True
  else:
    if not GET[i-K] or t != T[i-K]:
      ANS += POINT[t]
      GET[i] = True

print(ANS)

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

commands = [""]*n
ans = 0

for i, t in enumerate(T):
    if t == "p":
        command = "s"
        point = s
    elif t == "s":
        command = "r"
        point = r
    elif t == "r":
        command = "p"
        point = p

    if(i - k) >= 0 and (commands[i-k] == command):
        command = ""
        point = 0

    ans += point
    commands[i] = command
print(ans)

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

li = [-1]*N 
se = set(["r","s","p"])
dic = {"r":"p","s":"r","p":"s"}
score = {"r":0,"s":0,"p":0}

num = N//K
for i in range(K):
    for a in range(num+1):
        nex = i + K*a
        if nex>N-1:
            break 

        rival = T[nex]
        kati = dic[rival]
        
         
        if nex<K:
            li[nex]=kati
            score[kati]+=1
        elif not kati==li[nex-K]:
            li[nex]=kati
            score[kati]+=1
        
        
        
print(score["r"]*R+score["s"]*S+score["p"]*P)

Solution (Python):

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

ans = 0
commands = [''] * N

for i, t in enumerate(T):
    if t == 's':
        command = 'r'
        score = R
    elif t == 'r':
        command = 'p'
        score = P
    elif t == 'p':
        command = 's'
        score = S

    if (i-K >= 0) and (commands[i-K] == command):
        command = ''
        score = 0

    ans += score
    commands[i] = command

print(ans)

Solution (Python):

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

ans = []
for i in range(N):
    if T[i] == 's':
        ans.append('r')
    elif T[i] == 'r':
        ans.append('p')
    elif T[i] == 'p':
        ans.append('s')

    if i >= K:
        if ans[i-K] == ans[i]:
            ans[i] = ''

count = 0
for i in range(N):
    
    if (T[i] == ans[i]) or ('' == ans[i]):
        continue

    if ans[i] == 'r':
        count += R
    elif ans[i] == 's':
        count += S
    elif ans[i] == 'p':
        count += P

print(count)

Solution (Python):

def janken(idx,mine):
    if i >= K:
        if flg[i-K] and T[i-K]==T[i]:
            return 0
    flg[i] = True
    if mine=="r":
        return R
    if mine=="p":
        return P
    if mine=="s":
        return S

N, K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
flg = [False]*N
ans = 0
for i,v in enumerate(T):
    if v=="s":
        ans += janken(i,"r")
    elif v=="r":
        ans += janken(i,"p")
    elif v=="p":
        ans += janken(i,"s")
print(ans)

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

d = {i:[] for i in range(K)}
score = {"r":P, "s":R, "p":S}

for i in range(len(T)):
    d[i%K].append(T[i])
  
ans = 0
cnt = 1
for val in d.values():
    cnt = 1
    for i in range(len(val)-1):
        if(val[i] == val[i+1]):
            cnt += 1
            
        else:
            if(cnt > 1):
                if(cnt%2 == 0):
                    ans += score[val[i]]*(cnt//2)
                    cnt = 1
                else:
                    ans += score[val[i]]*(cnt//2+1)
                    cnt = 1
            else:
                ans += score[val[i]]
    if(cnt > 1):
        if(cnt%2 == 0):
            ans += score[val[i]]*(cnt//2)
        else:
            ans += score[val[i]]*(cnt//2+1)
    
    else:
        ans += score[val[-1]]
    
print(ans)

Solution (Python):
n,k = [int(i) for i in input().split()]
r,s,p = [int(i) for i in input().split()]
t = [i for i in input().split()[0]]
ts = [t[0+i::k] for i in range(k)]
for t in ts:
  for i in range(len(t)-1):
    if t[i] == t[i + 1]:
      t[i + 1] = 'n'
  for i in range(len(t)):
    if t[i] == 'r':
      t[i] = p
    if t[i] == 's':
      t[i] = r
    if t[i] == 'p':
      t[i] = s
    if t[i] == 'n':
      t[i] = 0
sc = [sum(t) for t in ts]
print(sum(sc))

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

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

def main(n,k,r,s,p,t):
    rireki=[]
    tokuten=0
    for i in range(n):
        c=t[i]
        if i<k:
            if(c=='r'):
                rireki.append('p')
                tokuten+=p
                
            elif(c=='s'):
                rireki.append('r')
                tokuten+=r
                
            elif(c=='p'):
                rireki.append('s')
                tokuten+=s
                
        else:
            
            if c=='r':
                if rireki[i-k]=='p':
                    rireki.append('x')
                    
                else:
                    rireki.append('p')
                    tokuten+=p
                    
            elif c=='s':
                if rireki[i-k]=='r':
                    rireki.append('x')
                    
                else:
                    rireki.append('r')
                    tokuten+=r
                    
            elif c=='p':
                if rireki[i-k]=='s':
                    rireki.append('x')
                    
                else:
                    rireki.append('s')
                    tokuten+=s
                    
        
    return tokuten

if __name__=='__main__':
    n,k,r,s,p,t=readinput()
    ans=main(n,k,r,s,p,t)
    print(ans)

Solution (Python):
def main():
    N,K = map(int, input().split())
    R,S,P = map(int, input().split())
    T = list(input())
    score=0
    for i in range(N):
        if T[i] == 'r':
            score+=P
            if i+K<N and T[i+K]=='r':
                T[i+K]='x'
        if T[i]=='s':
            score+=R
            if i+K<N and T[i+K]=='s':
                T[i+K]='x'
        if T[i]=='p':
            score+=S
            if i+K<N and T[i+K]=='p':
                T[i+K]='x'
    print(score)







if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
vstd = [0]*n
def jungkeng(x):
  global ans
  if x=='r':
    ans += p
  elif x=='s':
    ans += r
  else:
    ans += s
  return ans
  
for i in range(k):
  ans = jungkeng(t[i])
  vstd[i]=1
for i in range(k,n):
  if t[i]==t[i-k] and vstd[i-k]:
    continue
  ans = jungkeng(t[i])
  vstd[i] = 1
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
P = {'s':r, 'p':s, 'r':p}

mx = r*T.count('s')+s*T.count('p')+p*T.count('r')
if len(T)<k:
    print(mx)
else:
    for i in range(n-k):
        if T[i]==T[i+k]:
            mx -= P[T[i]]
            T[i+k] = '_'
print(mx)

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

ans = 0
for i in range(len(t)):
    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] = "x"
        if t[i] == "s":
            if t[i - k] != "s":
                ans += r
            else:
                t[i] = "x"
        if t[i] == "p":
            if t[i - k] != "p":
                ans += s
            else:
                t[i] = "x"
                
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t = input()
d = [0]*3*10**5
for i in range(n):
  if t[i] == 'r':
    d[i] = (10**4+1)+p
  if t[i] == 's':
    d[i] = 2*(10**4+1)+r
  if t[i] == 'p':
    d[i] = 3*(10**4+1)+s
ans = 0
for i in range(n):
  if d[i] == d[k+i]:
    d[k+i] = 0
  ans += d[i]%(10**4+1)
print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n,k = iim()
r,s,p = iim()
t = list(input())
ans = 0
flag = [True]*n
for i,item in enumerate(t):
    if i >= k and flag[i-k]:
        if t[i] == 'r' and t[i-k] != 'r':
            ans += p
        elif t[i] == 's' and t[i-k] != 's':
            ans += r
        elif t[i] == 'p' and t[i-k] != 'p':
            ans += s
        else:
            flag[i] = False

    else:
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        elif t[i] == 'p':
            ans += s

print(ans)

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

ans = 0
hands = ["" for _ in range(N)]

for k in range(K):
  if T[k] == "r":
    ans += P
    hands[k] = "p"
  elif T[k] == "s":
    ans += R
    hands[k] = "r"
  else:
    ans += S
    hands[k] = "s"
    
    
for n in range(K,N):
  if T[n] == "r":
    if hands[n-K] != "p":
      ans += P
      hands[n] = "p"
    else:
      hands[n] = "x"
      
  elif T[n] == "s":
    if hands[n-K] != "r":
      ans += R
      hands[n] = "r"
    else:
      hands[n] = "x"
      
  else:
    if hands[n-K] != "s":
      ans += S
      hands[n] = "s"
    else:
      hands[n] = "x"

print(ans)

Solution (Python):
def solve():
    def max2(x, y): return x if x >= y else y

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

    Uss = [Ts[i::K] for i in range(K)]

    ans = 0
    for Us in Uss:

        lenU = len(Us)
        dp = [[0]*(3) for _ in range(lenU+1)]
        for i, U in enumerate(Us, start=1):
            
            dp[i][0] = max2(dp[i-1][1], dp[i-1][2])
            if U == 's':
                dp[i][0] += R
            
            dp[i][1] = max2(dp[i-1][0], dp[i-1][2])
            if U == 'p':
                dp[i][1] += S
            
            dp[i][2] = max2(dp[i-1][0], dp[i-1][1])
            if U == 'r':
                dp[i][2] += P


        ans += max(dp[-1])

    print(ans)


solve()

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



r, s, p = 0, 0, 0

for i in range(N):
    i = int(i)
    if T[i] == T[i+K]:
        T = T[:i+K] + "0" + T[i+K+1:]
        """
        if T[i] == "r":
            rn += 1


        elif T[i] == "s":
            sn += 1

        else:
            pn += 1
        """
    else:
        if T[i] == "r":
            r += 1

        elif T[i] == "s":
            s += 1

        elif T[i] == "p":
            p += 1


    


sum = P*T.count("r") + R*T.count("s") + S*T.count("p")
print(sum)

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

def main():
    n, k = input_list()
    
    R, S, P = input_list()
    t = input()
    tt = list(t)
    for i in range(n - k):
        if tt[i] == t[i+k]:
            tt[i+k] = "x"
    r = tt.count("r") * P
    s = tt.count("s") * R
    p = tt.count("p") * S
    print(r + s + p)


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

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

did = "-" * k
ans = 0
for i in t:
    if i == "r":
        if did[-k] == "p":
            did += "-"
        else:
            did += "p"
            ans += p
    elif i == "s":
        if did[-k] == "r":
            did += "-"
        else:
            did += "r"
            ans += r
    else:
        if did[-k] == "s":
            did += "-"
        else:
            did += "s"
            ans += s

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
def score(x):
    if x=='r':
        return p
    elif x=='s':
        return r
    else:
        return s
totalscore=0
zero=[0]*(n+1)
for i in range(n):
    if i<=k-1:
        totalscore+=score(t[i])
    else:
        if t[i]!=t[i-k]:
            totalscore+=score(t[i])
        else:
            if zero[i-k]!=0:
                totalscore+=score(t[i])
            else:
                zero[i]=1
                
                
print(totalscore)

Solution (Python):
n,k = (int(a) for a in input().split())
r,s,p = (int(a) for a in input().split())
T = list(input())
point = 0
for i in range(n) :
    if i < k :
        pass
    elif  T[i] == T[i-k] :
        T[i] = 0
a = T.count("r")
b = T.count("s")
c = T.count("p")
print(a*p + r*b + c*s)

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

dp=[[0]*3 for i in range(N)]

for i in range(K):
  for j in range(i,N,K): 
    if j>=K:
      if T[j]=="r":
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])+P
      elif T[j]=="s":
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])+R
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])
      else:
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])+S
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])
           
    else:
      if T[j]=="r":
        dp[j][2]=P
      elif T[j]=="s":
        dp[j][0]=R
      else:
        dp[j][1]=S
ans=0
for i in range(K): 
  ans+=max(dp[-i-1])
    
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
score = {'r': R, 's': S, 'p': P}
T = input()
hand = {'r': 'p', 's': 'r', 'p': 's'}

history = []
cnt = 0
for i, t in enumerate(T):
    if i < K:
        u = hand[t]
        flag = 1
    elif K <= i < N - K:
        l = i - K
        r = i + K
        if hand[t] == history[l]:
            u = T[r]
            flag = 0
        else:
            u = hand[t]
            flag = 1
    else:
        l = i - K
        if hand[t] == history[l]:
            u = t
            flag = 0
        else:
            u = hand[t]
            flag = 1
    cnt += score[u] * flag
    history.append(u)

print(cnt)

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

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

Solution (Python):

"""
D - Prediction and Restriction
https://atcoder.jp/contests/abc149/tasks/abc149_d

"""
import sys


def solve(N, K, R, S, P, T):
    d = {'r': P, 's': R, 'p': S}
    lost = set()
    ans = 0
    for i, ch in enumerate(T):
        if i < K:
            ans += d[ch]
        else:
            if T[i-K] != ch or i-K in lost:
                ans += d[ch]
            else:
                lost.add(i)
    return ans


def main(args):
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    ans = solve(N, K, R, S, P, T)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):


n, k = map(int,input().split())
r, s, p = map(int,input().split())
q = input()
d = {"r":p,"s":r, "p":s}
ans = 0
wins = [False] * k
for i in range(len(q)):
    if not wins[i % k] or q[i] != q[i - k]:
        ans += d[q[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False
print(ans)

Solution (Python):

from sys import stdin



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

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

winmap = {
  "r": "p",
  "s": "r",
  "p": "s"
}

point = 0
T2 = list(T)


for i,t in enumerate(T):
  
  wintp = "x"
  if i>K-1:
    tp = T2[i-K]
    if tp!="x":
      wintp = winmap[tp]
  
  wint = winmap[t]
  if wint!=wintp:
    point += pmap[wint]
  else:
    T2[i] = "x"


print(point)

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

a=[[] for i in range(k)]
for i in range(n):
    a[i%k].append(T[i])

d={'r':p,'s':r,'p':s}
score=0
for j in range(k):
    b=a[j]
    flg=0
    for l in range(len(b)):
        if l==0:
            score+=d[b[l]]
        else:
            if b[l-1]==b[l] and flg==0:
                flg=1
                continue
            elif b[l-1]==b[l] and flg==1:
                score+=d[b[l]]
            elif b[l-1]!=b[l]:
                score+=d[b[l]]
            flg=0
print(score)

Solution (Python):
n,k=map(int,input().split())

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

T=list(input())

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

for i in range(n):
    a[i%k].append(T[i])


score=0
for j in range(k):
    b=a[j]
    flg=0
    for l in range(len(b)):
        if l==0:
            if b[l]=='r':
                score+=p
            elif b[l]=='s':
                score+=r
            elif b[l]=='p':
                score+=s
        else:
            if b[l-1]==b[l] and flg==0:
                flg=1
                continue
            elif b[l-1]==b[l] and flg==1:
                if b[l]=='r':
                    score+=p
                elif b[l]=='s':
                    score+=r
                elif b[l]=='p':
                    score+=s
                flg=0
            elif b[l-1]!=b[l]:
                if b[l]=='r':
                    score+=p
                elif b[l]=='s':
                    score+=r
                elif b[l]=='p':
                    score+=s
                flg=0

print(score)

Solution (Python):
from collections import deque
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
points = {"r":r,"s":s,"p":p}
d = {"r":"p","s":"r","p":"s"}
que = deque([None]*k)
ans = 0
for ti in t:
    x = que.popleft()
    if d[ti] != x:
        
        que.append(d[ti])
        ans+=points[d[ti]]
    else:
        
        que.append(None)
    
    
print(ans)