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:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = read().rstrip().decode('utf-8')

cnt = 0
a = ""

for i in range(n):
    if i < k:
        if t[i] == "r":
            a += "p"
        elif t[i] == "s":
            a += "r"
        else:
            a += "s"
    else:
        if t[i] == "r":
            if t[i] == t[i-k] and a[i-k] != "?":
                a += "?"
            else:
                a += "p"
        elif t[i] == "s":
            if t[i] == t[i-k] and a[i-k] != "?":
                a += "?"
            else:
                a += "r"
        else:
            if t[i] == t[i-k] and a[i-k] != "?":
                a += "?"
            else:
                a += "s"

cnt = a.count("r") * r + a.count("s") * s + a.count("p") * p

print(cnt)

Submission status: Accepted

Solution code:
N, K=map(int, input().split())
R, S, P=map(int, input().split())
T=list(input())
ans=0
for i in range(N):
  if i<K:
    if T[i]=="r":
      ans+=P
      T[i]="rw"
    elif T[i]=="s":
      ans+=R
      T[i]="sw"
    elif T[i]=="p":
      ans+=S
      T[i]="pw"
  else:
    if T[i]=="r" and T[i-K]!="rw":
      ans+=P
      T[i]="rw"
    elif T[i]=="s" and T[i-K]!="sw":
      ans+=R
      T[i]="sw"
    elif T[i]=="p" and T[i-K]!="pw":
      ans+=S
      T[i]="pw"
      
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(k):
    if t[i]=='r':
        ans+=p
        te.append('p')
    elif t[i]=='p':
        ans+=s
        te.append('s')
    elif t[i]=='s':
        ans+=r
        te.append('r')

for i in range(k,n):
    if t[i]=='r' and te[i-k]!='p':
        ans+=p
        te.append('p')
    elif t[i]=='p' and te[i-k]!='s':
        ans+=s
        te.append('s')
    elif t[i]=='s' and te[i-k]!='r':
        ans+=r
        te.append('r')
    else:
        te.append('*')
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
q=str(input())
mukou=[0]*n
ans=0

for i in range(n):
  if i>=k:
    if q[i]==q[i-k] and mukou[i-k]==0:
      mukou[i]=1

for i in range(n):
  if mukou[i]==0:
    if q[i]=="r": ans+=p
    elif q[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()
d = {"r":p, "s":r, "p":s}
ans = 0
for i in range(k):
    pa = ""
    while i < n:
        if t[i] == pa:
            pa = ""
        else:
            ans += d[t[i]]
            pa = t[i]
        i += k
print(ans)

Submission status: Accepted

Solution code:

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

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

ans = 0
for r in range(K):
    i = r
    p = '' #一個前の文字
    while i<N:
        if T[i] == p:
            p = ''
        else:
            ans += score[T[i]]
            p = T[i]
        i += K

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split()) #N回ジャンケン, K回前と同じ手を出せない
R, S, P = map(int, input().split())
T = input()

t_k = [[] for i in range(K)]

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

hand = ['r', 's', 'p']
ans = 0
for i_k in t_k:
    n = len(i_k)
    dp = [[0,0,0] for i in range(n+1)]
    cnt = 1
    for s in i_k:
        a = hand.index(s) #相手の手
        if a==0:
            point = [0, 0, P]
        elif a==1:
            point = [R, 0, 0]
        else:
            point = [0, S, 0]
        dp[cnt][0] = max(dp[cnt-1][1]+point[0], dp[cnt-1][2]+point[0])
        dp[cnt][1] = max(dp[cnt-1][0]+point[1], dp[cnt-1][2]+point[1])
        dp[cnt][2] = max(dp[cnt-1][0]+point[2], dp[cnt-1][1]+point[2])
        cnt += 1
    ans += max(dp[n])


print(ans)

Submission status: Accepted

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

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

ans=0
result=[]

for i in range(k):
    ans+=point[t[i]]
    result.append(t[i])

for i in range(k,n):
    if t[i]==result[i-k]:
        result.append("n")
    else:
        ans+=point[t[i]]
        result.append(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())
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]="x"
print(ans)

Submission status: Accepted

