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 = list(input())

point = 0
for i in range(n):
    if (i >= k) & (t[i] == t[i - k]):
        t[i] = 'a'
        continue
    else:
        if t[i] == 'r':
            point += p
        elif t[i] == 's':
            point += r
        else:
            point += s
print(point)

Submission status: Accepted

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

ans=0
dp=[0 for i in range(N)]
for i in range(K):
    dp[i]=win[T[i]]
    ans+=score[dp[i]]
        
for i in range(K,N):
    if T[i-K]==T[i] and dp[i-K]==win[T[i]]:
        dp[i]=T[i]
    elif T[i-K]==T[i]:
        dp[i]=win[T[i]]
        ans+=score[dp[i]]
    else:
        dp[i]=win[T[i]]
        ans+=score[dp[i]]
print(ans)

Submission status: Accepted

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

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

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

ans = [None] * N

sum_ = 0
for i, t in enumerate(T):
    if i < K or ans[i - K] != win[t]:
        ans[i] = win[t]
        sum_ += tensu[ans[i]]

print(sum_)

Submission status: Accepted

Solution code:
def f(c):
    """
    入力に対する勝ち手を出力
    """
    if c=="r":
        return "p"
    elif c=="s":
        return "r"
    else:
        return "s"

def main():
    """
    まずは全て勝つ組み合わせを作って、合計ポイントを求める。
    iを0からN-Kまで変化させ、T(i)=T(k+i)となる時、
    T(k+i)をT(i)、T(i+2K)と異なる手に変更する。
    ※T(i)を変更すると、T(i)=T(i+k)=T(i+2K)=...=と連続した時に、
    全て変更されてスコアが最大にならない。
    T(k+i)は勝ちから"あいこまたは負け"に変化するので、合計ポイントから差っ引く。
    """

    #input
    N,K=map(int,input().strip().split())
    R,S,P=map(int,input().strip().split())
    T=list(input())
    
    win_T=list(map(lambda x:f(x),T))
    point={"r":R,"s":S,"p":P}
    score=sum([point[win_T[n]] for n in range(N)])
    for i in range(N-K):
        l=["r","s","p"]
        if win_T[i]==win_T[i+K]:
            score-=point[win_T[i+K]]
            l.remove(win_T[i])
            if i<=N-2*K-1 and win_T[i]!=win_T[i+2*K]:
                l.remove(win_T[i+2*K])
            win_T[i+K]=l[0]
    return score

if __name__=="__main__":
    print(main())

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=list(input())
cnt=0
u=[]
for i in range(n):
  if t[i]=='r':
    u.append("p")
  elif t[i]=='s':
    u.append("r")
  else:
    u.append("s")

for i in range(n):
  if u[i]=="r":
    if i<k:
      cnt+=r
    else:
      if u[i-k]=="r":
        cnt+=0
        u[i]="#"
      else:
        cnt+=r

  elif u[i]=="s":
    if i<k:
      cnt+=s
    else:
      if u[i-k]=="s":
        cnt+=0
        u[i]="#"
      else:
        cnt+=s

  elif u[i]=="p":
    if i<k:
      cnt+=p
    else:
      if u[i-k]=="p":
        cnt+=0
        u[i]="#"
      else:
        cnt+=p
#print("".join(u))
print(cnt)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[[] for _ in range(K)]
for i in range(N):
    A[i%K].append(T[i])

count=0
for i in range(K):
  cnt=0
  for j in range(len(A[i])):
    if j==0:
      if A[i][j]=='p':
        count+=S
      if A[i][j]=='s':
        count+=R
      if A[i][j]=='r':
        count+=P
    else:
      if A[i][j]==A[i][j-1]:
        cnt+=1
      if A[i][j]!=A[i][j-1]:
        cnt=0
      if cnt%2==0 and A[i][j]=='p':
          count+=S
      if cnt%2==0 and A[i][j]=='r': 
          count+=P
      if cnt%2==0 and A[i][j]=='s':
          count+=R
print(count)

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

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

