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())
rsp_score = list(map(int, input().split()))
rsp = input("")
rsp_order = list(rsp)
t_score = []
t_order = []
#
for i in range(n):
    if rsp_order[i] == "r":
        if i < k:
            t_score.append(rsp_score[2])
            t_order.append("p")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[2])
                t_order.append("p")

            else:
                t_score.append(0)
                t_order.append("c")

    elif rsp_order[i] == "s":
        if i < k:
            t_score.append(rsp_score[0])
            t_order.append("r")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[0])
                t_order.append("r")

            else:
                t_score.append(0)
                t_order.append("c")

    elif rsp_order[i] == "p":
        if i < k:
            t_score.append(rsp_score[1])
            t_order.append("s")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[1])
                t_order.append("s")

            else:
                t_score.append(0)
                t_order.append("c")

print(sum(t_score))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
hand = {"r": "p", "s": "r", "p": "s"}
score = {"r": r, "s": s, "p": p}
other = "o"
t = input()
hist = [other] * k

result = 0
for i, v in enumerate(t):
    i %= k
    if hist[i] != hand[v]:
        hist[i] = hand[v]
        result += score[hand[v]]
    else:
        hist[i] = other
print(result)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  r,s,p=LI()
  t=S()

  dp=[[0 for _ in range(n+1)]for __ in range(3)]

  for i in range(n):
    hyoka=t[i]

    dp[0][i+1]=max(dp[0][i],dp[1][i],dp[2][i])
    dp[1][i+1]=max(dp[0][i],dp[1][i],dp[2][i])
    dp[2][i+1]=max(dp[0][i],dp[1][i],dp[2][i])

    if i-k<0:
      if hyoka=='s':
        dp[0][i+1]+=r
      elif hyoka=='p':
        dp[1][i+1]+=s
      else:
        dp[2][i+1]+=p
    else:
      if hyoka=='s':
        if not (dp[0][i+1-k]>dp[1][i+1-k] and dp[0][i+1-k]>dp[2][i+1-k]):
          dp[0][i+1]+=r
      elif hyoka=='p':
        if not (dp[1][i+1-k]>dp[2][i+1-k] and dp[1][i+1-k]>dp[0][i+1-k]):
          dp[1][i+1]+=s
      else:
        if not (dp[2][i+1-k]>dp[0][i+1-k] and dp[2][i+1-k]>dp[1][i+1-k]):
          dp[2][i+1]+=p

  # for x in dp:
  #   print(x)
  
  return max(dp[0][n],dp[1][n],dp[2][n])

# main()
print(main())

Submission status: Accepted

Solution code:
# 1<=K<=N-1なので少なくとも１回は制約を考える必要ある
# 合計スコアを出力
#
# Kこ前のやつで場合分けすれば良いのかな

# Kのあまりが同じ数列で、同じ手が連続した場合だけ点数を取りこぼす
# rrrrr なら3*R点しかとれない. rrrrなら2*R点
# xがt個連続するとすると, 点数はx*(t+1)//2点
# 連続しないなら必ず勝てる

N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = [c for c in input()]

score = dict(r=P, s=R, p=S)  # 筐体がrならPで勝つ,...
total = 0
for res in range(K):  # Kで割ったあまり
    i = res
    while i < N:
        lasthand = T[i]
        cont = 0
        while i < N and T[i] == lasthand:
            cont += 1
            i += K
        total += (cont+1)//2 * score[lasthand]

print(total)

Submission status: Accepted

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

ans = []
def get_ans(h):
    if h == 'r':
        return('p')
    elif h == 's':
        return('r')
    elif h == 'p':
        return('s')

for i in range(k):
    ans.append(get_ans(hand[i]))
for i in range(k,n):
    if (hand[i] =='r' and ans[i-k] != 'p') or (hand[i] =='p' and ans[i-k] != 's') or (hand[i] =='s' and ans[i-k] != 'r'):
        # 勝てる
        ans.append(get_ans(hand[i]))
    else:
        # 勝てない
        try:
            h = ['r', 's', 'p']
            h.remove(get_ans(hand[i-k]))
            h.remove(get_ans(hand[i+k]))
            ans.append(h[0])
        except:
            ans.append(h[0])
