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())
pointMap = {
  'r': p, 's': r, 'p': s
}

t = input()

pointSum = 0
preList = ['x'] * k # 一手前を覚えておくリスト

for i, c in enumerate(t):
  if preList[i % k] == c:
    preList[i % k] = 'x'
    continue
  pointSum += pointMap[c]
  preList[i % k] = c

print(pointSum)

Submission status: Accepted

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

ans = 0

H = [""] * N


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


for i in range(N):
    if T[i] == "r":
        H[i] = "p"
    elif T[i] == "s":
        H[i] = "r"
    else:
        H[i] = "s"

for i in range(K, N):
    if H[i] == H[i-K]:
        H[i] = ""

for i in range(N):
    if H[i] == "r":
        ans += R
    elif H[i] == "s":
        ans += S
    elif H[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=list(input())
L=[[] for i in range(k)]

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

ans=0
for i in range(k):
  dp=[[0]*3 for i2 in range(len(L[i])+1)]
  for j in range(1,len(L[i])+1):
    dp[j][0]=max(dp[j-1][1],dp[j-1][2])
    if L[i][j-1]=='s':
      dp[j][0]+=r
    dp[j][1]=max(dp[j-1][0],dp[j-1][2])
    if L[i][j-1]=='r':
      dp[j][1]+=p
    dp[j][2]=max(dp[j-1][0],dp[j-1][1])
    if L[i][j-1]=='p':
      dp[j][2]+=s
  ans+=max(dp[len(L[i])])
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

#貪欲
ret = 0
cur_my_hand = []
ca = cur_my_hand.append
for i,v in enumerate(t):
    if i < k:
        if v == "r":
            ret+=p
            ca("p")
        elif v == "s":
            ret+=r
            ca("r")
        elif v == "p":
            ret+=s
            ca("s")
    else:
        if v == "r" and cur_my_hand[i-k]!="p":
            ret+=p
            ca("p")
        elif v == "s" and cur_my_hand[i-k]!="r":
            ret+=r
            ca("r")
        elif v == "p" and cur_my_hand[i-k]!="s":
            ret+=s
            ca("s")
        else:
            ca("o")
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
T2RSP = {'r':P, 's':R, 'p':S}
ans = 0
for i, t in enumerate(T):
    ans += T2RSP[t]
for k in range(K):
    partT = T[k+K::K]
    pre_t = T[k]
    count = 1
    for t in partT:
        if pre_t == t:
            count += 1
        else:
            ans -= T2RSP[pre_t] * (count // 2)
            pre_t = t
            count = 1
    if count >= 2:
        ans -= T2RSP[pre_t] * (count // 2)


print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split(" "))
R, S, P = map(int, input().split(" "))
T = list(input())
te = [""] * N
kinsi = ''
score = 0
for i in range(N):
    if i >= K:
        kinsi = te[i-K]
    if T[i] == 'r':
        if kinsi != 'p':
            te[i] = 'p'
            score += P
        else:
            te[i] = ''
    if T[i] == 's':
        if kinsi != 'r':
            te[i] = 'r'
            score += R
        else:
            te[i] = ''
    if T[i] == 'p':
        if kinsi != 's':
            te[i] = 's'
            score += S
        else:
            te[i] = ''
print(score)

Submission status: Accepted

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


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

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

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

hand=[-1]*N
res=0

#print(score)
#print(op)
for i in range(len(T)):
    if i>=K:
        if hand[i-K]!=op[T[i]]:
            res+=score[op[T[i]]]
            hand[i] = op[T[i]]
    else:
        res += score[op[T[i]]]
        hand[i]=op[T[i]]

print(res)

Submission status: Accepted

Solution code:
from collections import deque

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

score = deque()

for i in range(len(T)):
    if T[i] == "r":
        score.append(P)
    elif T[i] == "s":
        score.append(R)
    elif T[i] == "p":
        score.append(S)

for i in range(len(score)-K):
    if T[i] == T[i+K]:
        if score[i] >= score[i+K]:
            score[i+K] = 0
        else:
            score[i] = 0

print(sum(score))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

n,k= [int(i) for i in input().split()]
pts = {}
pts["s"],pts["p"],pts["r"] = [int(i) for i in input().split()]
t = input()
take,notake, chose = [0 for _ in range(n)], [0 for _ in range(n)], ["w" for _ in range(n)]
for i in range(len(t)):
    if i == 0:
        take[0] = pts[t[i]]
        notake[0] = 0
        chose[0] = t[i]
    else:
        if t[i] == chose[i-k]:
            take[i] = max(take[i-1],notake[i-1])
            notake[i] = max(take[i-1],notake[i-1])
        else:
            take[i] = take[i-1] + pts[t[i]]
            notake[i] = max(take[i-1],notake[i-1])
            chose[i] = t[i]
print(max(take[-1],notake[-1]))

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

n,k=LI()
r,s,p=LI()
t=input()
m=list('rsp')
a=[p,r,s]
dc=dict(zip(m,a))
z=[]
for i in range(k):
    z.append(''.join(t[i::k]))
for zz in z:
    pre=None
    for zzz in zz:
        if pre!=zzz:
            ans+=dc[zzz]
            pre=zzz
        else:
            pre=None
#        show(ans,zzz)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
ans=0

for i in range(N):
    if T[i]=="r":
        if i>=K:
            if T[i-K]==T[i]:
                T[i]="o"
            else:
                ans+=P
        else:
            ans+=P
    elif T[i]=="s":
        if i>=K:
            if T[i-K]==T[i]:
                T[i]="o"
            else:
                ans+=R
        else:
            ans+=R
    elif T[i]=="p":
        if i>=K:
            if T[i-K]==T[i]:
                T[i]="o"
            else:
                ans+=S
        else:
            ans+=S

print(ans)

Submission status: Accepted

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

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

    i = 0
    score = 0
    while i != K:
        before = ""
        #print(T[i::K])
        for c in T[i::K]:
            #print(c, before)
            if c != before:
                if c == 'r':
                    score += P
                elif c == 's':
                    score += R
                else:
                    score += S
                before = c
            else:
                before = ""
                #print("?")
        i += 1
    print(score)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N,K=map(int,input().split())
    R,S,P=map(int,input().split())
    T=input()
    L=['']*N
    score=0
    for i in range(N):
        if T[i]=='r':
            if i>=K and L[i-K]=='p':
                pass
            else:
                score+=P
                L[i]='p'
        elif T[i]=='s':
            if i>=K and L[i-K]=='r':
                pass
            else:
                score+=R
                L[i]='r'
        else:
            if i>=K and L[i-K]=='s':
                pass
            else:
                score+=S
                L[i]='s'
    print(score)


main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
A = 0
rsp = {
    "r":P,
    "s":R,
    "p":S,
}
A += T.count("r")*P
A += T.count("s")*R
A += T.count("p")*S
a = []
for i in range(N):
    if i-K >= 0 and T[i] == a[i-K]:
        A -= rsp[T[i]]
        a.append("")
    else:
        a.append(T[i])
print(A)

Submission status: Accepted

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

score=0

def point(c):
    if c == 'r':
        return p
    if c == 's':
        return r
    if c == 'p':
        return s
    

draw = [False for i in range(n)]

for i in range(n):
    if i < k or t[i] != t[i-k]:
        score += point(t[i])
    elif t[i] == t[i-k]:
        if draw[i-k]:
            score += point(t[i])
        else:
            draw[i] = True    
    
print(score)

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(K):
    t=0
    l=T[i:N:K]
    for j in l:
        if j=="r":
            if t!=1:
                a+=P
                t=1
            else:
                t=0
        if j=="s":
            if t!=2:
                a+=R
                t=2
            else:
                t=0
        if j=="p":
            if t!=3:
                a+=S
                t=3
            else:
                t=0
print(a)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(c):
        if c == "r":
            return 0
        elif c == "s":
            return 1
        return 2
    def g(i,j):
        if (i+1)%3 == j:
            return 1
        return 0
    n,K = LI()
    r = LI()
    t = input()
    t = [f(i) for i in t]
    c = [1]*n
    ans = 0
    for i in range(n):
        if not c[i]:
            continue
        j = i
        l = []
        while j < n:
            l.append(j)
            c[j] = 0
            j += K
        dp = [[0]*3 for j in range(len(l))]
        for j in range(3):
            li = t[l[0]]
            dp[0][j] = r[j]*g(j,li)
        for j in range(1,len(l)):
            nj = j-1
            cj = t[l[j]]
            for k in range(3):
                q = r[k]*g(k,cj)
                for m in range(3):
                    if k == m:
                        continue
                    nd = dp[nj][m]+q
                    if dp[j][k] < nd:
                        dp[j][k] = nd

        ans += max(dp[-1])
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

out=0
te=''
for i in range(k):
  if t[i]=='r':
    out+=p
    te+='p'
  elif t[i]=='s':
    out+=r
    te+='r'
  else:
    out+=s
    te+='s'
    
for i in range(k, n):
  if t[i]=='r':
    if te[i-k]=='p':
      te+='n'
    else:
      te+='p'
      out+=p
  elif t[i]=='s':
    if te[i-k]=='r':
      te+='n'
    else:
      te+='r'
      out+=r
  else:
    if te[i-k]=='s':
      te+='n'
    else:
      te+='s'
      out+=s
      
print(out)

Submission status: Accepted

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

hoge = {i: t[i::k] for i in range(k)}
score_map = {"r": p, "s": r, "p": s}

ans = 0
for i in range(k):
  prev_hand = None
  for hand in hoge[i]:
    if hand != prev_hand:
      ans += score_map[hand]
      prev_hand = hand 
    else:
      prev_hand = None

print(ans)

Submission status: Accepted

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

dp = []
for i in range(N):
    if i < K:
        if T[i] == "s":
            dp.append([R, 0, 0])
        elif T[i] == "p":
            dp.append([0, S, 0])
        else:
            dp.append([0, 0, P])
    else:
        dpi = dp[-K]
        maxdpi = max(dpi)
        if T[i] == "s":
            dp.append([max(dpi[1], dpi[2]) + R, maxdpi, maxdpi])
        elif T[i] == "p":
            dp.append([maxdpi, max(dpi[0], dpi[2]) + S, maxdpi])
        else:
            dp.append([maxdpi, maxdpi, max(dpi[0], dpi[1]) + P])

ans = sum(max(dp[i]) for i in range(N - K, N))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

win = {"r":"p", "s":"r", "p":"s"}  # 勝つ手に変換
points = {"r":R, "s":S, "p":P}  # 得点換算

hands = []
ans = 0

for i in range(N):
    if i < K:
        hands.append(str(win[T[i]]))
        ans += points[str(win[T[i]])]
    elif str(win[T[i]]) == hands[-K]:
        hands.append("n")
    else:
        hands.append(str(win[T[i]]))
        ans += points[str(win[T[i]])]

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[]
for x in T:
    if x == 's':
        A.append('r')
    if x == 'p':
        A.append('s')
    if x == 'r':
        A.append('p')

for i in range(K,N):
    if A[i-K] == A[i]:
        A[i] = 'x'

ans = 0
for x in A:
    if x == 's':
        ans += S
    if x == 'p':
        ans += P
    if x == 'r':
        ans += R
print(ans)

Submission status: Accepted

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

t_ls=list(t)
ls=[]
for i,hand in enumerate(t_ls):
    try:
        if hand == "r":
            t_ls[i] = "p"
        elif hand == "s":
            t_ls[i] = "r"
        elif hand == "p":
            t_ls[i] = "s"
        if t_ls[i+k] == hand:
            t_ls[i+k] = "*"
    except:
        pass

ans = 0
for i in t_ls:
    if i == "r":
        ans+=r
    elif i == "s":
        ans+=s
    elif 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()
ans = 0
for i in range(K):
    k = i
    if T[k]=='r':
        ans += P
    elif T[k]=='s':
        ans += R
    else:
        ans += S
    flag = False
    while k < N:

        if k+K < N:
            if T[k+K]!=T[k]:
                if T[k+K]=='r':
                    ans += P
                elif T[k+K]=='s':
                    ans += R
                else:
                    ans += S
                flag = False
            else:
                if not flag:
                    flag = True
                else:
                    flag = False

                    if T[k+K]=='r':
                        ans += P
                    elif T[k+K]=='s':
                        ans += R
                    else:
                        ans += S
        k += K
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P =map(int, input().split())
T = input().rstrip()
_mapping = {"r": P, "s": R, "p": S}
d = [None] * K
ans = 0
for i, t in enumerate(T):
    k = i % K
    if d[k] == t:
        d[k] = None
        continue
    else:
        d[k] = t
        ans += _mapping[t]
        continue
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n, k = ril()
r, s, p = ril()
t = rs()

dic = {'r': p, 's': r, 'p': s}
ls = list(t)
lss = [ls[i::k] for i in range(k)]
res = 0
for ls in lss:
    prev = ls[0]
    res += dic[prev]
    for x in ls[1:]:
        if x != prev:
            res += dic[x]
            prev = x
        else:
            prev = None
print(res)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input().replace("s","R").replace("p","S").replace("r","P")

ans=0

def win(type):
  if type=="R":
    return R
  elif type=="S":
    return S
  elif type=="P":
    return P
  return 0

yaku=set(["R","S","P"])
def rest(a,b):
  re=yaku-set([a,b])
  for c in re:
    return c

def restRand(a):
  re=yaku-set([a])
  for c in re:
    return c

me=[""]*len(T)
for i in range(len(T)):
  if i-K<0:
    ans+=win(T[i])
    me[i]=T[i]
  else:
    if me[i-K]==T[i]:
      # 勝つ手は出せない
      # 残り二つのうち、K回あとに勝負がある場合、K回あとに勝てる手以外を出す
      if i+K<len(T):
        me[i]=rest(T[i],T[i+K])
      else:
        me[i]=restRand(T[i])
    else:
      # 勝つ手を出せる
      ans+=win(T[i])
      me[i]=T[i]

print(ans)

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 = read().rstrip().decode('utf-8')

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

def solve(A):
    # 直前と同じ手は出せない、というルール
    r,s,p = 0,0,0 # 直前に出した手 -> 最大スコア
    for x in A:
        if x == 'r':
            r,s,p = max(s,p),max(r,p),max(r,s)+P
        elif x == 's':
            r,s,p = max(s,p)+R,max(r,p),max(r,s)
        elif x == 'p':
            r,s,p = max(s,p),max(r,p)+S,max(r,s)
    return max(r,s,p)

answer = sum(solve(A) for A in k_to_word)
print(answer)

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())
dp = [[0]*3 for _ in range(N+1)]

def main():
    for i in range(1,N+1):
        if i<K:
            if T[i-1] == 's':
                dp[i] = [R,0,0]
            if T[i-1] == 'p':
                dp[i] = [0,S,0]
            if T[i-1] == 'r':
                dp[i] = [0,0,P]
        else:
            dp[i][0] = max(dp[i-K][1],dp[i-K][2])
            dp[i][1] = max(dp[i-K][0],dp[i-K][2])
            dp[i][2] = max(dp[i-K][0],dp[i-K][1])
            if T[i-1] == 's':
                dp[i][0] += R
            if T[i-1] == 'p':
                dp[i][1] += S
            if T[i-1] == 'r':
                dp[i][2] += P
    ans = 0
    for i in range(K):
        ans += max(dp[N-i])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def count(a1, a2, a3, n, t, k):
    ans = ['a'] * n

    if a1 == 'r':
        win = 's'
        lose = 'p'
    elif a1 == 's':
        win = 'p'
        lose = 'r'
    elif a1 == 'p':
        win = 'r'
        lose = 's'

    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a1
        elif i >= k:
            if t[i] == win and ans[i-k] != a1:
                ans[i] = a1

    if a2 == 'r':
        win = 's'
        lose = 'p'
    elif a2 == 's':
        win = 'p'
        lose = 'r'
    elif a2 == 'p':
        win = 'r'
        lose = 's'
    
    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a2
        elif i >= k:
            if t[i] == win and ans[i-k] != a2:
                ans[i] = a2

    if a3 == 'r':
        win = 's'
        lose = 'p'
    elif a3 == 's':
        win = 'p'
        lose = 'r'
    elif a3 == 'p':
        win = 'r'
        lose = 's'

    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a3
        elif i >= k:
            if t[i] == win and ans[i-k] != a3:
                ans[i] = a3
    return ans


b1 = count('r', 's', 'p', n, t, k)
b2 = count('r', 'p', 's', n, t, k)

b3 = count('s', 'r', 'p', n, t, k)
b4 = count('s', 'p', 'r', n, t, k)

b5 = count('p', 's', 'r', n, t, k)
b6 = count('p', 'r', 's', n, t, k)

ans = 0
for i in [b1, b2, b3, b4, b5, b6]:
    tmp = 0
    for j in range(n):
        if i[j] == 'r' and t[j] == 's':
            tmp += r
        elif i[j] == 's' and t[j] == 'p':
            tmp += s
        elif i[j] == 'p' and t[j] == 'r':
            tmp += p
    ans = max(tmp, ans)
print(ans)

Submission status: Accepted

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

ans = 0
buf = ""
for i in range(N):
    t = T[i]

    if t == "r":
        if i >= K:
            if buf[i - K] == "p":
                buf = buf + " "
                continue
        ans += P
        buf = buf + "p"
    elif t == "p":
        if i >= K:
            if buf[i - K] == "s":
                buf = buf + " "
                continue
        ans += S
        buf = buf + "s"
    elif t == "s":
        if i >= K:
            if buf[i - K] == "r":
                buf = buf + " "
                continue
        ans += R
        buf = buf + "r"

print(ans)

Submission status: Accepted

Solution code:
# Setup
import sys
sys.setrecursionlimit(4100000)

n,k= [int(i) for i in input().split()]
pts = {}
pts["s"],pts["p"],pts["r"] = [int(i) for i in input().split()]
t = input()
take,notake, chose = [0 for _ in range(n)], [0 for _ in range(n)], ["w" for _ in range(n)]
for i in range(len(t)):
    if i == 0:
        take[0] = pts[t[i]]
        notake[0] = 0
        chose[0] = t[i]
    else:
        if t[i] == chose[i-k]:
            take[i] = max(take[i-1],notake[i-1])
            notake[i] = max(take[i-1],notake[i-1])
        else:
            take[i] = take[i-1] + pts[t[i]]
            notake[i] = max(take[i-1],notake[i-1])
            chose[i] = t[i]
#     print(take,notake,chose)
# print(take,notake,chose,pts)
print(max(take[-1],notake[-1]))

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

dp = [[0, 0, 0] for _ in range(N)]

for i in range(K):
    Q = T[i]
    if Q == "s":
        dp[i][0] = R
    elif Q == "p":
        dp[i][1] = S
    else:
        dp[i][2] = P

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

ans = 0
for i in range(K):
    ans += max(dp[-1-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
dict = {"r":p,"s":r,"p":s}

dp = [[0,0] for i in range(n+1)]

for i in range(n):
    if i >= k and t[i] == t[i-k] and dp[i+1-k][1] == 1:
        dp[i+1][0] = dp[i][0]
        dp[i+1][1] = 0
    elif i >= k and t[i] == t[i-k]:
        dp[i+1][0] = dp[i][0] + dict[t[i]]
        dp[i+1][1] = 1
    else:
        dp[i+1][0] = dp[i][0] + dict[t[i]]
        dp[i+1][1] = 1

print(dp[n][0])

Submission status: Accepted

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


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]

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

    res = 0
    F = [True] * N
    for i, t in enumerate(T):
        if i >= K:
            if T[i - K] == T[i] and F[i - K]:
                F[i] = False
                continue
        
        if t == "r":
            res += P
        elif t == "s":
            res += R
        elif t == "p":
            res += S
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
l=list(input())
ans=0
v=[0]*n
for i in range(n):
    if i>=k and l[i]==l[i-k] and v[i-k]==1:
            continue
    else:
        v[i]=1
        if l[i]=='s':
            ans+=r
        elif l[i]=='r':
            ans+=p
        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()

ans = 0

te = ''

for i in range(n):
    l = t[i]
    if i < k:
        if l == 'r':
            ans += p
            te += 'p'
        elif l == 'p':
            ans += s
            te += 's'
        else:
            ans += r
            te += 'r'
    else:
        if l == 'r':
            if te[i-k] != 'p':
                ans += p
                te += 'p'
            else:
                te += 'a'
        elif l == 'p':
            if te[i-k] != 's':
                ans += s
                te += 's'
            else:
                te += 'a'
        else:
            if te[i-k] != 'r':
                ans += r
                te += 'r'
            else:
                te += 'a'

print(ans)

Submission status: Accepted

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

    string = ''
    ans = 0
    for i, ti in enumerate(t):
        if i < k:
            if ti == 'r':
                ans += p
            elif ti == 's':
                ans += r
            else:
                ans += s
            string += ti
        else:
            if ti == string[i-k]:
                string += 'e'
            else:
                if ti == 'r':
                    ans += p
                elif ti == 's':
                    ans += r
                elif ti == 'p':
                    ans += s
                string += ti
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

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

    table = [False]*n

    ans = 0
    for index, i in enumerate(t[:k]):

        if i == 'r':
            ans += p
            table[index] = True
        elif i == 'p':
            ans += s
            table[index] = True
        elif i == 's':
            ans += r
            table[index] = True

    for index, (i, j) in enumerate(zip(t[k:], t)):
        if i == j:
            if table[index]:
                table[index+k] = False
                continue
        if i == 'r':
            ans += p
            table[index+k] = True
        elif i == 'p':
            ans += s
            table[index+k] = True
        elif i == 's':
            ans += r
            table[index+k] = True
    print(ans)