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())

tmp = 1
mod = 1
score =0

while mod <= K:
    i = 0
    while i * K + mod - 1 < len(T):
        if (i + 1) * K + mod - 1 >= len(T):
            if T[i * K + mod - 1] == 'r':
                score += P
            elif T[i * K + mod - 1] == 's':
                score += R
            else:
                score += S
            i += 1
        else:
            if T[i * K + mod - 1] != T[(i + 1) * K + mod - 1]:
                if T[i * K + mod - 1] == 'r':
                    score += P
                elif T[i * K + mod - 1] == 's':
                    score += R
                else:
                    score += S
                i += 1
            else:
                if T[i * K + mod - 1] == 'r':
                    score += P
                elif T[i * K + mod - 1] == 's':
                    score += R
                else:
                    score += S
                i += 2
    mod += 1

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()
res = 0
for i in range(0,k):
    pre = True
    for j in range(0,n//k+1):
        index = j*k + i
        if index < n:
            if pre:
                if t[index] == 'r':
                    res += p
                elif t[index] == 's':
                    res += r
                else:
                    res += s
                pre = False
            else:
                if t[index] == t[index-k]:
                    pre = True
                else:
                    if t[index] == 'r':
                        res += p
                    elif t[index] == 's':
                        res += r
                    else:
                        res += s
print(res)

Submission status: Accepted

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

i = 0
adptl = []
res = 0
while i < N:
    if i-K >= 0 and T[i-K] == T[i] and adptl[i-K] == 1:
        i += 1
        adptl.append(0)
        continue
    res += score[T[i]]
    adptl.append(1)
    i += 1

print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    S, T = input().split()

    print(T+S)

    return

# B


def B():
    A, B, K = LI()
    temp = A-K
    if temp >= 0:
        print(temp, B)
        return
    B = B+temp
    if B <= 0:
        print(0, 0)
        return
    else:
        print(0, B)

    return

# C


def true():
    S = input()
    S = S[:-1]

    tmp = 0
    for x in range(1 << (len(S)-1)):
        func = S[-1]
        for i in range(len(S)-1):
            if(x & (1 << i)):
                tmp += int(func)
                func = S[-2-i]
            else:
                func = S[-2-i]+func
        tmp += int(func)
    print(tmp)

    return


def flagcounter(x):
    if (x == 0):
        return 0
    return flagcounter(x >> 1)+(x & 1)


def C():
    X = II()
    if X == 2:
        print(2)
        return
    while(1):
        if sosu(X):
            print(X)
            return
        X += 1

    return


# D
def D():
    N, K = LI()
    R, S, P = LI()
    T=list(input().rstrip())
    ans = 0
    for j in range(K):
        if T[j] == 'r':
            ans += P
        if T[j] == 'p':
            ans += S
        if T[j] == 's':
            ans += R
    j = 0
    rps=['r','p','s']

    for i in range(K, N):
        if T[j] == T[i]:
            if i+K>=N:
                j += 1
                continue
            T[i]=rps[rps.index(T[i+K])-1]
            j += 1
            continue
        if T[i] == 'r':
            ans += P
        if T[i] == 'p':
            ans += S
        if T[i] == 's':
            ans += R
        j += 1
    print(ans)
    return


def Dsyakutori():

    N, K = LI()
    A = LI()
    MAX = sum(A)
    if MAX < K:
        print(0)
        return
    Temp = MAX
    ans = 1
    for i in range(N):
        Temp = Temp-A[i]
        if (Temp >= K):
            ans += 1
        else:
            T = Temp+A[i]
            break
    Temp = T
    ckeep = N
    for k in range(N):
        c = N-1
        i -= 1
        Temp = T

        for j in range(N):
            Temp = Temp-A[c]
            if (Temp >= K):
                ans += 1

            elif(c < ckeep and i >= 0):
                ans += i
                ckeep = c
                break
            else:
                ckeep = c
                break
            c -= 1
        if i < 0:
            break
        T = T+A[i]
    print(ans)

    return

# E


def E():
    N, K = LI()
    A = LI()
    F = LI()
    A.sort(reverse=True)
    F.sort()
    cost = []
    for i in range(len(N)):
        cost.append(A[i]*F[i])

    a = F[cost.index(max(cost))]

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    N = len(S)

    pass


# Solve
if __name__ == '__main__':

    D()

Submission status: Accepted

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

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

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

win_hand = ''
for ti in t:
  win_hand += win[ti]

ans = 0
same_flag = [False]*n

for i in range(k):
  ans += point[win_hand[i]]

i=k
while i<n:

  if win_hand[i] == win_hand[i-k]:
    if not same_flag[i-k]:
      same_flag[i]=True
    else:
      ans += point[win_hand[i]]
  else:
    ans += point[win_hand[i]]
  i+=1

print(ans)

Submission status: Accepted

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

t1 = t.replace("r","P")
t2 = t1.replace("s","R")
t3 = t2.replace("p","S")

# print(t3)
T = list(t3)
# print(T)
minus = 0
ls = ["R","S","P"]
for i in range(k,n):
    if i < n-k:
        tmp = T[i]
        before = T[i-k]
        after = T[i+k]
        if tmp == before:
            ls.remove(tmp)
            if tmp == after:
                T[i] = ls[0]
            else:
                ls.remove(after)
                T[i] = ls[0] 
                ls.append(after)
            ls.append(tmp)
            if T[i] == "R":
                minus += r
            elif T[i] == "S":
                minus += s
            elif T[i] == "P":
                minus += p
        else:
            pass
    elif i >= n-k:
        tmp = T[i]
        before = T[i-k]
        # after = T[i+k]
        if tmp == before:
            ls.remove(tmp)
            T[i] = ls[0]
            ls.append(tmp)
            if T[i] == "R":
                minus += r
            elif T[i] == "S":
                minus += s
            elif T[i] == "P":
                minus += p
        else:
            pass

# print(T)
p1 = r * T.count("R")
# print(p1)
p2 = s * T.count("S")
# print(p2)
p3 = p * T.count("P")
# print(p3)
# print(minus)

print(p1+p2+p3-minus)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def cnt(str):
    r_num = str.count('r') - str.count('rr')
    s_num = str.count('s') - str.count('ss')
    p_num = str.count('p') - str.count('pp')
    return P*r_num + R*s_num + S*p_num
sum = 0
for i in range(K):
  str = ''
  for j in range(N//K+1):
    if i+K*j < N:
      str = str + T[i+K*j]
  sum = sum + cnt(str)
print(sum)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
rsp = list(map(int, input().split()))
t = input()
dp = [[0]*(n+1) for _ in range(3)]
d = {"r":0, "s":1, "p":2}
for i in range(1, n+1):
    now = d[t[i-1]]
    if i <= k:
        for j in range(3):
            if (now - j)%3 == 1:
                dp[j][i] = rsp[j]
    else:
        for j in range(3):
            c = 0
            for l in range(3):
                if l != j:
                    c = max(c, dp[l][i-k])
            dp[j][i] += c
            if (now - j)%3 == 1:
                dp[j][i] += rsp[j]
ans = 0
for i in range(n-k+1, n+1):
    c = 0
    for j in range(3):
        c = max(c, dp[j][i])
    ans += c
print(ans)

Submission status: Accepted

Solution code:
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = [0,0,0]
if N%K != 0:
  for _ in range(K - N%K):
    T = T + "W"
A = len(T)//K
r = [[] for _ in range(K)]
for i in range(K):
  for j in range(A):
    r[i].append(T[i + j*K])
for i in range(K):
  f = r[i]
  for j in range(len(f)-1):
    if f[j+1] == f[j]:
      f[j+1] = "W"
  for h in range(len(f)):
    if f[h] == "r":
      ans[2] += 1
    elif f[h] == "s":
      ans[0] += 1
    elif f[h] == "p":
      ans[1] += 1
a = ans[0]*R + ans[1]*S + ans[2]*P
print(a)

Submission status: Accepted

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

kanri=[]
for i in range(N):
   kanri.append(1)

score=0
for i in range(N):
   if i<K:
      if T[i]=="r":
         score+=P
      elif T[i]=="s":
         score+=R
      else:
         score+=S

   elif i>=K:
      if T[i]==T[i-K] and kanri[i-K]==1:
         kanri[i]=0
      elif T[i]==T[i-K] and kanri[i-K]==0:
         if T[i]=="r":
            score+=P
         elif T[i]=="s":
            score+=R
         else:
            score+=S 
      else:
         if T[i]=="r":
            score+=P
         elif T[i]=="s":
            score+=R
         else:
            score+=S      
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = [0] * n
for i, c in enumerate(t):
    if i >= k and c == t[i - k] and ans[i - k] > 0:
        continue
    if c == 'r':
        ans[i] = p
    elif c == 's':
        ans[i] = r
    else:
        ans[i] = s
print(sum(ans))

Submission status: Accepted

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

score = [[0, 0, 0] for i in range(N + 1)]

my_dict = {}

for i, your_hand in enumerate(T):
    i += 1

    if i <= K:
        if your_hand == "s":
            score[i][0] = R + max(score[i-1])
            score[i][1] = max(score[i-1])
            score[i][2] = max(score[i-1])
            my_dict[i % K] = "r"
        elif your_hand == "p":
            score[i][0] = max(score[i-1])
            score[i][1] = S + max(score[i-1])
            score[i][2] = max(score[i-1])
            my_dict[i % K] = "s"
        else:
            score[i][0] = max(score[i-1])
            score[i][1] = max(score[i-1])
            score[i][2] = P + max(score[i-1])
            my_dict[i % K] = "p"

    else:
        if your_hand == "s":
            if my_dict[i % K] == "r":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = R + max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = "r"
        elif your_hand == "p":
            if my_dict[i % K] == "s":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = max(score[i-1])
                score[i][1] = S + max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = "s"
        else:
            if my_dict[i % K] == "p":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = P + max(score[i-1])
                my_dict[i % K] = "p"

ans = max(score[N])
print(ans)

Submission status: Accepted

Solution code:
class Hand:

    def __init__(self):
        self.flag_r = False
        self.flag_s = False
        self.flag_p = False

    def set_flag(self,str,bool):

        if str == "r":
            self.flag_r = bool

        elif str == "s":
            self.flag_s = bool

        elif str == "p":
            self.flag_p = bool

    def get_flag(self,str):

        if str == "r":
            return self.flag_r

        elif str == "s":
            return self.flag_s

        elif str == "p":
            return self.flag_p

class Point:

    def __init__(self,r,s,p):
        self._R = r#ぐ
        self._S = s#ch
        self._P = p#pa

    def get_point(self,str):

        if str == "r":
            return self._P

        elif str == "s":
            return self._R

        elif str == "p":
            return self._S


if __name__ == '__main__':

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


    Hands = [Hand() for i in range(len(T))]
    Point = Point(R,S,P)
    ans = 0

    for i in range(len(T)):

        #じゃんけん
        if Hands[i].get_flag(T[i]) == False:
            #print(Point.get_point(T[i]))
            ans += Point.get_point(T[i])

            #k回目のflagがtrue
            try:
                Hands[i+K].set_flag(T[i],True)
            except:
                pass

        else:
            continue


    print(ans)

Submission status: Accepted

Solution code:
#!python3

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

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

    s = 0
    for i in range(K):
        s += score[T[i]]

    for i in range(K, N):
        if T[i-K] == T[i]:
            T[i] = ""
            continue
        s += score[T[i]]

    print(s)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
# 20:42
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
dic = {'r':p, 's':r, 'p':s}
t1 = ['']*n
score = 0
for i in range(k):
    score += dic[t[i]]
    t1[i] = t[i]
for i in range(k,n):
    if t[i] != t1[i-k]:
        score += dic[t[i]]
        t1[i] = t[i]
    else:
        t1[i] = 'x'
print(score)

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(K,N,1) :
    if T[i]==T[i-K]:
        T[i] = "NG"
        
R2=T.count("r")
S2=T.count("s")
P2=T.count("p")
print(P*R2+R*S2+S*P2)

Submission status: Accepted

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

################ DANGER ################
test = ""
#test = \
"""
5 2
8 7 6
rsrpr
ans 27
"""

"""
7 1
100 10 1
ssssppr
ans 211
"""

"""
30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr
ans 4996
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  
        
n, k = map(int, input2().split())
r, s, p = map(int, input2().split())
t = list(input2())

rsp = {"r", "s", "p"}

for i in range(k, n-1):
    if t[i] == t[i-k]:
        t[i] = rsp - {t[i], t[i+1]}
        
if t[n-1] == t[n-1-k]:
    t[n-1] = rsp - {t[n-1]}

ans = (t.count("r") * p +
       t.count("s") * r +
       t.count("p") * s)

print(ans)

Submission status: Accepted

Solution code:
temp = input().split()
n, k = int(temp[0]), int(temp[1])
temp = input().split()
r, s, p = int(temp[0]), int(temp[1]), int(temp[2])

t = input()
ans = ""
for i in range(k):
    l = [t[j] for j in range(i, len(t), k)]
    # print(l)
    txt = ''.join(l)
    txt = txt.replace("rr", "r*")
    txt = txt.replace("ss", "s*")
    txt = txt.replace("pp", "p*")
    ans += txt
    # print(ans)

print(p*ans.count('r') + r*ans.count('s') + s*ans.count('p'))

Submission status: Accepted

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

a1 = a.replace("r","P")
a1 = a1.replace("p","S")
a1 = a1.replace("s","R")
  
for j, i in enumerate(a1):
  if j < K:
    pass
  else:
    if i == a1[j-K]:
      a1 = a1[:j] + "n" + a1[j+1:]
    else:
      pass
    
point1 = a1.count("P") * P
point2 = a1.count("R") * R
point3 = a1.count("S") * S

ans =  point1 + point2 + point3

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()
F = [True] * len(T)
point = {"r": P, "s":R, "p":S}
ans = 0
for i in range(N):
    if i < K:
        ans += point[T[i]]
        F[i] = False
    # elif i+K <= len(T) - 1:
    else:
        if T[i] == T[i-K]:
            if F[i-K]:
                ans += point[T[i]]
                F[i] = False
            # else:
            #     F[i] = False
        else:
            ans += point[T[i]]
            F[i] = False
    # print(ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

prev = []
ans = 0
for _t in t:
    _p = ""
    if len(prev) >= k:
        _p = prev.pop(0)

    if _t == "r" and _p != "p":
        ans += p
        prev.append("p")
    elif _t == "s" and _p != "r":
        ans += r
        prev.append("r")
    elif _t == "p" and _p != "s":
        ans += s
        prev.append("s")
    else:
        prev.append("")

print(ans)

Submission status: Accepted

Solution code:
#D - Prediction and Restriction
# N,K = 30,5
# R,S,P=325,234,123
# T='rspsspspsrpspsppprpsprpssprpsr'

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

T=T.replace('r','P')
T=T.replace('s','R')
T=T.replace('p','S')

T_list = list(T)

for i in range(K,N):
    if T_list[i] == T_list[i-K]:
        T_list[i] = int(0)

for i in range(0,N):
    if T_list[i] == 'R':
        T_list[i] = R
    if T_list[i] == 'S':
        T_list[i] = S
    if T_list[i] == 'P':
        T_list[i] = P

print(sum(T_list))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
r,s,p=map(int,input().split())
t=[i for i in input()]
tmp=0
for i in range(K):
    if t[i]=='s':
        tmp+=r
    elif t[i]=='p':
        tmp+=s
    else:
        tmp+=p   
    #print(tmp)
    #print('制約なしで勝ち')
flg=0
for i in range(K,N):
    if t[i]!=t[i-K]:
        if t[i]=='s':
            tmp+=r
            #print('グーで勝ち')
        elif t[i]=='p':
            tmp+=s
            #print('チョキで勝ち↓')
        else:
            tmp+=p
            #print('パーで勝ち')
    else:
        t[i]='w'
        #print('かぶった（1回目）')
        
            
    #print(tmp)
#print('ans='+str(tmp))
print(tmp)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(str(input()))
ans = 0
for i in range(len(t)):
    #print(i)
    if i >= k:
        if t[i] == t[i-k]:
            t[i] = '0'
            continue
    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 = [int(i) for i in input().split(" ")]
R, S, P = [int(i) for i in input().split(" ")]
T = list(input())

d = {"r":"p", "s":"r", "p":"s"}
r_d = {"p":"r", "r":"s", "s":"p"}

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

t = [None]*len(T)
for i in range(len(T)):
    t[i] = d[T[i]]

pp = 0
c = {"r":1, "s": 1, "p": 1}
cont = False
a = list()
for i in range(len(t)):
    if i-K>=0:
        if t[i]==t[i-K]:
            if a[i-K]==t[i]:
                a.append("")
                continue
    a.append(t[i])
    pp+=p_d[t[i]]

print(pp)

Submission status: Accepted

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

    ans_dict = {"r":0, "s":0, "p":0}
    ix_list = [i for i in range(K)]

    for i in range(N):
        if i < K:
            ans_dict[T[i]] += 1
            #print(T[i])
        else:
            if T[i] == T[i-K]:
                T[i] = "t"
            else:
                ans_dict[T[i]] += 1
                #print(T[i])
    ans = (ans_dict["r"]*P) + (ans_dict["s"]*R) + (ans_dict["p"]*S)
    return ans

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

Submission status: Accepted

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

selected  = []
get_points = []

def win_method(b):
  if b == 'r':
    return 'p'
  elif b == 's':
    return 'r'
  elif b == 'p':
    return 's'

for a, t in enumerate(tt):
  win_this = win_method(t)
  selectable = {'r': r, 's': s, 'p': p}
  # k回前に出した手以外を選択
  if len(selected) >= k:
    bf = selected[-k]
    del selectable[bf]
  else:
    bf = ''
  # k回後に得られる点数より低い場合は次回に譲るようにする
  if a + k < n:
    nx = win_method(tt[a + k])
    if win_this != nx and bf != nx:
      del selectable[nx]
  if win_this in selectable:
    selected.append(win_this)
    get_points.append(selectable[win_this])
  else:
    selected.append(next(iter(selectable)))
    get_points.append(0)
  # print(a)
  # print(selected)
  # print(get_points)
    
print(sum(get_points))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n,k=MI()
    r,s,p=MI()
    t=input()
    ans=0
    for m in range(k):
        pc="a"
        for c in t[m::k]:
            if c==pc:
                pc="a"
                continue
            if c=="r":ans+=p
            if c=="s":ans+=r
            if c=="p":ans+=s
            pc=c
    print(ans)

main()

Submission status: Accepted

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

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

    for i in range(K):
        prev = ''
        for j in range(N//K+1):
            idx = j*K+i
            if idx >= N:
                break
            current = T[idx]
            if current != prev:
                score += scores[current]
                prev = current
            else:
                prev = ''
    print(score)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
scores = inpl()
T = input()
ctoi = {'r':0, 's':1, 'p':2}
ans = 0
for i in range(K):
    points = [0,0,0]
    j = i
    while j < N:
        en = ctoi[T[j]]
        points = [max(points[k] for k in ((c+1)%3,(c+2)%3)) + (scores[c] if (en-c) % 3 == 1 else 0) for c in range(3)]
        j += K
    ans += max(points)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = input()
cnt = 0
for i in range(k):
    lst = [t[i]]
    cont_cnt = 1
    for j in range(i+k, n, k):
        if t[j] == t[j-k]:
            cont_cnt += 1
        else:
            lst.append(cont_cnt)
            lst.append(t[j])
            cont_cnt = 1
    lst.append(cont_cnt)
    for j in range(0,len(lst), 2):
        if lst[j] == "r":
            cnt += (lst[j+1]+1) // 2 * p
        elif lst[j] == "s":
            cnt += (lst[j+1]+1) // 2 * r
        elif lst[j] == "p":
            cnt += (lst[j+1]+1) // 2 * s
print(cnt)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
tt=[]
ans=0
def score(c):
    if c=='r':
        return p
    if c=='s':
        return r
    if c=='p':
        return s


for i in range(n):
    if i-k>=0 and i+k>=n:
        if t[i]==t[i-k]:
            ans+=0
        else:
            ans+=score(t[i])
    elif i-k>=0:
        if t[i]==t[i-k]:
            if t[i]=='r':
                if t[i+k]!='s':
                    t=t[:i]+'s'+t[i+1:]
                else:
                    t=t[:i]+'p'+t[i+1:]
            elif t[i]=='s':
                if t[i+k]!='p':
                    t=t[:i]+'p'+t[i+1:]
                else:
                    t=t[:i]+'r'+t[i+1:]
            elif t[i]=='p':
                if t[i+k]!='r':
                    t=t[:i]+'r'+t[i+1:]
                else:
                    t=t[:i]+'s'+t[i+1:]
        else:
            ans+=score(t[i])
        
    else:
        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=input()
points=0
renzoku=[1 for i in range(k)]
for i in range(n):
	if i<k:
		if t[i]=='r':
			points+=p
		if t[i]=='s':
			points+=r
		if t[i]=='p':
			points+=s
	else:
		if t[i]==t[i-k]:
			renzoku[i%k]+=1
			if renzoku[i%k]%2==1:
				if t[i]=='r':
					points+=p
				if t[i]=='s':
					points+=r
				if t[i]=='p':
					points+=s
		else:
			renzoku[i%k]=1
			if t[i]=='r':
				points+=p
			if t[i]=='s':
				points+=r
			if t[i]=='p':
				points+=s
print(points)

Submission status: Accepted

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