def main():
    n,k = readInt()
    r,s,p = readInt()
    rsp = {"s":r,"p":s,"r":p}
    t = input()
    ans = 0
    u = []
    for i in range(n):
        if i<k:
            ans += rsp[t[i]]
            u.append(t[i])
        else:
            if t[i]!=u[i-k]:
                ans += rsp[t[i]]
                u.append(t[i])
            else:
                u.append("f")
    print(ans)
    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()
ans = 0
hand = []

for i in range(len(t)):
    if t[i] == 'r':
        if i - k >= 0 and hand[i - k] == 'p':
            hand.append(' ')
            continue
        else:
            ans += p
            hand.append('p')
    elif t[i] == 's':
        if i - k >= 0 and hand[i - k] == 'r':
            hand.append(' ')
            continue
        else:
            ans += r
            hand.append('r')
    else:
        if i - k >= 0 and hand[i - k] == 's':
            hand.append(' ')
            continue
        else:
            hand.append('s')
            ans += s

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(len(t)):
    tmp = t[i]
    if i >= k:
        if tmp == t[i-k]:
            t[i] = "c"
            continue
    if tmp == "r":
        ans += p
    elif tmp == "s":
        ans += r
    elif tmp == "p":
        ans += s
        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def mi():
    return map(int, input().split())

