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:
#  Copyright: this code was written at Dec 30th, 2019. for AtCoder, by Silviase
n, k = map(int, input().split())
r, s, p = map(int, input().split())  # r,s,p
t = input()
hand = [True for i in range(len(t))]  # 勝ったかどうか
result = 0
for i in range(0, k):
    for j in range(i, len(t), k):
        if j >= k and t[j] == t[j - k] and hand[j - k] is True:
            hand[j] = False
            continue
        if t[j] == "r":
            result += p
        elif t[j] == "s":
            result += r
        elif t[j] == "p":
            result += s
print(result)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
# Kを無視した勝ち手のリスト
kachite = []
for i in range(N):
  if T[i] == 'r':
    kachite.append('p')
  elif T[i] == 's':
    kachite.append('r')
  else:
    kachite.append('s')

# 勝ち手リストを頭から見ていき、jとj-Kの勝ち手が同じ場合は変更する
for j in range(N):
  if j-K>=0:
    if kachite[j]==kachite[j-K] and kachite[j-K]!='':
      #K回前の勝ち手が同じ(得点が同じ)、かつ、空文字ではない場合
      kachite[j] = '' # K回前、K回後のいずれでもない手を出したことにする
# 最終得点を出力
rpoint = kachite.count('r')*R
spoint = kachite.count('s')*S
ppoint = kachite.count('p')*P
print(rpoint+spoint+ppoint)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int,input().split())
T=input()
score=0
A=[]
for i in range(k):#1-k回目
    if T[i]=='r':
        score+=p
        A.append('p')
    if T[i]=='s':
        score+=r
        A.append('r')
    if T[i]=='p':
        score+=s
        A.append('s')
for i in range(k,n):#k,k+1,...n-1回までの合計n-k回
    if T[i]=='r':
        if A[i-k]!='p':
            score+=p
            A.append('p')
        else:
            A.append('o')
    if T[i]=='s':
        if A[i-k]!='r':
            score+=r
            A.append('r')
        else:
            A.append('o')
    if T[i]=='p':
        if A[i-k]!='s':
            score+=s
            A.append('s')
        else:
            A.append('o')
print(score)

Submission status: Accepted

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

score = t.count('r') * p + t.count('s') * r + t.count('p') * s

for i in range(k):
    li = []
    idx = i
    while idx < n:
        li.append(t[idx])
        idx += k

    pre = "x"
    cnt = 1
    for c in li:
        if c == pre:
            cnt += 1
        if c != pre and cnt > 1:
            cnt //= 2
            if pre == "r":
                score -= p * cnt
            elif pre == "s":
                score -= r * cnt
            else:
                score -= s * cnt
            cnt = 1
        pre = c
    else:
        if cnt > 1:
            cnt //= 2
            if pre == "r":
                score -= p * cnt
            elif pre == "s":
                score -= r * cnt
            else:
                score -= s * cnt
print(score)

Submission status: Accepted

Solution code:
*I,t=open(0).read().split()
n,k,r,s,p=map(int,I)

d={'r':p,'s':r,'p':s}
b=['']*n
a=0

for i,c in enumerate(t[:n]):
    if c!=b[i-k]:
        b[i]=c
        a+=d[c]

print(a)

Submission status: Accepted

Solution code:
*n,t=open(0).read().split()
n,k,r,s,p=map(int,n)
d={'r':p,'s':r,'p':s}
b=['']*n*2
a=0
for i,c in enumerate(t[:n]):
  if c!=b[i-k]:
    b[i]=c
    a+=d[c]
print(a)

Submission status: Accepted

Solution code:
n, k=list(map(int, input().split()))
R, S, P= list(map(int, input().split()))
A = str(input())
U = {'r':0,'s':0,'p':0}
W = [0]*n
for i in range(k):
    U[A[i]]=U[A[i]]+1
    W[i]=1
for i in range(k, n):
    if A[i] == A[i-k]:
        if W[i-k] == 0:
            U[A[i]]=U[A[i]]+1
            W[i]=1
    else:
        U[A[i]]=U[A[i]]+1
        W[i] = 1
print(U['r']*P + U['s']*R + U['p']*S)

