Problem:


Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996



Sample input:
5 2
8 7 6
rsrpr


Sample output:
27

Submission status: Accepted

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

from collections import deque
que = deque([])

ans = 0
for i in range(n):
    if i >= k:
        pre = que.popleft()
        if t[i] == 'r':
            if pre == 'p':  # k回前の手のせいで勝てないとき
                que.append('')  # k回後に勝てるような手を出す
            else:
                que.append('p')
                ans += p
        elif t[i] == 's':
            if pre == 'r':
                que.append('')
            else:
                que.append('r')
                ans += r
        elif t[i] == 'p':
            if pre == 's':
                que.append('')
            else:
                que.append('s')
                ans += s 
    else:
        if t[i] == 'r':
            que.append('p')
            ans += p
        elif t[i] == 's':
            que.append('r')
            ans += r
        elif t[i] == 'p':
            que.append('s')
            ans += s
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
lr, ls, lp = -n, -n, -n
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" and t[i-k] != "r":
        ans += p
    elif t[i] == "s" and t[i-k] != "s":
        ans += r
    elif t[i] == "p" and t[i-k] != "p":
        ans += s
    else:
        t[i] = "2"

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, 'p': S, 's': R}
ans = 0

for i in range(K):
    flag = True

    while i < N:
        if i >= K and T[i] == T[i-K]:
            flag = not flag
        else:
            flag = True

        if flag:
            ans += score[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=list(input())
l=[]
for i in range(n):
  if t[i] == 'r':
    l.append(p)
  elif t[i] == 's':
    l.append(r)
  else:
    l.append(s)

score=sum(l)
for j in range(k,n):
  if t[j] == t[j-k]:
    t[j]='-'
    score-=l[j]

print(score)

Submission status: Accepted

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

ans_str = T.replace('r', 'a').replace('s', 'b').replace('p', 'c')
ans_str = list(ans_str.replace('a', 'p').replace('b', 'r').replace('c', 's'))

def rsp(arg):
    for x in 'rsp':
        if x not in arg: break
    return x

for i_str in range(K):
    idx = i_str
    while idx < len(ans_str):
        if idx > i_str:
            if ans_str[idx] == ans_str[idx - K]:
                if idx + K < len(ans_str):
                    ans_str[idx] = rsp((ans_str[idx - K], ans_str[idx + K]))
                else:
                    ans_str[idx] = rsp((ans_str[idx - K],))
        #
        idx += K

ans = 0
for ansi, Ti in zip(ans_str, list(T)):
    if ansi == 'r' and Ti == 's':
        ans += R
    if ansi == 's' and Ti == 'p':
        ans += S
    if ansi == 'p' and Ti == 'r':
        ans += P

print(ans)

Submission status: Accepted

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

def point(aite):
    if aite == "r":
        return p
    elif aite == "s":
        return r
    else:
        return s

ten = 0
kako = ["o"]*k
for i in range(n):
    if kako[i%k] == t[i]:
        kako[i%k] = "o"
    else:
        kako[i%k] = t[i]
        ten += point(t[i])

print(ten)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = {"r":p, "s":r, "p":s}
b = [1]*n
score = 0
for i in range(n):
  if i>k-1 and t[i]==t[i-k]:
    b[i] = 1-b[i-k]
  score += a[t[i]] if b[i]==1 else 0
print(score)

Submission status: Accepted

Solution code:
import math

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
M = math.ceil(N/K)

H = [T[i::K] for i in range(K)]
res = 0
for h in H:
    dp = [[0]*3 for _ in range(len(h))]
    if h[0] == "r":
        dp[0][2] = P
    elif h[0] == "s":
        dp[0][0] = R
    else:
        dp[0][1] = S

    for i in range(1, len(h)):
        dp[i][0] = max(dp[i-1][1], dp[i-1][2])
        dp[i][1] = max(dp[i-1][0], dp[i-1][2])
        dp[i][2] = max(dp[i-1][0], dp[i-1][1])

        if h[i] == "r":
            dp[i][2] += P
        elif h[i] == "s":
            dp[i][0] += R
        else:
            dp[i][1] += S

    res += max(dp[-1])

print(res)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
memo = [0] * N

def score(c):
    if c == "r":
        return P
    if c == "s":
        return R
    if c == "p":
        return S

ans = 0

for i in range(N):
    if i < K:
        ans += score(T[i])
        memo[i] = 1
        continue
    if T[i] == T[i - K]:
        if memo[i - K] == 0:
            ans += score(T[i])
            memo[i] = 1
    else:
        ans += score(T[i])
        memo[i] = 1

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
win_t=t.replace('r','P').replace('p','S').replace('s','R')

ans=''
cnt=0

for i in range(n):
  if i<k:
    ans+=win_t[i]
    continue
  if win_t[i]!=ans[i-k]:
    cnt+=1
    ans+=win_t[i]
  else:
    ans+='x'
    
print(ans.count('P')*p+ans.count('S')*s+ans.count('R')*r)

Submission status: Accepted

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

"""
Considering mod K, we may assume K = 1.
Thus we first consider this case.
"""
def func(hand):
    if hand == "r":
        return P
    elif hand == "s":
        return R
    elif hand == "p":
        return S
    else:
        return 0

def maxValue(lst):
    l = len(lst)
    score = 0
    prev = None
    repeat = 0

    for hand in lst:
        if prev != hand:
            if repeat % 2 == 1:
                score += func(prev) * (repeat + 1) // 2
            else:
                score += func(prev) * (repeat // 2 + 1)
            prev = hand
            repeat = 0
        else:
            repeat += 1
    if repeat % 2 == 1:
        score += func(lst[-1]) * (repeat + 1) // 2
    else:
        score += func(lst[-1]) * (repeat // 2 + 1)

    return score


ans = 0
for i in range(K):
    lst = []
    l = (N - 1 - i) // K
    for j in range(l + 1):
        lst.append(T[i + j * K])
    val = maxValue(lst)
    #print("list={}, value={}".format(lst, val))
    ans += val

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().split())
    T = list(map(str,input()))
    d = {"s":R,"p":S,"r":P}
    s = {"s":"r","p":"s","r":"p"}
    ans = 0
    x = []
    for i in range(K):
        ans += d[T[i]]
        x.append(s[T[i]])

    for i in range(N-K):
        if (T[i] == T[K+i] and x[-K] == s[T[K+i]]):
            x.append(T[K+i])
        else:
            ans += d[T[K+i]]
            x.append(s[T[K+i]])
            
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def win(t):
  if t == "g":
    return "p"
  if t == "c":
    return "g"
  if t == "p":
    return "c"

N, K = map(int, input().split())
G, C, P = map(int, input().split())
T = input().replace("r", "g").replace("s", "c")

point = {"g": G, "c": C, "p": P}

check = [None] * (N + K)
answer = 0
for i in range(N):
  w = win(T[i])
  if check[i] != w:
    answer += point[w]
    check[i + K] = w
    
print(answer)

Submission status: Accepted

Solution code:
#11:24
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = input()
raw = []
for i in range(n):
  if t[i] == 'r':
    raw.append(2)
  elif t[i] == 's':
    raw.append(0)
  else:
    raw.append(1)
ans = 0
result = []
for i in range(n):
  if i - k < 0:
    result.append(raw[i])
    ans += a[raw[i]]
  else:
    if raw[i] != result[i-k]:
      result.append(raw[i])
      ans += a[raw[i]]
    else:
      result.append(3)
print(ans)

Submission status: Accepted

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

for i in range(k,n):
    if flag[i-k] == t[i]:
        flag.append('')
        continue
    elif t[i] == 'r':
        flag.append('r')
        ans += p
    elif t[i] == 's':
        flag.append('s')
        ans += r
    elif t[i] == 'p':
        flag.append('p')
        ans += s

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
RSP = list(map(int, input().split()))
d = {'r': 0, 's': 1, 'p': 2}
T = input()

ans = 0
flags = [True] * K
for n in range(N):
    if flags[n % K]:
        ans += RSP[(d[T[n]] - 1) % 3]
        if n + K < N:
            if T[n] == T[n + K]:
                flags[n % K] = False
    else:
        flags[n % K] = True
print(ans)

Submission status: Accepted

Solution code:
args=input().split()
N=int(args[0])
K=int(args[1])
args=input().split()
R=int(args[0])
S=int(args[1])
P=int(args[2])
T=input()

resSS=[]
for i in T:
  if i=="r":
    resSS.append(P)
  elif i=="s":
    resSS.append(R)
  else:
    resSS.append(S)

if N>=K:
  for i in range(N-K):
    if T[i+K]==T[i] and resSS[i+K]==resSS[i]:
      resSS[i+K]=0
res=0
for i in resSS:
  res+=i
print(res)

Submission status: Accepted

Solution code:
N, K = (int(i) for i in input().split())  
R, S, P = (int(i) for i in input().split())  
T = str(input())
T = list(T)
max_w = max(R,S,P)*N
dp = [0] * (N+1)
dp_flg = [0] * (N+1)
def junk_win(x):
    if x == 'r':
        return('p')
    elif x == 's':
        return('r')
    else:
        return('s')
T_win = []
for i in range(N):
    T_win.append(junk_win(T[i]))
def get_w(x):
    if x == 'r':
        return(R)
    elif x == 's':
        return(S)
    else:
        return(P)
T_win_w = []
for i in range(N):
    T_win_w.append(get_w(T_win[i]))
for i in range(K):
    #print(i)
    dp[i] = dp[i-1] + T_win_w[i]
    dp_flg[i] = 1
    
for i in range(K,N):
    #print(i)
    if T_win[i]==T_win[i-K] and dp_flg[i-K]==1:#勝つために成約がある場合
        dp[i] = dp[i-1]
    else:
        dp[i] = dp[i-1] + T_win_w[i]
        dp_flg[i] = 1
print(dp[-2])

Submission status: Accepted

Solution code:
from itertools import groupby
n,k = [int(x) for x in input().split()]
r,s,p = [int(x) for x in input().split()]
T = input()
a = (n+k-1)//k
klist=[[""]*a for _ in range(k)]
for i in range(k):
  m=0
  klist[i] = T[i::k]
ans = 0
for i in range(k):
  t = list(klist[i])
  for key, value in groupby(t):
    if key=="r":
      ans+=(len(list(value))+1)//2*p
    if key=="s":
      ans+=(len(list(value))+1)//2*r
    if key=="p":
      ans+=(len(list(value))+1)//2*s
print(ans)

Submission status: Accepted

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

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

log = ["" for _ in range(k)] 
ans = 0
for i, play in enumerate(t):
    if play == log[i % k]:
        log[i % k] = ""
        continue
    log[i % k] = play
    ans += point[play]
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 rem in range(k):
    dp = [[0]*3 for _ in range(n//k + int(n%k > rem))]
    ind = 0
    for i in range(rem,n,k):
        dp[ind][0] = max(dp[ind-1][1], dp[ind-1][2]) + int(t[i] == 's') * r
        dp[ind][1] = max(dp[ind-1][2], dp[ind-1][0]) + int(t[i] == 'p') * s
        dp[ind][2] = max(dp[ind-1][0], dp[ind-1][1]) + int(t[i] == 'r') * p

        ind += 1
    ind -= 1
    ans += max(dp[ind][0], dp[ind][1], dp[ind][2])
print(ans)

Submission status: Accepted

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

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

#K回前に出した手を管理しておく
#iとi - Kは mod K が等しいため、K個前の手は mod K で表現できる
#出した手を全て記録する必要はなく、直近K個だけあれば十分
before = ["*" for _ in range(K)]

for i,now in enumerate(T):
  #K回前に出した手と、今勝てる手が違うのであれば
  #得点を足し、手を書き換える
  if before[i % K] != now:
    total += point[now]
    before[i % K] = now
  else:
    before[i % K] = "*"

print(total)

Submission status: Accepted

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

d = dict()
ans = 0
for i, move in enumerate(t):
    if move == "r" and d.get(i, 0) != "r":
        ans += p
        d[i+k] = d.get(i+k, "r")
    elif move == "s" and d.get(i, 0) != "s":
        ans += r
        d[i+k] = d.get(i+k, "s")
    elif move == "p" and d.get(i, 0) != "t":
        ans += s
        d[i+k] = d.get(i+k, "t")
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}
t = input()

ans = 0
prev = ["#" for _ in range(k)]
for i, move in enumerate(t):
    if prev[i%k] != move:
        ans += d[move]
        prev[i%k] = move
    else:
        prev[i%k] = "#"
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

n,k = map(int,input().split())
s = list(map(int,input().split()))
score = {"r":s[0], "s":s[1],"p":s[2], "n":0}
stronger = {"r":"p", "s":"r","p":"s"}
rsp = input()


d = deque()
[d.append("n") for i in range(k)]

ans = 0
for i in range(n):
    bk = d.popleft()
    st = stronger[rsp[i]]
    if st  != bk:
        ans += score[st]
        d.append(st)
    else:
        d.append("n")

print(ans)

Submission status: Accepted

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

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

flag = [0 for i in range(N)]

for i in range(N):
  if i < K or T[i] != T[i-K]:
    flag[i] = win(T[i])
  elif flag[i-K] == 0:
    flag[i] = win(T[i])

print(sum(flag))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    N, K = Scanner.map_int()
    R, S, P = Scanner.map_int()
    T = Scanner.string()
    ans = 0
    M = {"r": P, "s": R, "p": S}
    prevs = [""] * N
    for i in range(N):
        if 0 <= i - K:
            if T[i] == prevs[i-K]:
                continue
        ans += M[T[i]]
        prevs[i] = T[i]
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i, s in enumerate(input()):
    d[i%K].append(s)

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

for l in d:
    prev_hand = "a"
    for s in l:
        if s!=prev_hand:
            ans += score[s]
            prev_hand = s
        else:
            prev_hand = "a"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout
rock = 0
scissor = 1
paper = 2

def win(i, j):
    if j == 'r' and i == paper:
        return True
    if j == 's' and i == rock:
        return True
    if j == 'p' and i == scissor:
        return True
    return False

N, K = map(int, stdin.readline().split())
prize = list(map(int, stdin.readline().split())) # rock, scissors, paper

T = stdin.readline()
length = len(T)
dp = [[0 for _ in range(3)] for _ in range(length)]

ans = 0
for i in range(K):
    for j in range(i, length, K):
        for k in range(3):
            if j >= K:
                dp[j][k] = max(dp[j-K][(k+1)%3], dp[j-K][(k+2)%3])
            if win(k, T[j]):
                dp[j][k] = dp[j][k] + prize[k]
        if j+K >= length:
            ans = ans + max(dp[j][0], dp[j][1], dp[j][2])

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

n,k = map(int, input().split())
r,s,p = map(int, input().split())
p = {'s': r, 'p':s, 'r':p}
T = input()

kk = [[] for _ in range(k)]
for i in range(n):
	kk[i % k].append(T[i])
ans = 0
for i in range(len(kk)):
	ans += p[kk[i][0]]
	j = 1
	while j < len(kk[i]):
		pj = kk[i][j-1]
		if(kk[i][j]==pj):
			if(j+1 < len(kk[i])):
				nj = kk[i][j+1]
				ans += p[kk[i][j+1]]
				j+=2
			else : j+=1
		else:
			ans += p[kk[i][j]]
			j+=1

print(ans)

Submission status: Accepted

Solution code:
N,K = [int(i) for i in input().rstrip().split(' ')]
R,S,P = [int(i) for i in input().rstrip().split(' ')]
r = 0
T = input()
te = ["p","r","s"]
point = {"p":P,"r":R,"s":S}
Play = []
for i,t in enumerate(T):
    best = te[te.index(t)-1]
    if Play[i-K] != best if i >= K else True:
        Play += [best]
        r += point[best]
    else:
        Play += ["#"]
print(r)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

score_dict = {"r": p, "s":r, "p":s}
winning = []
ans = 0
for i, ch in enumerate(t):
    if i < k:
        winning.append(1)
        ans += score_dict[ch]
        continue
    if t[i-k] == ch and winning[i-k] == 1:
        winning.append(0)
    else:
        winning.append(1)
        ans += score_dict[ch]

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
}
point = [None for i in range(n)]
for i in range(len(t)):
    if point[i] == None:
        point[i] = d[t[i]]
        if i + k < len(t) and t[i] == t[i + k]:
            point[i + k] = 0
print(sum(point))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
RSP=list(map(int,input().split()))
RSP.append(0)
T=list(input())
A=[0]*N

for i in range(N):
  if T[i]=='r':
    A[i]=2
  elif T[i]=='s':
    A[i]=0
  else:
    A[i]=1

ans=0
for i in range(K):
  B=A[i::K]
  for j in range(1,len(B)):
    if B[j-1]==B[j]:
      B[j]=3
  for k in B:
    ans+=RSP[k]

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby as gb
n,k,g,t,p,s=map(lambda v:v if v[0] in "rsp" else int(v),open(0).read().split())
r,ss,pa=0,0,0
l=[""]*k
for x,i in enumerate(list(s)):
  if l[x%k]==i:l[x%k]="";continue
  if i=="r":pa+=1
  elif i=="s":r+=1
  else:ss+=1
  l[x%k]=i
print(r*g+ss*t+pa*p)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
point = list(map(int,input().split()))
t = str(input())
janken = ["s","p","r"]
ans = 0
iziwaru = [False for i in range(n)]
for i in range(n):
    if not iziwaru[i]:
        ans += point[janken.index(t[i])]
        if i + k < n:
            if t[i] == t[i+k]:iziwaru[i+k] = True
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
temp = t[:]
desirable = t[:]
for i in range(0, n):
  if t[i] == 'r':
    desirable[i] = 'p'
  elif t[i] == 's':
    desirable[i] = 'r'
  elif t[i] == 'p':
    desirable[i] = 's'
for i in range(0, k):
  if t[i] == 'r':
    temp[i] = 'p'
    ans += p
  elif t[i] == 's':
    temp[i] = 'r'
    ans += r
  elif t[i] == 'p':
    temp[i] = 's'
    ans += s
for i in range(k, n):
  if t[i] == 'r' and temp[i - k] != 'p':
    ans += p
    temp[i] = 'p'
  elif t[i] == 'r' and temp[i - k] == 'p':
    if i + k < n:
      if desirable[i + k] == 's':
        temp[i] = 'r'
      elif desirable[i + k] == 'r':
        temp[i] = 's'
      else:
        temp[i] = 's'
  if t[i] == 's' and temp[i - k] != 'r':
    ans += r
    temp[i] = 'r'
  elif t[i] == 's' and temp[i - k] == 'r':
    if i + k < n:
      if desirable[i + k] == 's':
        temp[i] = 'p'
      elif desirable[i + k] == 'p':
        temp[i] = 's'
      else:
        temp[i] = 's'
  if t[i] == 'p' and temp[i - k] != 's':
    ans += s
    temp[i] = 's'
  elif t[i] == 'p' and temp[i - k] == 's':
    if i + k < n:
      if desirable[i + k] == 'r':
        temp[i] = 'p'
      elif desirable[i + k] == 'p':
        temp[i] = 'r'
      else:
        temp[i] = 'r'
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
score_dict = dict(zip("spr", map(int, input().split())))
t = list(input())

score = sum([score_dict[h] for h in t[:k]])
for i in range(k, n):
    if t[i] != t[i - k]:
        score += score_dict[t[i]]
    else:
        t[i] = None

print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = dict(zip('spr', map(int, input().split())))
t = list(input())
r = sum(s[h] for h in t[0:k])
for i in range(k, n):
  if t[i-k] != t[i]:
    r += s[t[i]]
  else:
    t[i] = None
print(r)

Submission status: Accepted

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

# 勝つ手を選んだときはTrueになる。制約で出せなかったときにFalseに更新。
hands = [True] * N

scores = 0

for i in range(len(T)):
  if i >= K:
    # K回前と同じてを出したいのかかつ出せるのか判定
    if T[i-K] == T[i] and hands[i-K]:
      hands[i] = False
      continue
      
  if T[i] == "r":
    scores += P
  elif T[i] == "s":
    scores += R
  elif T[i] == "p":
    scores += S
    
print(scores)

Submission status: Accepted

Solution code:
def jyanken_win(enemy, r, s, p):
    score = 0
    if enemy == 's':
            score += r
    elif enemy == 'p':
            score += s
    elif enemy == 'r':
            score += p
    
    return score


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

    mine = []
    final_score = 0

    for i in range(n):
        if i >= k and mine[i-k] != 'not_win' and t[i-k] == t[i]: 
            mine.append('not_win')
        
        else:
            final_score += jyanken_win(t[i],r,s,p)
            mine.append('win')

    print(final_score)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#D
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=str(input()) ###相手
te=[]
score=0
for i in range(n):
  if i>=k and t[i]==t[i-k] and t[i]!=te[i-k]:###k回前に相手が同じ手を出して自分が勝っていたとき
    te.append(t[i]) ###あいこ
  else: #ほかは勝てる
    if t[i]=='r':
      score+=p
      te.append('p')
    elif t[i]=='s':
      score+=r
      te.append('r')
    elif t[i]=='p':
      score+=s
      te.append('s')
print(score)#print(i,score,t,te)

Submission status: Accepted

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

answer=0
oklist=[False]*K
for i in range(N):
    if oklist[i%K]:
        oklist[i%K]=False
        if T[i]=="r":
            answer+=P
        elif T[i]=="s":
            answer+=R
        else:
            answer+=S
        continue


    if i>=K and T[i]==T[i-K]:
        oklist[i%K]=True
        continue
    
    if T[i]=="r":
        answer+=P
    elif T[i]=="s":
        answer+=R
    else:
        answer+=S

print(answer)

Submission status: Accepted

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
r, s, p = map(int, sys.stdin.readline().split())
t = sys.stdin.readline()
ans = 0
for i in range(k, n):
  if t[i] == t[i-k]:
#    t[i] = "a"
    t = t[:i] + "a" + t[i+1:]
ans = t.count("r")*p + t.count("s")*r + t.count("p")*s

print(ans)

Submission status: Accepted

Solution code:
n,k = list(map(int,input().split(" ")))
s1,s2,s3 = list(map(int,input().split(" ")))
t = input()

scores = dict()
win = dict()
scores["r"] = s1
scores["s"] = s2
scores["p"] = s3

win["r"] = "p"
win["s"] = "r"
win["p"] = "s"

isWin = [False for _ in range(len(t))]

total = 0
for i in range(n):
    if i < k:
        total += scores[win[t[i]]]
        isWin[i] = True
    else:
        if t[i] != t[i-k]:
            total += scores[win[t[i]]]
            isWin[i] = True
        else:
            if not isWin[i-k]:
                total += scores[win[t[i]]]
                isWin[i] = True
            else:
                isWin[i] = False

print(total)