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



Sample input:
5 2
8 7 6
rsrpr


Sample output:
27

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
li = [[] for _ in range(K)]
for k in range(N):
  if T[k] == 'r':
    li[k%K].append([P, 'r'])
  elif T[k] == 's':
    li[k%K].append([R, 's'])
  else:
    li[k%K].append([S, 'p'])
ans = 0
for k in range(K):
  j = 0
  while j < len(li[k])-1:
    if li[k][j][1] == li[k][j+1][1]:
      count = 0
      while(li[k][j+count][1] == li[k][j+count+1][1]):
        count += 1
        if j + count > len(li[k])-2:
          break
      dp = [0, 0]
      for l in range(count+1):
        dp[l%2] += li[k][j+l][0]
        dp[l%2] = max(dp)
      ans += max(dp)
      j += count
    else:
      ans += li[k][j][0]
    j += 1
  if j == len(li[k]) - 1:
    ans += li[k][-1][0]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
#    ti=[t[ii] for ii in range(n) if ii%k==i]
#    ni=len(ti)
#    dp=[[0]*3 for i in range(ni+1)]
    dpir=0
    dpis=0
    dpip=0
    for i2 in range(i,n,k):
        if t[i2]=="r":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif t[i2]=="s":
#            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif t[i2]=="p":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2
#    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
    vsum+=max(dpir,dpis,dpip)
print(vsum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding=UTF-8
from collections import deque
from operator import itemgetter
from bisect import bisect_left, bisect
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)
input = sys.stdin.readline


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

    myhand = [None] * n
    point = 0

    for i in range(n):
        if t[i] == "r":
            if myhand[i] != "np":
                point += p
                if i+k < n:
                    myhand[i+k] = "np"
            else:
                if i+k < n:
                    myhand[i + k] = None
        elif t[i] == "s":
            if myhand[i] != "nr":
                point += r
                if i+k < n:
                    myhand[i+k] = "nr"
            else:
                if i+k < n:
                    myhand[i + k] = None
        elif t[i] == "p":
            if myhand[i] != "ns":
                point += s
                if i+k < n:
                    myhand[i+k] = "ns"
            else:
                if i+k < n:
                    myhand[i + k] = None

    print(point)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
A = []
count = 0

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

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

for i in range (0, N):
	if A[i] == 'r':
		count+=R
	if A[i] == 's':
		count+=S
	if A[i] == 'p':
		count+=P
        
print(count)

Submission status: Accepted

Solution code:
def f(x):
    if x=="r":
        return 2
    elif x=="s":
        return 0
    else:
        return 1

N,K=map(int,input().split())
L=list(map(int,input().split()))
T=["*"]+list(map(f,input()))


Z=[[0,0,0] for _ in range(N+1)]

for x in range(1,N+1):
    for a in range(3):
        if x<=K:
            Z[x][a]=L[a]*(T[x]==a)
        else:
            b=(a+1)%3
            c=(b+1)%3

            Z[x][a]=max(Z[x-K][b],Z[x-K][c])+L[a]*(T[x]==a)

X=0
for i in range(1,K+1):
    X+=max(Z[-i])
print(X)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
memo = [0 for i in range(N)]
for i in range(N):
    check = T[i]
    if i >= K and T[i-K] == check and memo[i-K] == 0:
        memo[i] = 1
        continue
    if check == "r":
        ans += P
    elif check == "p":
        ans += S
    elif check == "s":
        ans += R
print(ans)

Submission status: Accepted

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

scores = {'r': P, 's': R, 'p': S, 'default': 0}
win_flag = [False]*(N+1)
ans = 0

# Kの剰余で別々に考えていい
for i_k in range(K):
    prev = 'default'
    for i_n in range(i_k, N, K):
        # K個前と違う手 もしくは K個前で勝たないことにした 場合
        #   => 点がもらえる
        if (T[i_n] != prev) or (not win_flag[i_n-K]):
            win_flag[i_n] = True
            ans += scores[T[i_n]]
        prev = T[i_n]

print(ans)

Submission status: Accepted

Solution code:
import os
import sys
import numpy as np

def solve(N, K, S, P, R, T):
    dp = np.zeros((N+1, 3), dtype=np.int64)
    for i in range(1, N+1):
        c = T[i]
        dp[i, 0] = max(dp[i-K, 1], dp[i-K, 2]) + (c == "r") * R
        dp[i, 1] = max(dp[i-K, 0], dp[i-K, 2]) + (c == "s") * S
        dp[i, 2] = max(dp[i-K, 0], dp[i-K, 1]) + (c == "p") * P
    #d = dp.max(1)
    d = np.zeros(N+1, dtype=np.int64)
    for i in range(N+1):
        d[i] = dp[i].max()
    ans = d[-K:].sum()
    return ans


