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()
score = {"r":P, "s":R, "p":S} 
m = {"r":"p", "s":"r", "p":"s"}
command= []
mm = []
ans = 0


for i, t in enumerate(T):
    if t == "r":
      c = "p"
      point = P
    elif t == "s":
      c ="r" 
      point = R
    elif t == "p":
      c = "s"
      point = S
    
    if (i - K >= 0) and (command[i - K] == c):
        c = ""
        point = 0
    
    ans += point
    command.append(c)
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
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()
L = [0]*N
dic = dict()
dic["r"] = P
dic["s"] = R
dic["p"] = S
dic["0"] = 0
res = 0
for i in range(N):
    if i < N-K and T[i] == T[i+K] and L[i] == 0:
        L[i+K] = 1
    if L[i] == 0:
        res += dic[T[i]]
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = [i for i in input()]
b = [0] * n
for i in range(n - k):
    if t[i] == t[i + k] and b[i] == 0:
        if t[i] == 's':
            b[i + k] -= r
        elif t[i] == 'p':
            b[i + k] -= s
        else:
            b[i + k] -= p
print(t.count('s') * r + t.count('p') * s + t.count('r') * p + sum(b))

Submission status: Accepted

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

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

com = [0]*N

for i in range(N):
    if i>=K and T[i]==T[i-K] and com[i-K]!=0:
        continue
    
    elif T[i] =="r":
        com[i] =P
    elif T[i] =="s":
        com[i] =R
    else:
        com[i] =S