score = 0
for i in range(n):
    if ans[i] == 'r' and hand[i] == 's':
        score += r
    if ans[i] == 'p' and hand[i] == 'r':
        score += p
    if ans[i] == 's' and hand[i] == 'p':
        score += s
print(score)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    point_r, point_s, point_p = map(int, input().split())
    t = list(input())

    point = {
        'r': point_p,
        's': point_r,
        'p': point_s
    }

    score = 0
    for i in range(n):
        if i < k:
            score += point[t[i]]
        else:
            if t[i] != t[i - k]:
                score += point[t[i]]
            else:
                t[i] = 'x'

    print(score)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
def pin(type=int):
    return map(type,input().split())
N,K =pin()
R,S,P =pin()
T, =pin(str)


#Tに勝つ手を列挙する 
winT = []
win = "rpsr"
for i in range(N):
    for j in range(3):
        if T[i] ==win[j]:
            winT.append(win[j+1])
#print(winT)

#K個前の手を使えない条件…？
#print(K,"個前を使えない")
#i番目に使った手をi+K番目に使えない.使ったら消す
for i in range (N-K):
    if winT[i] == winT[i+K]:
        winT[i+K] = "0"
        #print(i)
#print(winT)

ans=0
for i in range(N):
    if winT[i]=="p":
        ans+=P
    elif winT[i]=="r":
        ans+=R
    elif winT[i]=="s":
        ans+=S
print(ans)

Submission status: Accepted

Solution code:
import math
import bisect
N,K = list(map(int,input().split(" ")))
r,s,p = list(map(int,input().split(" ")))

di = {}
di["r"] = p
di["s"] = r
di["p"] = s
di["@"] = 0
T = input()
T = list(T)
for i in range(K,N):
    if T[i - K] == T[i]:
        T[i] = "@"
ans = 0
for temp in T:
    ans += di[temp]

print(ans)

Submission status: Accepted

Solution code:
from itertools import compress

def solve(RSP, T, K):
    w = [True] * K
    for i, (t, tt) in enumerate(zip(T, T[K:])):
        w.append(True if t != tt else not w[i])
    return sum(map(lambda t: RSP[t], compress(T, w)))

def main():
    _, K = list(map(int, input().split()))
    RSP = dict(zip('spr', map(int, input().split())))
    T = input()
    print(solve(RSP, T, K))

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

ans = 0
d = {'s':R , 'p':S, 'r':P}
for i,t in enumerate(T):
    if i<K:
        ans += d[t]
    else:
        if T[i-K]!=T[i]:
                ans += d[t]
        else:
            T[i]=''

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import floor, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

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

ideal = [win[x] for x in T]

ans = 0
for i in range(N):
  if i < K:
    ans += point[ideal[i]]
  elif ideal[i-K] == ideal[i]:  # i の手を変える
    ideal[i] = None
  else:
    ans += point[ideal[i]]

print(ans)

Submission status: Accepted

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

commands = [''] * N
ans = 0
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()

def point(str):
  if str == "r":
    return p
  if str == "s":
    return r
  if str == "p":
    return s

total_point = 0
memory = [""] * k
for i in range(n):
  if t[i] != memory[i % k]:
    memory[i % k] = t[i]
    total_point += point(t[i])
  else:
    memory[i % k] = ""
print(total_point)

Submission status: Accepted

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

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

ans = 0
for i in range(min(n,k)):
    before = T[i]
    counter = 0
    for s in T[i::k]:
        if before == s:
            counter += 1
        else:
            ans += (counter+1)//2 * point[before]
            before = s
            counter = 1
    ans += (counter+1)//2 * point[before]
print(ans)

Submission status: Accepted

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

BAN = [""]*N
score = 0

for i in range(K):#手が制限されるまではどんな手を出そうが問題はない
    if T[i]=="r":
        score += P
        BAN[i] = "p"
    elif T[i]=="s":
        score += R
        BAN[i] = "r"
    else:
        score += S
        BAN[i] = "s"