# >>> numba compile >>>
numba_config = [
    [solve, "i8(i8,i8,i8,i8,i8,string)"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba import njit
    from numba.pycc import CC
    cc = CC("my_module")
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature)(func)
        cc.export(func.__name__, signature)(func)
    cc.compile()
    exit()
elif os.name == "posix":
    exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
    from numba import njit
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature, cache=True)(func)
    print("compiled!", file=sys.stderr)
# <<< numba compile <<<


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

main()

Submission status: Accepted

Solution code:
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

n,k = MI()
R,S,P = MI()
T = input()
li = [[] for _ in range(k)]
for i in range(len(T)):
    j = i%k
    li[j].append(T[i])
score = 0
for i in range(k):
    prev = "-1"
    for j in range(len(li[i])):
        if li[i][j] == "r":
            if prev == "p":
                prev = "-1"
            else:
                score += P
                prev = "p"
        if li[i][j] == "s":
            if prev == "r":
                prev = "-1"
            else:
                score += R
                prev = "r"
        if li[i][j] == "p":
            if prev == "s":
                prev = "-1"
            else:
                score += S
                prev = "s"
print(score)

Submission status: Accepted

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

d = {'r' : p, 's' : r, 'p' : s}
ans = 0
l = [1] * k + [0] * (n - k)

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

for i in range(k, n):
    if t[i] == t[i - k] and l[i - k]: continue
    ans += d[t[i]]
    l[i] = 1

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
R, S, P = [int(i) for i in input().split()]
d = {'r': P, 's': R, 'p': S}
T = input()
checked = [False for i in range(N)]
# 勝てるだけ勝てばいい
for i in range(N-K):
    if T[i] == T[i+K]:
        if checked[i] == False:
            checked[i+K] = True
result = 0
for i in range(N):
    if checked[i] == False:
        result += d[T[i]]
print(result)

Submission status: Accepted

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

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

def score(card):
    if card == "r":
        return R
    elif card == "s":
        return S
    elif card == "p":
        return P

mycard = ""
score_sum = 0
for i in range(N):
    if i-K<0:
        mycard = mycard+wincard(T[i])
        score_sum += score(wincard(T[i]))
    else:
        if wincard(T[i]) != mycard[i-K]:
            mycard = mycard+wincard(T[i])
            score_sum += score(wincard(T[i]))
        else:
            mycard = mycard+"w"

print(score_sum)

Submission status: Accepted

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

    else:
        if (t[i - k] == t[i]):
            t = t[:i] + 'n' + t[i + 1:]
        else:
            if (t[i] == 'r'):
                ans += p
            elif (t[i] == 's'):
                ans += r
            else:
                ans += s
print(ans)

Submission status: Accepted

Solution code:
def janken(aite):
    tokuten = 0
    kachi = 1
    if aite[0]=='r':
        tokuten += pa_p
    elif aite[0]=='s':
        tokuten += gu_r
    else:
        tokuten += choki_s
    #print(0,aite[0],tokuten,kachi)
    for i in range(1,len(aite)):
        if aite[i-1]!=aite[i] or kachi==0:
            kachi = 1
            if aite[i]=='r':
                tokuten += pa_p
            elif aite[i]=='s':
                tokuten += gu_r
            else:
                tokuten += choki_s
            #print(i,aite[i],tokuten,kachi)
        else:
            kachi=0
    return tokuten

n,k = map(int,input().split())
gu_r,choki_s,pa_p = map(int,input().split())
t = str(input())
kaisu = {}
memo = [str('') for i in range(k)]
for i in range(n):
    kaisu.setdefault(t[i],0)
    kaisu[t[i]] += 1
    #print(i%k)
    memo[i%k] += t[i]
ans = 0
for i in range(len(memo)):
    ans += janken(memo[i])
#print(kaisu)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
point = [0]*K
flag = ["a"]*K
k = -1
#K本のリストを用意する　剰余系の中で、同じ手が連続するとき、１、３、５、...は別の手に変える
for i in range(N):
  k = (k+1)%K  #mod
  if T[i] == "r":
    if flag[k] == "r":
      flag[k] = "a"
    else:
      flag[k] = "r"
      point[k] += P
  elif T[i] == "s":
    if flag[k] == "s":
      flag[k] = "a"
    else:
      flag[k] = "s"
      point[k] += R
  if T[i] == "p":
    if flag[k] == "p":
      flag[k] = "a"
    else:
      flag[k] = "p"
      point[k] += S
