Problem:


Score : 400 points

Problem Statement

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

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

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

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

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

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

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

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

Notes

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

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

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

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

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

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

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

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

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

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

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

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996



Sample input:
5 2
8 7 6
rsrpr


Sample output:
27

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())[: -1]
#print(T)
res = 0
for i in range(N):
  if T[i] == "r":
    res += p
  if T[i] == "s":
    res += r
  if T[i] == "p":
    res += s
#print(res)
for i in range(N - K):
  if T[i] == T[i + K]:
    T[i + K] = "x"
    if T[i] == "r":
      res -= p
    if T[i] == "s":
      res -= r
    if T[i] == "p":
      res -= s
print(res)

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):
    if T[i]=='p':
        T[i] = 's'
    elif T[i]=='s':
        T[i] = 'r'
    else:
        T[i] = 'p'
ans = []
for i in range(N):
    ref = '.'
    if i>=K:
        ref = ans[i-K]
    if T[i] != ref:
        ans.append(T[i])
    else:
        for s in 'rsp':
            if s != T[i]:
                if i+K>=N or s!=T[i+K]:
                    ans.append(s)
                    break
s = 0
for i in range(N):
    if ans[i]==T[i]:
        if ans[i]=='p':
            s += P
        elif ans[i]=='r':
            s += R
        else:
            s += S
print(s)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
D = {"r": 0, "s": 1, "p": 2}
T = [D[a] for a in input()]
X = [[0] * 3 for _ in range(N)]
for i in range(N):
    if i < K:
        X[i][T[i]] = 0
        X[i][T[i]-1] = A[T[i]-1]
        X[i][T[i]-2] = 0
    else:
        X[i][T[i]] = max(X[i-K][T[i]-1], X[i-K][T[i]-2])
        X[i][T[i]-1] = A[T[i]-1] + max(X[i-K][T[i]], X[i-K][T[i]-2])
        X[i][T[i]-2] = max(X[i-K][T[i]], X[i-K][T[i]-1])

print(sum([max(x) for x in X[-K:]]))

Submission status: Accepted

Solution code:
inputs = input().split(" ")
n = int(inputs[0])
k = int(inputs[1])
inputs = input().split(" ")
r = int(inputs[0])
s = int(inputs[1])
p = int(inputs[2])
t = input()
out = ""

result = 0

for i in range(0, k):
    if t[i] == 's':
        result += r
        out += 'r'
    elif t[i] == 'p':
        result += s
        out += 's'
    else:
        result += p
        out += 'p'

for i in range(k, n):
    tmp = 0
    tmp_hand = None
    if t[i] == 's':
        tmp += r
        tmp_hand = 'r'
    elif t[i] == 'p':
        tmp += s
        tmp_hand = 's'
    else:
        tmp += p
        tmp_hand = 'p'
    if tmp_hand != out[i-k]:
        result += tmp
        out += tmp_hand
    else:
        out += 'n'

print(result)

Submission status: Accepted

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

def best(string):
    dp = [[-1, -1, -1] for i in range(len(string))]
    dp[0][0] = R if string[0] == 's' else 0
    dp[0][1] = P if string[0] == 'r' else 0
    dp[0][2] = S if string[0] == 'p' else 0
    for i in range(1, len(string)):
        dp[i][0] = (R if string[i] == 's' else 0) + max(dp[i-1][1], dp[i-1][2])
        dp[i][1] = (P if string[i] == 'r' else 0) + max(dp[i-1][0], dp[i-1][2])
        dp[i][2] = (S if string[i] == 'p' else 0) + max(dp[i-1][0], dp[i-1][1])
    # print(string, dp)
    return max(dp[-1])

strings = []
for i in range(K):strings.append(T[i::K])

print(sum(map(best, strings)))

Submission status: Accepted

Solution code:
from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 1000001
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
    for i in range(2,MAXN,2):
        spf[i] = 2
    for i in range(3,int(MAXN**0.5)+1):
        if spf[i]==i:
            for j in range(i*i,MAXN,i):
                if spf[j]==j:
                    spf[j]=i