for j in range(K,N):
    if T[j]=="r":#相手の手はなんですか
        if BAN[j-K]=="p" :#もしこの手がBANされたものであるならあいこまたは負けの処理
            continue
        else:#でなければ得点になりK回先の手が制限される
            score += P
            BAN[j] = "p"
    elif T[j] == "s":
        if BAN[j-K] == "r":
            continue
        else:
            score += R
            BAN[j] = "r"
    else:
        if BAN[j-K] == "s":
            continue
        else:
            score += S
            BAN[j] = "s"

print(score)

Submission status: Accepted

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

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

wins = []

ans = 0
for i, pose in enumerate(T):
    if i < K:
        wins.append('win')
        ans += pose2point[pose]
    else:
        pre_pose = T[i-K]
        if wins[i-K] == 'win':
            if pose == pre_pose:
                wins.append('lose')
            else:
                wins.append('win')
                ans += pose2point[pose]
        else:
            wins.append('win')
            ans += pose2point[pose]
print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
def point(hand):
  if(hand == "r"):
    return P
  elif(hand == "s"):
    return R
  else:
    return S
ans = 0
lose = [0] * N
for i in range(K):
  ans += point(T[i])
for i in range(K,N):
  if((T[i] != T[i-K]) or (lose[i-K] == 1)):
    ans += point(T[i])
  else:
    lose[i] = 1
print(ans)

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=input()
vic=""
ans=0
for i in t:
    if i=="r":
        vic+="p"
    if i=="p":
        vic+="s"
    if i=="s":
        vic+="r"
#print(vic[:k])
for i in vic[:k]:
    if i=="r":
        ans+=r
    elif i=="p":
        ans+=p
    elif i=="s":
        ans+=s
#print(vic)
#print(ans)
for i in range(k,n):
    #print(i,":",vic[i])
    if vic[i]==vic[i-k]:
        #print("lose")
        if not i==n-1:
            vic=vic[:i]+'n'+vic[i+1:]
    else:
        #print("win")
        if vic[i]=="r":
            ans+=r
        elif vic[i]=="p":
            ans+=p
        elif vic[i]=="s":
            ans+=s
        else:
            pass
            #print("wrong input")

    #print()

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[n,k]=input().split()
[r,s,p]=input().split()
t=input()
n=int(n)
k=int(k)
r=int(r)
s=int(s)
p=int(p)
r_miss=0
s_miss=0
p_miss=0
wl='w'
for i in range(1,k):
    wl=wl+'w'

for i in range(k,n):
    if t[i]==t[i-k] and wl[i-k]=='w':
        wl=wl+'l'
        if t[i]=='r':
            r_miss=r_miss+1
        elif t[i]=='s':
            s_miss=s_miss+1
        elif t[i]=='p':
            p_miss=p_miss+1
    else:
        wl=wl+'w'
            
score=(t.count('r')-r_miss)*p+(t.count('s')-s_miss)*r+(t.count('p')-p_miss)*s
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
scores = list(map(int, input().split()))
t = [0 if i=="r" else (1 if i=="s" else 2) for i in input()]

score = 0
no = [-1 for i in t]

for i, x in enumerate(t):
	if no[i] != x:
		score += scores[x-1]
		try:	
			no[i+k] = x
		except:
			pass
	
print(score)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    N, K = map(int, f_i.readline().split())
    R, S, P = map(int, f_i.readline().split())
    T = f_i.readline().rstrip()
    
    pair = {'r': 0, 's': 1, 'p': 2}
    point = {1: R, 2: S, 0: P}
    
    score = sum(point[pair[c]] for c in T) # full score
    
    for k in range(K):
        H = [pair[h] for h in T[k::K]] + [None]
        pre_h = None
        for h, next_h in zip(H, H[1:]):
            if h == pre_h:
                score -= point[h]
                h = (h + 1) % 3
                if h == next_h:
                    h = (h + 1) % 3
            pre_h = h
    
    print(score)

solve()

Submission status: Accepted

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

total = 0

for i in range(n):
    if i < k:
        if t[i] == "r":
            total += p
        elif t[i] == "s":
            total += r
        else:
            total += s
    else: # i >= k
        if t[i-k] == t[i]:
            t[i] = "."
        else:
            if t[i] == "r":
                total += p
            elif t[i] == "s":
                total += r
            else:
                total += s