def main():
    N, K = mi()
    R, S, P = mi()
    T = input()
    pt = 0
    my_choices = ['']*N

    for i in range(K):
        tmp = T[i::K]
        for j in range(len(tmp)):
            if j == 0:
                if tmp[j] == 'r':
                    my_choices[i] = 'p'
                    pt += P
                if tmp[j] == 's':
                    my_choices[i] = 'r'
                    pt += R
                if tmp[j] == 'p':
                    my_choices[i] = 's'
                    pt += S
            else:
                if tmp[j] == 'r' and my_choices[i+(j-1)*K] != 'p':
                    my_choices[i+j*K] = 'p'
                    pt += P
                if tmp[j] == 's' and my_choices[i+(j-1)*K] != 'r':
                    my_choices[i+j*K] = 'r'
                    pt += R
                if tmp[j] == 'p' and my_choices[i+(j-1)*K] != 's':
                    my_choices[i+j*K] = 's'
                    pt += S

    print(pt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def subtask(sseq, pt):
    n = len(sseq)
#    for i in range(n):
    dp = [[0]*3 for _ in range(n+1)]
    for i in range(n):
        e = sseq[i]
        w = (e+2)%3
        l = (e+1)%3

        dp[i+1][w] = max(dp[i][e]+pt[w] ,dp[i][l]+pt[w])
        dp[i+1][l] = max(dp[i][e] ,dp[i][w])
        dp[i+1][e] = max(dp[i][w] ,dp[i][l])

    return max(dp[n])

def main():
    N, K = map(int, input().split())
    pt = list(map(int, input().split()))
    Sr = [[] for _ in range(K)]
    S = input()
    S = S.translate(str.maketrans('rsp','012'))

    for i, a in enumerate(S):
        Sr[i%K].append(int(a))

    ret=0
    for sq in Sr:
        ret += subtask(sq, pt)

    print(ret)
#    print(Sr)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(k):
    for j in range(i, n, k):
        if j + k < n:
            if t[j] == t[j + k]:
                t[j + k] = "0"

ans = t.count("r")*r + t.count("p")*p + t.count("s")*s
print(ans)

Submission status: Accepted

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

    ans = 0
    for i in range(K):
        p = 'z'
        for c in T[i::K]:
            if c == p:
                p = 'z'
                continue
            if c == 'r':
                ans += P
            elif c == 's':
                ans += R
            else:
                ans += S
            p = c
    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()
 
flags = [0] * len(T)
 
point_map = {'r': P, 's': R, 'p': S}
 
point_sum = 0
 
for i, t in enumerate(T):
    if i < K:
        point_sum += point_map[t]
        continue
    if t == T[i-K] and not flags[i-K]:
        flags[i] = 1#勝ちを0，勝てなかったことをを1で表現している
        continue
    point_sum += point_map[t]

print(point_sum)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
lis = ['' for _ in range(len(t))]
ans = 0

for i in range(k):
  if t[i] == 'r':
    ans += p
    lis[i] = 'p'
  elif t[i] == 'p':
    ans += s
    lis[i] = 's'
  else:
    ans += r
    lis[i] ='r'
    
for j in range(k,len(t)):
  if t[j] == 'p' and lis[j-k] != 's':
    ans+=s
    lis[j] = 's'
  elif t[j] == 'p' and lis[j-k] == 's':
    lis[j] = 'katenai'

    
  elif t[j] == 's' and lis[j-k] != 'r':
    ans+=r
    lis[j] = 'r'

  elif t[j] == 's' and lis[j-k] == 'r':
    lis[j] = 'katenai'

    
  elif t[j] == 'r' and lis[j-k] != 'p':
    ans+=p
    lis[j] = 'p'

  elif t[j] == 'r' and lis[j-k] == 'p':
    lis[j] = 'katenai'
      

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction


def main():
    num, k = map(int, input().split())
    R, S, P = map(int, input().split())
    data = list(input())

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

    for i in range(num):
        hand = data[i]
        if i < k:
            ans += ans_dic[hand]
        else:
            if data[i - k] == hand:
                data[i] = -1
            else:
                ans += ans_dic[hand]
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
R,S,P = map(int,input().split())
po = {'r':P, 's':R, 'p':S}
t = list(input())
sub = [[] for _ in range(k)]

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

point = 0
for j in range(k):
    m = len(sub[j])
    point += po[sub[j][0]]
    for l in range(1,m):
        if sub[j][l-1] != sub[j][l]:
            point += po[sub[j][l]]
        else:
            sub[j][l] = '#'

print(point)

Submission status: Accepted

Solution code:
# 626~
# 解法700 DPを使う
# dp[i][l] : iまで選択して，i+1ばん目に出した手がlである場合の最大ポイント
# 計算量はO(N*3)になるので問題なし
# dpの更新式 dp[i+k][0] = (dp[i][1]+(R[1],0),dp[i][2]+(R[2],0))

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
rsp = ['r','s','p']


def janken(my_s,op_s):
    if my_s == 'r' and op_s=='s':
        return R
    if my_s == 's' and op_s=='p':
        return S
    if my_s == 'p' and op_s=='r':
        return P
    return 0


dp = [[0,0,0] for _ in range(N+1)]
# 0: R 1: P 2:Sを出す
for n in range(K):
    dp[n] = [janken(rsp[i],T[n]) for i in range(3)]

for i in range(N-K):
    dp[i+K][0] = max(dp[i][1]+janken(rsp[0],T[i+K]), dp[i][2]+janken(rsp[0],T[i+K]))
    dp[i+K][1] = max(dp[i][2]+janken(rsp[1],T[i+K]), dp[i][0]+janken(rsp[1],T[i+K]))
    dp[i+K][2] = max(dp[i][0]+janken(rsp[2],T[i+K]), dp[i][1]+janken(rsp[2],T[i+K]))
ans = 0
for i in range(K):
    ans += max(dp[N-1-i])
print(ans)

Submission status: Accepted

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

flags = [0] * len(T)

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

point_sum = 0

for i, t in enumerate(T):
    if i < K:
        point_sum += point_map[t]
        continue
    if t == T[i-K] and not flags[i-K]:
        flags[i] = 1
        continue
    point_sum += point_map[t]

print(point_sum)

Submission status: Accepted

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

v=[]
ans=0
for i in t:
  if i=='r':
    v.append('p')
    ans+=P
  if i=='s':
    v.append('r')
    ans+=R
  if i=='p':
    v.append('s')
    ans+=S

for i in range(k,n):
  if v[i]==v[i-k]:
    if v[i]=='r':
      ans-=R
    if v[i]=='s':
      ans-=S
    if v[i]=='p':
      ans-=P

    v[i]='?'

print(ans)

Submission status: Accepted

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

#じゃんけんの実装
def R(i):
    if T[i-1]==("s"):
        return r
    else:
        return 0
def S(i):
    if T[i-1]==("p"):
        return s
    else:
        return 0

def P(i):
    if T[i-1]==("r"):
        return p
    else:
        return 0

dp=[[0]*3 for _ in range(n+1)]
dp[0][0],dp[0][1],dp[0][2]=0,0,0
for i in range(1,k+1):
    dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+R(i)
    dp[i][1]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+S(i)
    dp[i][2]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+P(i)
    
#dp[n回目のジャンケンか][どの手で勝つか]=n回目のジャンケン後の点数の最大値
for i in range(0,n-k):
    if T[i]!="r":
        dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    else:
        if dp[i+1][2]==0:
            dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    if T[i]!="s":
        dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)
    else:
        if dp[i+1][0]==0:
            dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)

    if T[i]!="p":
        dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)
    else:
        if dp[i+1][1]==0:
            dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)

    