print(sum(point))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit
import sys
sys.setrecursionlimit(10**8)
# input = stdin.readline

# @njit
def solve(n,k,r,s,p,t):
  winHand = {"r":"p", "s":"b", "p":"s"}
  bestHandMemo = [""]*(n+1) # i手目の勝ち手
  
  memo = [-1]*(n+1)
  def calcPoint(char):
    if char == "r":
      return p
    elif char == "s":
      return r
    elif char == "p":
      return s
    else:
      raise ValueError
  def dp(i):
    if i == 0:
      return 0
    elif memo[i] != -1:
      return memo[i]
    elif i <= k:
      bestHandMemo[i-1] = winHand[t[i-1]] # 自由に出せる
      return dp(i-1) + calcPoint(t[i-1])
    elif winHand[t[i-1]] == bestHandMemo[i-k-1]:
      bestHandMemo[i-1] = "" # あいこでも負けても同じこと
      return dp(i-1)
    else: # 勝てる
      bestHandMemo[i-1] = winHand[t[i-1]]
      return dp(i-1) + calcPoint(t[i-1])
  
  for i in range(n+1):
    memo[i] = dp(i)

  return memo[n]
    



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

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
POINTS = (R, P, S)
T = [("r", "p", "s").index(c) for c in input()]
WIN = [(t + 1) % 3 for t in T]

ans = 0
for i in range(K):
    p = [0] * 3
    p[WIN[i]] = POINTS[WIN[i]]
    j = i + K
    while j < N:
        n = [-1] * 3
        for px, pp in enumerate(p):
            if pp < 0:
                continue
            for nx in range(3):
                if nx == px:
                    continue
                np = pp
                if nx == WIN[j]:
                    np += POINTS[WIN[j]]
                n[nx] = max(n[nx], np)
        p = n
        j += K
    ans += max(p)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
a=list(input())
ans1=[]

ans=0
for i in range(n):
  x=a[i]
  if i-k>=0:
    if x==a[i-k]:
      if ans1[i-k]==0:
        if x=='r':
          ans1.append(p)
        elif x=='p':
          ans1.append(s)
        else:
          ans1.append(r)
          
      else:
        ans1.append(0)
    else:
      if x=='r':
          ans1.append(p)
      elif x=='p':
          ans1.append(s)
      else:
          ans1.append(r)
  else:
    if x=='r':
          ans1.append(p)
     
    elif x=='p':
          ans1.append(s)
        
    else:
          ans1.append(r)

print(sum(ans1))

Submission status: Accepted

