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:
MOVES = 'rsp'

def points(me, rival):
    if me == 'r' and rival == 's':
        return r
    if me == 's' and rival == 'p':
        return s
    if me == 'p' and rival == 'r':
        return p
    return 0

def pos(hand):
    return MOVES.index(hand)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().rstrip()
ret = 0
me = []
for i, rival in enumerate(t):
    if rival == 'r':
        if i - k >= 0 and me[i - k] == 'p':
            if i + k < n:
                if t[i + k] == 'p':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('s')
                elif t[i + k] == 'r':
                    me.append('s')
        else:
            me.append('p')
            ret += p
    elif rival == 's':
        if i - k >= 0 and me[i - k] == 'r':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('s')
                elif t[i + k] == 'p':
                    me.append('p')
                elif t[i + k] == 's':
                    me.append('s')
        else:
            me.append('r')
            ret += r
    elif rival == 'p':
        if i - k >= 0 and me[i - k] == 's':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('p')
                elif t[i + k] == 'p':
                    me.append('r')
        else:
            me.append('s')
            ret += s
print(ret)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
z_list = list(map(int,input().split()))
T = str(input())
sum = 0
aiko_flag=[0] * K

for i in range(N):
    if i > K-1:
        if T[i] == 'r' and (T[i-K] != 'r' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[2]
            aiko_flag[i % K] = 0
        elif T[i] == 's'and (T[i-K] != 's' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[0]
            aiko_flag[i % K] = 0
        elif T[i] == 'p' and (T[i-K] != 'p' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[1]
            aiko_flag[i % K] = 0
        else:
            aiko_flag[i % K] = 1
    elif i < K:
        if T[i] == 'r':
            sum = sum + z_list[2]
        elif T[i] == 's':
            sum = sum + z_list[0]
        elif T[i] == 'p':
            sum = sum + z_list[1]

print(sum)

Submission status: Accepted

Solution code:
import sys

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

n=int(n)
k=int(k)
r=int(r)
s=int(s)
p=int(p)

esp=list(t)

totalWin=0

for i in range(len(esp)):
    win=0
    if esp[i]=="r": #paper
        win=p
    elif esp[i]=="s": #rock
        win=r
    elif esp[i]=="p": #sisers
        win=s
    
    bef=(i-k)

    if bef>=0:
        if esp[bef]==esp[i]:
            win=0
            esp[i]="v"

    totalWin+=win

print(totalWin)
sys.exit()

Submission status: Accepted

Solution code:
from collections import deque

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

history = deque()
score = 0

def bestChoiceTo(op):
    if op == 'r':
        return 'p'
    if op == 's':
        return 'r'
    if op == 'p':
        return 's'

for i, op in enumerate(t):
    bestChoice = bestChoiceTo(op)
    futureBestChoice = '' if i + k >= n else bestChoiceTo(t[i + k])
    prohibitedChoice = '' if i < k else history.popleft()
    if bestChoice != prohibitedChoice:
        choice = bestChoice
        if choice == 'r': score += r
        if choice == 's': score += s
        if choice == 'p': score += p
    else:
        if futureBestChoice == prohibitedChoice:
            choice = 'x'
        else:
            choices = ['r', 's', 'p']
            if prohibitedChoice != '':
                choices.remove(prohibitedChoice)
            if futureBestChoice != '':
                choices.remove(futureBestChoice)
            choice = choices[0]
    history.append(choice)

print(score)

Submission status: Accepted

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

score_dic = {}
score_dic['r']=P
score_dic['s']=R
score_dic['p']=S

past_list = []

score = 0
for i in range(N):
    t = T[i]
    if i >= K:
#         print("ik:",i,K)
#         print("T,plist[i-k]",t,past_list[i-K])
        if past_list[i-K] != t:
            score += score_dic[t]
            past_list.append(t)
        else:
            past_list.append("n")
    else:
        score += score_dic[t]
        past_list.append(t)
#     print("i,t,score", i,t,score)
print(score)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
point=list(map(int,input().split()))
s=input()
t=[]
d={'r':0,'s':1,'p':2}
for x in s:
    t.append(d[x])
d=[]
for i in range(k):
    d.append(point[ (t[i]+2)%3 ])
    
for i in range(k,n):
    if t[i]==t[i-k] and d[i-k]!=0:
        d.append(0)
    else:
        d.append(point[ (t[i]+2)%3 ])
print(sum(d))

Submission status: Accepted

Solution code:
l = input().split()
n = int(l[0])
k = int(l[1])

l = input().split()
r = int(l[0])
s = int(l[1])
p = int(l[2])

t = list(input())

point = []

for i in range(n):
  if i >= k:
    if t[i] == t[i - k]:
      if point[i - k] != 0:
        point.append(0)
        continue

  if t[i] == "r":
    point.append(p)
  elif t[i] == "s":
    point.append(r)
  elif t[i] == "p":
    point.append(s)

print(sum(point))

Submission status: Accepted

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

A = []

for i in range(0, N):
    if i <= K-1:
        A.append(optimal[T[i]])
        ans = ans + point[A[i]]
    elif A[i-K] != optimal[T[i]]:
        A.append(optimal[T[i]])
        ans = ans + point[A[i]]
    elif i + K <= N-1:
        if optimal[T[i]] != 'r' and optimal[T[i+K]] != 'r':
            A.append('r')
        elif optimal[T[i]] != 's' and optimal[T[i+K]] != 's':
            A.append('s')
        else:
            A.append('p')
    else:
        if optimal[T[i]] != 'r':
            A.append('s')
        elif optimal[T[i]] != 's':
            A.append('p')
        else:
            A.append('r')

print(ans)

Submission status: Accepted

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

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    pre = [None] * n

    ans = 0
    for i, j in enumerate(t):
        if i < k or j != pre[i-k]:
            pre[i] = j
            if j == 'r':
                ans += p
            elif j == 's':
                ans += r
            else:
                ans += s
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
T_my = ['?' for i in range(N)]
ans = 0
same_flag = 0
# 0:K個前が違う手
# 1:K個前が同じ手，かつ，勝っている
# 2:K個前が同じ手，かつ，負けている

for i in range(N):
  if i < K: # K回目までは常に勝てる
    if T[i] == 'r':
      ans += P
      T_my[i] = 'p'
    if T[i] == 's':
      ans += R
      T_my[i] = 'r'
    if T[i] == 'p':
      ans += S
      T_my[i] = 's'
  else:
    if T[i] == 'r':
      T_my[i] = 'p'
    if T[i] == 's':
      T_my[i] = 'r'
    if T[i] == 'p':
      T_my[i] = 's'
    if T[i] == T[i-K]:
      if T_my[i-K] == T_my[i]:
        T_my[i] = '?'
      else:
        if T[i] == 'r':
          ans += P
        if T[i] == 's':
          ans += R
        if T[i] == 'p':
          ans += S
    else:
      if T[i] == 'r':
        ans += P
      if T[i] == 's':
        ans += R
      if T[i] == 'p':
        ans += S
        
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

i = 0
minus = 0
for i in np.arange(K):
    arr = T[np.arange(i, N, K)]
    c = 1
    for j in np.arange(arr.shape[0] - 1):
        if arr[j] == arr[j + 1]:
            c += 1
        else:
            if c >= 2:
                if arr[j] == 'r':
                    score = P
                elif arr[j] == 's':
                    score = R
                else:
                    score = S
                minus += c // 2 * score
            c = 1
    else:
        if c >= 2:
            if arr[j] == 'r':
                score = P
            elif arr[j] == 's':
                score = R
            else:
                score = S
            minus += c // 2 * score

counter = Counter(T)


max_score = 0
for i, j in counter.items():
    if i == 'r':
        max_score += P * j
    elif i == 's':
        max_score += R * j
    else:
        max_score += S * j
print(max_score - minus)

Submission status: Accepted

Solution code:
import math
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = 0
def calc_score(a):
    ret = 0
    if a == "s":
        ret = R
    elif a == "p":
        ret = S
    elif a == "r":
        ret = P
    return ret
    
for i in range(K):
    tmp = T[i::K]
    #print(T[i::K])
    score += calc_score(tmp[0])
    flag = True
    for j in range(1,len(tmp)):
        if tmp[j] == tmp[j - 1]:
            flag = not flag
        else:
            flag = True
        if flag:
            #print(calc_score(tmp[j]))
            score += calc_score(tmp[j])
print(score)

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 = input()
hands = []
a = ['r', 's', 'p']

def add(s):
    if s == 'r': return P
    if s == 's': return R
    else: return S
   
def win(s):
    if s == 'r': return 'p'
    if s == 's': return 'r'
    else: return 's'
         
point = 0
for i in range(N):
    if i < K: 
        hands.append(win(T[i]))
        point += add(T[i])
    else:
        if win(T[i]) != hands[i-K]:
            hands.append(win(T[i]))
            point += add(T[i])
        else:
            if i+K < N:
                a.remove(win(T[i]))
                if win(T[i+K]) != win(T[i]):
                    a.remove(win(T[i+K]))
                
                hands.append(a[0])
                a = ['r', 's', 'p']
            else:
                hands.append(T[i])

print(point)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
point = {"r": r, "s": s, "p": p}
v = {"r": "p", "s": "r", "p": "s"}
t = input()
ans = 0
for i in range(k):
    for j in range(i, n, k):
        if j == i:
            prev = v[t[j]]
            ans += point[v[t[j]]]
        else:
            if v[t[j]] != prev:
                ans += point[v[t[j]]]
                prev = v[t[j]]
            else:
                # rに勝つ手が出せない
                if t[j] == "r":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "s"
                        elif t[j + k] == "s":
                            prev = "s"
                        else:
                            prev = "r"
                elif t[j] == "s":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "s"
                        elif t[j + k] == "s":
                            prev = "s"
                        else:
                            prev = "p"
                elif t[j] == "p":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "r"
                        elif t[j + k] == "s":
                            prev = "p"
                        else:
                            prev = "r"
print(ans)
# dp = [{x: 0 for x in ["r", "s", "p"]} for y in range(n + 1)]
# v = {"r": "p", "s": "r", "p": "s"}
# for i in range(1, n + 1):
#     for j in ["r", "s", "p"]:
#         now_v = v[t[i - 1]]
#         if i < k + 1:
#             if j == now_v:
#                 dp[i][j] = max(dp[i - 1].values()) + 1
#             else:
#                 dp[i][j] = max(dp[i - 1].values())
#         else:
#             if j == now_v:
#                 if now_v == "r":
#                     dp[i][j] = max(dp[i - 1][j] + dp[i - k]["s"], dp[i - 1][j] + dp[i - k]["p"])
#                 elif now_v == "s":
#                     dp[i][j] = max(dp[i - 1][j] + dp[i - k]["r"], dp[i - 1][j] + dp[i - k]["p"])
#                 elif now_v == "p":
#                     dp[i][j] = max(dp[i - 1][j] + dp[i - k]["r"], dp[i - 1][j] + dp[i - k]["s"])
# print(dp)
# for i in dp:
#     print(i)

Submission status: Accepted

Solution code:
def GetPoint(a, b, p):
    if (a == 0 and b == 1) or (a == 1 and b == 2) or (a == 2 and b == 0):
        return p
    else:
        return 0


N, K = map(int, input().split())
G, C, P = map(int, input().split())
s = input()
A = [[] for i in range(K)]
for i in range(N):
    c = i % K
    if s[i] == 'r':
        A[c].append(0)
    elif s[i] == 's':
        A[c].append(1)
    else:
        A[c].append(2)

ans = 0

for S in A:
    l = len(S)
    dp = [[0 for i in range(3)] for i in range(l + 1)]
    for i in range(l):
        dp[i + 1][0] = max(dp[i][1], dp[i][2]) + GetPoint(0, S[i], G)
        dp[i + 1][1] = max(dp[i][0], dp[i][2]) + GetPoint(1, S[i], C)
        dp[i + 1][2] = max(dp[i][0], dp[i][1]) + GetPoint(2, S[i], P)

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

print(ans)

Submission status: Accepted

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

ll = []
for i in range(k):
    ll.append(t[i::k])

ans = 0
for l in ll:
    x = ""
    for v in l:
        if x != v:
            if v == "r":
                ans += p
            elif v == "s":
                ans += r
            else:
                ans += s
            x = v
        else:
            x = ""
            
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    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 i in range(K):
        prev = ''
        while i <= N - 1:
            if T[i] != prev:
                ans += score[T[i]]
                prev = T[i]
            else:
                prev = ''
            i += K
    print(ans)


main()

Submission status: Accepted

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

def score(s):
  if s == 'r':
    output = P
  elif s == 's':
    output = R
  else:
    output = S
  return output

k_list = [0]*K 
answer = 0
for i in range(K):
  s = T[i]
  ind = i%K
  k_list[ind] = s
  answer += score(s)
for j in range(K,N):
  s = T[j]
  ind = j%K
  if s == T[j-K] and k_list[ind] == s:
    k_list[ind] = 0
  elif s == T[j-K] and k_list[ind] != s:
    k_list[ind] = s
    answer += score(s)
  else:
    k_list[ind] = s
    answer += score(s)
print(answer)

Submission status: Accepted

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

st = ''
for i in t:
  if i == 'r':
    st += 'p'
  elif i == 'p':
    st += 's'
  else:
    st += 'r'

stt = st[:k]

for i in range(k,n):
  if st[i] != stt[i-k]:
    stt += st[i]
  else:
    stt += 'x'
    
sum = 0
for i in stt:
  if i == 'r':
    sum += r
  elif i == 's':
    sum += s
  elif i == 'p':
    sum += p
  else:
    pass
print(sum)

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

def win(m):
    if m == 0:
        return 2
    if m == 1:
        return  0
    if m == 2:
        return 1


N, K = MI()
RSP = LI()
RSP += [0]
T = input()

M = []
for t in T:
    if t == 'r':
        M.append(0)
    if t == 's':
        M.append(1)
    if t == 'p':
        M.append(2)

P = [win(m) for m in M]

for i in range(K, N):
    if P[i] == P[i - K]:
        P[i] = 3

PP = [RSP[p] for p in P]
print(sum(PP))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
raw = input()
new = []
for i in range(n):
  if raw[i] == 'r':
    new.append(0)
  elif raw[i] == 's':
    new.append(1)
  else:
    new.append(2)
#print(new)
ans = 0
for i in range(n):
  if i - k >= 0:
    if new[i] == new[i-k]:
      new[i] = 3
      #print(i,'c')
      continue
  if new[i] == 0:
    ans += p
  elif new[i] == 1:
    ans += r
  else:
    ans += s
  #print(i,ans)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=str(input())
li=[]
ans=0
for i in range(len(t)):
    li.append(t[i])
    if i<k:
        if li[i]=="r":
            ans+=p
        elif li[i]=="s":
            ans+=r 
        else:
            ans+=s 
    else:
        if li[i]==li[i-k]:
            li[i]=0
            ans+=0
        elif li[i]=="r":
            ans+=p
        elif li[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 = list(input())
for i in range(N-K):
    if T[i] == T[i+K]:
        T[i+K] = "@"
dic1 = {"r":P,"s":R,"p":S,"@":0}
points = 0
for i in range(N):
    points += dic1[T[i]]
print(points)

Submission status: Accepted

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

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    pre = ['x'] * n

    ans = 0
    for i, j in enumerate(t):
        if i < k or j != pre[i-k]:
            pre[i] = j
            if j == 'r':
                ans += p
            elif j == 's':
                ans += r
            else:
                ans += s
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()[:-1]

    res = 0
    A = {'r': P, 's': R, 'p': S}
    for k in range(K):
        tmp = A[T[k]]
        cnt = 1
        for i in range(k,N-K,K):
            # print([T[i], cnt])
            if i+K>=N:
                break
            if T[i]==T[i+K]:
                cnt += 1
            else:
                res += tmp * ((cnt + 1) // 2)
                tmp = A[T[i+K]]
                cnt = 1
            # print(res)  
        res += tmp * ((cnt + 1) // 2)

    print(res)    


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(n):
  
  if i < k:
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  elif i >= k and t[i] != t[i-k]:
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  elif i >= k and t[i] == t[i-k] and w[i-k] =='l':
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  else:
    w += 'l'
    
print(sc)

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(0,n):
  if t[i]=="r":
    ans+=p
  elif t[i]=="s":
    ans+=r
  elif t[i]=="p":
    ans+=s

for i in range(0,n-k):
  if t[i]==t[i+k]:
    t[i+k]="M"
    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())
t=input()

ja=[[] for i in range(k)]

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

sum=0
for i in range(k):
    for j in range(len(ja[i])):
        if j!=0 and ja[i][j-1]==ja[i][j]:
            c+=1
        else:
            c=1
        if c%2 ==1:
            if ja[i][j]=='r':
                sum+=p
            elif ja[i][j]=='s':
                sum+=r
            else:
                sum+=s

print(sum)

Submission status: Accepted

Solution code:
def get_reward(x, r, s, p):
    if x == "r":
        return p
    if x == "s":
        return r
    if x == "p":
        return s


def rotate(x):
    if x == "r":
        return "s"
    if x == "s":
        return "p"
    if x == "p":
        return "r"


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

total = 0

for i, x in enumerate(T):
    if i <= K-1:
        total += get_reward(x, R, S, P)
        continue
    if T[i-K] != x:
        total += get_reward(x, R, S, P)
        continue
    if N > i+K and T[i+K] == x:
        T[i] = rotate(x)


print(total)

Submission status: Accepted

Solution code:
#N = int(input())
N,K= map(int,input().split())
S,P,R= map(int,input().split())
T = list(input())
#v =list(map(int,input().split()))
# [list(map(int,input().split())) for i in range(N)]
res =[]
answer = T.count('s')*S+T.count('p')*P+T.count('r')*R

l = [0]*N
for i in range(K,N):
    if T[i] == T[i-K]:
        if T[i]==('s'):
            answer -= S
            l[i]=1
        elif T[i]==('p'):
            answer -= P
            l[i]=1
        elif T[i]==('r'):
            answer -= R
            l[i]=1

for j in range(K*2,N):
    if T[j] == T[j-K] and T[j] == T[j-K*2] and l[j-K]==1 and l[j-K*2]==0:
        if T[j]==('s'):
            answer += S
            l[j] = 0
        elif T[j]==('p'):
            answer += P
            l[j] = 0
        elif T[j]==('r'):
            answer += R
            l[j] = 0
print(answer)

Submission status: Accepted

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

d = {"s":r, "p":s, "r":p}
score = 0
for idx in range(k):
    List = []
    for i in range(n):
        if idx+i*k > n-1:
            break
        else:
           List.append(t[idx+i*k])
    not_liset = True
    tmp = List[0]
    score += d[tmp]
    for i in range(1, len(List)):
        if tmp == List[i] and not_liset:
            not_liset = False
        else:
            score += d[List[i]]
            not_liset = True
        tmp = List[i]
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
score = {'r':P, 's':R, 'p':S}
# rならP点、sならR点、pならS点
T = input()

rCount = T.count('r')
sCount = T.count('s')
pCount = T.count('p')
maxValue = rCount*P + sCount*R + pCount*S

if N <= K:
    print(maxValue)
else:
    A = []
    for i in range(K):
        A.append(T[i::K])
    
    for a in A:
        minus = {'r':0, 's':0, 'p':0}
        listA = list(a)
        for i in range(len(listA)-1):
            if listA[i] == listA[i + 1]:
                minus[listA[i]] += score[listA[i]]
                listA[i+1] = 'chenge'
        maxValue -= (minus['r'] + minus['s'] + minus['p'])

print(maxValue)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=[[] for i in range(k)]
for i in range(n):
    ans[i%k]+=[t[i]]
cnt=0
#print(ans)
for i in range(k):
    if ans[i][0]=="r":
        cnt+=p
        ans[i][0]="R"
    elif ans[i][0]=="s":
        cnt+=r
        ans[i][0]="S"
    elif ans[i][0]=="p":
        cnt+=s
        ans[i][0]="P"
    for j in range(1,len(ans[i])):
        if ans[i][j]=="r" and ans[i][j-1]!="R":
            cnt+=p
            ans[i][j]="R"
        elif ans[i][j]=="s" and ans[i][j-1]!="S":
            cnt+=r
            ans[i][j]="S"
        elif ans[i][j]=="p" and ans[i][j-1]!="P":
            cnt+=s 
            ans[i][j]="P"
#print(ans)
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(k,n) :
    if t[i] == t[i-k] :
        t[i] = 'x'
for i in range(n) :
    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:
def main():
    n,k=map(int,input().split())
    r,s,p=map(int,input().split())
    t=input()
    cnt = 0
    check=[0]*n
    for i in range(0,k,1):
        for j in range(i+k,n,k):
            if t[j] == t[j-k] and check[j-k] == 0 and check[j]==0:
                check[j] = 1


    i=0
    for tmp in t:
        if check[i] == 1:
            i+=1
            continue
        if tmp == 'r' :
            cnt += p
        elif tmp == 's':
            cnt+=r
        elif tmp == 'p':
            cnt+=s
        i+=1
    # for i in range(n-1,k-1,-1):
    #     if i >= k and t[i] == t[i-k]:
    #         if check[i-k] == 0 and check[i] == 0:
    #             check[i-k] = 1
    #         if check[i] == 1:
    #             if t[i] == 'r':
    #                 cnt -= p
    #                 print(i)
    #
    #             elif t[i] == 's':
    #                 cnt-=r
    #                 print(i)
    #             elif t[i] == 'p':
    #                 cnt-=s
    #                 print(i)

    print(cnt)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()
N, K = IL()
r, s, p = IL()
t = S()

judge = {'r':p,'s':r,'p':s}

ans = 0
for i in range(N):
  ans += judge[t[i]]

for n in range(K):
  tmp = ''
  cnt = 0
  for i in range(n,N,K):
    if t[i] == tmp:
      cnt += 1
    else:
      if tmp != '':
        ans -= judge[tmp] * (cnt//2)
      tmp = t[i]
      cnt = 1
  else:
    if tmp != '':
      ans -= judge[tmp] * (cnt//2)

print(ans)

Submission status: Accepted

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

cnt = [0] * N
for i in range(N):
    if t[i] == 'r':
        cnt[i] = P
    elif t[i] == 's':
        cnt[i] = R
    else:
        cnt[i] = S

for i in range(K, N):
    if t[i] == t[i-K]:
        t[i] = 'A'
        cnt[i] = 0
ans = sum(cnt)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = input()
P = {'r':P, 's':R, 'p':S}
score = [-1 for _ in range(N)]

for i,t in enumerate(T):
	if i < K:
		score[i] = P[t]
	else:
		if t == T[i-K] and score[i-K] != 0:
			score[i] = 0
		else:
			score[i] = P[t]
			
print(sum(score))

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()] # グー：r チョキ：s パー：p
t = list(input())

addscoremap = {'r': p, 's': r, 'p': s}

score = 0

for i in range(n):
  if (0 <= i-k) and (t[i] == t[i-k]):
    t[i] = 'x'
    continue
  score += addscoremap[t[i]]

print(score)

Submission status: Accepted

Solution code:
def p_d():
    N, K = map(int, input().split())
    *rsp, = map(int, input().split())
    li = {
        "r": 0,
        "s": 1,
        "p": 2
    }
    t = input()
    ans = 0
    for k in range(K):
        dp = [0] * 3
        for i in range(k, N, K):
            x = li[t[i]]
            NEXT = [0] * 3
            for p in range(3):
                NEXT[p] = max(dp[(p + 1) % 3], dp[(p - 1) % 3])
                if (p + 1) % 3 == x:
                    NEXT[p] += rsp[p]
            dp = NEXT
        ans += max(dp)
    print(ans)
    
p_d()

Submission status: Accepted

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

score = {'r':P,'s':R,'p':S}
ans = 0
for i in range(N):
    if i+K<=N-1:
        if a[i]==0 and T[i]==T[i+K]:
            a[i+K]=1
    if a[i]==0:
        ans+=score[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())
d=[]
ct=0
for i in range(k):
    #for m in range(k):
     #   if (n-1-m)%k==
    for j in range(i,n,k):
        d.append(t[j])
    for l in range(len(d)-1):
        if d[l]==d[l+1]:
            d[l+1]='out'
   # print(d,i,n,k)
    ct+=d.count('r')*p+d.count('s')*r+d.count('p')*s
    d=[]
    #print(ct)
    #ct+=

print(ct)    
#print(t.count("r")*r+)