print(total)

Submission status: Accepted

Solution code:
def hand_to_win(hand):
    if hand == 'r':
        return 'p'
    elif hand == 'p':
        return 's'
    elif hand == 's':
        return 'r'


def main():
    """
    r,s,pからスコアの大きい順に手を決めていく。
    kだけ前の値がかぶったらk後の手を邪魔しない手を選ぶ。
    """
    n, k = map(int, input().split(' '))
    r, s, p = map(int, input().split(' '))
    t = (input())

    score_map = {'r': r, 's': s, 'p': p}
    history = []

    score = 0

    for i, hand in enumerate(t):
        tobe_win = hand_to_win(hand)
        if 0 <= i - k and history[i - k] == tobe_win:
            h = set(['r', 'p', 's']) - set([tobe_win])
            if i + k <= n - 1:
                h -= set([hand_to_win(t[i + k])])
            history.append(h.pop())
            continue
        history.append(tobe_win)
        score += score_map[tobe_win]
    print(score)


main()

Submission status: Accepted

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

ans = 0
my_hand = list() #my_hand[i]:=r,p,sの場合、～勝てる手を出した。=? の場合、あいこか負けにしました。

for i in range(N):
    
    if i >= K:
        #print(i,T[i],my_hand[i-K])
        
        if T[i] != my_hand[i-K]:
            if T[i] == "r":
                ans += P
                my_hand.append("r")
            elif T[i] == "p":
                ans += S
                my_hand.append("p")
            elif T[i] == "s":
                ans += R
                my_hand.append("s")
        else:
            my_hand.append("?")
            
    else:
        #print(i,T[i],"''")
        if T[i] == "r":
            ans += P
            my_hand.append("r")
        elif T[i] == "p":
            ans += S
            my_hand.append("p")
        elif T[i] == "s":
            ans += R
            my_hand.append("s")
        
print(ans)

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

score = {'r': p, 's': r, 'p': s, 'l': 0}

ans = 0
for i in range(n):
    ans += score[t[i]]
    if i + k < n and t[i] == t[i + k]:
        t[i + k] = 'l'

print(ans)

Submission status: Accepted

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

dic = dict([('s',r),('p',s),('r',p)])


ans = 0
for i in range(k):
    A = [t[j] for j in range(i,n,k)]
    flag = A[0]
    ans += dic[flag]
    for j in range(1,len(A)):
        if A[j]==flag:
            if j!=len(A)-1:
                flag = (set(['r','s','p'])-set(A[j+1])).pop()
        else:
            ans += dic[A[j]]
            flag = A[j]
print(ans)

Submission status: Accepted

Solution code:
x,y=map(int,input().split())
a,b,c=map(int,input().split())
n=list(input())
s=0
for i in range(y):
    if n[i]=="r":
        s+=c
    elif n[i]=="s":
        s+=a
    else:
        s+=b
for i in range(y,x):
    if n[i-y]==n[i]:
        n[i]="!"
    else:
        if n[i]=="r":
            s+=c
        elif n[i]=="s":
            s+=a
        elif n[i]=="p":
            s+=b
    #print(s,n[i])        
print(s)

Submission status: Accepted

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

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
score = {'r': P, 's': R, 'p': S}
ans = 0

for i in range(N):
    if i<K:
        ans += score[T[i]]
    else:
        if T[i]!=T[i-K]:
            ans += score[T[i]]
        else:
            T[i] = '?'
            
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 100)

def dfs(i, type_):
    if i >= N:
        return 0
    if dp[i][type_] != -1:
        return dp[i][type_]
    ret = 0
    for t in range(3):
        if t == type_:
            continue
        ret = max(ret, dfs(i + K, t))
    dp[i][type_] = ret + ((type_ + 1) % 3 == T[i]) * score[type_]
    return dp[i][type_]


N, K = map(int, input().split())
score = [int(x) for x in input().split()]
dic = {"r": 0, "s": 1, "p": 2}
T = [dic[x] for x in input()]
dp = [[-1] * 3 for _ in range(N)]
ans = 0
for i in range(K):
    ans += max(dfs(i, t) for t in range(3))