Solution code:
p=0
N,K=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
L=list()
L_append = L.append
for i in range(N):
  if i<K:
    if s[i]=="r":
      p+=c
      L_append(i)
    elif s[i]=="s":
      p+=a
      L_append(i)
    else:
      p+=b
      L_append(i)
  else:
    if s[i]=="r" and s[i-K]!="r":
      p+=c
      L_append(i)
    elif s[i]=="s" and s[i-K]!="s":
      p+=a
      L_append(i)
    elif s[i]=="p" and s[i-K]!="p":
      p+=b
      L_append(i)
    if s[i]=="r" and s[i-K]=="r" and i-K not in L:
      p+=c
      L_append(i)
    elif s[i]=="s" and s[i-K]=="s" and i-K not in L:
      p+=a
      L_append(i)
    elif s[i]=="p" and s[i-K]=="p" and i-K not in L:
      p+=b
      L_append(i)
print(p)

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):
    t = T[i::K]
    dp = [[0] * 3 for _ in range(len(t) + 1)]
    for i in range(len(t)):
        if t[i] == "r":
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][1] = max(dp[i][0] + P, dp[i][2] + P)
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
        elif t[i] == "p":
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][2] = max(dp[i][0] + S, dp[i][1] + S)
        else:
            dp[i + 1][0] = max(dp[i][1] + R, dp[i][2] + R)
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
    ans += max(dp[-1])
print(ans)

Submission status: Accepted

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

N,K = map(int,readline().split())
R,S,P = map(int,readline().split())
T = list(readline().rstrip())

P = {"r":P,"s":R,"p":S}
ans = 0
for i in range(len(T)):
  if i < K:
    ans += P[T[i]]
    continue
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += P[T[i]]
print(ans)

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=list(input())
t.append
# a 4+3i でいう４
# b          3
a=n%k
b=n//k

ans=[]
#1   1個前同じで一個前使ってる
flg=0
for i in range(k):
  if i<=a-1:
    flg=0
    for j in range(b):
      if flg==1:
        flg=0
      else:
        if t[i+k*j]==t[i+k*j+k]:
          flg=1
          ans.append(t[i+k*j])
        else:
          ans.append(t[i+k*j])
    if flg==0:
      ans.append(t[i+b*k])
  else:
    flg=0
    for j in range(b-1):
      if flg==1:
        flg=0
      else:
        if t[i+k*j]==t[i+k*j+k]:
          flg=1
          ans.append(t[i+k*j])
        else:
          ans.append(t[i+k*j])
    if flg==0:
      ans.append(t[i+(b-1)*k])

      
from collections import Counter
zisyo=Counter(ans)
print(zisyo["r"]*p+zisyo["s"]*r+zisyo["p"]*s)

Submission status: Accepted

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

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

for k in range(K):
    K_list[k] = K_list[k].replace("rr", "r")
    K_list[k] = K_list[k].replace("ss", "s")
    K_list[k] = K_list[k].replace("pp", "p")

ans = 0
for k_list in K_list:
    for s in k_list:
        ans += hands[s]

print(ans)

Submission status: Accepted

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

pr = input()
pool = []

def iWannaRemotework():
    for i, x in enumerate(pr):
        if i >= k:
            no = pool[i-k]
        else:
            no = ""

        if x == no:
            pool.append("")
            yield 0
        elif x == "r":
            pool.append("r")
            yield p
        elif x == "s":
            pool.append("s")
            yield r
        else:
            pool.append("p")
            yield s

print(sum(iWannaRemotework()))

Submission status: Accepted

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


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    # 案①
    # 1. 勝ちパターンに変換し点数を計算 2 * O(N)
    # 2. K番目以降を確認し点数の差分を計算 O(N)?
    # →手を修正すると、修正後の手に応じてその先の手も修正が必要になるのでこの方法は無理。
    #
    # 案②
    # 修正後の手も考慮しつつ探索すれば良さそう。
    # あいこと負けどちらを選ぶかは、K手先を見て決める
    # 計算量は大体 O((N/K) * K) = O(N)

    m = {'r': 'p', 's': 'r', 'p': 's'}
    nt = list(''.join([m[i] for i in T]))
    pm = {'r': R, 's': S, 'p': P}
    flgs = [0] * N
    ans = 0
    for i in range(N):
        if flgs[i]:
            continue
        piece = nt[i::K]
        for j, jv in enumerate(piece):
            if j - 1 >= 0 and jv == piece[j - 1]:
                jv = piece[j] = m[jv]
                if j + 1 < len(piece) and jv == piece[j + 1]:
                    jv = piece[j] = m[jv]
            else:
                ans += pm[jv]
            idx = i + (j * K)
            nt[idx] = jv
            flgs[idx] = 1
    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

