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:
from collections import deque,Counter

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

T = T.translate(str.maketrans({'r': 'p', 's': 'r','p': 's'}))
cnt = Counter(T)
dic = {"r":R, "s":S, "p":P}

ans = cnt["r"]*R + cnt["s"]*S +cnt["p"]*P
checked = []
for i in range(N-K+1):
    if i%K in checked:
        continue
    else:
        j = i
        tmp = 0
        while j <len(T):
            if tmp == T[j]:
                ans -= dic[T[j]]
                tmp = 0
            else:
                tmp = T[j]
            j += K
        checked.append(i%K)

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T= input()
def score(x):
  if x == 'r':
    return P
  elif x == 's':
    return R
  elif x == 'p':
    return S
  else:
    return 0
s = 0
List = [x for x in T]
for t in range(len(T)-K):
  if List[t] == List[t+K]:
    List[t+K] = 'z' 
for y in List:  
  s += score(y)
print(s)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
judge = [False] * n
t = input().strip()
for i in range(n - k):
    if t[i] == t[i + k] and (not judge[i]):
        judge[i + k] = True
ans = 0
for i in range(n):
    if judge[i]:
        continue
    else:
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        elif t[i] == 'p':
            ans += s
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
d={"r":p,"s":r,"p":s}
s=input()
memo=s[:k]
ans=0
for i,j in enumerate(s):
    if i<k:
        ans+=d[j]
    else:
        if memo[0]!=j:
            ans+=d[j]
            memo=memo[1:]+j
        else:
            memo=memo[1:]+"#"
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
# import math
N, K = [int(x) for x in stdin.readline().rstrip().split()]
R, S, P = [int(x) for x in stdin.readline().rstrip().split()]
s = input()
ans = 0
d = {'r': P, 's': R, 'p': S}
# print(d)
temp = [0 for i in range(len(s))]
for i, v in enumerate(s):
    # print(i, v, temp)
    if i < len(s)-K and temp[i] != 1:
        if s[i+K] == s[i]:
            ans += d[s[i]]
            temp[i+K] = 1
        else:
            ans += d[s[i]]
    else:
        if temp[i] != 1:
            ans += d[s[i]]
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
#rのときpの点、sのときｒ、ｐのときｓ

from collections import Counter

taka = ''
ans = 0


for te in t[:k]:
    if te == 'r':
        taka += 'p'
        ans += p
    elif te == 's':
        taka += 'r'
        ans += r
    else:
        taka += 's'
        ans += s

for i in range(k,len(t)):    
    if t[i] == 'r':
        if taka[i-k]!='p':
            taka += 'p'
            ans += p
        else:
            taka += 'q'
    elif t[i] == 's':
        if taka[i-k]!='r':
            taka += 'r'
            ans += r
        else:
            taka += 'q'
    else:
        if taka[i-k]!='s':
            taka += 's'
            ans += s
        else:
            taka += 'q'


print(ans)

Submission status: Accepted

Solution code:
N, K = [int(_) for _ in input().split()]
R, S, P = [int(_) for _ in input().split()]
T = input()
h = []
s = 0
for i, t in enumerate(T):
    if t == 'r':
        if i + 1 > K and h[i - K] == 'p':
            h.append('a')
            continue
        s += P
        h.append('p')
    elif t == 's':
        if i + 1 > K and h[i - K] == 'r':
            h.append('a')
            continue
        s += R
        h.append('r')
    elif t == 'p':
        if i + 1 > K and h[i - K] == 's':
            h.append('a')
            continue
        s += S
        h.append('s')
print(s)

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split())
    r,s,p = map(int,input().split())
    t = list(input())
    res = 0
    dic = {'r':p,'s':r,'p':s}

    for i in range(n):
        if i < k or t[i-k] != t[i]:
            res += dic[t[i]]
        else:
            t[i] = ' '
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