print(max(dp[n]))

Submission status: Accepted

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

def R(i):
    if T[i-1]==("s"):
        return r
    else:
        return 0
def S(i):
    if T[i-1]==("p"):
        return s
    else:
        return 0

def P(i):
    if T[i-1]==("r"):
        return p
    else:
        return 0

dp=[[0]*3 for _ in range(n+1)]
dp[0][0],dp[0][1],dp[0][2]=0,0,0
for i in range(1,k+1):
    dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+R(i)
    dp[i][1]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+S(i)
    dp[i][2]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2])+P(i)
    

for i in range(0,n-k):
    if T[i]!="r":
        dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    else:
        if dp[i+1][2]==0:
            dp[i+k+1][2]=max(dp[i+k])+P(i+k+1)
    if T[i]!="s":
        dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)
    else:
        if dp[i+1][0]==0:
            dp[i+k+1][0]=max(dp[i+k])+R(i+k+1)

    if T[i]!="p":
        dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)
    else:
        if dp[i+1][1]==0:
            dp[i+k+1][1]=max(dp[i+k])+S(i+k+1)

    
print(max(dp[n]))

Submission status: Accepted

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

for i in t[:k]:
    if i == "r":
        point += p
    elif i == "s":
        point += r
    else:
        point += s

for i in range(k,n):
    if t[i-k] == t[i]:
        t[i] = "x"
        continue
    if t[i] == "r":
        point += p
    elif t[i] == "s":
        point += r
    else:
        point += s

print(point)

Submission status: Accepted

Solution code:
n,k= map(int,input().split())
r,s,p= map(int,input().split())
pdic = {'r':p,'s':r,'p':s}
jan= input()
words=['']*k
junbite=[]
junbisu=[]
for i in range(n):words[i%k]+=jan[i]
for i in range(k):
    pre=''
    count=1
    tempte=[]
    tempsu=[]
    for j,c in enumerate(words[i]):
        if j==0:pre=c
        else:
            if pre==c:count+=1
            else:
                tempte.append(pre)
                tempsu.append(count)
                count=1
                pre=c
    else:
        tempte.append(pre)
        tempsu.append(count)
    junbite.append(tempte)
    junbisu.append(tempsu)
g=0
for i in range(k):
    for te,su in zip(junbite[i],junbisu[i]):
        point = su//2
        if su%2==1:point+=1
        g += pdic[te]*point
print(g)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
wins = [False] * k