d = {"r": p, "s": r, "p": s}  # key: value = 筐体が出す手: 勝つともらえる点数
ans = 0
wins = [False] * k  # 剰余の周期ごとに、前回の勝ち負けを管理。勝ってたらTrueにして次回は負けないといけない。

for i in range(n):
    # kの剰余を使って周期ごとに管理。例えば剰余１の周期だけ見ると、出す手がずっと同じだと○×○×となる。
    # 出す手が前と異なるときは気にせず点数を追加する。
    if not wins[i % k] or t[i] != t[i-k]:
        ans += d[t[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

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

point = 0

for i in range(k):
    li = ["x"]+t[i::k]
    for j in range(1,len(li)):
        if li[j-1] == li[j]:
            li[j] = "x"
            continue
        if li[j] == "r":
            point += p
        elif li[j] == "s":
            point += r
        else:
            point += s
    
print(point)

Submission status: Accepted

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

for i in range(n):
  if i-k>=0:
    if t[i]==t[i-k]:
      if t[i]=="r":
        if S[i-k]=="p":
          S+="r"
        elif S[i-k]=="r":
          S+="p"
          ans+=p
      elif t[i]=="s":
        if S[i-k]=="r":
          S+="s"
        elif S[i-k]=="s":
          S+="r"
          ans+=r
      elif t[i]=="p":
        if S[i-k]=="s":
          S+="p"
        elif S[i-k]=="p":
          S+="s"
          ans+=s
      continue
  if t[i]=="r":
    ans+=p
    S+="p"
  elif t[i]=="s":
    ans+=r
    S+="r"
  elif t[i]=="p":
    ans+=s
    S+="s"
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
score = list(map(int, input().split()))
score = {"r": score[2], "s": score[0], "p": score[1]}
# ord(r) = 114
# r の時はp s -> r p -> s を出すことになる。
t = input()
l = len(t)
ans = 0
for i in range(k):
    cnt = 0
    pre = ""
    while True:
        if k * cnt + i >= l:
            break
        if t[k * cnt + i] != pre:
            ans += score[t[k * cnt + i]]
            # print(k * cnt + i, t[k * cnt + i], ans)
            pre = t[k * cnt + i]
        else:
            pre = ""
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
l = input()
ans =0
for i in range(k):
    #print(i)
    if l[i] =="r":
        ans += p
    elif l[i] =="s":
        ans += r
    elif l[i] =="p":
        ans += s
        
        
daida=[]
for o in range(len(l)):
    daida.append(l[o])
for j in range(k,n):
    if l[j]== l[j-k]:
        jj=["r","s","p"]
        jj.remove(l[j])
        if l[j] == daida[j-k]:
            daida[j]=jj[0]
            continue
        else:
            if l[j] =="r":
                ans += p
            elif l[j] =="s":
                ans += r
            elif l[j] =="p":
                ans += s
            continue
            
    else:
        if l[j] =="r":
            ans += p
        elif l[j] =="s":
            ans += r
        elif l[j] =="p":
            ans += s
print(ans)

Submission status: Accepted

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

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

    ret = 0
    for r in range(K):
        i = r
        p = ''
        while i < N:
            if T[i] == p:
                p = ''
            else:
                ret += score[T[i]]
                p = T[i]
            i += K
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,k=map(int, input().split())
    r,s,p=map(int, input().split())
    t=input()
    def kachip(te):
        if te=='r':
            return p
        elif te=='s':
            return r
        else:
            return s
    def kachite(te):
        if te == 'r':
            return 'p'
        elif te == 's':
            return 'r'
        else:
            return 's'

    check=[0]*k # 出した手
    ans=0
    for i in range(n):
        point=kachip(t[i])
        te=kachite(t[i])
        if check[i%k]==te:
            check[i%k]=0
        else:
            ans+=point
            check[i%k]=te
    print(ans)
resolve()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
D = {"r":P,"s":R,"p":S}
T = input().strip()
hist = [0 for _ in range(N)]
ans = 0
for i in range(N):
    a = T[i]
    if hist[i]==0:
        hist[i] = 1
        cnt = 1
        j = i
        while j+K<N and T[j+K]==a:
            hist[j+K]=1
            cnt += 1
            j += K
        ans += D[a]*(cnt-cnt//2)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    for i in range(n):
        if i < k:
            res += p if t[i] == "r" else r if t[i] == "s" else s
        else:
            if t[i - k] != t[i]:
                res += p if t[i] == "r" else r if t[i] == "s" else s
            else:
                t[i] = "e"

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = list(input())
    score = {"r": p, "s": r, "p": s}
    answer = 0
    for i in range(n):
        if i < k:
            answer += score[t[i]]
            if i + k < n and t[i] == t[i + k]:
                t[i + k] = t[i + k] + "*"
        elif i + k < n:
            if "*" not in t[i]:
                answer += score[t[i][0]]
                if t[i][0] == t[i + k][0]:
                    t[i + k] = t[i + k] + "*"
        else:
            if "*" not in t[i]:
                answer += score[t[i][0]]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dic = defaultdict(list)
for i in range(N):
    dic[i%K].append(T[i])
ans = 0
D = []
for t_s in dic.values():
    before = 'z'
    for t in t_s:
        if t == 's':
            if not before == 's':
                ans += R
                before = t
            else:
                before = 'z'
                
        elif t == 'r':
            if not before == 'r':
                ans += P
                before = t
            else:
                before = 'z'
        else:
            if not before == 'p':
                ans += S
                before = t
            else:
                before = 'z'

print(ans)

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}
result=["none"]*N
ans=0
for i in range(N):
    if i<K:
        ans+=D[T[i]]
        result[i]=T[i]
    else:
        if T[i]!=result[i-K]:
            ans+=D[T[i]]
            result[i]=T[i]
print(ans)

Submission status: Accepted

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

ans = 0
hand = [""] * n

for i in range(n):
    if i < k:
        if t[i] == "r":
            ans += p
            hand[i] = "p"
        elif t[i] == "s":
            ans += r
            hand[i] = "r"
        elif t[i] == "p":
            ans += s
            hand[i] = "s"
    else:
        if t[i] == "r":
            if hand[i-k] != "p":
                ans += p
                hand[i] = "p"
            else:
                hand[i] = "?"
        elif t[i] == "s":
            if hand[i-k] != "r":
                ans += r
                hand[i] = "r"
            else:
                hand[i] = "?"
        elif t[i] == "p":
            if hand[i-k] != "s":
                ans += s
                hand[i] = "s"
            else:
                hand[i] = "?"
print(ans)

Submission status: Accepted

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

for i in range(n):
  if i<k:
    if t[i]=="r":
      ans+=p
    elif t[i]=="s":
      ans+=r
    elif t[i]=="p":
      ans+=s
  else:
    if t[i]=="r" and t[i-k]!=t[i]:
      ans+=p
    elif t[i]=="s" and t[i-k]!=t[i]:
      ans+=r
    elif t[i]=="p" and t[i-k]!=t[i]:
      ans+=s
    else:
      t[i]="X"
    
print(ans)

Submission status: Accepted

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

for i in range(K, N):
  if T[i] == 'r':
    if T[i-K] != 'r':
      ans += P
    else:
      T[i] = 'q'
    
  elif T[i] == 's':
    if T[i-K] != 's':
      ans += R
    else:
      T[i] = 'q'
    
  elif T[i] == 'p':
    if T[i-K] != 'p':
      ans += S
    else:
      T[i] = 'q'
      
print(ans)

Submission status: Accepted

Solution code:
# r:グー　s:ちょき　p:パー
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
point = {"r":p, "s":r, "p":s}
d = {"r":"p", "s":"r", "p":"s"}

janken = []
score = 0
for i in range(n):
  if len(janken) < k or d[t[i]]!=janken[-k]:
    score += point[t[i]]
    janken.append(d[t[i]])
  else:
    janken.append("")
print(score)

Submission status: Accepted

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

dp = [[0]*3 for _ in range(N)]
for i in range(K):
    for j in range(i,N,K):
        c = T[j]
        if j < K:
            if c=='r':
                dp[j][0] = P
            elif c=='s':
                dp[j][1] = R
            else:
                dp[j][2] = S
        else:
            if c=='r':
                dp[j][0] = max(dp[j-K][1], dp[j-K][2]) + P
                dp[j][1] = max(dp[j-K][0], dp[j-K][2])
                dp[j][2] = max(dp[j-K][0], dp[j-K][1])
            elif c=='s':
                dp[j][0] = max(dp[j-K][1], dp[j-K][2])
                dp[j][1] = max(dp[j-K][0], dp[j-K][2]) + R
                dp[j][2] = max(dp[j-K][0], dp[j-K][1])
            else:
                dp[j][0] = max(dp[j-K][1], dp[j-K][2])
                dp[j][1] = max(dp[j-K][0], dp[j-K][2])
                dp[j][2] = max(dp[j-K][0], dp[j-K][1]) + S
ans = 0
for i in range(N-1,N-1-K,-1):
    ans += max(dp[i])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,copy
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
R,S,P = inpl()
t = list(input())
di = {'r':P, 's':R, 'p':S}
res = 0
ch = True
for i in range(k):
    for j in range(i,n,k):
        if j > i and t[j] == t[j-k] and ch:
            ch = False
            continue
        res += di[t[j]]
        ch = True
        # print(i,j)
        # print(res)
print(res)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
score = {'r': p, 's': r, 'p': s}
for i in range(k):
    j = i
    while j<n:
        if j>=k and t[j]==t[j-k]:
            if j+k<n:
                ans += score[t[j+k]]
            j += 2*k
        else:
            ans += score[t[j]]
            j += k
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
d = list(map(int, input().split()))
t = input()
ans = 0
lis = [-1] * n
for i in range(n):
    if t[i] == 'r':
        ans += d[2]
        val = 2
    if t[i] == 's':
        ans += d[0]
        val = 0
    if t[i] == 'p':
        ans += d[1]
        val = 1
    lis[i] = val
    if i >= k:
        if lis[i-k] == val:
            ans -= d[val]
            lis[i] = -1
print(ans)

Submission status: Accepted

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

hand = {"r":p,"s":r,"p":s}
ans = 0
for i in range(n):
    if a[i-k] == a[i] and i-k >= 0:
        a[i] = "0"
    else:
        ans += hand[a[i]]
print(ans)

Submission status: Accepted

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

    hands = {"r": p, "s": r, "p": s}
    ans = 0
    skip = [0]*n
    for i in range(n):
        if skip[i] == 1:
            continue

        if i+k > n-1:
            ans += hands[ts[i]]

        else:
            if ts[i] != ts[i+k]:
                ans += hands[ts[i]]
            else:
                ans += hands[ts[i]]
                skip[i+k] = 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

hand={"r":p,"s":r,"p":s}
ans=0
for i in range(n):
    if a[i-k]==a[i] and i-k>=0:
        a[i]="0"
    else:
        ans+=hand[a[i]]
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
l=['']*k
for i in range(n):
  l[i%k]+=s[i]
ans=0
d={'s':a,'p':b,'r':c}
for i in range(k):
  x=l[i]
  now=x[0]
  memo=0
  for j in range(len(x)):
    if x[j]==now:
      memo+=1
    else:
      ans+=(max(1,(memo+1)//2))*d[now]
      now=x[j]
      memo=1
  ans+=(max(1,(memo+1)//2))*d[now]
print(ans)

Submission status: Accepted

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

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

lines = [""] * K
for ix in range(len(T)):
    lines[ix%K] += T[ix]

ans = 0
for l in lines:
    before = ""
    for c in l:
        if before == c:
            before = ""

        else:
            ans += mp_score[c]
            before = c

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
V = {'r':P, 's':R, 'p':S}
ans = 0
 
 
for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Submission status: Accepted

Solution code:
n,k=[int(x) for x in input().rstrip().split()]
r,s,p=[int(x) for x in input().rstrip().split()]
t=input()
dp=["" for x in range(n)]
ans=0
for i in range(k):
  if t[i]=="r":
    ans+=p
    dp[i]="p"
  elif t[i]=="p":
    ans+=s
    dp[i]="s"
  else:
    ans+=r
    dp[i]="r"


for i in range(k,n):
  if t[i]=="r":
    if dp[i-k]!="p":
      ans+=p
      dp[i]="p"
  elif t[i]=="p":
    if dp[i-k]!="s":
      dp[i]="s"
      ans+=s
  else:
    if dp[i-k]!="r":
      dp[i]="r"
      ans+=r
print(ans)

Submission status: Accepted

Solution code:
def main():
    ans = 0
    mt = ""
    for i in range(N):
        if i - K >= 0:
            if mt[i-K] == win[T[i]][0]:
                if i+K < N and win[T[i+K]][0] == T[i]:
                    mt += lose[T[i]]
                else:
                    mt += T[i]
            else:
                mt += win[T[i]][0]
                ans += win[T[i]][1]
        else:
            mt += win[T[i]][0]
            ans += win[T[i]][1]
    print(ans)


if __name__ == "__main__":
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    win = {"r": ("p", P), "s": ("r", R), "p": ("s", S)}
    lose = {"r": "s", "s": "r", "p": "r"}
    T = input()
    main()

Submission status: Accepted

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

ans=0
prev=['']*k
for i in range(n):
    if t[i]!=prev[i%k]:
        prev[i%k]=t[i]
        if t[i]=='r':
            ans+=p
        elif t[i]=='p':
            ans+=s
        elif t[i]=='s':
            ans+=r
    else:
        prev[i%k]=''
print(ans)

Submission status: Accepted

Solution code:
N,K=list(map(int,input().split()))
R,S,P= list(map(int,input().split()))
T= list(input())
lock_list=[""] * K
ans=0
dic={"r":"p","p":"s","s":"r"}
for i,j in enumerate(T):
   if lock_list[i%K] != dic[j]:
      lock_list[i%K] = dic[j]
      if j == "r":
         ans+=P
      elif j == "s":
         ans+=R
      else:
         ans+=S
   else:
      try:
         lock_list[i%K] = ""
      except IndexError:
         pass
print(ans)

Submission status: Accepted

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

#kT = [''] * K
kT = {}
points = {'r': P, 's': R, 'p': S}

for i in range(N):
  #kT[i % K] += T[i]
  if i % K not in kT.keys():
    kT[i % K] = list(T[i])
  else:
    kT[i % K].append(T[i])
    
ans = 0
  
for i in kT.keys():
  for j in range(len(kT[i])):
    if j == 0:
      ans += points[kT[i][j]]
    else:
      if kT[i][j-1] == kT[i][j]:
        kT[i][j] = 'X'
      else:
        ans += points[kT[i][j]]
        
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**5)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    N, K = geta(int)
    R, S, P = geta(int)
    T = gete()

    def winner(c):
        return (R, 0, 0) if c == "s" else (0, P, 0) if c == 'r' else (0, 0, S)

    def fn(s):
        n = len(s)
        ret = 0
        dp = [[0] * 3 for _ in range(n)]

        dp[0] = winner(s[0])
        for i in range(1, n):
            w = winner(s[i])
            for j in range(3):
                dp[i][j - 2] = max(dp[i - 1][j - 1], dp[i - 1][j]) + w[j - 2]

        return (max(dp[n - 1]))

    print(sum(fn(T[i::K]) for i in range(K)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
rsp = [r, s, p]
T = input()
S = ''
a = [True]*n

for t in T:
    if t == 'r':
        S += '2'
    elif t == 's':
        S += '0'
    else:
        S += '1'

ans = 0
for i in range(n):
    if not a[i]:
        continue
    if i < n-k:
        if S[i] == S[i+k]:
            a[i+k] = False
    ans += rsp[int(S[i])]

print(ans)

Submission status: Accepted

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

for j in range(k, len(t)):
    if t[j] == t[j-k]:
        t[j] = ''
ts = collections.Counter(t)
print(ts['p']*s + ts['s']*r + ts['r']*p)