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

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

lstk = [0]*n
ans = 0
def f(index):
    if lstk[index%k] == lst1[index]:
        lstk[index%k] = "free"
        return False
    else:
        lstk[index%k] = lst1[index]
        return True

"""
def score(index):
    global lst1
    global ans
    if lst1(index) == "r":
        ans += p
    elif lst1(index) == "s":
        ans += r
    else:
        ans += s
"""
for i in range(n):
    if i < k:
        lstk[i] = lst1[i]
        if lst1[i] == "r":
            ans += p
        elif lst1[i] == "s":
            ans += r
        else:
            ans += s
    else:
        if f(i): 
            if lst1[i] == "r":
                ans += p
            elif lst1[i] == "s":
                ans += r
            else:
                ans += s

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
res = 0
f = [True for _ in range(n)]
for i in range(n):
    m = t[i]
    if i >= k:
        if m == t[i - k]:
            if f[i - k]:
                f[i] = False
                continue
    if m == "r":
        res += p
    elif m == "s":
        res += r
    else:
        res += s
print(res)

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

score = 0
mine = ''
for i, t_ in enumerate(t):
    if t_ == 'r':
        if i >= k:
            if mine[i-k] != 'p':
                score += p
                mine += 'p'
            else:
                mine += '*'
        else:
            score += p
            mine += 'p'
    elif t_ == 's':
        if i >= k:
            if mine[i-k] != 'r':
                score += r
                mine += 'r'
            else:
                mine += '*'
        else:
            score += r
            mine += 'r'
    else:
        if i >= k:
            if mine[i-k] != 's':
                score += s
                mine += 's'
            else:
                mine += '*'
        else:
            score += s
            mine += 's'

print(score)

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

a = n // k
b = n % k

ans = 0

for i in range(k):
    dp = [[0, 0, 0] for i in range(a+2)]
    tj = 0
    for j in range(a+1):
        l = i + k * j
        if l >= n:
            break
        tj = j
        c = t[l]
        if c == 'r':
            dp[j+1][0] = max(dp[j][1], dp[j][2])
            dp[j+1][1] = max(dp[j][0], dp[j][2])
            dp[j+1][2] = max(dp[j][0]+p, dp[j][1]+p)
        if c == 'p':
            dp[j+1][0] = max(dp[j][1], dp[j][2])
            dp[j+1][1] = max(dp[j][0]+s, dp[j][2]+s)
            dp[j+1][2] = max(dp[j][0], dp[j][1])
        if c == 's':
            dp[j+1][0] = max(dp[j][1]+r, dp[j][2]+r)
            dp[j+1][1] = max(dp[j][0], dp[j][2])
            dp[j+1][2] = max(dp[j][0], dp[j][1])
    ans += max(dp[tj+1])
print(ans)

Solution (Python):

def solve1(n, r, s, p, t):
    cur = {'r': 0, 's': 0, 'p': 0}
    for c in t:
        prv = cur
        cur = {'r': 0, 's': 0, 'p': 0}
        for x in 'rsp':
            if x == 'r' and c == 's':
                score = r
            elif x == 's' and c == 'p':
                score = s
            elif x == 'p' and c == 'r':
                score = p
            else:
                score = 0
            for y in 'rsp':
                if y != x:
                    cur[x] = max(cur[x], prv[y] + score)
    return max(cur.values())

def solve(n, k, r, s, p, t):
    u = [[] for _ in range(k)]
    for i, c in enumerate(t):
        u[i % k].append(c)
    return sum([solve1(n, r, s, p, u[j]) for j in range(k)])

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input().split()
    Ts = ["" for _ in range(K)]
    for i, c in enumerate("".join(T)):
        Ts[i % K] += c

    ans = 0
    for t in Ts:
        c = t[0]
        if c == "r":
            ans += P
        elif c == "s":
            ans += R
        else:
            ans += S
        for i in range(1, len(t)):
            if t[i] == c:
                c = ""
            else:
                c = t[i]
                if c == "r":
                    ans += P
                elif c == "s":
                    ans += R
                else:
                    ans += S
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
"""
dp = [[0 for i in range(3)] for j in range(N)]
for i in range(N):
	r,s,p = 0,0,0
	if i > 0:
		r,s,p = dp[i-1][0],dp[i-1][1],dp[i-1][2]
	m = max(r,s,p)
	if T[i] == "r":
		if i < K:
			dp[i][0] = m
			dp[i][1] = m
			dp[i][2] = m + P
		else:
			dp[i][0] = max(s,p)
			dp[i][1] = max(r,p)
			dp[i][2] = max(r,s) + P
	elif T[i] == "s":
		if i < K:
			dp[i][0] = m + R
			dp[i][1] = m
			dp[i][2] = m
		else:
			dp[i][0] = max(s,p) + R
			dp[i][1] = max(r,p)
			dp[i][2] = max(r,s)
	else:
		if i < K:
			dp[i][0] = m
			dp[i][1] = m + S
			dp[i][2] = m
		else:
			dp[i][0] = max(s,p)
			dp[i][1] = max(r,p) + S
			dp[i][2] = max(r,s)
print(max(dp[-1]))
"""
ans = 0
point = {
	"r": P,
	"s": R,
	"p": S
}
win = [False for i in range(N)]
for i in range(N):
	if i < K:
		win[i] = True
		ans += point[T[i]]
	else:
		if T[i] == T[i-K] and win[i-K]:
			continue
		win[i] = True
		ans += point[T[i]]