for i in range(n):
    if not wins[i % k] or t[i] != t[i-k]:
        ans += c[t[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False

print(ans)

Submission status: Accepted

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

ans = 0
counter = [{v:0 for v in "rsp"} for i in range(k)]

for i in range(n):
    if t[i] == 'r':
        counter[i%k]['r'] += 1
        counter[i%k]['s'] = 0
        counter[i%k]['p'] = 0
        if counter[i%k]['r'] %2 == 1:
            ans += p
    if t[i] == 's':
        counter[i%k]['s'] += 1
        counter[i%k]['r'] = 0
        counter[i%k]['p'] = 0
        if counter[i%k]['s'] %2 == 1:
            ans += r
    if t[i] == 'p':
        counter[i%k]['p'] += 1
        counter[i%k]['s'] = 0
        counter[i%k]['r'] = 0
        if counter[i%k]['p'] %2 == 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()
k = []
ans = 0
for i in range(K):
  if T[i] == 's':
    ans += R
  elif T[i] == 'p':
    ans += S
  elif T[i] == 'r':
    ans += P
  k.append(T[i])
for i in range(K, N):
  if T[i] == 's':
    if k[0] != 's':
      ans += R
      k.pop(0)
      k.append('s')
    else:
      k.pop(0)
      k.append('0')
  elif T[i] == 'p':
    if k[0] != 'p':
      ans += S
      k.pop(0)
      k.append('p')
    else:
      k.pop(0)
      k.append('0')
  elif T[i] == 'r':
    if k[0] != 'r':
      ans += P
      k.pop(0)
      k.append('r')
    else:
      k.pop(0)
      k.append('0')
print(ans)

Submission status: Accepted

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

ans = 0
hand = [""] * n

for i in range(n):
    if i < k:
        if t[i] == "r":
            ans += p
            hand[i] = "p"
        elif t[i] == "s":
            ans += r
            hand[i] = "r"
        elif t[i] == "p":
            ans += s
            hand[i] = "s"
    else:
        if t[i] == "r":
            if hand[i-k] != "p":
                ans += p
                hand[i] = "p"
            else:
                hand[i] = "?"
        elif t[i] == "s":
            if hand[i-k] != "r":
                ans += r
                hand[i] = "r"
            else:
                hand[i] = "?"
        elif t[i] == "p":
            if hand[i-k] != "s":
                ans += s
                hand[i] = "s"
            else:
                hand[i] = "?"
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}
ans = 0
for i in range(k):
    ans += scores[t[i]]
    flg = False
    for j in range(i+k, n, k):
        if (not flg) and t[j]==t[j-k]:
            flg = True
            continue
        flg = False
        ans += scores[t[j]]
print(ans)

Submission status: Accepted

Solution code:
#グーで勝つR　チョキで勝つS　パーで勝つP
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
point = {"r":p, "s":r, "p":s}
do = {"r":"p", "s":"r", "p":"s"}
d = []

score = 0
for i in range(n):
    if i < k:
        score += point[t[i]]
        d.append(do[t[i]])
    else:
        if d[-k] == do[t[i]]:
            d.append("")
        else:
            score += point[t[i]]
            d.append(do[t[i]])
print(score)

Submission status: Accepted

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

L = [[] for _ in range(K)]
for i in range(N):
  temp = T[i]
  MOD = i%K
  L[MOD].append(temp)
ans = 0
for i in range(len(L)):
  for j in range(len(L[i])):
    if j == 0:
      Flag = True #なんでも出せる
    elif not Flag: #前回が制限された
      Flag = True
    elif Flag and L[i][j] == L[i][j-1]:
      Flag = False
    if L[i][j] == "r" and Flag: 
      ans += P
    elif L[i][j] == "s" and Flag: 
      ans += R
    elif L[i][j] == "p" and Flag: 
        ans += S
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 i in range(n + 1)]
for i in range(n):
    if i + 1 - k < 0:
        if t[i] == "r":
            dp[i + 1][0] = p
        if t[i] == "s":
            dp[i + 1][1] = r
        if t[i] == "p":
            dp[i + 1][2] = s
    else:
        if t[i] == "r":
            dp[i + 1][0] = p + max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = max(dp[i + 1 - k][0], dp[i + 1 - k][1])
        if t[i] == "s":
            dp[i + 1][0] = max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = r + max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = max(dp[i + 1 - k][0], dp[i + 1 - k][1])
        if t[i] == "p":
            dp[i + 1][0] = max(dp[i + 1 - k][1], dp[i + 1 - k][2])
            dp[i + 1][1] = max(dp[i + 1 - k][2], dp[i + 1 - k][0])
            dp[i + 1][2] = s + max(dp[i + 1 - k][0], dp[i + 1 - k][1])
ans = 0
for i in range(k):
    ans += max(dp[-i - 1])
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    R, S, P = map(int, input().split())
    t = list(input())
    res = 0
    for i in range(n):
        if i < k:
            res += R if t[i] == "s" else S if t[i] == "p" else P
        else:
            if t[i - k] != t[i]:
                res += R if t[i] == "s" else S if t[i] == "p" else P
            else:
                t[i] = "0"
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
win_hand = { "r": P, "s": R, "p": S }
prev_hand = {}
LOST,WON = -1,1