print(ans)

Submission status: Accepted

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

def point(op):
    if op == 'r':
        return p
    elif op == 's':
        return r
    else:
        return s

res = 0
for mod in range(k):
    len = (n-mod+k-1) // k
    dp_free = [0] * len
    dp_win = [0] * len
    dp_win[0] = point(t[mod])
    for i in range(1, len):
        dp_free[i] = max(dp_win[i-1], dp_free[i-1])
        dp_win[i] = dp_free[i-1] + point(t[mod+k*i])
        if t[mod+k*i] != t[mod+k*(i-1)]:
            dp_win[i] = max(dp_win[i], dp_win[i-1] + point(t[mod+k*i]))
    res += max(dp_win[-1], dp_free[-1])
print(res)

Submission status: Accepted

Solution code:
def main():
    N,K = map(int,input().split())
    R,S,P = map(int,input().split())
    dic = {"r":P,"s":R,"p":S}
    T = list(input())
    ans = 0
    for i in range(K):   
        for j in range(i,N,K):
            if j == i:
                ans += dic[T[j]]
            else:
                if T[j-K]==T[j]:
                    T[j]="x"
                else:
                    ans += dic[T[j]]
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  N, K = [int(i) for i in input().split()]
  R, S, P = [int(i) for i in input().split()]
  T = input()
  point = {"r":P, "s":R, "p":S}
  ans = 0
  
  for i in range(K):
    last = ''
    j = i
    while j<N:
      aite = T[j]
      if aite!=last:
        ans = ans+point[aite]
        last = aite
      else:
        last = ''
      j = j+K
   
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

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

pt={"r":P,"s":R,"p":S}
for i in range(N):
    idx[T[i]].append(i)
idxlst={"r":set(idx["r"]),"s":set(idx["s"]),"p":set(idx["p"])}
#print(idx)
value=0
for t in idx:
    for i in range(len(idx[t])-1,-1,-1):
        if idx[t][i] in idxlst[t]:
            a=idx[t][i]-K
            if a in idxlst[t]:
                idxlst[t].remove(a)
    value+=pt[t]*len(idxlst[t])
print(value)

Submission status: Accepted

Solution code:



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

point = 0

judge = {"r": R, "s": S, "p": P}
skip = [False] * N

for i in range(N):
  if i < K:
    if T[i] == "r":
      point += judge["p"]
    elif T[i] == "s":
      point += judge["r"]
    elif T[i] == "p":
      point += judge["s"]
  else:
    # K回前を意識しないといけない
    if T[i-K] != T[i] or skip[i-K]:
      if T[i] == "r":
        point += judge["p"]
      elif T[i] == "s":
        point += judge["r"]
      elif T[i] == "p":
        point += judge["s"]
    else:
      skip[i] = True
      
print(point)

Submission status: Accepted

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

ans_str = ''
for i in range(n):
    if t[i] == 'r':
        ans_str += 'p'
    elif t[i] == 's':
        ans_str += 'r'
    else:
        ans_str += 's'

ans_str = list(ans_str)
for i in range(k, n):
    if ans_str[i] == ans_str[i-k]:
        ans_str[i] = 'z'

pnt = 0
for i in range(n):
    if ans_str[i] == 'r':
        pnt += R
    elif ans_str[i] == 's':
        pnt += S
    elif ans_str[i] == 'p':
        pnt += P

print(pnt)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
p=[int(x) for x in input().split()]
T=[-1]*K+[0 if x=='r' else (1 if x=='s' else 2) for x in input()]

s=0
for i in range(K,N+K):
  if T[i]==T[i-K]:
    T[i]=-1
  else:
    s+=p[(T[i]+2)%3]

print(s)

Submission status: Accepted

Solution code:
n=list(map(int,input().split()))
score=list(map(int,input().split()))
s=input()

def rscore(ss):
  if ss=='r':
    return score[2]
  elif ss=='s':
    return score[0]
  elif ss=='p':
    return score[1]

result=0
flg=[]
for i in range(n[1]):
  result+=rscore(s[i])
  flg.append(True)