Solution code:
from itertools import groupby
N, K = map(int, input().split())
R, S, P = map(int, input().split())
D = {"r":P, "s":R, "p":S}
T = list(input())
c = 0
for i in range(K):
  gr = groupby(T[i::K])  
  for key, group in gr:
    l = len(list(group))
    d = D[key]
    c += d*((l+1)//2)
print(c)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

safe = [False] * n

result = [True] * n

for i in range(n-k):
    if t[i] == t[i+k] and (not safe[i]):
        result[i] = False
        safe[i+k] = True


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

score = 0

for i in range(n):
    if result[i]:
        score += points[t[i]]

print(score)

Submission status: Accepted

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

ans=0
for k in range(K):
    dp=[0,0,0]
    for i in range(k,N,K):
        #print(i)
        x=T[i]
        if x=='r':
            r,s,p=max(dp[1],dp[2]),max(dp[0],dp[2]),max(dp[0],dp[1])+P
        elif x=='s':
            r,s,p=max(dp[1],dp[2])+R,max(dp[0],dp[2]),max(dp[0],dp[1])
        else:
            r,s,p=max(dp[1],dp[2]),max(dp[0],dp[2])+S,max(dp[0],dp[1])
        dp=[r,s,p]
    ans+=max(dp)
print(ans)

Submission status: Accepted

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

def addScore(hand):
    if hand == 'r':
        return P
    elif hand == 's':
        return R
    else :
        return S

score = 0
score_flag = [0 for i in range(N)]

for l in range(len(T)):
    if l < K:
        score += addScore(T[l])
        score_flag[l] = 1
    else :
        if T[l] == T[l-K] and score_flag[l-K] == 1:
            continue
        else :
            score += addScore(T[l])
            score_flag[l] = 1
            
print(score)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
r =[0]*100001
s=[0]*100001
p=[0]*100001
for i in range(N):
  if T[i]=='r':
    r[i]=1
    if i>=K:
      if r[i-K]==1:
        r[i]=0
  elif T[i] =='s':
    s[i]=1
    if i>=K:
      if s[i-K]==1:
        s[i]=0
  elif T[i]=='p':
    p[i]=1
    if i>=K:
      if p[i-K]==1:
        p[i]=0
count=0
for i in range(N):
  if r[i]==1:
    count+=P
  elif s[i]==1:
    count+=R
  elif p[i]==1:
    count+=S
print(count)

Submission status: Accepted

Solution code:
def solve():
  N, K = map(int, input().split())
  R, S, P = map(int, input().split())
  dic = {'r':P,'s':R,'p':S}
  T = input()
  point = [0]*N
  for i in range(N):
    if i<K:
      point[i] = dic[T[i]]
    else:
      if T[i]!=T[i-K] or point[i-K]==0:
        point[i]=dic[T[i]]
  ans = sum(point)
  return ans
print(solve())

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
li = [[""]*((n+k)//k) for i in range(k)]
for i in range(n):
    li[i%k][i//k] = t[i]
ans = 0
for i in li:
    P,R,S = 0,0,0
    hand_o = ""
    for j in i:
        if j == "r" and hand_o != "r":
            P += p
        elif j == "s" and hand_o != "s":
            R += r
        elif j == "p" and hand_o != "p":
            S += s
        else:
            hand_o = ""
            continue
        hand_o = j
    ans += (P+R+S)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
points = {'r': P, 's': R, 'p': S}
is_changed = [False] * K + [False] * (N - K)
score = 0
for i in range(K):
    score += points[T[i]]

for i in range(K, N):
    if T[i] == T[i - K] and not is_changed[i - K]:
        is_changed[i] = True
    else:
        score += points[T[i]]

print(score)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
t = list(input())
ans = []
for i in range(len(t)):
    if t[i] == 'r':
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[2])
    elif t[i] == 's':
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[0])
    else:
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[1])
print(sum(ans))

Submission status: Accepted

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

dict = {'s': r, 'p': s, 'r': p}
cnt, ans = set(), 0
for i, ti in enumerate(t):
    if (i - k in cnt) and ti == t[i - k]:
        continue
    ans += dict[ti]
    cnt.add(i)
print(ans)

Submission status: Accepted

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

cnt, ans = set(), 0
for i, ti in enumerate(t):
    if (i - k in cnt) and ti == t[i - k]:
        continue
    ans += r * (ti == 's')
    ans += s * (ti == 'p')
    ans += p * (ti == 'r')
    cnt.add(i)
print(ans)

Submission status: Accepted

Solution code:
# 筐体の手が'k'回前と同じ場合, その回は勝てないが, 'k'回後は勝てる
# 筐体が出した各回の手をメモ, ただし'k'回前と同じ場合のみ ’-1’をメモする
# 勝ち手が'k'回前と同じ場合は加算なし, それ以外('-1'を含む)は勝ち点を加算 

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

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

    else:
        memo.append('-1')

print(ans)

Submission status: Accepted

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

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

    else:
        memo.append('-1')

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

ans = 0
for i in range(N):
    t = T[i]
    if i<K:
        if t=='r':
            ans += P
        elif t=='s':
            ans += R
        else:
            ans += S
    else:
        if T[i-K]==T[i]:
            T[i] = '0'
            continue
        else:
            if t=='r':
                ans += P
            elif t=='s':
                ans += R
            else:
                ans += S  
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t=input()
l=["" for i in range(n)]

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

for i in range(k,n):
  if t[i]=="r":
    if l[i-k]!="p":
      l[i]="p"
      ans+=p

  elif t[i]=="p":
    if l[i-k]!="s":
      ans+=s
      l[i]="s"

  else:
    if t[i]=="s":
      if l[i-k]!="r":
        ans+=r
        l[i]="r"

print(ans)

Submission status: Accepted

Solution code:

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

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

    ans += ctr[T[i]]
    dp[i] = T[i]

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
V = {"s":R, "p":S, "r":P}
ans = 0
for i in range(K):
    check = T[i]
    ans += V[T[i]]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = -1
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def battle(enemy, r, s, p):
    if enemy == "r":
        return p
    if enemy == "s":
        return r
    if enemy == "p":
        return s


def main():
    N, K = MI()
    r, s, p = MI()
    t = input()
    splited_t = [[] for i in range(K)]

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

    ans = 0
    for i in range(K):
        targets = splited_t[i]
        score = battle(targets[0], r, s, p)
        ans += score
        won = True
        for j in range(1, len(targets)):
            if targets[j] == targets[j - 1] and won:
                won = False
                continue
            score = battle(targets[j], r, s, p)
            won = True
            ans += score
    print(ans)


main()

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, k = MAP()
R, S, P = MAP()
t = input()
s = ''

ans = 0
for i in range(len(t)):
    if t[i] == 'r':
        if i >= k and s[i-k] == 'p':
            s += 'x'
        else:
            ans += P
            s += 'p'
    elif t[i] == 's':
        if i >= k and s[i-k] == 'r':
            s += 'x'
        else:
            ans += R
            s += 'r'
    else:
        if i >= k and s[i-k] == 's':
            s += 'x'
        else:
            ans += S
            s += 's'
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

T = list(input())
T_ =T.copy()

# N=2*10**5
# T=[random.choice(['s', 'r', 'p']) for _ in range(N)]

check=set(['s', 'r', 'p'])

#得点
point={'r':P,'s':R,'p':S}
point

T_point = [0]*N
for n in range(N):
    T_point[n]=point[T[n]]
    
for k in range(K,N):
    check_out = set()
    if T[k] == T[k-K]: # K回前の手と同じ場合
        check_out.add(T[k])
        if k+K < N:
            check_out.add(T[k+K])
            
        check_out=check-check_out
        
        T[k]=list(check_out)[0]
         
ans=0

for t in range(len(T)):
    if T[t] == T_[t]:
        ans += point[T[t]]
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
dp = [0]*n
fl = [True]*k
for i in range(n):
    if t[i] == "r":
        h = p
    elif  t[i] == "s":
        h = r
    else:
        h = s
    if i < k:
        if i == 0:
            dp[i] = h
            fl[i%k-1] = False
        else:
            dp[i] = h + dp[i-1]
            fl[i%k-1] = False
    else:
        if t[i] == t[i-k]:
            if fl[i%k-1] == True:
                dp[i] = dp[i-1]+h
                fl[i%k-1] = False
            else:
                dp[i] = dp[i-1]
                fl[i%k-1] = True
        else:
            dp[i] = dp[i-1] + h
            fl[i%k-1] = False    
print(dp[-1])

Submission status: Accepted

Solution code:
from collections import deque
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()

q = deque([])
points = 0
for i in range(n):
    if i <= k-1:
        if t[i] == 'r':
            points += p
            q.append('p')
        elif t[i] == 's':
            points += r
            q.append('r')
        elif t[i] == 'p':
            points += s
            q.append('s')
    else:
        ban = q.popleft()
        if t[i] == 'r':
            if ban != 'p':
              points += p
              q.append('p')
            else:
                q.append('x')
        elif t[i] == 's':
            if ban != 'r':
              points += r
              q.append('r')
            else:
                q.append('x')
        elif t[i] == 'p':
            if ban != 's':
              points += s
              q.append('s')
            else:
                q.append('x')
                
print(points)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.append(0)
s=input()
ans=0
for i in range(k,n):
  if s[i]==s[i-k]:
    s=s[:i]+'z'+s[i+1:]
for i in range(n):
  ans+=a['sprz'.index(s[i])]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n,k = readInts()
r,s,p = readInts()
t = list(input())

his = ["-1"]
ans = 0
for i in range(len(t)):
  if t[i]=="r":
    me = "p"
    point = p
  elif t[i]=="s":
    me = "r"
    point = r
  else:
    me = "s"
    point = s
  if his[max(0,i-k+1)]==me:
    his.append(i)
  else:
    ans+=point
    his.append(me)

print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9+7
def resolve():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = list(input())
    
    score = 0
    hands = {
        "r": ("p", P),
        "s": ("r", R),
        "p": ("s", S)
    }
    hist = []
    for i, t in enumerate(T):
        hand, point = hands[t]
        if i >= K and hand == hist[-K]:
            hist.append("*")
            continue
        score += point
        hist.append(hand)
    #print(hist)
    print(score)
    

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

POINT = tuple(map(int,input().split()))

T = input()
m = {'r':0,'s':1,'p':2}
T = [m[h] for h in T]

def helper(T):
    dp = [0]*3
    for t in T:
        wh = (t-1)%3
        op = max(dp)
        dp[wh] = max(v for i,v in enumerate(dp) if i != wh)+POINT[wh]
        for h in range(3):
            if h != wh:
                dp[h] = op
    return max(dp)


res = sum(helper(T[i::K]) for i in range(min(K,N)))

print(res)