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())
for i in range(len(T)):
    if T[i] == "r":
        T[i] = "p"
    elif T[i] == "s":
        T[i] = "r"
    else:
        T[i] = "s"
for i in range(k): #kでわった余り0~(k-1)
    j = i+k
    while j <= n-1: #j:何文字目
        if T[j-k] == T[j]:
            T[j] = "n"
        j = j+k
points = 0
for t in T:
    if t == "r":
        points += r
    elif t == "s":
        points += s
    elif t == "p":
        points += p
print(points)

Submission status: Accepted

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

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import copy
N,K=map(int,input().split())
R,S,P=map(int,input().split())
point=score={'r':R,'s':S,'p':P}
T=input()
l=[[]for i in range(K)]
for i in range(N):
  l[i%K].append(T[i])
def get_point(s1,s2):
    num={'r':0,'s':1,'p':2}
    if num[s1]==(num[s2]+1)%3:
        return point[s2]
    else:return 0
def rest(hand):
    if hand=='r':return['s','p']
    elif hand=='s':return['r','p']
    elif hand=='p':return['r','s']
def cal(mini_l):
    size=len(mini_l)
    kind=['r','s','p']
    score={'r':[],'s':[],'p':[]}
    for hand in kind:
        score[hand].append(get_point(mini_l[0],hand))
    for i in range(1,size):
        for hand in kind:
            choice=''
            tmp_score=-1
            for pre_hand in rest(hand):
                if score[pre_hand][i-1]+get_point(mini_l[i],hand)>tmp_score:
                    choice=pre_hand
                    tmp_score=score[pre_hand][i-1]+get_point(mini_l[i],hand)
            score[hand].append(tmp_score)
    tmp_score=-1
    for hand in kind:
        if score[hand][-1]>tmp_score:
            ans=hand
            tmp_score=score[hand][-1]
    return tmp_score
ans=0
for ll in l:
    ans+=cal(ll)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
def janken(a):
    global ans
    if "r" == a:
        return p
    if "s" == a:
        return r
    if "p" == a:
        return s

for i in range(k):
    ti = t[i::k]
    odd = []
    even = []
    before = ""
    for j in range(len(ti)):
        if ti[j] == before:
            if j % 2 == 0:
                even.append(ti[j])
            else:
                odd.append(ti[j])
        else:
            if j != 0:
                ans += janken(before)*max(len(odd),len(even))
                even = []
                odd = []
            if j % 2 == 0:
                even.append(ti[j])
            else:
                odd.append(ti[j])
        if j == len(ti)-1:
            ans += janken(ti[j])*max(len(odd),len(even))
        before = ti[j]

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)


input = sys.stdin.readline


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = list(input())
    M = {'r': p, 's': r, 'p': s}

    ans = 0
    win = [False]*n
    for i in range(n):
        if win[i-k] and t[i-k] == t[i]:
            continue
        ans += M[t[i]]
        win[i] = True

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K=[int(i) for i in input().split()]
R,S,P=[int(i) for i in input().split()]
T=list(input())
M={"r":P,"s":R,"p":S}

done=[]
point=0
for i in range(K):
    temp=T[i]
    point+=M[temp]
    done.append(temp)

# print(T)
# print(done)

for j in range(K,N):
    temp=T[j]
    back=done[j-K]
    # print(temp,back)
    if temp!=back:
        point+=M[temp]
        done.append(temp)
    else:
        done.append('u')

print(point)

Submission status: Accepted

Solution code:
def ABC149D():
	N, K = map(int,input().split())
	RSP = list(map(int,input().split()))
	T = [0] + list(input())
	hand_dic ={'r' : 0, 's' : 1, 'p' : 2}
	ans=0
	mod_l = [ [] for _ in range(K)]
	for i in range(1,N+1):
		mod_l[i%K].append(T[i])
	for a in range(K):
		dp = [ [0 for i in range(3)] for j in range(len(mod_l[a])+5)]
		for b in range(len(mod_l[a])):
			for c in range(3):
				if hand_dic[mod_l[a][b]] == c:
					dp[b+1][c] = max(dp[b][(c+1)%3], dp[b][(c+2)%3])
					dp[b+1][(c+1)%3] = max(dp[b][c], dp[b][(c+2)%3])
					dp[b+1][(c+2)%3] = max(dp[b][c] + RSP[(c+2)%3], dp[b][(c+1)%3] + RSP[(c+2)%3])
		ans += max(dp[len(mod_l[a])])

	print(ans)

