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())


dic = {'r':P, 's':R, 'p':S}
flag = [0]*N
rlt = 0

T = input()

for i in range(N):
  if i < K or T[i] != T[i-K] or flag[i-K] == 1:
    rlt += dic[T[i]]
  else:
    flag[i] = 1
    
print(rlt)

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(K):
  last = "D"
  wl = "l"
  for j in range(N//K+1):
    if  j*K + i >= N:
      break
    else:
      te = T[j*K+i]
      if te != last:
        last = te
        wl = "w"
        if te == "r":
          ans += P
        elif te == "s":
          ans += R
        else:
          ans += S
      else:
        if wl == "l":
          last = te
          wl = "w"
          if te == "r":
            ans += P
          elif te == "s":
            ans += R
          else:
            ans += S
        else:
          last = te
          wl = "l"
print(ans)

Submission status: Accepted

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T = input().rstrip()
s =T.replace('r','P') #P
ss=s.replace('s','R') #R
sss=ss.replace('p','S') #S
sl = list(sss)

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

sc = collections.Counter(sl)

print(sc['R']*R + sc['P']*P + sc['S']*S)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    T = list(input())
    l = list()
    ans = 0
    for i in range(k):
        l.append(T[i])
    for i in range(k, n):
        if l[i-k] == T[i]:
            l.append(None)
        else:
            l.append(T[i])
    res = Counter(l)
    ans = res['r'] * p + res['s'] * r + res['p'] * s
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
card = {x : i for x, i in zip (("s", "p", "r") , map(int, input().split()))}
t = input()
ans = [0]*k

for i in range(k):
    chk = 0
    for j in range(i, n, k):
        if j < k:
            tmp, chk = card[t[j]], 1
        elif t[j] != t[j-k]:
            tmp, chk = card[t[j]], 1
        elif chk == 0 and t[j] == t[j-k]:
            tmp, chk = card[t[j]], 1
        else:
            tmp, chk = 0, 0
        ans[i] += tmp
print(sum(ans))

Submission status: Accepted

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

N,K=(int(x) for x in input().rstrip('\n').split())
R,S,P=(int(x) for x in input().rstrip('\n').split())
T = list(input().rstrip('\n'))
s_d = {'r':P,'s':R,'p':S}
score = 0
for i in range(K):
  now=i
  now_h=''
  while now<N:
    next= T[now]
    if next!=now_h:
      score += s_d[next]
      now_h=next
    else:
      now_h=''
    now += K
print(score)

Submission status: Accepted

Solution code:
#abc149-d
n,k=map(int,input().split())
s,p,r=map(int,input().split())
f={'s':s,'p':p,'r':r}
t=str(input())
ans=0
for i in range(k):
    a=i+k
    last=t[i]
    ans+=f[last]
    while a<n-k:
        if t[a]==last:
            if t[a+k]==last:
                if last=='s':
                    last='r'
                else:
                    last='s'
            else:
                if last=='s':
                    if t[a+k]=='r':
                        last='p'
                    else:
                        last='r'
                elif last=='r':
                    if t[a+k]=='s':
                        last='p'
                    else:
                        last='s'
                else:
                    if t[a+k]=='r':
                        last='s'
                    else:
                        last='r'
        else:
            last=t[a]
            ans+=f[last]
        a+=k
    
    if a<n:
        if t[a]!=last:
            ans+=f[t[a]]

print(ans)

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}
d_={'r':'p','s':'r','p':'s'}
s=0
for i in range(k):
  a=0
  z='0'
  t_=t[i::k]
  for a in range(len(t_)):
    if a==0:
      s+=d[t_[a]]
      z=d_[t_[a]]
      a+=1
    else:
      if z==d_[t_[a]]:
        z='0'
        a+=1
      else:
        s+=d[t_[a]]
        z=d_[t_[a]]
        a+=1
print(s)

Submission status: Accepted

Solution code:
import collections
n, k = [int(w) for w in input().split()]
r, s, p = [int(w) for w in input().split()]
score = {"s": r, "p": s, "r": p}

t = input()

cnt = collections.Counter(t)

ans = 0
ans += cnt["s"] * r
ans += cnt["p"] * s
ans += cnt["r"]*p