def fib(n,m):
    if n == 0:
        return [0, 1]
    else:
        a, b = fib(n // 2)
        c = ((a%m) * ((b%m) * 2 - (a%m)))%m
        d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
        if n % 2 == 0:
            return [c, d]
        else:
            return [d, c + d]

def charIN(x= ' '):
    return(sys.stdin.readline().strip().split(x))

def arrIN(x = ' '):
    return list(map(int,sys.stdin.readline().strip().split(x)))

def ncr(n,r):
    num=den=1
    for i in range(r):
        num = (num*(n-i))%MOD
        den = (den*(i+1))%MOD

    return (num*(pow(den,MOD-2,MOD)))%MOD

def flush():
    return sys.stdout.flush()

'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
n,k = arrIN()
r,s,p = arrIN()
d = {"r":r,"s":s,"p":p,"x":0}
t = input()
ans = [0]*(n)
for i in range(k):
    if t[i]=='r':
        ans[i] = "p"
    elif t[i]=='s':
        ans[i] = "r"
    else:
        ans[i] = "s"

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

Submission status: Accepted

Solution code:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict

sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

score = 0
myhands = []
for i, chara in enumerate(T):
    if i < K:
        if chara=='r':
            score += P
            myhands.append('p')
        elif chara=='s':
            score += R
            myhands.append('r')
        else:
            score += S
            myhands.append('s')
    else:
        if chara=='r':
            if myhands[i-K]!='p':
                score += P
                myhands.append('p')
            else:
                myhands.append('q')
        elif chara=='s':
            if myhands[i-K]!='r':
                score += R
                myhands.append('r')
            else:
                myhands.append('q')

        elif chara=='p':
            if myhands[i-K]!='s':
                score += S
                myhands.append('s')
            else:
                myhands.append('q')
print(score)

Submission status: Accepted

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

    ans = 0
    for i, t in enumerate(T):
        if i <= K - 1:
            if t == 'r':
                hand[i] = 'p'
                ans += P
            if t == 's':
                hand[i] = 'r'
                ans += R
            if t == 'p':
                hand[i] = 's'
                ans += S
        else:
            if t == 'r':
                if hand[i - K] == 'p':
                    continue
                else:
                    hand[i] = 'p'
                    ans += P
            if t == 's':
                if hand[i - K] == 'r':
                    continue
                else:
                    hand[i] = 'r'
                    ans += R
            if t == 'p':
                if hand[i - K] == 's':
                    continue
                else:
                    hand[i] = 's'
                    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()
d = {'r':P, 's':R, 'p':S}

ans = 0
for k in range(K):
    ban = True
    pre = '_'
    for i in range(k,N,K):
        if T[i] != pre:
            ans += d[T[i]]
            ban = True
        elif ban:
            ban = False
        else:
            ans += d[T[i]]
            ban = True
        pre = T[i]
print(ans)

Submission status: Accepted

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


RSP = ['r', 's', 'p']
ANS = ['p', 'r', 's']
ANS_SCORE = [P, R, S]
hands = []
ans = 0
for i in range(N):
    # K回前をきにする
    if i >= K:
        idx = RSP.index(T[i])
        if hands[-K] == ANS[idx]:
            hands.append('x')
        else:
            hands.append(ANS[idx])
            ans += ANS_SCORE[idx]
    else:
        idx = RSP.index(T[i])
        hands.append(ANS[idx])
        ans += ANS_SCORE[idx]
print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
r,s,p = LI()
t = input()
ans = 0
que = deque()
for i in range(n):
    if i < k:
        if t[i] == 'r':
            ans += p
            que.append('p')
        elif t[i] == 's':
            ans += r
            que.append('r')
        else:
            ans += s
            que.append('s')
    else:
        x = que.popleft()
        if t[i] == 'r' and x != 'p':
            ans += p
            que.append('p')
            continue
        elif t[i] == 's' and x != 'r':
            ans += r
            que.append('r')
            continue
        elif t[i] == 'p' and x != 's':
            ans += s
            que.append('s')
            continue
        if i+k < n:
            if t[i+k] == 'r':
                que.append('r')
            elif t[i+k] == 's':
                que.append('s')
            else:
                que.append('p')
        else:
            que.append(t[i])
print(ans)

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

import itertools

#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
def main():
    n,k = map(int, input().split())
    s,p,r=map(int, input().split())
    t=list(input())
    points=0
    for i in range(0,k):
        if t[i]=="r":
            points+=r
        elif t[i]=="s":
            points+=s
        else:
            points+=p
    for i in range(k,n):
        if t[i-k]==t[i]:
            t[i]="x"
        else:
            if t[i] == "r":
                points += r
            elif t[i] == "s":
                points += s
            else:
                points += p
    print(points)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
a = []
for i in range(n):
    if t[i] == 'r':
        tmp = p
        a.append('p')
    elif t[i] == 's':
        tmp = r
        a.append('r')
    elif t[i] == 'p':
        tmp = s
        a.append('s')
    ans += tmp

    if i >= k and a[i] == a[i - k]:
        ans -= tmp
        a.pop()
        a.append('x')

print(ans)

Submission status: Accepted

Solution code:
#D
import math
N,K = map(int,input().split())
R,S,P = map(int,input().split())

def get(st):
    if st == "r":
        return P
    if st == "s":
        return R
    if st == "p":
        return S

T = list(str(input()))
ans = 0
for j in range(K):
    klist = []
    for i in range(math.ceil((N-j)/K)):
        klist.append(T[j+i*K])
    
    klen = len(klist)
    old = 0
    for i in range(klen):
        if i < klen-1:
            if old == 0:
                if klist[i] != klist[i+1]:
                    ans += get(klist[i])
                else:
                    ans += get(klist[i])
                    old = 1
            else:
                old = 0
        else:
            if old == 0:
                ans+=get(klist[i])
    #print(klist)
                
print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.readline
N, K = map(int, read().split())
R, S, P = map(int, read().split())
T = input()
t = [0] * N

for i in range(N):
    s = T[i]
    if s == 'p':
        t[i] = S
    elif s == 's':
        t[i] = R
    else:
        t[i] = P

for i in range(K, N):
    if T[i] == T[i - K]:
        if t[i - K] != 0:
            t[i] = 0

print(sum(t))

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, readline().split()))
    r, s, p = list(map(int, readline().split()))
    t = str(readline().rstrip().decode('utf-8'))
    ans = []
    score = 0
    for i, v in enumerate(t):
        if v == "r":
            ans.append("p")
        elif v == "s":
            ans.append("r")
        else:
            ans.append("s")
    for i, v in enumerate(ans):
        if i >= k:
            if ans[i-k] == ans[i]:
                ans[i] = "x"
    cnt = 0
    for v in ans:
        if v == "r":
            cnt += r
        elif v == "s":
            cnt += s
        elif v == "p":
            cnt += p
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