print(sum(com))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
d={'r':p,'s':r,'p':s}
t=input()
ans=0
for i in range(k):
  a=t[i::k]
  flag=a[0]
  cnt=2
  for j in a[1:]:
    if j!=flag:
      ans+=d[flag]*(cnt//2)
      flag=j
      cnt=2
    else:cnt+=1
  ans+=d[flag]*(cnt//2)
print(ans)

Submission status: Accepted

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

kaburi = list(t)
for i in range(n-k):
    if kaburi[i] == kaburi[i+k]:
        kaburi[i+k] = 'x'
result = 0
for i in range(n):
    if kaburi[i] == 'r':
        result += p
    elif kaburi[i] == 's':
        result += r
    elif kaburi[i] == 'p':
        result += s
    else:
        pass


print(result)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
history = []
device2point = {'r' : p, 's': r, 'p': s}
device2hand = {'r': 'p', 's': 'r', 'p': 's'}
point = 0
for i in range(n):
    d = t[i]
    if i - k >= 0 and device2hand[d] == history[i-k]:
        history.append(None)
    else:
        point += device2point[d]
        history.append(device2hand[d])
print (point)

Submission status: Accepted

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

t_list = []

for i in range(k):
  t_list.append(t[i])

for i in range(k,n):
  if t[i] != t_list[i-k] or t_list[i-k] == 'all':
    t_list.append(t[i])
  else:
    t_list.append('all')

print(t_list.count('r') * p + t_list.count('s') * r + t_list.count('p') * s)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    hand = ['r', 's', 'p']
    score = [p, r, s]
    dic_score = dict(zip(hand, score))
    win = [0] * n
    ans = 0
    for i in range(k):
        ans += dic_score[t[i]]
        win[i] = 1
    for i in range(k, n):
        if t[i] == t[i - k] and win[i - k]:
            continue
        ans += dic_score[t[i]]
        win[i] = 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
r,s,p=I()
t = X()
for i in range(n-k):
    if t[i+k]==t[i]:
        t[i+k]=0

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[21]:


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


# In[22]:


points = {}
points["r"] = P
points["s"] = R
points["p"] = S

ans = 0
mylist = []
for i in range(N):
    mylist.append(T[i])
    if i >= K:
        if T[i] == mylist[-K-1]:
            mylist[-1] = "x"
        else:
            ans += points[T[i]]
    else:
        ans += points[T[i]]
print(ans)


# In[ ]:

Submission status: Accepted

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

hand=["" for _ in range(k)]
point=[0 for _ in range(k)]

for i in range(n):
    index=i%k
    #初めの手（制限なし）
    if index==i:
        if t[i]=="r":
            hand[index]="p"
            point[index]+=p
        elif t[i]=="s":
            hand[index]="r"
            point[index]+=r
        else:
            hand[index]="s"
            point[index]+=s
    #２番目以降の手
    else:
        if t[i]=="r":
            if hand[index]=="p":
                hand[index]="x"
            else:
                hand[index]="p"
                point[index]+=p
        elif t[i]=="s":
            if hand[index]=="r":
                hand[index]="x"
            else:
                hand[index]="r"
                point[index]+=r
        else:
            if hand[index]=="s":
                hand[index]="x"
            else:
                hand[index]="s"
                point[index]+=s

print(sum(point))

Submission status: Accepted

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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

X = [[] for _ in range(K)]

for i in range(N):
    X[i % K].append(T[i])

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

score = 0
for k in range(K):
    last = None
    for st in X[k]:
        if st != last:
            score += pt[st]
            last = st
        else:
            last = None

print(score)

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
    
print(T.count('r') * P + T.count('s') * R + T.count('p') * S)

Submission status: Accepted

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

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

ans = 0
for i in range(k): ans += rsp[t[i]]
for i in range(k,n):
  if t[i] != t[i-k]: ans += rsp[t[i]]
  else: t[i] = "a"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

def sc(st):
    if st == "p":  return s
    elif st == "r":  return p
    return r

for i in range(K):
    pre = T[i]
    cnt = i
    ans += sc(T[i])
    while(1):
        cnt += K
        if cnt >= N: break 
        if T[cnt] == pre:  pre = "#"
        else:  
            ans += sc(T[cnt])
            pre = T[cnt]
print(ans)

Submission status: Accepted

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

    res=0
    T_=[]
    for i in range(N):
        if T[i]=="r":
            res+=P
            T_.append("p")
        elif T[i]=="s":
            res+=R
            T_.append("r")
        else:
            res+=S
            T_.append("s")
    for i in range(N):
        if i>=K:
            if T_[i-K]==T_[i]:
                if T_[i]=="r":
                    res-=R
                elif T_[i]=="s":
                    res-=S
                else:
                    res-=P
                T_[i]="#"

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

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

graph = {"r":P,"s":R,"p":S,"*":0}

for i,t in enumerate(T):
  if len(T)-i-K > 0:
    if t == T[i+K]:
      T[i+K] = "*"

ans = 0
for t in T:
  ans += graph[t]
print(ans)

Submission status: Accepted

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

score=0
kekka=[]

def Tensu(Aite):
  if Aite=="r":
    return P
  elif Aite=="s":
    return R
  else:
    return S


for i in range(N):
  if i<K:
    score+=Tensu(T[i])
    kekka.append(1)
  else:
    if T[i]!=T[i-K] or kekka[i-K]==0:
      score+=Tensu(T[i])
      kekka.append(1)
    else:
      score+=0
      kekka.append(0)
      
print(score)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s,p,r=map(int,input().split()) #筐体の手に対する勝利ポイント
t=list(input())
ans=0
for i in range(n):
    if t[i]=="s":
        tmp=s
    elif t[i]=="p":
        tmp=p
    else:
        tmp=r
    if i>=k:
        if t[i]==t[i-k]:
            tmp=0
            t[i]=None
    ans+=tmp
print(ans)

Submission status: Accepted

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

flg = [False]*n
cnt = 0
for i in range(n):
    # print(cnt)
    if i >= k and t[i] == t[i-k] and not flg[i-k]:
        flg[i] = True
        continue
    elif t[i] == 'r':
        cnt += p
    elif t[i] == 's':
        cnt += r
    elif t[i] == 'p':
        cnt += s
print(cnt)

Submission status: Accepted

Solution code:
from itertools import groupby
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    ans = 0
    for i in range(k):
        x = i
        li = []
        while x < n:
            li.append(t[x])
            x += k
        for key, value in groupby(li):
            if key=='r':
                a=p
            elif key=='s':
                a=r
            else:
                a=s

            ans += ((len(list(value))+1)//2)*a
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r, s, p = map(int,input().split())
r_s_p_points = [r, s, p]
t = input()
my_hands = []
points = 0
r_s_p = [0, 1, 2]
t_num_list = []
for i in range(n):
    if t[i] == "r":
        t_num_list.append(0)
    elif t[i] == "s":
        t_num_list.append(1)
    else:
        t_num_list.append(2)

for i in range(k):
    if t_num_list[i] == 0:
        my_hands.append(2)
        points += p
    elif t_num_list[i] == 1:
        my_hands.append(0)
        points += r
    else:
        my_hands.append(1)
        points += s

for i in range(k, n):
    if r_s_p[(t_num_list[i] - 1) % 3] != my_hands[i - k]:
        my_hands.append(r_s_p[(t_num_list[i] - 1) % 3])
        points += r_s_p_points[r_s_p[(t_num_list[i] - 1) % 3]]
    else:
        if i + k <= n-1:
            for j in range(3):
                if j == r_s_p[(t_num_list[i] - 1) % 3]:
                    continue
                elif j == r_s_p[(t_num_list[i + k] - 1) % 3]:
                    continue
                else:
                    my_hands.append(j)
                    break
        else:
            my_hands.append(0)

print(points)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().rstrip()
d1 = {'r': ('p', p), 's': ('r', r), 'p': ('s', s)}
d2 = {'r': {'p': 'r', 's': 's', 'r': 's'},
      's': {'r': 's', 'p': 'p', 's': 's'},
      'p': {'r': 'r', 's': 'p', 'p': 'r'}}
ret = 0
me = []
for i, rival in enumerate(t):
    if i - k < 0 or me[i - k] != d1[rival][0]:
        me.append(d1[rival][0])
        ret += d1[rival][1]
    elif i - k >= 0 and me[i - k] == d1[rival][0] and i + k < n:
        me.append(d2[rival][t[i + k]])
print(ret)

Submission status: Accepted

Solution code:
n,k = list(map(int,input().split()))
point = list(map(int,input().split()))
rsp = ['r', 's', 'p']
t = input()
m = [rsp[rsp.index(i)-1] for i in t]
ans = 0
for i in range(n):
    if i<k :
        ans += point[rsp.index(m[i])]
    else:
        if m[i]!=m[i-k]:
            ans += point[rsp.index(m[i])]
        else:
            try:
                m[i] = rsp[rsp.index(m[i+k])-1]
            except:
                pass
    # print(ans)
print(ans)

Submission status: Accepted

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

win = {"r":P,"s":R,"p":S,"x":0}

ans = 0
for i in range(len(T)):
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += win[T[i]]

print(ans)

Submission status: Accepted

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

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

ans = 0
for i in range(K, len(T)):
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += win[T[i]]

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
D={"r":P,"s":R,"p":S}
T = input().strip()
A = {i:[] for i in range(K)}
for i in range(N):
    A[i%K].append(T[i])
cnt = 0
for k in range(K):
    if len(A[k])>0:
        cnt += D[A[k][0]]
        for i in range(1,len(A[k])):
            if A[k][i]==A[k][i-1]:
                A[k][i] = 0
            elif A[k][i]!=A[k][i-1]:
                cnt += D[A[k][i]]
print(cnt)

Submission status: Accepted

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

ans = 0
temp = [0]*n
for i in range(n):
    if i <= k-1:
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        else:
            ans += s
    else:
        if t[i-k] == t[i]:
            if temp[i-k] == 0:
                temp[i] = -1
            else:
                if t[i] == 'r':
                    ans += p
                elif t[i] == 's':
                    ans += r
                else:
                    ans += s
                temp[i] = 0
        else:
            if t[i] == 'r':
                ans += p
            elif t[i] == 's':
                ans += r
            else:
                ans += s
            temp[i] = 0

    #print(i, ans)
print(ans)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,k = readInts()
r,s,p = readInts()
T = list(input())
ans = 0
for i in range(n):
    if k <= i:
        if T[i] == T[i-k]:
            T[i] = '0'
    if T[i] == 's':
        ans += r
    if T[i] == 'p':
        ans += s
    if T[i] == 'r':
        ans += p
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
point = {"s":r, "p":s, "r":p}
x = {"s":"r", "p":"s", "r":"p"}
ans = []
P = 0
for i in t:
    if len(ans) < k or ans[-k] != x[i]:
        ans.append(x[i])
        P += point[i]
    else: ans.append("")
print(P)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ret = 0
for i in range(k):
    last = 'x'
    for c in t[i::k]:
        if last != c:
            ret += p if c == 'r' else r if c == 's' else s
            last = c
        else:
            last = 'x'
print(ret)

Submission status: Accepted

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


def win(t):
    if t == 'r':
        return P
    elif t == 's':
        return R
    else:
        return S


def janken(i):
    if i >= K:
        if T[i] != T[i - K] or own_win[i - K] is False:
            own_win[i] = True
            return win(T[i])
        else:
            return 0
    else:
        own_win[i] = True
        return win(T[i])


cnt = 0
for i in range(N):
    cnt += janken(i)

print(cnt)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
r, s, p = list(map(int, input().split(' ')))
d = dict(r=p,
         s=r,
         p=s)
ttt = input()
ans = 0
prevs = []
for i in range(n):
    if i < k or ttt[i] != prevs[i-k]:
        ans += d[ttt[i]]
        prevs.append(ttt[i])
    else:
        prevs.append('hoge')
print(ans)

Submission status: Accepted

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

#i回目、jの手を出した時の点数の最大値
dp = [[0]*3 for _ in range(N)]

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

Submission status: Accepted

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


def main():
  N, K = list(map(int, input().split()))
  R, S, P = list(map(int, input().split()))
  T = input()[:-1]

  mem = []
  hand_map = {'r': 'p', 's': 'r', 'p': 's'}
  score_map = {'r': P, 's': R, 'p': S}

  score = 0

  for i in range(K):
    mem.append(hand_map[T[i]])
    score += score_map[T[i]]

  for i in range(K, N):
    if not mem[i-K] == hand_map[T[i]]:
      mem.append(hand_map[T[i]])
      score += score_map[T[i]]
      continue
    cand = ['r', 's', 'p']
    # print(mem[i-K])
    cand.remove(mem[i-K])
    if i+K < N and not(mem[i-K] == hand_map[T[i+K]]):
      cand.remove(hand_map[T[i+K]])
    mem.append(cand[0])

  print(score)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
'''
rsp -> 012
'''
dp = [[0] * 3 for i in range(N)]
for i in range(0, K):
    for j in range(i, N, K):
        if j == i:
            if T[j] == 'r':
                dp[j][0] += 0
                dp[j][1] += 0
                dp[j][2] += P
            if T[j] == 's':
                dp[j][0] += R
                dp[j][1] += 0
                dp[j][2] += 0
            if T[j] == 'p':
                dp[j][0] += 0
                dp[j][1] += S
                dp[j][2] += 0
        else:
            dp[j][0] = max(dp[j - K][1], dp[j - K][2])
            dp[j][1] = max(dp[j - K][0], dp[j - K][2])
            dp[j][2] = max(dp[j - K][0], dp[j - K][1])
            if T[j] == 'r':
                dp[j][0] += 0
                dp[j][1] += 0
                dp[j][2] += P
            if T[j] == 's':
                dp[j][0] += R
                dp[j][1] += 0
                dp[j][2] += 0
            if T[j] == 'p':
                dp[j][0] += 0
                dp[j][1] += S
                dp[j][2] += 0

#print(dp)
ans = 0
for i in range(N-K,N):
    ans += max(dp[i])
print(ans)

Submission status: Accepted

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

point = 0
flg=[0]*n
for i in range(n):
  if t[i] == 'r' and flg[i] == 0:
    point += p
  elif t[i] == 's' and flg[i] == 0:
    point += r
  elif t[i] == 'p' and flg[i] == 0:
    point += s

  if i+k < n and t[i+k] == t[i]:
    if t[i] == 'r' and flg[i] == 0:
      flg[i+k] = 'r'
    elif t[i] == 's' and flg[i] == 0:
      flg[i+k] = 's'
    elif t[i] =='p' and flg[i] == 0:
      flg[i+k] = 'p'
print(point)

Submission status: Accepted

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

# 相手に勝つ時に得られる点数
def rsp(hand):  # hand : 相手の手
    if hand == 'r':
        ret = P
    elif hand == 's':
        ret = R
    else:
        ret = S
    return ret

ans = 0
for i in range(K):
    Ti = T[i : : K]
    cnt = 0
    for j in range(len(Ti)):
        if j == 0 or Ti[j] == Ti[j - 1]:
            cnt += 1
        else:
            ans += rsp(Ti[j - 1]) * ((cnt + 1) // 2)
            cnt = 1
    ans += rsp(Ti[-1]) * ((cnt + 1) // 2)

print(ans)

Submission status: Accepted

Solution code:
"""
入力例1

5 2
8 7 6
rsrpr

このとき、r1回目から3回目に対して勝敗が
oxo
となるようにするとよさそう。
"""
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 and T[i] == T[i - K] and memo[i - K]:
        memo.append(False)
        continue
    if T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
    memo.append(True)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
R,S,P = map(int,input().split())
points = {'r':P,'s':R,'p':S}
l = list(input())
com = ['']*n
ans = 0
for i,j in enumerate(l):
  p = points[j]
  com[i] = j
  if (i-k>=0) and com[i] == com[i-k]:
    com[i] = ''
    p = 0
  ans += p
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
rsp = [set() for i in range(3)]
ans = 0
for i in range(N):
    if T[i] == 'r' and i - K not in rsp[0]:
        ans += P
        rsp[0].add(i)
    elif T[i] == 's' and i - K not in rsp[1]:
        ans += R
        rsp[1].add(i)
    elif T[i] == 'p' and i - K not in rsp[2]:
        ans += S
        rsp[2].add(i)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T1=input()
T2=T1.replace('r', str(P)+' ').replace('s',str(R)+' ').replace('p',str(S)+' ')[:-1]
T2=list(map(int, T2.split()))
for i in range(N):
  if i >=K and T1[i]==T1[i-K] and T2[i-K] != 0:T2[i]=0
print(sum(T2))

Submission status: Accepted

Solution code:
def solve(T, R, S, P, K):
    N = len(T)
    Z =[None for i in range(N)]
    for i in range(N):
        if T[i] == 'r':
            Z[i] = P
        elif T[i] == 's':
            Z[i] = R
        else:
            Z[i] = S
        if i - K >= 0:
            if T[i-K] == T[i] and Z[i-K] != 0:
                Z[i] = 0
    return sum(Z)
N, K = map(int,input().split(' '))
R, S, P = map(int,input().split(' '))
T = str(input())
print(solve(T, R, S, P, K))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, k = [int(x) for x in input().split()]
    R, S, P = [int(x) for x in input().split()]
    s = input()
    ans = 0
    b = []
    for i in range(n):
        e = s[i]
        if i < k or s[i] != s[i-k] or s[i-k] == b[i-k]:
            if e == 'r':
                ans += P
                b.append('p')
            elif e == 'p':
                ans += S
                b.append('s')
            elif e == 's':
                ans += R
                b.append('r')
        else:
            b.append(e)
    print(ans)
    
    

        
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

R s
S p
P r

"""

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = readline().decode().rstrip()

t = list(t)

for i in range(k, n):
    if t[i] == t[i - k]:
        t[i] = 0

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

Submission status: Accepted

Solution code:
import numpy as np

N,K=map(int,input().split())
R,S,P=map(int,input().split())
T1=input()
T2=T1.replace('r', str(P)+' ').replace('s',str(R)+' ').replace('p',str(S)+' ')[:-1]
T2=np.array(list(map(int, T2.split())))

for i in range(N):
  if i >=K and T1[i]==T1[i-K] and T2[i-K] != 0:
    T2[i]=0

print(T2.sum())

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = readline().decode().rstrip()
t = list(t)
for i in range(k, n):
    if t[i] == t[i - k]:
        t[i] = 0

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
dp = []
order = input()
def jcount(x):
  if x=='r':
    ans = P
  elif x=='s':
    ans = R
  else:
    ans = S
  return ans
counter = 0
for i in range(N):
  if i < K:
    counter += jcount(order[i])
    dp.append(order[i])
  elif order[i] != dp[i-K]:
    counter += jcount(order[i])
    dp.append(order[i])
  else:
    dp.append('x')
print(counter)

Submission status: Accepted

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

#%%
win = []
for t in T:
    if t == 'r':
        win.append('p')
    elif t == 'p':
        win.append('s')
    elif t == 's':
        win.append('r')


def score(rps):
    if rps == 'r':
        return r
    elif rps == 'p':
        return p
    elif rps == 's':
        return s

ans = 0
for i in range(n):
    if i - k < 0:
       ans += score(win[i])
    else:
        if win[i] == win[i-k]:
            win[i] = None
        else:
            ans += score(win[i])

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(k,n):
    if t[i] == t[i-k]:
        t[i] = "0"

ans = [0] * n

for i in range(n):
    if t[i] == "r":
        ans[i] = p
    elif t[i] == "s":
        ans[i] = r
    elif t[i] == "p":
        ans[i] = s

print(sum(ans))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
dp = [[0]*n for _ in range(3)]
for i in range(n):
    if n < k:
        if t[i] == 'r':
            dp[2][i] = p
        elif t[i] == 's':
            dp[0][i] = r
        else:
            dp[1][i] = s
    else:
        if t[i] == 'r':
            dp[2][i] = max(dp[0][i-k], dp[1][i-k]) + p
            dp[0][i] = max(dp[1][i-k], dp[2][i-k])
            dp[1][i] = max(dp[0][i - k], dp[2][i - k])
        elif t[i] == 's':
            dp[0][i] = max(dp[1][i - k], dp[2][i - k]) + r
            dp[1][i] = max(dp[0][i - k], dp[2][i - k])
            dp[2][i] = max(dp[0][i - k], dp[1][i - k])
        else:
            dp[1][i] = max(dp[0][i - k], dp[2][i - k]) + s
            dp[0][i] = max(dp[1][i - k], dp[2][i - k])
            dp[2][i] = max(dp[0][i - k], dp[1][i - k])
res = 0
for i in reversed(range(n-k,n)):
    res += max(dp[0][i],dp[1][i],dp[2][i])
print(res)

Submission status: Accepted

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

U = []
for t in T:
    if t == 'r':
        u = 'p'
    elif t == 's':
        u = 'r'
    else:
        u = 's'
    U.append(u)

ans = 0
for i in range(N):
    if i >= K and U[i] == U[i - K]:
        U[i] = ''
    else:
        if U[i] == 'r':
            ans += R
        elif U[i] == 's':
            ans += S
        elif U[i] == 'p':
            ans += P

print(ans)