for i in range(k):
    sp_t = t[i:n: k]
    is_prev_lose = False
    for j in range(1, len(sp_t)):
        if sp_t[j - 1] == sp_t[j]:
            if is_prev_lose:
                is_prev_lose = False
            else:
                is_prev_lose = True
                ans -= score[sp_t[j]]
        else:
            is_prev_lose = False
print(ans)

Submission status: Accepted

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

(n, k), (r, s, p), t = map(int, input().split()), map(int, input().split()), input()[:-1]
d, res = {'r': p, 's': r, 'p': s}, 0
for i in range(k):
    b = False
    for j in range(i, n, k):
        if i == j: res += d[t[j]]; b = False; continue
        if (t[j] != t[j-k]) | b: res += d[t[j]]; b = False
        else: b = True
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
ans=0
dic={'r':'p','s':'r','p':'s'}
score={'r':R,'s':S,'p':P}
past=[]
for i in range(len(T)):
    if i>=K:
        if dic[T[i]]==past[i-K]:
            past.append('0')
            continue
        else:
            ans+=score[dic[T[i]]]
            past.append(dic[T[i]])
    else:
        ans+=score[dic[T[i]]]
        past.append(dic[T[i]])
print(ans)

Submission status: Accepted

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

dp = [[0]*3 for _ in range(N+1)]
for i in range(1, N+1):
    if i <= K:
        dp[i][0] = int(T[i-1]=='s') * R
        dp[i][1] = int(T[i-1]=='p') * S
        dp[i][2] = int(T[i-1]=='r') * P
    else:
        dp[i][0] = max(dp[i-K][1], dp[i-K][2]) + int(T[i-1]=='s') * R
        dp[i][1] = max(dp[i-K][0], dp[i-K][2]) + int(T[i-1]=='p') * S
        dp[i][2] = max(dp[i-K][0], dp[i-K][1]) + int(T[i-1]=='r') * P

ans = 0
for d in dp[-K:]:
    ans += max(d)
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

def get_point(x):
    if x == 'r':
        return P
    if x == 's':
        return R
    if x == 'p':
        return S

# think each mod K
ans = 0
for i in range(K):
    dp = [0] * 2
    pre = ''
    for j in range(i, N, K):
        dp2 = dp[:]
        dp2[0] = max(dp)
        if pre == T[j]:
            dp2[1] = dp[0] + get_point(T[j])
        else:
            dp2[1] = max(dp) + get_point(T[j])
        pre = T[j]
        dp = dp2
    ans += max(dp)

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
slist = ['']*k
for i in range(n):
    slist[i%k] += T[i]

ans = 0
for s in slist:
    dp = [[0,0,0]for i in range(len(s))]
    #dp[i][j] = 直前にｊを出したときの得点の最大値
    '''
    0..r
    1..s
    2..p
    '''
    dp[0][1] = S if s[0] == 'p' else 0
    dp[0][0] = R if s[0] == 's' else 0
    dp[0][2] = P if s[0] == 'r' else 0

    for i in range(1,len(s)):
        if s[i] == 'r':
            dp[i][2] = max(dp[i-1][0],dp[i-1][1]) + P
            dp[i][1] = max(dp[i-1][0],dp[i-1][2])
            dp[i][0] = max(dp[i-1][1],dp[i-1][2])
        elif s[i] == 's':
            dp[i][0] = max(dp[i-1][2],dp[i-1][1]) + R
            dp[i][1] = max(dp[i-1][0],dp[i-1][2])
            dp[i][2] = max(dp[i-1][1],dp[i-1][0])
        else:
            dp[i][1] = max(dp[i-1][2],dp[i-1][0]) + S
            dp[i][0] = max(dp[i-1][1],dp[i-1][2])
            dp[i][2] = max(dp[i-1][1],dp[i-1][0])
    
    ans += max(dp[len(s)-1][0],dp[len(s)-1][1],dp[len(s)-1][2])
#print(slist)
print(ans)

Submission status: Accepted

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


ok = [True] * N
d = {"r": P, "s": R, "p": S}
ans = 0
for i in range(N):
    if i >= K and T[i - K] == T[i] and ok[i - K]:
        ok[i] = False
    if ok[i]:
        ans += d[T[i]]
print(ans)