score = 0
for i in range(K):
    score += scoredict[T[i]]
    last = T[i]
    while True:
        i += K
        if i >= N:
            break
        if T[i] == last:
            last = None
            continue
        else:
            score += scoredict[T[i]]
            last = T[i]

print(score)

Submission status: Accepted

Solution code:
# D - Prediction and Restriction

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = [t for t in str(input())]
U = [''] * N
point = 0

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

print(point)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
R,S,P=map(int,input().split())
s=input()
c=Counter(s)
ans=c["r"]*P+c["s"]*R+c["p"]*S
for _ in range(k):
  i=0
  while (_+(i+1)*k)<n:
    if s[_+i*k]==s[_+(i+1)*k]:
      if s[_+(i+1)*k]=="r":
        ans-=P
        i+=1
      elif s[_+(i+1)*k]=="s":
        ans-=R
        i+=1
      elif s[_+(i+1)*k]=="p":
        ans-=S
        i+=1
    i+=1     
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
win = []
ans = 0
for i in range(n):
    if t[i] == "r":
        win.append("p")
    elif t[i] == "s":
        win.append("r")
    else:
        win.append("s")
for i in range(n):
    if i >= k:
        if win[i] == win[i - k]:
            if i + k >= n:
                pass
            elif win[i + k] != t[i]:
                win[i] = t[i]
            else:
                if win[i] != r and [i + k] != r:
                    win[i] = r
                elif win[i] != s and [i + k] != s:
                    win[i] = s
                elif win[i] != p and [i + k] != p:
                    win[i] = p
        else:
            if win[i] == "r":
                ans += r
            elif win[i] == "s":
                ans += s
            else:
                ans += p
    else:
        if win[i] == "r":
            ans += r
        elif win[i] == "s":
            ans += s
        else:
            ans += p
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
t = list(t)
max_score = 0
max_score += t.count("r") * p
max_score += t.count("s") * r
max_score += t.count("p") * s

for i in range(k,n):
  if t[i] == t[i-k]:
    if t[i] == "p":
      max_score -= s
    if t[i] == "r":
      max_score -= p
    if t[i] == "s":
      max_score -= r
    t[i] = "f"
    
print(max_score)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May  9 14:34:06 2020