last=['' for i in range(K)]

ans=0
for i,c in enumerate(T):
    rem=i%K
    if c=='r':
        if last[rem]!='r':
            ans+=P
            last[rem]=c
        else:
            last[rem]=''    
    elif c=='s':
        if last[rem]!='s':
            ans+=R
            last[rem]=c
        else:
            last[rem]=''
    elif c=='p':
        if last[rem]!='p':
            ans+=S
            last[rem]=c
        else:
            last[rem]=''

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    R,S,P=map(int,input().split())
    T=list(input())
    l=['']*N
    ans=0
    for i in range(N):
        if i<K:
            if T[i]=='r':
                l[i]='p'
                ans+=P
            elif T[i]=='p':
                l[i]='s'
                ans+=S
            else:
                l[i]='r'
                ans+=R
        else:
            if T[i]=='r':
                if l[i-K]=='p':
                    l[i]='z'
                else:
                    l[i]='p'
                    ans+=P
            elif T[i]=='p':
                if l[i-K]=='s':
                    l[i]='z'
                else:
                    l[i]='s'
                    ans+=S
            else:
                if l[i-K]=='r':
                    l[i]='z'
                else:
                    l[i]='r'
                    ans+=R
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve(n, k, r, s, p, t):
    res = 0
    u_list = [""] * n
    for i in range(k):
        if t[i] == "r":
            u_list[i] = "p"
            res += p
        elif t[i] == "s":
            u_list[i] = "r"
            res += r
        else:
            u_list[i] = "s"
            res += s

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


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


def test():
    assert solve(5, 2, 8, 7, 6, "rsrpr") == 27
    assert solve(7, 1, 100, 10, 1, "ssssppr") == 211
    assert solve(30, 5, 325, 234, 123, "rspsspspsrpspsppprpsprpssprpsr") == 4996


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

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

tot = 0
flag = [0]*K