Submission status: Accepted

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

  l = ["" for i in range(K)]
  s = 0
  for i in range(N):
    # print(l)
    if l[i%K] == T[i]:
      l[i%K]=""
      continue

    if l[i%K] == "":
      if T[i] == "r":
        s = s+P
        l[i%K] = "r"
      elif T[i] == "s":
        s = s+R
        l[i%K] = "s"
      else:
        s = s+S
        l[i%K] = "p"

    elif T[i]=="r":
      s = s+P
      l[i%K] = "r"
    elif T[i]=="s":
      s = s+R
      l[i%K] = "s"
    else:
      s = s+S
      l[i%K] = "p"

  print(s)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

from collections import Counter
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n, k = LI()
r, s, p = LI()
d = {'r': p, 's': r, 'p': s}
t = S()
ans = 0

# 結果が干渉し合うのは k = 4 とすると
# 1, 5, 9, ...
# 2, 6, 10, ...
# 3, 7, ...
# 4, 8, ...
# となり、各グループごとに独立に考えていい
# それぞれのグループで「連続して同じ手がだせない」じゃんけんをする
# 貪欲法 での解法
for j in range(k):
    is_win_last = False  # 前回勝ったかかどうか
    i = j
    while i < n:
        # 前回と手が同じで、前回は勝っている場合
        if i >= k and t[i - k] == t[i] and is_win_last:
            is_win_last = False
        else:  # 前回と手が異なる場合 もしくは 前回と手が同じだが負けている場合
            ans += d[t[i]]
            is_win_last = True
        i += k


print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
total=0
M=[0]*n
for i in range(n):
  if t[i]=='r' and (i<k or M[i-k]!='p'):
    M[i]='p'
    total+=p
  elif t[i]=='s' and (i<k or M[i-k]!='r'):
    M[i]='r'
    total+=r
  elif t[i]=='p' and (i<k or M[i-k]!='s'):
    M[i]='s'
    total+=s
  #print(total,t[i-k],t[i],i-k)
print(total)

Submission status: Accepted

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

def add(x):
    if x == 'r':
        return p
    elif x == 's':
        return r
    else:
        return s

nk =[0]*k
for i in range(n):
    key = i%k
    if nk[key]==0:
        nk[key] = [t[i]]
    else:
        nk[key].append(t[i])
ans = 0
for j in nk:
    if j ==0:
        continue
    ans += add(j[0])
    for k in range(1, len(j)):
        if j[k]== j[k-1]:
            j[k]='q'
        else:
            ans += add(j[k])
print(ans)

Submission status: Accepted

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

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

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

score = 0

for i in range(N):
    if i + K < N:
        if T[i] == T[i + K]:
            T[i + K] = 'N'
    
    score += P[T[i]]

print(score)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
dp=[0]*N
data={'r':P,'s':R,'p':S}
ans=0
for i in range(N):
  if 0<=i-K:
    if T[i-K]==T[i]:
      if dp[i-K]==0:
        ans+=data[T[i]]
        dp[i]=1
    else:
      ans+=data[T[i]]
      dp[i]=1
  else:
    dp[i]=1
    ans+=data[T[i]]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
result = []
for i in range(K):
    result.append(T[i])
for j in range(K, N):
    if result[j-K] == T[j]:
        result.append("d")
    else:
        result.append(T[j])

result = Counter(result)
print(result["r"]*P+result["s"]*R+result["p"]*S)

Submission status: Accepted

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

win = ''
for i in range(len(T)-k):
    if T[i]==T[i+k]:
        T[i+k]='#'
ans = 0
for i in T:
    if i=='r':ans+=p
    elif i=='s':ans+=r
    elif i=='p':ans+=s
print(ans)

Submission status: Accepted

Solution code:
def game(word):
    if word=="r":return p,"p"
    elif word=="s":return r,"r"
    else:return s,"s"


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

point=0
for i in range(n):
    num,word=game(t[i])
    if i<k:
        point +=num
        ans +=word
    else:
        if (t[i-k]==t[i] and ans[-k]!=word) or (t[i-k]!=t[i]):
            point +=num
            ans +=word
        else:
            ans +=t[i]

print(point)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

print(ans)

Submission status: Accepted