points = 0
for i in range(N):
  if i-K >= 0:
    if T[i] != T[i-K]:
      prev_hand[i] = WON
      points += win_hand[T[i]]
    else:
      if prev_hand[i-K] == WON:
        prev_hand[i] = LOST
      else:
        points += win_hand[T[i]]
        prev_hand[i] = WON
  else:
    prev_hand[i] = WON
    points += win_hand[T[i]]

print(points)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
r, s, p = p, r, s
point = {'p':p, 'r':r, 's':s}

t = input()
lose_flag = []
ans = 0
for i in range(len(t)):
    if i >= k and t[i] == t[i-k] and not lose_flag[i-k]:
        lose_flag.append(True)
        continue
    lose_flag.append(False)
    ans += point[t[i]]
    # print(point[t[i]])
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
u = [False] * n

res = 0
for i in range(n):
    if t[i] == 'r':
        if i - k >= 0 and t[i-k] == 'r' and u[i-k]:
            continue
        u[i] = True
        res += p
    elif t[i] == 's':
        if i - k >= 0 and t[i-k] == 's' and u[i-k]:
            continue
        u[i] = True
        res += r
    else:
        if i - k >= 0 and t[i-k] == 'p' and u[i-k]:
            continue
        u[i] = True
        res += s

print(res)

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)]
#dp[i][0,1,2] i回目にグーチョキパーを出したときの最大得点
def f(i,r):
    return K*i - r
for r in range(0, K):
    i = 1
    while f(i,r) <= N:
        if T[f(i,r)] == "r":
            if i==1:
                dp[f(i, r)][0] = 0
                dp[f(i, r)][1] = 0
                dp[f(i, r)][2] = P
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2])
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2])
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1]) + P
        if T[f(i,r)] == "s":
            if i==1:
                dp[f(i, r)][0] = R
                dp[f(i, r)][1] = 0
                dp[f(i, r)][2] = 0
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2]) + R
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2])
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1])
        if T[f(i,r)] == "p":
            if i==1:
                dp[f(i, r)][0] = 0
                dp[f(i, r)][1] = S
                dp[f(i, r)][2] = 0
            else:
                dp[f(i, r)][0] = max(dp[f(i-1, r)][1], dp[f(i-1, r)][2])
                dp[f(i, r)][1] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][2]) + S
                dp[f(i, r)][2] = max(dp[f(i-1, r)][0], dp[f(i-1, r)][1])
        i += 1

ans = 0
for j in range(1, K+1):
    ans += max(dp[-j])
print(ans)

Submission status: Accepted

Solution code:
#考察OK、３５分。

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

#並び替え
l = [[] for _ in range(k)]
for num, i in enumerate(t):
    q = num % k
    l[q].append(i)

points = []    
for num1, i in enumerate(l):
    points.append([])
    for num2, j in enumerate(i):
        if num2 == 0:
            if j == 'r':
                points[num1].append(p)
            elif j == 's':
                points[num1].append(r)
            else:
                points[num1].append(s)
        else:
            if j == i[num2-1]:
                if points[num1][num2-1] == 0:
                    if j == 'r':
                        points[num1].append(p)
                    elif j == 's':
                        points[num1].append(r)
                    else:
                        points[num1].append(s)
                else:
                    points[num1].append(0)
            else:
                if j == 'r':
                    points[num1].append(p)
                elif j == 's':
                    points[num1].append(r)
                else:
                    points[num1].append(s)
res = 0
for i in points:
    res += sum(i)

print(res)

Submission status: Accepted

Solution code:
import collections
N,K = list(map(int,input().split()))
R,S,P = list(map(int,input().split()))
T = list(input())
for i in range(N):
    if i+K < N and T[i] == T[i+K]:
        T[i+K] = 'x'
C = collections.Counter(T)
ans = 0
ans += C['r']*P
ans += C['s']*R
ans += C['p']*S
print(ans)