for i in range(N):
    if T[i] == 'r':
        if i < K or T[i] != T[i-K]:
            tot += P
            flag[i%K] = 0
        elif flag[i%K] == 0:
            flag[i%K] = 1
        else:
            tot += P
            flag[i%K] = 0
    elif T[i] == 's':
        if i < K or T[i] != T[i-K]:
            tot += R
            flag[i%K] = 0
        elif flag[i%K] == 0:
            flag[i%K] = 1
        else:
            tot += R
            flag[i%K] = 0
    else:
        if i < K or T[i] != T[i-K]:
            tot += S
            flag[i%K] = 0
        elif flag[i%K] == 0:
            flag[i%K] = 1
        else:
            tot += S
            flag[i%K] = 0
print(tot)

Submission status: Accepted

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

score = [0 for i in range(n)]

for i in range(n):
    if k<=i:
        if t[i]==t[i-k]:
            if score[i-k]!=0:
                continue
    if t[i] == 'p':
        score[i] = s
    elif t[i] == 'r':
        score[i] = p
    else:
        score[i] = r

print(sum(score))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
for a in range(n-k):
    if t[k+a] == t[a]:
        t[k+a] = 0
x = t.count('r') * p + t.count('s') * r + t.count('p') * s
print(x)

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}
t = list(input())

ans = 0
for i in range(k):
    # print(i, score[t[i]], ans)
    ans += score[t[i]]
    j = i + k
    f = 0
    while j < n:
        if t[j] != t[j - k]:
            ans += score[t[j]]
            # print(j, score[t[j]], ans)
        else:
            t[j] = '-'
        j += k
    # print(ans)

print(ans)

Submission status: Accepted

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

dp = [[0 for i in range(3)] for j in range(n+1)]


ans = 0
for l in range(k):
    hand = []
    for j in range(l, n, k):
        hand.append(t[j])

    dp = [[0 for i in range(3)] for j in range(len(hand)+1)]

    for i in range(len(hand)):
        if hand[i] == "r":
            dp[i+1][2] = max(dp[i][0], dp[i][1]) + p
            dp[i+1][1] = max(dp[i][0], dp[i][2])
            dp[i+1][0] = max(dp[i][1], dp[i][2])

        elif hand[i] == "s":
            dp[i+1][2] = max(dp[i][0], dp[i][1])
            dp[i+1][1] = max(dp[i][0], dp[i][2])
            dp[i+1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i+1][2] = max(dp[i][0], dp[i][1])
            dp[i+1][1] = max(dp[i][0], dp[i][2]) + s
            dp[i+1][0] = max(dp[i][1], dp[i][2])
    ans += max(dp[len(hand)])

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

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

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