@author: shinba
"""

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
    else:
        ans += s

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

print(ans)

Submission status: Accepted

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

dpp=[0]*(n+1)
dpw=[0]*(n+1)

for i in range(1, k+1):
  dpw[i]=1
  if t[i-1]== "r":
    dpp[i]=dpp[i-1]+p
  elif t[i-1]== "s":
    dpp[i]=dpp[i-1]+r
  else:
    dpp[i]=dpp[i-1]+s

for i in range(k+1, n+1):
  if dpw[i-k]==0:
      dpw[i] = 1
      if t[i-1]== "r":
        dpp[i]=dpp[i-1]+p
      elif t[i-1]== "s": 
        dpp[i]=dpp[i-1]+r
      else:
        dpp[i]=dpp[i-1]+s
  else:
      if t[i-k-1]==t[i-1]:
        dpw[i]=0
        dpp[i]=dpp[i-1]
      else:
        dpw[i]=1
        if t[i-1]== "r":
          dpp[i]=dpp[i-1]+p
        elif t[i-1]== "s":
          dpp[i]=dpp[i-1]+r
        else:
          dpp[i]=dpp[i-1]+s

print(dpp[n])

Submission status: Accepted

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

    if T[i] == 's':
      score += R
      ans += 's'

    if T[i] == 'p':
      score += S
      ans += 'p'

  else:
    if T[i] != ans[i-K]:
      if T[i] == 'r':
        score += P
        ans += 'r'

      if T[i] == 's':
        score += R
        ans += 's'

      if T[i] == 'p':
        score += S
        ans += 'p'
    else:
      if i + K < N:
        if T[i] == T[i+K]:
          if T[i+K] == 'r':
            ans += 'p'
          if T[i+K] == 's':
            ans += 'r'
          if T[i+K] == 'p':
            ans += 's'

        else:
          if T[i+K] == 'r':
            ans += 'p'
          if T[i+K] == 's':
            ans += 'r'
          if T[i+K] == 'p':
            ans += 's'

      else:
        ans += 'p'


print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n, k = ns()
    r, s, p = ns()
    dic_s = {"r": r, "s": s, "p": p}
    t = list(input())
    res = [0] * n
    dic = {"r": "p", "s": "r", "p": "s"}

    total = 0
    for i in range(n):
        if i - k > -1:
            if res[i - k] == dic[t[i]]:
                if i + k < n:
                    tmp = {dic[t[i]], dic[t[i + k]]}
                    for v in {"r","s","p"}:
                        if v not in tmp:
                            res[i] = v
                            break
                else:
                    continue

            else:
                res[i] = dic[t[i]]
                total += dic_s[res[i]]

        else:
            res[i] = dic[t[i]]
            total += dic_s[res[i]]

    print(total)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=(int(i) for i in input().split())
r,s,p=(int(i) for i in input().split())
dic={'r':r,'s':s,'p':p}
wazap={'r':'p','s':'r','p':'s'}
rail=input()
l=[[] for i in range(k)]
ans=0
for i in range(len(rail)):
  l[i%k].append(wazap[rail[i]])
for i in range(k):
  flag=0
  ans+=dic[l[i][0]]
  for j in range(len(l[i])-1):
    if l[i][j+1]==l[i][j]:
      if flag==1:
        ans+=dic[l[i][j+1]]
        flag=0
      else:
        flag=1
    else:
      ans+=dic[l[i][j+1]]
      flag=0
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())
dic={'r':r,'s':s,'p':p}
wazap={'r':'p','s':'r','p':'s'}
rail=input()
l=[[] for i in range(k)]
ans=0
for i in range(len(rail)):
  l[i%k].append(wazap[rail[i]])
for i in range(k):
  flag=0
  ans+=dic[l[i][0]]
  for j in range(len(l[i])-1):
    if l[i][j+1]==l[i][j]:
      if flag==1:
        ans+=dic[l[i][j+1]]
        flag=0
      else:
        flag=1
    else:
      ans+=dic[l[i][j+1]]
      flag=0
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc149/tasks/abc149_d

def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

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


wins = [0] * N
ans = 0
for i in range(N):
    if i < K:
        ans += point[T[i]]
        wins[i] = 1
        continue
    if T[i] == T[i-K] and wins[i-K] == 1:
        continue
    else:
        ans += point[T[i]]
        wins[i] = 1

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 1:
        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:
import math

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

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

li = [0 for _ in range(n)]
for i in range(len(t)):
    wp = w[t[i]]
    if not (i - k >= 0 and li[i - k] == wp and t[i] == t[i - k]):
        li[i] = wp

print(sum(li))

Submission status: Accepted

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

scoredict = {'r':R, 's':S, 'p':P}
winning = {'r':'p', 's':'r', 'p':'s'}
loss = {'r':'s', 's':'p', 'p':'r'}

picks = []
score = 0
for i in range(N):
    current = T[i]
    pick = winning[current]
    if i - K >= 0 and pick == picks[i-K]:
        if i + K < N and current == winning[T[i + K]]:
            pick == loss[current]
        else:
            pick = current
    else:
        score += scoredict[pick]
    picks.append(pick)

print(score)

Submission status: Accepted

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


def f(x):
    if x == "r":
        return P
    if x == "s":
        return R
    if x == "p":
        return S_


for i in range(K):
    S[i].append(T[i])
for i in range(N - K):
    S[i % K].append(T[i + K])
c = 0

for i in range(len(S)):
    t = ""
    for j in range(len(S[i])):
        if t != S[i][j]:
            c += f(S[i][j])
        else:
            S[i][j] = ""
        t = S[i][j]
print(c)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import numpy as np

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

    ans = [[-1 for _ in range(n)] for _ in range(3)]

    # 0 -> グーrを出したとき
    # 1 -> ちょきs
    # 2 -> パーp

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

    ans = np.array(ans)
    print(ans[:, (-1*k):].max(axis=0).sum())


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

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

n, k = Is()
r, s, p = Is()
t = S()
flag = [False]*n
ans = 0
for i in range(n):
    if i >= k and t[i-k] == t[i] and not flag[i-k]:
        flag[i] = True
        continue
    if t[i] == 'r':
        ans += p
    elif t[i] == 's':
        ans += r
    else:
        ans += s

print(ans)

Submission status: Accepted

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

output = []
all_score = 0
for i in range(N):
    if T[i] == "r":
        point = P
        te = "r"
    elif T[i] == "s":
        point = R
        te = "s"
    elif T[i] == "p":
        point = S
        te = "p"
    if i-K >=0:
        if output[i-K] == te:
            point = 0
            te = "None"
    all_score += int(point)
    output.append(te)

print(all_score)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

R,S,P=t.count("s"),t.count("p"),t.count("r")
ans=r*R+s*S+p*P
l=[0]*n

for i in range(n-k):
    if t[i]==t[i+k]:
        if l[i]==0:
            if t[i]=="r":
                ans-=p
            elif t[i]=="s":
                ans-=r
            else:
                ans-=s
            l[i+k]=1
        else:
            pass
        
print(ans)

Submission status: Accepted

Solution code:
#　149D
n, k = map(int, input().split())
point={}
point['s'],point['p'],point['r'] = map(int, input().split())
s = input()

ans =0
before = [0]*k
for i in range(len(s)):
    if i >= k:
        if (s[i] == s[i-k]) & (before[i%k]==1):
            before[i%k] = 0
        else:
            ans += point[s[i]]
            before[i%k] = 1
    else:
        ans += point[s[i]]
        before[i%k] = 1
print(ans)

Submission status: Accepted

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

ans = 0
h = []
for i in range(N):
    e = T[i]
    if i >= K and h[i-K] == hands[e]:
        h.append(None)
        continue

    if e == 'r':
        h.append('p')
        ans += P
    elif e == 'p':
        h.append('s')
        ans += S
    elif e == 's':
        h.append('r')
        ans += R

print(ans)

Submission status: Accepted

Solution code:
a, b, t = open(0)
n, k = map(int, a.split())
r, s, p = map(int, b.split())
d = {"r": ("p", p), "s": ("r", r), "p": ("s", s)}
command = [0]*n
point = 0
for i, s in enumerate(list(t.strip())):
  c, q = d[s]
  if i-k >= 0 and command[i-k] == c:
    continue
  command[i] = c
  point += q
print(point)

Submission status: Accepted

Solution code:
def main():
    N, K = (int(_) for _ in input().split())
    score = [int(_) for _ in input().split()]
    T = input()
    output = 0

    def win(i, j):
        if (T[i] == 'r' and j == 2) or (T[i] == 's' and j == 0) or (T[i] == 'p' and j == 1):
            return True
        else:
            return False

    for i in range(K):
        M = len(range(i, N, K))
        dp = [[0] * 3 for _ in range(M+1)]
        for j in range(M):
            for l1 in range(3):
                for l2 in range(3):
                    if l1 != l2:
                        cc = score[l1] if win(i + j * K, l1) else 0
                        dp[j+1][l1] = max(dp[j+1][l1], dp[j][l2] + cc)
        dp_max = -1
        for j in range(3):
            dp_max = max(dp_max, dp[M][j])
        output += dp_max
    print(output)
    return

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

def score(t):
    if t == 'r':return p
    if t == 's':return r
    else:return s

dp = [[0 for i in range(2)] for j in range(n + 1)]
for i in range(k):
    dp[i][0] = 0
    dp[i][1] = score(t[i])
    tmp = i
    for j in range(i + k,n,k):
        dp[j][0] = max(dp[j - k])
        if t[j] == t[j - k]:
            dp[j][1] = dp[j - k][0] + score(t[j])
        else:
            dp[j][1] = max(dp[j - k]) + score(t[j])
        tmp = j
    ans += max(dp[tmp])
print(ans)

Submission status: Accepted

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

n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = input()
win = {"r":2,"s":0,"p":1}
not_win = {"r":[0,1], "s":[1,2], "p":[0,2]}
not_win_i = {0:[1,2], 1:[0,2], 2:[0,1]}
point = {"r":p,"s":r,"p":s}

ans = 0
for i in range(k):
    plays = []
    for a in range(int(n/k)+1):
        if a*k+i < n:
            plays.append(a*k+i)
    #print(plays)
    dp = [[0] * 3 for _ in range(len(plays))]

    for j, play in enumerate(plays):
        if j == 0:
            f_play = t[plays[0]]
            dp[j][win[f_play]] += point[f_play]
        else:
            n_play = t[play]
            n0 = not_win[n_play][0]
            n1 = not_win[n_play][1]
            dp[j][win[n_play]] = max(dp[j-1][n0], dp[j-1][n1]) + point[n_play]
            dp[j][n0] = max(dp[j-1][not_win_i[n0][0]], dp[j-1][not_win_i[n0][1]])
            dp[j][n1] = max(dp[j-1][not_win_i[n1][0]], dp[j-1][not_win_i[n1][1]])
    #print(dp)
    ans += max(dp[-1])
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} #勝ちスコア

point = 0
for i in range(k): #kまでは普通に勝ってもよい
    point += d[t[i]]

for i in range(k, n): #kからnまでは、k個前と同じなら、後の方を無得点化する
    if t[i] == t[i - k]:
        t[i] = 'x'
        continue
    point += d[t[i]]

print(point)

Submission status: Accepted

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

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

point = {}
point["r"] = p
point["s"] = r
point["p"] = s

ans = 0
for i in range(k):
    j = 1
    count = 1
    letter = t[0+i]
    # print(letter)
    while(1):
        if j*k+i >= len(t):
            break

        letter_next = t[j*k+i]
        if letter == letter_next:
            count += 1
        else:
            # print((count+1//2)*point[letter])
            # print(count)
            ans += ((count+1)//2)*point[letter]
            count = 1
        letter = letter_next
        # print(letter)
        j += 1
    # print((count+1//2)*point[letter])
    # print(count)
    # print()
    ans += ((count+1)//2)*point[letter]

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

hand = []

for i in range(k):
    if t[i] == 'r':
        ans += p
        hand.append('p')
    elif t[i] == 's':
        ans += r
        hand.append('r')
    else:
        ans += s
        hand.append('s')

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

Submission status: Accepted

Solution code:
N,K = map( int ,input().split())
R,S,P = map( int ,input().split())
T = input()
A = list()
point = 0
for i in range(len(T)):
    if T[i] == 'r' :
        A.append('p')
    elif T[i] == 's':
        A.append('r')
    else:
        A.append('s')
#print(A)
point = A.count('p')*P + A.count('r')*R + A.count('s')*S
#print(point)
for j in range(len(A)-K):
    if A[j] == A[j+K]:
        if A[j] == 'r':
            A[j+K] = 'N'
            point -= R
        elif A[j] == 's':
            A[j+K] = 'N'
            point -= S
        else:
            A[j+K] = 'N'
            point -= P
#print(A)
print(point)

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}

point = 0
for i in range(k):
    point += d[t[i]]

for i in range(k, n):
    if t[i] == t[i - k]:
        t[i] = 'x'
        continue
    point += d[t[i]]

print(point)