Submission status: Accepted

Solution code:
# coding: utf-8
N, K = map(int, input().split())
r, s, p = map(int, input().split())
T = input()
F = [True] * len(T)
point = {"r":p, "s":r, "p":s}
ans = 0
for i in range(len(T)):
    if i < K:
        ans += point[T[i]]
        F[i] = False
    else:
        if T[i] == T[i-K]:
            if F[i - K]:
                ans += point[T[i]]
                F[i] = False
        else:
            ans += point[T[i]]
            F[i] = False
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=[int(j) for j in input().split()]
s,p,r=[int(j) for j in input().split()]
t=input()
ans=0
from itertools import*
for i in range(k):
    l=[]
    for j in range(-(-n//k)):
        tmp=i+k*j
        ss=""
        if tmp<n:
            l.append(t[tmp])
    for i,j in [(k,len(list(v))) for k,v in groupby(l)]:
       ans+=eval(i)*-(-j//2)

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
if  n%k!=0:
    leng=n//k+1
else:
    leng=n//k
data=[[0 for i in range(leng)] for j in range(k)]
ans=0
for i in range(0,k):
    for j in range(0,leng):
        if i+(k*j)<n:
            data[i][j]=t[i+(k*j)]
rz=0
sz=1
pz=2
for j in range(0,k):
    dp=[[0 for x in range(3)] for y in range(leng)]
    if data[j][0]=='r':
        dp[0][rz]=0
        dp[0][sz]=0
        dp[0][pz]=p
    elif data[j][0]=='s':
        dp[0][rz]=r
        dp[0][sz]=0
        dp[0][pz]=0
    elif data[j][0]=='p':
        dp[0][rz]=0
        dp[0][sz]=s
        dp[0][pz]=0

    for i in range(1,leng):
        if data[j][i]=='r':
            dp[i][rz]=dp[i][rz]+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+p+max(dp[i-1][sz],dp[i-1][rz])
        elif data[j][i]=='s':
            dp[i][rz]=dp[i][rz]+r+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+max(dp[i-1][sz],dp[i-1][rz])
        elif data[j][i]=='p':
            dp[i][rz]=dp[i][rz]+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+s+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+max(dp[i-1][sz],dp[i-1][rz])
        else:
            dp[i][rz]=dp[i-1][rz]
            dp[i][sz]=dp[i-1][sz]
            dp[i][pz]=dp[i-1][pz]
    ans+=max(dp[-1][rz],dp[-1][sz],dp[-1][pz])
print(ans)

Submission status: Accepted

Solution code:
n_k = list(map(int, input().split()))
R_S_P = list(map(int, input().split()))
"""
グーで勝った場合、 R点
チョキで勝った場合、 S点
パーで勝った場合、P点

r :グー
s :チョキ
p :パー
"""
n = input()
j = 0
a = ""
res = 0
dasita = []


for i in n:
    if j >= n_k[1]:
        a = dasita[j - n_k[1]]

    if i == "r":  # グーを出された場合
        if a == "p":
            dasita.append("N")
        else:
            res += R_S_P[2]
            dasita.append("p")
    elif i == "s":
        if a == "r":
            dasita.append("N")
        else:
            res += R_S_P[0]
            dasita.append("r")
    elif i == "p":
        if a == "s":
            dasita.append("N")
        else:
            res += R_S_P[1]
            dasita.append("s")
    j += 1
print(res)

Submission status: Accepted

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

point = 0
unPointed = []

for i in range(len(T)):
    ifAdd = True
    if i - K >= 0:
        if T[i] == T[i - K]:
            if unPointed[i - K] == 0:
                ifAdd = False
    if ifAdd is True:
        if T[i] == 'r':
            point += P
        elif T[i] == 's':
            point += R
        elif T[i] == 'p':
            point += S
        unPointed.append(0)
    else:
        unPointed.append(1)

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
for i in range(n):
  if i>=k and t[i]==t[i-k]:
    t[i]=' '
  if t[i]=="r":
    ans+=p
  elif t[i]=="s":
    ans+=r
  elif t[i]=="p":
    ans+=s
print(ans)

Submission status: Accepted

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

def point(k):
    if k=="r": return P
    if k=="s": return R
    if k=="p": return S

p=0

ser=True



for i in range(0,K):
    p+=point(T[i])
    for j in range(i+K,N,K):
        if T[j-K]==T[j]:
            if ser==True:
                ser=False
            else:
                ser=True
                p+=point(T[j])
        else:
            p+=point(T[j])
            ser=True
    ser=True


print(p)

Submission status: Accepted

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


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


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

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
R,S,P=map(int, input().split())
T=input()
score=0
hand = []
for i in range(len(T)):
  if T[i] == 'r':
    hand.append('p')
    if i >= K:
      if hand[i - K] == 'p':
        hand.pop()
        hand.append("z")
  elif T[i] == 's':
    hand.append('r')
    if i >= K:
      if hand[i - K] == 'r':
        hand.pop()
        hand.append("z")
  else:
    hand.append('s')
    if i >= K:
      if hand[i - K] == 's':
        hand.pop()
        hand.append("z")
for i in hand:
  if i == 'r':
    score+=R
  elif i == 's':
    score+=S
  elif i == 'p':
    score+=P
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
cnt = 0
score = {'r': p, 's': r, 'p': s}
for i in range(k):
  a, b = 0, score[t[i]]
  for j in range(i+k,n,k):
    a, b = b, b + score[t[j]] if t[j] != t[j-k] else a + score[t[j]]
  cnt += max(a, b)
print(cnt)

Submission status: Accepted

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

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

n,k = map(int,input().split())

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

t = str(input())

m = n
a = 0
ss = [' '] * m

if m <= k:
    for i in range(m):
        a += g(f(t[i]))
    print(a)
else:
    for i in range(k):
        a += g(f(t[i]))
        ss[i] = f(t[i])
    for i in range(k,m):
        if t[i-k] != t[i]:
            a += g(f(t[i]))
            ss[i] = f(t[i])
        elif (t[i-k] == t[i]) and (ss[i-k] != f(t[i])):
            a += g(f(t[i]))
            ss[i] = f(t[i])
    print(a)

Submission status: Accepted

Solution code:

N,K = map(int,input().split())
r,s,p = map(int,input().split())
T = list(input())

def battle(i,b):
    st=['r','s','p']
    a = st[i]
    if a=='r':
        if b=='p':
            return p
        else:
            return 0
    elif a=='s':
        if b=='r':
            return r
        else:
            return 0
    elif a=='p':
        if b=='s':
            return s
        else:
            return 0
# dp = [[0]*3 for _ in range(N+1)]
# for i in range(K):
#     for j in range(3):
#         dp[i+1][j] = 
#%%
ans = 0
hand = ['n']*N
for i,t in enumerate(T):
    if i<K:
        if t=='r':
            ans += p
            hand[i] = 'p'
        elif t=='s':
            ans += r
            hand[i] = 'r'
        elif t=='p':
            ans += s
            hand[i] = 's'
    else:
        if t=='r' and hand[i-K]!='p':
                ans += p
                hand[i] = 'p'               
        elif t=='s' and hand[i-K]!='r':
            ans += r
            hand[i] = 'r'
        elif t=='p' and hand[i-K]!='s':
            ans += s
            hand[i] = 's'
print(ans)

Submission status: Accepted

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


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


ans = 0
for st in range(K):
    t = T[st::K]

    l = len(t)
    dp = [[0 for _ in range(3)] for _ in range(l+1)]

    for i in range(l):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                dp[i+1][k] = max(dp[i+1][k], dp[i][j] + pon(t[i], k))
    ans += max(dp[i+1])
print(ans)

Submission status: Accepted

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

win = {"r":P,"s":R,"p":S}
tmp = [1]*N
ans = 0

for i in range(N):
    if i<K:
        ans += win[T[i]]
        tmp[i] = 0
    else:
        if T[i]!=T[i-K] or tmp[i-K]:
            ans += win[T[i]]
            tmp[i] = 0

print(ans)

Submission status: Accepted

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


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

    Ts = ["" for _ in range(K)]
    for i, c in enumerate(T):
        Ts[i % K] += c

    ans = 0
    p = {"r": P, "s": R, "p": S}
    for t in Ts:
        c0 = ""
        for c in t:
            if c0 == c:
                c0 = ""
            else:
                ans += p[c]
                c0 = 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())
w=list(input())
l=[]#出す手1r2s3p
ans=0
for i in range(k):
  if w[i]=="r":
    l.append(3)
    ans+=p
  elif w[i]=="s":
    l.append(1)
    ans+=r
  else:
    l.append(2)
    ans+=s
for i in range(k,n):
  if w[i]=="r":
    if l[i-k]==3:
      l.append(0)
    else:
      l.append(3)
      ans+=p
  elif w[i]=="s":
    if l[i-k]==1:
      l.append(0)
    else:
      l.append(1)
      ans+=r
  else:
    if l[i-k]==2:
      l.append(0)
    else:
      l.append(2)
      ans+=s
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    s, p, r = map(int, input().split())
    point_dict = {'s': s, 'p': p, 'r': r}
    t = list(input())

    subs = [[] for _ in range(n)]
    for i in range(n):
        m = i % k
        subs[m].append(t[i])
    point = 0
    for sub in subs:
        i_b = None
        for i in sub:
            if i_b != i:
                point += point_dict[i]
                i_b = i
            else:
                i_b = None
    print(point)
    
resolve()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
p=[int(i) for i in input().split()]
d={"r":2,"s":0,"p":1}
T=[d[i] for i in input()]
flag=[True for _ in range(n)]

sum_=0
for i in range(n):
    if i >=k:
        if T[i] == T[i-k] and flag[i-k] :
            flag[i] = False
        else:
            sum_ += p[T[i]]
            
    else:
        sum_ += p[T[i]]
print(sum_)

Submission status: Accepted

Solution code:
H = list(map(int, input().split()))
A = list(map(int, input().split()))
T = input()
P=A[2]
R=A[0]
S=A[1]
k=H[1]
ans = 0
T = list(T)

for i in range(int(H[0])):
    a=i-k
    b=T[0]
    if(a<0):
        if(T[i]=="r"):
                ans+=P
                #print("p")
        if(T[i]=="s"):
                ans+=R
                #print("r")
        if(T[i]=="p"):
                ans+=S
                #print("s")
    else:
        if(T[i]=="r"):
            if(T[a] !="r"):
                ans+=P
                #print("p")
            else:
                T[i]="l"
                #print("n")

        if(T[i]=="s"):
            if(T[a] !="s"):
                ans+=R
                #print("r")
            else:
                T[i]="l"
                #print("n")
        if(T[i]=="p"):
            if(T[a] !="p"):
                ans+=S
                #print("s")
            else:
                T[i]="l"
                #print("n")
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
dp=[0]*n
for i in range(n):
  if i>=k:
    if t[i-k]=='s' and t[i]=='s':
      if dp[i-k]!='r':
        ans+=r
        dp[i]='r'
      else:
        dp[i]='s'
    elif t[i-k]=='p' and t[i]=='p':
      if dp[i-k]!='s':
        ans+=s
        dp[i]='s'
      else:
        dp[i]='p'
    elif t[i-k]=='r' and t[i]=='r':
      if dp[i-k]!='p':
        ans+=p
        dp[i]='p'
      else:
        dp[i]='r'
    else:
      if t[i]=='r':
        ans+=p
        dp[i]='p'
      elif t[i]=='p':
        ans+=s
        dp[i]='s'
      else:
        ans+=r
        dp[i]='r'
  else:
    if t[i]=='r':
        ans+=p
        dp[i]='p'
    elif t[i]=='p':
        ans+=s
        dp[i]='s'
    else:
        ans+=r
        dp[i]='r'
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'}
a = {'r' : R, 's' : S, 'p' : P}
l = [d[s] for s in T]
cnt = 0
for i, s in enumerate(T):
    if i < K:
        cnt += a[l[i]]
        continue
    if s == 'r' and l[i-K] != 'p':
        cnt += P
    elif s == 'r' and l[i-K] == 'p':
        #cnt += R
        l[i] = 'a'

    if s == 's' and l[i-K] != 'r':
        cnt += R
    elif s == 's' and l[i-K] == 'r':
        #cnt += S
        l[i] = 'a'

    if s == 'p' and l[i-K] != 's':
        cnt += S
    elif s == 'p' and l[i-K] == 's':
        #cnt += P
        l[i] = 'a'
print(cnt)

Submission status: Accepted

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

TE = []
for i in range(K):
    TE.append([])

for i, ti in enumerate(T):
    TE[i%K].append(ti)


def f(te):
    if te == 'r':
        return P
    elif te == 's':
        return R
    elif te == 'p':
        return S
    else:
        return 0

# print(TE)
ans = 0
for te in TE:
    for i in range(len(te)):
        if i == 0:
            continue
        elif te[i] == te[i-1]:
            te[i] = '!'

# print(TE)

ans = 0
for te in TE:
    for ti in te:
        ans += f(ti)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
score = 0
win = ["others" for i in range(len(t))]
for i in range(n):
	if t[i] == "r":
		if win[i - k] != "p":
			win[i] = "p"
		else:
			win[i] = "others"
	elif t[i] == "s":
		if win[i - k] != "r":
			win[i] = "r"
		else:
			win[i] = "others"
	elif t[i] == "p":
		if win[i - k] != "s":
			win[i] = "s"
		else:
			win[i] = "others"
#print(win)
for i in range(n):
	if win[i] == "r": score += r
	elif win[i] == "s": score += s
	elif win[i] == "p": score += p 
	else: continue
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
h = {'s': 'r', 'p': 's', 'r': 'p'}
t = {'r': R, 's': S, 'p': P}

a = [0] * N
ans = 0
for i in range(K):
    a[i] = h[T[i]]
    ans += t[a[i]]

for i in range(K, N):
    if h[T[i]] != a[i - K]:
        a[i] = h[T[i]]
        ans += t[a[i]]
    else:
        if i + K < N:
            a[i] = T[i+K]
        else:
            a[i] = T[i]
print(ans)

Submission status: Accepted

Solution code:

def solve(N,K,R,S,P,T):
    check = [0]*N
    ans = 0
    for i in range(N):
        if i < K or T[i] != T[i-K] or not check[i-K]:
            ans += P if T[i] == 'r' else R if T[i] == 's' else S
            check[i] = 1
    print(ans)

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=[int(s) for s in input().split()]
point=[int(s) for s in input().split()]
point=point+point
kyotai=input()
kyo=[None]*N
for i in range(N):
  if kyotai[i]=='s':
    kyo[i]=0
  elif kyotai[i]=='p':
    kyo[i]=1
  else:
    kyo[i]=2
hand=[] #出した手
score=0 #得点
##K回目までは勝つ
for i in range(K):
  hand.append(kyo[i])
  score+=point[kyo[i]]
##N-K回目までは、勝てるならかつ。勝てないなら次を見る
for i in range(K,N-K):
  if hand[i-K]!=kyo[i]:
    hand.append(kyo[i])
    score+=point[kyo[i]]
  elif hand[i-K]==kyo[i+K]:
    hand.append((kyo[i]+1)%3)
  else:
    hand.append(3-kyo[i+K]-hand[i-K])
##N-K+1回目からは、勝てるならかつ
for i in range(N-K,N):
  if hand[i-K]!=kyo[i]:
    score+=point[kyo[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
hands = []
for i in range(N):
    if i < K:
        # グー
        if T[i] == 'r':
            ans += P
            hands.append('p')
        # チョキ
        elif T[i] == 's':
            ans += R
            hands.append('r')
        # パー
        else:
            ans += S
            hands.append('s')
    else:
        if T[i] == 'r':
            if hands[i-K] != 'p':
                ans += P
                hands.append('p')
            else:
                hands.append('-')
        # チョキ
        elif T[i] == 's':
            if hands[i-K] != 'r':
                ans += R
                hands.append('r')
            else:
                hands.append('-')
        # パー
        else:
            if hands[i-K] != 's':
                ans += S
                hands.append('s')
            else:
                hands.append('-')
print(ans)