for i in range(n[1],len(s)):
  if(s[i-n[1]]==s[i] and flg[i-n[1]]):
    flg.append(False)
    continue
  else:
    flg.append(True)
    result+=rscore(s[i])
print(result)

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 = list(input())
dp = [0]*n
for i in range(n):
  if i>=k and t[i]==t[i-k]:
    dp[i] = 0
    t[i] = "x"
  else:
    dp[i] = d[t[i]]
print(sum(dp))

Submission status: Accepted

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

ans = 0

for i in range(N):
    if i < K:
        ans += d[T[i]]
    else:
        if T[i] != T[i - K]:
            ans += d[T[i]]
        else:
            T[i] = 'z'

print(ans)

Submission status: Accepted

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

T = list(map(lambda c: 0 if c == 'r' else 1 if c == 's' else 2, list(input())))




#dp[i][c] : i回目にcを出す時のその時点でのi(modK)回目の類の得点の最大値
dp = [[0 for c in range(3)] for i in range(N + 1)]

for i in range(1,N+1):
    for c in range(3):
        bonus = B[c] if (T[i - 1] - c) % 3 == 1 else 0

        if i <= K:
            dp[i][c] = bonus
        else:
            dp[i][c] = bonus + max(dp[i - K][(c + 1) % 3], dp[i - K][(c + 2) % 3])

print(sum([max(dp[i]) for i in range(N - K + 1, N + 1)]))
# print(dp[N - K + 1 : N + 1])



# print(dp)

Submission status: Accepted

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

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K
memo = [{} for _ in range(K)]

for i in range(N):
    l[i % K] += T[i]

def okjudge(m, ss, p):
    if p in memo[m]:
        return memo[m][p]

    lss = len(ss)
    if p == lss:
        ret =  0
        memo[m][p] = ret
        return ret

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            ret = P
        elif c == 's':
            ret =  R
        else:
            ret =  S
        memo[m][p] = ret
        return ret

    if ss[p + 1] == ss[p]:
        if c == 'r':
            ret =  max(okjudge(m, ss, p + 1), P + okjudge(m, ss, p + 2))
        elif c == 's':
            ret =  max(okjudge(m, ss, p + 1), R + okjudge(m, ss, p + 2))
        else:
            ret =  max(okjudge(m, ss, p + 1), S + okjudge(m, ss, p + 2))
    elif c == 'r':
        ret =  P + okjudge(m, ss, p + 1)
    elif c == 's':
        ret =  R + okjudge(m, ss, p + 1)
    else:
        ret =  S + okjudge(m, ss, p + 1)

    memo[m][p] = ret
    return ret

print(sum([okjudge(mi, v, 0) for mi,v in enumerate(l)]))

Submission status: Accepted

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

dic = {'r':p, 's':r, 'p':s}
score = 0
for i in range(k):
    flg = True
    for j in range(i, n-k, k):
        if t[j]==t[j+k] and flg:
            score -= dic[t[j]]
            flg = False
        else:
            flg = True

for i, j in dic.items():
    score += t.count(i)*j
print(score)

Submission status: Accepted

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

score = 0
back = 0

for fst in range(K):
  index = fst
  while True:
    ch = T[index]
    if ch != back:
      back = ch
      if ch == "r":
        score += P
      elif ch == "p":
        score += S
      else:
        score += R
    else:
        back = 0
    
    index += K
    if index >= N:
      break
  back = 0
  
print(score)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=input()
ans=0
for i in range(k):
  u=t[i::k]
  u=u.replace("rr", "rx")
  u=u.replace("pp", "px")
  u=u.replace("ss", "sx")
  ans+=u.count("s")*r
  ans+=u.count("p")*s
  ans+=u.count("r")*p
print(ans)

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"}
lose_hand = {"r":"s", "s":"p", "p":"r"}
score_hand = {"r":R, "s":S, "p":P}

score = 0

for k in range(K):
  n = k
  preb_hand = ""
  while n < N:
    if win_hand[T[n]] != preb_hand:
      score = score + score_hand[win_hand[T[n]]]
      preb_hand = win_hand[T[n]]
    else:
      if (n + K) < N:
        preb_hand = lose_hand[T[n + K]]
      else:
        preb_hand = ""
    n = n + K

print(score)