if __name__ == '__main__':
	ABC149D()

Submission status: Accepted

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

cnt={'r':0,'s':0,'p':0}
for i in range(n):
  if i<=k-1:
    cnt[t[i]]+=1
  elif t[i]==t[i-k]:
    t[i]='o'
    continue
  else:
    cnt[t[i]]+=1
ans=p*cnt['r']+r*cnt['s']+s*cnt['p']
print(ans)

Submission status: Accepted

Solution code:
def getChoice(pt):
    return 'rs' if pt == 'p' else 'rp' if  pt == 's' else 'sp'
n, k = map(int, input().split(' '))
r, s, p = map(int, input().split(' '))
t = input()
rsp = {'r': ('p', p), 's': ('r', r), 'p': ('s', s)}
total = 0
co = 0
last = []
for s in t:
    pt, score = rsp[s]
    l = getChoice(pt)
    if co - k >= 0:
        if pt not in last[co - k]:
            score = 0
            if co + k < n:
                fp, fs = rsp[t[co + k]]
                ca = l[0] if fp != l[0] else l[1]
                l = getChoice(ca)
          
    last.append(l)
    total += score
    co += 1
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import numpy as np
from functools import lru_cache
import time

def main(): 
    if True:
        N,K = list(map(int, input().split()))
        R,S,P = list(map(int, input().split()))
        T = input()
        
    else:
        A = 6
        for i in range(0,A,4):
            print(i)
    rsp = {}
    rsp["s"] = R
    rsp["p"] = S
    rsp["r"] = P
    rsp["n"] = 0
    
    NN = N if N % K == 0 else (N // K + 1) * K
    score = 0
    T = np.asarray(list(T + "n"*(NN-N))).reshape((NN//K,K))
    for k in range(K):
        tmp = ""
        t = T[:,k]
        #print(t)
        for tt in t:
            if tmp == tt:
                tmp = ""
                #print(k,tt)
            else:
                tmp = tt
                score += rsp[tt]
    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=str(input())
V=[]
W=[]
A=[]
x=0
for i in T:
  if i=="r":
    V.append("p")
  elif i=="s":
    V.append("r")
  else:
    V.append("s")
A=V[:]
for j in range(K):
  l=j
  while l<len(V):
    W.append(V[l])
    l=l+K
  #print(W)
  for i in range(1,len(W)):
    if W[i]==W[i-1]:
      if W[i]=="r":
        if i<len(W)-1 and W[i+1]=="s":
          W[i]="p"
        else:
          W[i]="s"
      elif W[i]=="s":
        if i<len(W)-1 and W[i+1]=="p":
          W[i]="r"
        else:
          W[i]="p"
      elif W[i]=="p":
        if i<len(W)-1 and W[i+1]=="r":
          W[i]="s"
        else:
          W[i]="r"
  #print(W)
  for i in range(len(W)):
    V[i*K+j]=W[i]
  W.clear()
"""print(V)
print(W)
print(A)"""
for i in range(len(V)):
  if V[i]==A[i]=="r":
    x=x+R
  elif V[i]==A[i]=="s":
    x=x+S
  elif V[i]==A[i]=="p":
    x=x+P
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict as DD
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

D = DD(int)
D["s"] = R
D["p"] = S
D["r"] = P

ans = 0
C = [0 for i in range(N)]
for i in range(N):
  if i < K:
    ans +=  D[T[i]]
    C[i] = 1    
  elif T[i-K] == T[i]:
    if C[i-K] != 1:
      ans +=  D[T[i]]
      C[i] = 1
  else:
      ans +=  D[T[i]]
      C[i] = 1    
print (ans)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

N,K = LI()
R,S,P = LI()
T = list(input())
hand = []

def main():
    ans = 0
    for i in range(N):
        if T[i] == 's':
            if i<K or hand[i-K]!='r':
                ans += R
                hand.append('r')
            else:
                hand.append('x')
        if T[i] == 'p':
            if i<K or hand[i-K] != 's':
                ans += S
                hand.append('s')
            else:
                hand.append('x')
        if T[i] == 'r':
            if i<K or hand[i-K] != 'p':
                ans += P
                hand.append('p')
            else:
                hand.append('x')
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

# mod KでK個のグループに分ける
T2 = [[] for i in range(K)]
for i, t in enumerate(T):
    T2[i%K].append(t)

ans = 0
for k in range(K):
    M = len(T2[k])
    # dp[i][j] := i回目まで見て、前回出した手がr,s,p(j=0,1,2)だった時の最大スコア
    dp = list2d(M+1, 3, 0)
    for i, t in enumerate(T2[k]):
        # 前回出した手j
        for j in range(3):
            # 今回出す手l
            for l in range(3):
                # 初回以外で、前回と同じ手なら遷移不可
                if i != 0 and j == l:
                    continue
                nxt = 0
                # 相手がrならp(2)の時に得点
                if t == 'r' and l == 2:
                    nxt = P
                # 相手がsならr(0)の時に得点
                elif t == 's' and l == 0:
                    nxt = R
                # 相手がpならs(1)の時に得点
                elif t == 'p' and l == 1:
                    nxt = S
                dp[i+1][l] = max(dp[i+1][l], dp[i][j] + nxt)
    # このグループでのスコアを合計に追加
    ans += max(dp[M])
print(ans)

Submission status: Accepted

Solution code:
def main():
 
  n,k=map(int,input().split())
  r,s,p=map(int,input().split())
  t=list(input())
 
  point=dict()
  point["p"]=s  #s wins p
  point["s"]=r  #r wins s
  point["r"]=p  #p wins r
  point["checked"]=0
 
  ans=0
  for i in range(n):
    tmp=i-k
    if tmp>=0 and t[tmp]==t[i]:
      ans+=0
      t[i]="checked"
    else:
      ans+=point[t[i]]
  print(ans)
 
main()

Submission status: Accepted

Solution code:
def DP(s):
    N = len(s)
    dp = [[0] * (3) for i in range(N+1)]
    rsp = {'s':0, 'p':1, 'r':2}
    for i in range(N):
        for j in range(3):
            for k in range(3):
                win = int(rsp[s[i]] == k) * RSP[k]
                if j != k: dp[i+1][k] = max(dp[i+1][k], dp[i][j] + win)
    return max(dp[N])

N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

ans = 0
for i in range(K): ans += DP(T[i:N:K])
print(ans)

Submission status: Accepted

Solution code:
def DP(s):
    N = len(s)
    dp = [[0] * (3) for i in range(N+1)]
    rsp = {'s':0, 'p':1, 'r':2}
    for i in range(N):
        for j in range(3):
            for k in range(3):
                win = int(rsp[s[i]] == k) * RSP[k]
                if j != k: dp[i+1][k] = max(dp[i+1][k], dp[i][j] + win)
    return max(dp[N])

N, K = map(int, input().split())
RSP = list(map(int, input().split()))
T = input()

ans = 0
for i in range(K):
    t = ''
    for j in range(i, N, K): t += T[j]
    ans += DP(t)
print(ans)

Submission status: Accepted

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

if __name__=='__main__':
  n, k = map(int, input().strip().split())
  r, s, p = map(int, input().strip().split())
  score = {'r':p, 's':r, 'p':s}
  t = list(input().strip())
  count = {'r':0, 's':0, 'p':0}
  for i in range(n-k):
    if t[i] == t[i+k]:
      t[i+k] = '*'
      count[t[i]] += 1
  ans = 0
  for x in t:
    if x!='*':
      ans += score[x]
  print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=str(input())
T=[z for z in T]
c=0
for i in range(K):
  M=[]
  t=0
  j=i
  while j <= N-1:
    M.append(T[j])
    j=j+K
  k=0
  while k <= len(M)-1:
    if M[k]=='r':
      t=t+P
    elif M[k]=='p':
      t=t+S
    else:
      t=t+R
    if k<len(M)-1 and M[k]==M[k+1]:
      k=k+2
    else:
      k=k+1
  c=c+t
print(c)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque


   
def resolve():
    
    input = sys.stdin.readline
    N,K=map(int,input().rstrip().split())
    R,S,P=map(int,input().rstrip().split())
    T=list(input().rstrip())

    ans=0
    flag=[0 for _ in range(N)]
    for i in range(N):
        if T[i]==T[i-K] and i>=K and flag[i-K]==True: #足さない条件、K個前が既に制約で足せなくなっていた時は、今回は足せるようになるのでflag管理
            continue
        if T[i]=="r":
            ans+=P
        if T[i]=="s":
            ans+=R
        if T[i]=="p":
            ans+=S
        flag[i]=True
    print(ans)

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5 2
8 7 6
rsrpr"""
        output = """27"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """7 1
100 10 1
ssssppr"""
        output = """211"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr"""
        output = """4996"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

dp = [None for _ in range(n)]

map_ = {
        's': 'r',
        'r': 'p',
        'p': 's'
    }
for play in range(n):
    c_play = t[play] 
    dp[play] = map_.get(c_play)

for play in range(k, n):
    if dp[play] == dp[play-k]:
        dp[play] = None

ans = 0
map_2 = {
    None: 0,
    'r': r,
    's': s,
    'p': p        
}
for play in dp:
    ans += map_2.get(play)

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    T_ = [True for _ in range(N)]

    def play(com, player):
        if com == 'r' and player == 'p':
            return P
        elif com == 's' and player == 'r':
            return R
        elif com == 'p' and player == 's':
            return S
        else:
            return 0

    score = 0
    for n in range(N):
        if n < K:
            score += max(play(T[n], 'r'), play(T[n], 's'), play(T[n], 'p'))
        else:
            if T[n - K] == T[n] and T_[n - K]:
                score += 0
                T_[n] = False
            else:
                score += max(play(T[n], 'r'), play(T[n], 's'), play(T[n], 'p'))

    print(score)


main()

Submission status: Accepted

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

Submission status: Accepted

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

ans=0

for i in range(k):
  if T[i]=="r":ans+=P
  elif T[i]=="s":ans+=R
  else:ans+=S

for i in range(k,n):
  if T[i]==T[i-k]:T[i]="a"
  else:
    if T[i]=="r":ans+=P
    elif T[i]=="s":ans+=R
    else:ans+=S

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
array = [""]*k
for i in range(n):
    array[i % k]+=t[i]
a = 0

for l in array:
    l = l.replace("rr", "r0").replace("ss", "s0").replace("pp", "p0")
    a += p * l.count("r") + r * l.count("s") + s * l.count("p")
print(a)

Submission status: Accepted

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

Ts = [[] for _ in range(K)]
for i, x in enumerate(T):
    k = i % K
    Ts[k].append(x)


def solve(t):
    r, s, p = 0, 0, 0   # player の現時点の手ごとの最大得点

    for x in t[::-1]:   # 相手の手を逆順にして、最終手から探索
        if x == 'r':
            r, s, p = max(s, p), max(p, r), max(r, s) + P
            # 現時点で
            # 現時点で r を出す場合、次時点で s または p を出す。現時点では引き分けのため、無加点
            # 現時点で s を出す場合、次時点で p または r を出す。現時点では負けのため、無加点
            # 現時点で p を出す場合、次時点で r または s を出す。現時点では勝ちのため、P 加点
        elif x == 's':
            r, s, p = max(s, p) + R, max(p, r), max(r, s)
        elif x == 'p':
            r, s, p = max(s, p), max(p, r) + S, max(r, s)

    return max(r, s, p)


answer = sum(solve(t) for t in Ts)
print(answer)

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
hand=input()
ans=0
for i in range(k):
  free=True
  j=i
  while j<n:
    if free==True or hand[j-k]!=hand[j]:
      if hand[j]=='r':
        ans+=p
      elif hand[j]=='s':
        ans+=r
      else:
        ans+=s
      free=False
    else:
      free=True
    j+=k
print(ans)

Submission status: Accepted

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

dp_mod_k = {}

score = 0
for i in range(n):
    if i % k in dp_mod_k:
        dp_r, dp_s, dp_p = dp_mod_k[i % k]
    else:
        dp_r, dp_s, dp_p = 0, 0, 0
    opponent_hand = t[i]
    if opponent_hand == "r":
        new_dp_r = max(dp_s, dp_p)
        new_dp_s = max(dp_r, dp_p)
        new_dp_p = max(dp_r, dp_s) + p
    elif opponent_hand == "s":
        new_dp_r = max(dp_s, dp_p) + r
        new_dp_s = max(dp_r, dp_p)
        new_dp_p = max(dp_r, dp_s)
    else:
        new_dp_r = max(dp_s, dp_p)
        new_dp_s = max(dp_r, dp_p) + s
        new_dp_p = max(dp_r, dp_s)
    dp_mod_k[i % k] = [new_dp_r, new_dp_s, new_dp_p]
    if i + k >= n:
        score += max(new_dp_r, new_dp_s, new_dp_p)

print(score)

Submission status: Accepted

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

ans = []
for i in range(n):
    c = t[i]
    if i - k >= 0 and t[i-k] == c and ans[i-k]:
        ans.append(0)
    else:
        ans.append(win[c])
print(sum(ans))

Submission status: Accepted

Solution code:

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

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

score = 0

for i in range(N):

    tmp = i - K
    if 0 <= tmp and T[i] == T[tmp]:
        score += 0
        T[i]=""

    else:
        score+= point[T[i]]
print(score)

Submission status: Accepted

Solution code:
*n,t=open(0).read().split()
n,k,r,s,p=map(int,n)
*t,=t
for i in range(n-k):
  if t[i+k]==t[i]:t[i+k]=5
a,b,c=map(t.count,'rsp')
print(a*p+b*r+c*s)

Submission status: Accepted

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

win = {'r' : 'p', 's': 'r', 'p' : 's'}
point = {'r' : R, 's' : S, 'p' : P}
rsp = ['r', 's', 'p']
def calc_score(substr):
#    actions = []
    my_prev = win[substr[0]]
    score = point[my_prev]
    for idx in range(1, len(substr) - 1):
        my_now = win[substr[idx]]
        if my_now != my_prev:
            score += point[my_now]
        else:
            my_next = win[substr[idx + 1]]
            for elem in rsp:
                if elem != my_prev and elem != my_next:
                    my_now = elem # lose with elem with no score
                    break
#        actions.append(my_now)
        my_prev = my_now
#    print(actions)
    my_now = win[substr[-1]]
    if my_now != my_prev:
        score += point[my_now]
    return score

all_score = 0
for offset in range(K):
    all_score += calc_score(T[offset::K])

print(all_score)

Submission status: Accepted

Solution code:
#N = int(input())
#L = list(map(int, input().split()))
import math
import sys

NK = list(map(int, input().split()))
RSP = list(map(int, input().split()))
T = input()
ansrr =[0]*NK[0]
for i in range(NK[1]):
    if T[i]=="r":
        ansrr[i] += RSP[2]
    elif T[i] == "p":
        ansrr[i] += RSP[1]
    else:
        ansrr[i] += RSP[0]

for i in range(NK[1],NK[0]):
    if T[i] == T[i-NK[1]]:
        if ansrr[i-NK[1]] == 0:
            if T[i] == "r":
                ansrr[i] += RSP[2]
            elif T[i] == "p":
                ansrr[i] += RSP[1]
            else:
                ansrr[i] += RSP[0]
        else:
            ansrr[i]=0
    else:
        if T[i] == "r":
            ansrr[i] += RSP[2]
        elif T[i] == "p":
            ansrr[i] += RSP[1]
        else:
            ansrr[i] += RSP[0]
#a = list(map(int, input().split()))
ans = 0
for i in range(NK[0]):
    ans += ansrr[i]
print(ans)

Submission status: Accepted

Solution code:
def main():

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

  point=dict()
  point["p"]=s  #s wins p
  point["s"]=r  #r wins s
  point["r"]=p  #p wins r
  point["checked"]=0

  ans=0
  for i in range(n):
    tmp=i-k
    if tmp>=0 and t[tmp]==t[i]:
      ans+=0
      t[i]="checked"
    else:
      ans+=point[t[i]]
  print(ans)

main()

Submission status: Accepted

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

point=dict()
point["p"]=s  #s wins p
point["s"]=r  #r wins s
point["r"]=p  #p wins r
point["checked"]=0

ans=0
for i in range(n):
  tmp=i-k
  if tmp>=0 and t[tmp]==t[i]:
    ans+=0
    t[i]="checked"
  else:
    ans+=point[t[i]]
print(ans)

Submission status: Accepted

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

subGames = [[] for _ in range(k)]

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

subPts = [0 for _ in range(k)]
for i, hand in enumerate(t):
    subGame = subGames[i % k]
    winPt = pattern[hand]
    winHand = win[hand]
    if subGame and win[hand] == subGame[-1]:
        winPt = 0
        winHand = t[i+k] if i+k < n else hand

    subGame.append(winHand)
    subPts[i % k] += winPt

ans = sum(subPts)

print(ans)

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}
t = list(input())
tf_list = [0] * n
ans = 0

u = [[] for _ in range(k)]


for i in range(k):
    u[i%k] = t[i]
    ans += dic[t[i]]

for i in range(k, n):
    if u[i%k] != t[i]:
        ans += dic[t[i]]
        u[i%k] = t[i]
    elif u[i%k] == t[i] and tf_list[i-k] == 1:
        ans += dic[t[i]]
        tf_list[i] = 0
    elif u[i%k] == t[i] and tf_list[i-k] == 0:
        tf_list[i] = 1

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=list(input())
pattern = {'r':p,'s':r,'p':s,'':0}
ans = 0
for i in range(n):
    tmp = i - k
    if tmp >= 0 and t[tmp] == t[i]:
        t[i] = ''
    ans += pattern[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())
pattern = {
    'r':P,
    's':R,
    'p':S
}
ret = 0
for i in range(N):
    tmp = i - K
    if tmp >= 0 and T[tmp] == T[i]:
        T[i] = ''
        continue
    ret += pattern[T[i]]
print(ret)

Submission status: Accepted

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

    ans = 0
    for k in range(K):
        slicedT = T[k::K]
        dp = [[0] * 3 for i in range(len(slicedT)+1)]

        for i in range(len(slicedT)):
            for frm in range(3):
                for to in range(3):
                    if frm == to:
                        continue
                    dp[i+1][to] = max(dp[i+1][to], dp[i][frm] + calcPoint(to,
                                                                        slicedT[i],
                                                                        R,
                                                                        S,
                                                                        P ))
                    
        ans += max(*dp[-1])
    
    print(ans)

# 0 -> R, 1 -> S, 2 -> P
def calcPoint(player, opponent, R, S, P):
    if player == 0 and opponent == 's':
        return R
    if player == 1 and opponent == 'p':
        return S
    if player == 2 and opponent == 'r':
        return P
        
    return 0

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

count = 0

arr = [True] * n
for i in range(n):
    if i >= k:
        if t[i] == t[i-k]:
           if arr[i-k]:
               arr[i] = False
               
for i in range(n):
    if arr[i]:
        if t[i] == "r":
            count += p
        elif t[i] == "s":
            count += r
        else:
            count += s

print(count)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
res_list = {'r':'p','s':'r','p':'s'}
t=input()
res = [0]*n
ans = 0
for i,rsp in enumerate(t):
    res_rsp = res_list[rsp]
    if i<k or res[i-k] != res_rsp:
        ans += eval(res_rsp)
        res[i] = res_rsp
print(ans)

Submission status: Accepted

Solution code:
import sys

nk = list(input().split(" "))
n = int(nk[0])
k = int(nk[1])
rsp = list(input().split(" "))
r = int(rsp[0])
s = int(rsp[1])
p = int(rsp[2])
T = str(input())

my_choice = [""] * n
ans = 0

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

if k == n:
    print(ans)
    sys.exit()

for i in range(k,n):
    if T[i] == "r" and my_choice[i - k] != "p":
        my_choice[i] = "p"
        ans += p
    elif T[i] == "s" and my_choice[i - k] != "r":
        my_choice[i] = "r"
        ans += r
    elif T[i] == "p" and my_choice[i - k] != "s":
        my_choice[i] = "s"
        ans += s


print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
INF = 500000
MOD = 10**9+7

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
for i in range(0, min(K, N)):
    if T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
    flg = True
    for j in range(i+K, N, K):
        if T[j-K] == T[j] and flg:
            flg = False
        else:
            if T[j] == 'r':
                ans += P
            elif T[j] == 's':
                ans += R
            else:
                ans += S
            flg = True
print(ans)