print(ans)

Solution (Python):
import sys

def point(s, S, P, R):
    if s == "s": return S
    elif s == "p": return P
    else: return R

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    S, P, R = map(int, input().split())
    T = input()

    total = 0
    Takahashi = [None] * N
    for i in range(K):
        total += point(T[i], S, P, R)
        Takahashi[i] = T[i]
    for i in range(K, N):
        if T[i] == Takahashi[i-K]:
            Takahashi[i] = "NG"
        else:
            Takahashi[i] = T[i]
            total += point(T[i], S, P, R)
    print(total)


    return 0

if __name__ == "__main__":
    solve()

Solution (Python):

def getRunLengthEncoding(Ss):
    anss = []
    Ss += '$'
    SPrev = Ss[0]
    num = 1
    for S in Ss[1:]:
        if S == SPrev:
            num += 1
        else:
            anss.append((SPrev, num))
            SPrev = S
            num = 1
    return anss

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

ans = 0
for i in range(K):
    As = Ts[i::K]
    RLE = getRunLengthEncoding(As)
    for c, num in RLE:
        m = (num+1)//2
        if c == 'r':
            ans += P*m
        elif c == 's':
            ans += R*m
        else:
            ans += S*m

print(ans)

Solution (Python):
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    

    N, K = get_inputs(int)
    R, S, P = get_inputs(int)
    T = get_input()

    points = {'r': R, 's': S, 'p': P}
    towin = {'r': 'p', 's': 'r', 'p': 's'}
    todraw = {'r': 'r', 's': 's', 'p': 'p'}

    ret = ''
    score = 0
    for i in range(N):
        if i < K:
            choice = towin[T[i]]
            ret += choice
            score += points[choice]
        else:
            if ret[-K] != towin[T[i]]:
                choice = towin[T[i]]
                ret += choice
                score += points[choice]
            else:
                choices = set(['r', 's', 'p'])
                choices -= set([ret[-K],])
                if i + K < len(T):
                    choices -= set([towin[T[i+K]],])
                choice = list(choices)[0]
                ret += choice


    print(score)


if __name__ == '__main__':
    main()

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

win = []

for i in range(n):
   if i >= k:
      if t[i-k] == t[i]:
         if win[i-k] == 1:
            win.append(0)
         else:
            win.append(1)
      else:
         win.append(1)
   else:
      win.append(1)
   
ans = 0
for w,tt in zip(win, t):
   if w == 1:
      if tt == "r":
         ans += p
      if tt == "s":
         ans += r
      if tt == "p":
         ans += s
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
sco = {"r":P,"s":R,"p":S}
T = input()
B = [-1]*N
ans = 0
for i in range(N):
    if i<K:
        ans+=sco[T[i]]
    elif T[i]!=T[i-K]:
        ans+=sco[T[i]]
    elif B[i-K]==-1:
        B[i]=0
    else:
        ans+=sco[T[i]]
print(ans)

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

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

ans = 0
for start in range(k):
    prev = "nil"
    for e in t[start::k]:
        if e != prev:
            ans += point[e]
            prev = e
        else:
            prev = "nil"

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())[: -1]

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

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)

Solution (Python):
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)

Solution (Python):
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:]]))

Solution (Python):
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)

Solution (Python):
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])
    
    return max(dp[-1])

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

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

Solution (Python):
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)))

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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):
    
    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)

Solution (Python):


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)

Solution (Python):

MOD = 10 ** 9 + 7

import math

import itertools



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




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

Solution (Python):
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)

Solution (Python):

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

Solution (Python):
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))

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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):
    
    ans += score[t[i]]
    j = i + k
    f = 0
    while j < n:
        if t[j] != t[j - k]:
            ans += score[t[j]]
            
        else:
            t[j] = '-'
        j += k
    

print(ans)

Solution (Python):
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)