Solution code:
#create date: 2020-07-01 11:19

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    r, s, p = na()
    t = ns()
    ans = 0
    for j in range(k):
        m = (n-j-1)//k+1
        dp = [[0]*m for _ in range(3)]
        o = t[j]
        if o=="s":
            dp[0][0] = r
        if o=="r":
            dp[1][0] = p
        if o=="p":
            dp[2][0] = s
        for i in range(1, m):
            o = t[k*i+j]
            premax = max(dp[0][i-1], dp[1][i-1], dp[2][i-1])
            if o == "s":
                dp[0][i] = max(dp[1][i-1], dp[2][i-1]) + r
                dp[1][i] = premax
                dp[2][i] = premax
            if o == "r":
                dp[1][i] = max(dp[0][i-1], dp[2][i-1]) + p
                dp[0][i] = premax
                dp[2][i] = premax
            if o == "p":
                dp[2][i] = max(dp[0][i-1], dp[1][i-1]) + s
                dp[0][i] = premax
                dp[1][i] = premax
        ans += max([dp[0][-1], dp[1][-1], dp[2][-1]])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
point = list(map(int, input().split())) #R, S, P
T = input()
te = ["r", "s", "p"]
past = []
ans = 0

for i,char in enumerate(T):
    index_win = (te.index(char) + 2) % 3
    te_win = te[index_win]
    if i >= K:
        if te_win == past[i - K]:
            past.append("$")
            continue
        else:
            ans += point[index_win]
    else:
        ans += point[index_win]
    past.append(te_win)

print(ans)

Submission status: Accepted

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

ans = 0

for i in range(n):
    valian = t[i]
    if valian == 'r':
        ans += p
    elif valian == 's':
        ans += r
    else:
        ans += s
        
for i in range(k):
    check = ''
    for j in range(i, n, k):
        if check == '':
            check = t[j]
            continue
        
        if check == t[j]:
            if t[j] == 'r':
                ans -= p
            elif t[j] == 's':
                ans -= r
            else:
                ans -= s
            check = ''
        else:
            check = t[j]
        
print(ans)

Submission status: Accepted

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

hands = ["any"]*n

for i in range(n):
  if i-K < 0 or hands[i-K] != t[i]:
    hands[i] = t[i]
    
score = 0
for h in hands:
  if h == "r":
    score += P
  elif h == "s":
    score += R
  elif h == "p":
    score += S
print(score)

Submission status: Accepted

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

ans=0
for i in range(N):
    if i>=K and T[i-K]==T[i]:
            T[i]="o"
    else:
        ans+=d[T[i]]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9+7

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

memo = [None] * n
ans = 0

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
dp = [0] * n #勝ったら1
ans = 0
for i in range(k):
    if t[i] == 'r':
        ans += p 
    elif t[i] == 's':
        ans += r 
    else:
        ans += s 
    dp[i] = 1
for i in range(k, n):
    if t[i] == 'r':
        if t[i - k] == t[i] and dp[i - k] == 1:
            continue
        else:
            dp[i] = 1
            ans += p 
            
    elif t[i] == 's':
        if t[i - k] == t[i] and dp[i - k] == 1:
            continue
        else:
            dp[i] = 1 
            ans += r 
    else:
        if t[i - k] == t[i] and dp[i - k] == 1:
            continue
        else:
            dp[i] = 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()
keys = [i for i in range(N)]
hands = {key: 0 for key in keys}  # n回目の手を記録

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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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):
    # 勝てる場合にはTrueと勝てる手を教えてくれる
    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)

Submission status: Accepted

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

ans = 0
commands = [''] * N
# enumerateでインデックスと要素を同時に取得
# iがインデックス(0,1,2...)、tが手(r or s or p)
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

 # 制限がかかり始めるK回目以降で、K個前の手と同じになってしまう場合
 # 更にK回後の邪魔にならない手は必ず存在する、ここで「何も出さず0ポイント」と解釈してよい
    if (i - K >= 0) and (commands[i - K] == command):
        command = '-'
        point = 0

    ans += point
    commands[i] = command

print(ans)
#print(commands)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#from collections import defaultdict
import math
#from collections import deque




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
#    d = deque()
    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)
    print(ans)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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):
  #print(i)
  #print(prevs)
  for j in range(i*k, (i+1)*k):
    if j >= n:
      break
    prevs_i = j%k
    prev = prevs[prevs_i]
    #print(prev)
    
    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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)