ans = 0
for i in range(K):
    A = T[i::K]
    ans += sum((len(tuple(v)) + 1) // 2 * C[k] for k, v in groupby(A))

print(ans)

Submission status: Accepted

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


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    memo = [""]*N

    for i in range(N):
        t = T[i]
        if t == "r":
            if i < K:
                memo[i] = "p" 
            else:
                if memo[i-K] == "p":
                    memo[i] = "."
                else:
                    memo[i] = "p"

        elif t == "s":
            if i < K:
                memo[i] = "r" 
            else:
                if memo[i-K] == "r":
                    memo[i] = "."
                else:
                    memo[i] = "r"
        else:
            if i < K:
                memo[i] = "s" 
            else:
                if memo[i-K] == "s":
                    memo[i] = "."
                else:
                    memo[i] = "s"
    score = 0

    for i in range(N):
        if memo[i] == "p":
            score+=P
        elif memo[i] == "s":
            score += S
        elif memo[i] == "r":
            score += R
        else:
            continue
    
    print(score)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    score = [R, S, P]

    ans = 0
    past = []
    for i, t in enumerate(T):
        if t == "r":
            cand = 2
        elif t == "s":
            cand = 0
        else:
            cand = 1
        if i - K >= 0 and past[i-K] == cand:
            ans += 0
            past.append(-1)
        else:
            ans += score[cand]
            past.append(cand)
    print(ans)
    # print(past)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


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


def solve(T):
    N = len(T)
    if N == 0:
        return 0
    # dp[i][c]: i 番目に c を出したときのそれまでのスコアの最大値
    dp = [[0, 0, 0] for _ in range(N)]
    dp[0][0] = R if T[0] == 's' else 0
    dp[0][1] = S if T[0] == 'p' else 0
    dp[0][2] = P if T[0] == 'r' else 0
    for i, cpu in enumerate(T[1:], start=1):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + R * int(cpu == 's')
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + S * int(cpu == 'p')
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + P * int(cpu == 'r')
    return max(dp[-1])


ans = 0
for k in range(K):
    ans += solve(T[k::K])
print(ans)

Submission status: Accepted

Solution code:
def examA():
    S, T = LSI()
    ans = T+S
    print(ans)
    return

def examB():
    A, B, K = LI()
    if A>K:
        A -=K
        print(A,B)
        return
    if A+B>K:
        B -=(K-A)
        A = 0
        print(A,B)
        return
    A = 0; B = 0
    print(A,B)
    return

def examC():
    def is_prime(n):
        if n == 1: return False
        for k in range(2, int(math.sqrt(n)) + 1):
            if n % k == 0:
                return False
        return True
    X = I()
    while(True):
        if is_prime(X):
            ans = X
            break
        X +=1
    print(ans)
    return

def examD():
    N, K = LI()
    R, S, P = LI()
    T = SI()
    C = Counter(T)
    sumT = C["r"]*P + C["s"]*R + C["p"]*S
#    print(sumT)
    ans = sumT
    d = defaultdict(bool)
    for i in range(K,N):
        if T[i]==T[i-K] and not d[i-K]:
            if T[i]=="r":ans -=P
            elif T[i]=="s":ans -=R
            else:ans -=S
            d[i] = True
    print(ans)
    return

def examE():
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,inf
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()

Submission status: Accepted

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

def get_score(c):
    if c == 'r':
        return p
    elif c == 's':
        return r
    else:
        return s

ans = 0
dp = [0] * n
for i in range(k):
    ans += get_score(t[i])
    dp[i] = 1

for i in range(k, n):
    if t[i] == t[i-k]:
        if dp[i-k] == 0:
            dp[i] = 1
            ans += get_score(t[i])
        else:
            dp[i] = 0
    else:
        ans += get_score(t[i])
        dp[i] = 1

print(ans)

Submission status: Accepted

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

T = input()
used = [False] * N

score = 0
for i, c in enumerate(T):
    if i >= K:
        if c == T[i - K] and used[i - K]:
            pass
        else:
            if c == 'r':
                score += P
            elif c == 'p':
                score += S
            else:
                score += R
            used[i] = True
    else:
        if c == 'r':
            score += P
        elif c == 'p':
            score += S
        else:
            score += R
        used[i] = True

print(score)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
rsp = [[R,'r'],[S,'s'],[P,'p']]
rsp.sort(key=lambda x:x[0], reverse=True)
dics = {'r':'s','s':'p','p':'r'}
out = ['0' for i in range(N)]
tot = 0
for i in range(3):
    for j in range(N):
        if j < K:
            if T[j] == dics[rsp[i][1]]:
                out[j] = rsp[i][1]
                tot += rsp[i][0]
        elif out[j-K] != rsp[i][1] and T[j] == dics[rsp[i][1]]:
            out[j] = rsp[i][1]
            tot += rsp[i][0]
print(tot)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
r, s, p = map(int, input().split())
T = input()
T = [T[i] for i in range(N)]

ans = 0
point = {'r': p, 's': r, 'p': s}
for i in range(N):
    if i < K:
        ans += point[T[i]]
    else:
        if T[i] != T[i-K]:
            ans += point[T[i]]
        else:
            T[i] = 'o'
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 i in range(k):
    dp = [0]*3
    for j in range(i, n, k):
        nxt = [-float("inf")]*3
        nxt[0] = max(dp[1], dp[2])
        if t[j] == "s":
            nxt[0] += r
        nxt[1] = max(dp[0], dp[2])
        if t[j] == "p":
            nxt[1] += s
        nxt[2] = max(dp[1], dp[0])
        if t[j] == "r":
            nxt[2] += p
        dp = nxt
    ans += max(dp)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
rsp = list(map(int,input().split()))
T = list(input())
ans = 0
for i in range(K):
    ans += rsp["spr".index(T[i])]
for i in range(K,N):
    if T[i] != T[i-K]:
        ans += rsp["spr".index(T[i])]
    else:
        T[i] = "z"
print(ans)