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=list(map(int,input().split()))
R,S,P= list(map(int,input().split()))
T= list(input())
lock_list=[""] * K
ans=0
dic={"r":"p","p":"s","s":"r"}
for i,j in enumerate(T):
   if lock_list[i%K] != dic[j]:
      lock_list[i%K] = dic[j]
      if j == "r":
         ans+=P
      elif j == "s":
         ans+=R
      else:
         ans+=S
   else:
      try:
         lock_list[i%K] = ""
      except IndexError:
         pass
print(ans)

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


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

for i in range(N):
  
  if i % K not in kT.keys():
    kT[i % K] = list(T[i])
  else:
    kT[i % K].append(T[i])
    
ans = 0
  
for i in kT.keys():
  for j in range(len(kT[i])):
    if j == 0:
      ans += points[kT[i][j]]
    else:
      if kT[i][j-1] == kT[i][j]:
        kT[i][j] = 'X'
      else:
        ans += points[kT[i][j]]
        
print(ans)

Solution (Python):
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    N, K = geta(int)
    R, S, P = geta(int)
    T = gete()

    def winner(c):
        return (R, 0, 0) if c == "s" else (0, P, 0) if c == 'r' else (0, 0, S)

    def fn(s):
        n = len(s)
        ret = 0
        dp = [[0] * 3 for _ in range(n)]

        dp[0] = winner(s[0])
        for i in range(1, n):
            w = winner(s[i])
            for j in range(3):
                dp[i][j - 2] = max(dp[i - 1][j - 1], dp[i - 1][j]) + w[j - 2]

        return (max(dp[n - 1]))

    print(sum(fn(T[i::K]) for i in range(K)))


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
rsp = [r, s, p]
T = input()
S = ''
a = [True]*n

for t in T:
    if t == 'r':
        S += '2'
    elif t == 's':
        S += '0'
    else:
        S += '1'

ans = 0
for i in range(n):
    if not a[i]:
        continue
    if i < n-k:
        if S[i] == S[i+k]:
            a[i+k] = False
    ans += rsp[int(S[i])]

print(ans)

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

for j in range(k, len(t)):
    if t[j] == t[j-k]:
        t[j] = ''
ts = collections.Counter(t)
print(ts['p']*s + ts['s']*r + ts['r']*p)

Solution (Python):
N, K = map(int,input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
dp = [[0]*3 for _ in range(N)]
for i in range(K):
    
    if T[i] == "r":
        dp[i][2] = P
        temp = P
    elif T[i] == "s":
        dp[i][0] = R
        temp = R
    else:
        dp[i][1] = S
        temp = S
    for j in range(i+K, N, K):
        if T[j] == "r":
            dp[j][2] = max(dp[j-K][0], dp[j-K][1]) + P
            dp[j][0] = max(dp[j-K][1], dp[j-K][2])
            dp[j][1] = max(dp[j-K][0], dp[j-K][2])
        elif T[j] == "s":
            dp[j][0] = max(dp[j-K][2], dp[j-K][1]) + R
            dp[j][1] = max(dp[j-K][2], dp[j-K][0])
            dp[j][2] = max(dp[j-K][0], dp[j-K][1])
        else:
            dp[j][1] =max(dp[j-K][0], dp[j-K][2]) + S
            dp[j][0] =max(dp[j-K][1], dp[j-K][2])
            dp[j][2] =max(dp[j-K][0], dp[j-K][1])
        temp = max(temp, dp[j][0], dp[j][1], dp[j][2])
    ans += temp
print(ans)

Solution (Python):
n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = list(input())
cnt = 0
for i in range(k):
    if t[i] == 'r':
        cnt += p
    elif t[i] == 's':
        cnt += r
    else:
        cnt += s
 
for i in range(k, n):
    if t[i] != t[i-k]:
        if t[i] == 'r':
            cnt += p
        elif t[i] == 's':
            cnt += r
        else:
            cnt += s
    else:
        t[i] = ''
 
print(cnt)

Solution (Python):
from collections import deque
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

W = deque([])
ans = 0
def janken(i):
    W.append(1)
    if T[i] == "r":
        return P
    elif T[i] == "s":
        return R
    else:
        return S
    

for i in range(N):
    if i < K:
        ans += janken(i)
    else:
        if T[i] == T[i - K] and W[i - K] == 1:
            W.append(0)
        else:
           ans += janken(i)
print(ans)

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

def janken(a):
    if a == 'r':
        return p
    elif a == 's':
        return r
    else:
        return s

for i in range(n):
    if i < k:
        ans += janken(t[i])
    elif i >= k and t[i] != t[i-k]:
        ans += janken(t[i])
    else:
        t[i] = ''
print(ans)

Solution (Python):
import sys
import numpy as np
input = lambda: sys.stdin.readline().rstrip()


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

    ans = 0
    q = N // K
    for i in range(K):
        dp = np.zeros((3, q + 2), dtype='i8')
        for j in range(q + 1):
            ti = i + j * K
            if ti >= N:
                continue
            rsp = T[ti]
            if rsp == 'r':
                dp[0][j + 1] = max(dp[1][j], dp[2][j])
                dp[1][j + 1] = max(dp[0][j], dp[2][j])
                dp[2][j + 1] = max(dp[0][j] + P, dp[1][j] + P)
            elif rsp == 's':
                dp[0][j + 1] = max(dp[1][j] + R, dp[2][j] + R)
                dp[1][j + 1] = max(dp[0][j], dp[2][j])
                dp[2][j + 1] = max(dp[0][j], dp[1][j])
            elif rsp == 'p':
                dp[0][j + 1] = max(dp[1][j], dp[2][j])
                dp[1][j + 1] = max(dp[0][j] + S, dp[2][j] + S)
                dp[2][j + 1] = max(dp[0][j], dp[1][j])
        ans += np.max(dp)

    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
n,k=map(int,input().split())
S=list(map(int,input().split()))
s=input()
T=[s[i::k] for i in range(k)]
a=0
for t in T:
    b='x'
    for c in t:
        if c!=b:
            a+=S['spr'.index(c)]
            b=c
        else:
            b='x'
print(a)

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

ans=0

for j in range(len(T)):
    if j<K:
        if T[j]=='r':
            ans+=P
        elif T[j]=='s':
            ans+=R
        else:
            ans+=S
    elif j>=K and T[j]!=T[j-K]:
        if T[j]=='r':
            ans+=P
        elif T[j]=='s':
            ans+=R
        else:
            ans+=S
    else:
        T[j]=''
print(ans)

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

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

    dp = ['r'] * n
    score = 0
    to_win = {
        'r': 'p',
        's': 'r',
        'p': 's',
    }
    to_lose = {

    }
    rps_score = {
        'r': r,
        's': s,
        'p': p,
    }
    for i in range(k):
        rps = to_win[t[i]]
        dp[i] = rps
        score += rps_score[rps]

    for i in range(k, n):
        rps = to_win[t[i]]
        if dp[i-k] != rps:
            dp[i] = rps
            score += rps_score[rps]
        else:
            if i+k < n:
                dp[i] = t[i+k]

    print(score)
main()

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)








n,k=n1()
r,s,p=n1()
t=s0()

def win(h):
    if h=="r":return p
    elif h=="s": return r
    else: return s

ans=0
t2=[False]*n
for i in range(k):

    ans+=win(t[i])
    t2[i]=True
for i in range(k,n):

    if t[i-k]!=t[i] or t2[i-k]==False:
        ans+=win(t[i])
        t2[i]=True
        
print(ans)

Solution (Python):
n, k = list(map(int, input().split()))
s = list(map(int, input().split()))
T = list(input())
ans = 0
for i in range(k):
    T_sub = T[i::k]
    score = 0
    pre = 3
    for t_sub in T_sub:
        if t_sub == pre:
            pre = 3
        else:
            if t_sub == 'r':
                score += s[2]
            elif t_sub == 's':
                score += s[0]
            else:
                score += s[1]
            pre = t_sub
    ans += score
print(ans)

Solution (Python):
def main():
    from itertools import groupby
    from math import ceil
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()

    q = {'r': p, 's': r, 'p': s}
    ans = 0
    for i in range(k):
        j = t[i::k]

        for key, group in groupby(j):
            g = list(group)
            ans += q[key] * ceil(len(g)/2)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
R,S,P =map(int,input().split())
T = list(input())
value= {'r':P,'s':R,'p':S}
ans = 0

for i in range(k):
    num = i
    pre = ''
    while num<n:
        if pre == T[num]:
            pre = ''
        else:
            pre = T[num]
            ans += value[pre]
        num += k
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
te=[]
ans=0
for i in range(n):
  if i-k<0:
    if t[i]=='r':
      te.append('p')
      ans+=p
    elif t[i]=='s':
      te.append('r')
      ans+=r
    elif t[i]=='p':
      te.append('s')
      ans+=s
  else:
    if t[i]=='r' and te[i-k]!='p':
      te.append('p')
      ans+=p
    elif t[i]=='s' and te[i-k]!='r':
      te.append('r')
      ans+=r
    elif t[i]=='p' and te[i-k]!='s':
      te.append('s')
      ans+=s
    else:
      te.append('other')
print(ans)

Solution (Python):

def fight(i, win_t):
	global rsp
	global ans
	global track
	
	if past_t(i) != win_t:
		ans += rsp[win_t]
		track += win_t
	else:
		future_t_ = future_t(i)
	
		for t in rsp:
			if t != win_t and t != future_t_:
				track += t
				break


def past_t(count):
	
	global k
	global track
	
	if count >= k:
		return track[count-k]
	else:
		return ''
		

def future_t(count):
	global k
	global cpu_t

	try:
		cpu = cpu_t[count+k]
		
		if cpu == 'r':
			return 'p'
		elif cpu == 's':
			return 'r'
		else:
			return 's'
	except:
		return ''
			

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

rsp = dict()
rsp['r'] = r
rsp['s'] = s
rsp['p'] = p

cpu_t = input()
track = ''
i = 0
ans = 0

for cpu in cpu_t:
	if cpu == 's':
		fight(i, 'r')
	elif cpu == 'p':
		fight(i, 's')
	else:
		fight(i, 'p')
										
	i += 1

	if i == 3:
		pass

print(ans)

Solution (Python):

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

k_list = ['']*k
for i in range(n):
  if len(k_list[i%k]) == 0:
    k_list[i%k] += t[i]  
  elif k_list[i%k][-1] == t[i]:
    k_list[i%k] += 'z'
  else:
    k_list[i%k] += t[i]
ans = 0
for h in k_list:
  ans += p*h.count('r')+r*h.count('s')+s*h.count('p')
print(ans)

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

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

total = 0
for i in range(len(T)):
    if i - K >= 0:
        if T[i - K] == T[i]:
            T[i] = "done"
            continue
    total += point_dict[T[i]]
print(total)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
def point(i, t):
    if i == 0 and t == "s":
        return r
    elif i == 1 and t == "p":
        return s
    elif i == 2 and t == "r":
        return p
    else:
        return 0
ans = 0
for i in range(k):
    count = (n-i-1) // k + 1
    dp = [[0 for k in range(3)] for l in range(count)]
    dp[0][0] = max(dp[0][0], point(0, t[i]))
    dp[0][1] = max(dp[0][1], point(1, t[i]))
    dp[0][2] = max(dp[0][2], point(2, t[i]))
    for j in range(count-1):
        for l in range(3):
            if l == 0:
                dp[j+1][1] = max(dp[j+1][1], dp[j][l] + point(1,t[i+(j+1)*k]))
                dp[j+1][2] = max(dp[j+1][2], dp[j][l] + point(2,t[i+(j+1)*k]))
            if l == 1:
                dp[j+1][0] = max(dp[j+1][0], dp[j][l] + point(0,t[i+(j+1)*k]))
                dp[j+1][2] = max(dp[j+1][2], dp[j][l] + point(2,t[i+(j+1)*k]))
            if l == 2:
                dp[j+1][0] = max(dp[j+1][0], dp[j][l] + point(0,t[i+(j+1)*k]))
                dp[j+1][1] = max(dp[j+1][1], dp[j][l] + point(1,t[i+(j+1)*k]))
    ans += max(dp[-1][0], dp[-1][1], dp[-1][2])
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
lis = [0] * k
def win(string):
    if string == "r":
        return p
    if string == "s":
        return r
    if string == "p":
        return s
def upper(n):
    return (n+1)//2
ans = 0
for i in range(n):
    now = i % k
    lis[now] += 1
    if i+k >= n or t[i] != t[i+k]:
        ans += win(t[i])*upper((lis[now]))
        lis[now] = 0
print(ans)

Solution (Python):
from collections import Counter

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
ans = 0
pre = [0] * k
d = Counter(t)
for i in range(0, n+(k-1)//k, k):
    L = list(t[i:i+k])
    for j in range(len(L)):
        if pre[j] == L[j]:
            d[L[j]] -= 1
            L[j] = 'x'
    pre = L
ans = d['r'] * p + d['s'] * r + d['p'] * s
print(ans)

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

wn = {'r': 'p', 's': 'r', 'p': 's'}
pt = {'r': P, 's': R, 'p': S}

ans = 0
t = [''] * len(s)
for i in range(len(s)):
    if i + 1 <= K:
        ans += pt[s[i]]
        t[i] = wn[s[i]]
    else:
        if wn[s[i]] != t[i-K]:
            ans += pt[s[i]]
            t[i] = wn[s[i]]

print(ans)

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

Solution (Python):
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
import math








import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

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

    for j in range(K):
        TT = [T[i] for i in range(j, N, K)]

        gr = groupby(TT)
        for key, group in gr:  
            """ex.
            0: [0, 0, 0]
            1: [1, 1]
            0: [0, 0, 0]
            1: [1, 1]
            0: [0]
            1: [1]
            """
            if key == 'r':
                val += math.ceil(len(list(group))/2)*P
            elif key == 's':
                val += math.ceil(len(list(group))/2)*R
            else:
                val += math.ceil(len(list(group))/2)*S

    print(val)

resolve()

Solution (Python):
n, k = map(int, input().split())
r, ss, p = map(int, input().split())
s = input()
hands = [None]*n
wins = {'r':'p', 's':'r', 'p':'s'}
h_s = {'r':r, 's':ss, 'p':p}
ans = 0
for i, c in enumerate(s):
    if i - k >= 0:
        if wins[c] == hands[i-k]:
            hs = set(wins.keys())
            hs.remove(wins[c])
            if i + k < n:
                if wins[s[i+k]] in hs:
                    hs.remove(wins[s[i+k]])
            hands[i] = hs.pop()
            continue
    hand = wins[c]
    hands[i] = hand
    ans += h_s[hand]

print(ans)

Solution (Python):
from collections import defaultdict

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

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    
    x = sq[0]
    if x == "r":
        prev, score = "r", p
    elif x == "s":
        prev, score = "s", r
    else:
        prev, score = "p", s
    cnt = 1

    
    for x in sq[1:]:
        if prev != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            prev = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Solution (Python):
from collections import defaultdict

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

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(sq):
    
    x = sq[0]
    if x == "r":
        before, score = "r", p
    elif x == "s":
        before, score = "s", r
    else:
        before, score = "p", s
    cnt = 1

    
    for x in sq[1:]:
        if before != x:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            before = x
            cnt = 1
        elif cnt % 2 == 1:
            cnt += 1
        else:
            if x == "r":
                score += p
            elif x == "s":
                score += r
            else:
                score += s
            cnt += 1
    return score


ans = []
for i in range(k):
    ans.append(solve(cir[i]))

print(sum(ans))

Solution (Python):

n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = list(input())
point = 0
for i in range(k):
    if t[i] == 'r':
        point += p
    elif t[i] == 's':
        point += r
    else:
        point += s

for i in range(k, n):
    if t[i] != t[i-k]:
        if t[i] == 'r':
            point += p
        elif t[i] == 's':
            point += r
        else:
            point += s
    else:
        t[i] = ''

print(point)

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

Solution (Python):
n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = list(input())
score = 0
for i in range(k):
    if t[i] == "r":
        score += p
    elif t[i] == "s":
        score += r
    else:
        score += s

for i in range(k, len(t)):
    if t[i] != t[i-k]:
        if t[i] == "r":
            score += p
        elif t[i] == "s":
            score += r
        else:
            score += s
    else:
        t[i] = ""

print(score)

Solution (Python):
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = list(input())
win_point = {"r":P,"p":S,"s":R}
score = 0
for i in range(K):
    score += win_point[T[i]]
for i in range(K,N):
    if T[i] == T[i-K]:
        T[i] = "."
        continue
    score += win_point[T[i]]
print(score)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N, K = getNM()
R, S, P = getNM()
T = input()

ans = 0
flag = [0] * N
for i in range(N):
    if i >= K and T[i] == T[i - K] and flag[i - K] == 1:
        continue
    if T[i] == "r":
        ans += P
    elif T[i] == "s":
        ans += R
    else:
        ans += S
    flag[i] = 1
print(ans)

Solution (Python):
n,k = map(int, raw_input().split(' '))
r,s,p = map(int, raw_input().split(' '))
seq = raw_input()

import collections
q,i = collections.deque(), 0
while(i < len(seq)):
	u = q.popleft() if len(q) == k else [0,0,0]
	q.append( [ max(u[1], u[2]) + (r if seq[i]  == 's' else 0) ,max(u[0], u[2]) + (s if seq[i]  == 'p' else 0) ,max(u[0], u[1]) + (p if seq[i]  == 'r' else 0)])
	i +=1
print sum([max(cc) for cc in q] or [0])

Solution (Python):
n,k = map(int, raw_input().split(' '))
r,s,p = map(int, raw_input().split(' '))
seq = raw_input()
import collections
q = collections.deque()
i = 0
while(i < len(seq)):
	prev = q.popleft() if len(q) == k else [0,0,0]
	c = [0,0,0]
	c[0] = max(prev[1], prev[2]) + (r if seq[i]  == 's' else 0)
	c[1] = max(prev[0], prev[2]) + (s if seq[i]  == 'p' else 0)
	c[2] = max(prev[0], prev[1]) + (p if seq[i]  == 'r' else 0)
	q.append(c)
	i +=1
print sum([max(C) for C in q] or [0])

Solution (Python):
n,k = map(int, raw_input().split(' '))
r,s,p = map(int, raw_input().split(' '))
seq = raw_input()


dp = [[0,0,0] for _ in range(len(seq))]
"""
	0 rock
	1 paper
	2 scisor
"""

for i in range(len(dp)):
	if i - k < 0:
		dp[i][0] = r if seq[i]  == 's' else 0
		dp[i][1] = s if seq[i]  == 'p' else 0
		dp[i][2] = p if seq[i]  == 'r' else 0
	elif i - k >=0:
		dp[i][0] = max(dp[i- k][1], dp[i-k][2]) + 	(r if seq[i]  == 's' else 0)
		dp[i][1] = max(dp[i- k][0], dp[i-k][2]) + 	(s if seq[i]  == 'p' else 0)
		dp[i][2] = max(dp[i- k][0], dp[i-k][1]) + 	(p if seq[i]  == 'r' else 0)

cumul = 0
seen = set([])
for j in range(len(dp)-1, -1, -1):
	
	if (j % k) not in seen:
		seen.add(j % k)
		cumul += max(dp[j])
	else:
		break

print cumul

Solution (Python):
n,k = map(int, raw_input().split(' '))
r,s,p = map(int, raw_input().split(' '))
seq = raw_input()


dp = [[0,0,0] for _ in range(len(seq))]
"""
	0 rock
	1 paper
	2 scisor
"""

for i in range(len(dp)):
	if i - k < 0:
		dp[i][0] = r if seq[i]  == 's' else 0
		dp[i][1] = s if seq[i]  == 'p' else 0
		dp[i][2] = p if seq[i]  == 'r' else 0
	elif i - k >=0:
		dp[i][0] = max(dp[i- k][1], dp[i-k][2]) + 	(r if seq[i]  == 's' else 0)
		dp[i][1] = max(dp[i- k][0], dp[i-k][2]) + 	(s if seq[i]  == 'p' else 0)
		dp[i][2] = max(dp[i- k][0], dp[i-k][1]) + 	(p if seq[i]  == 'r' else 0)

cumul = 0
seen = set([])
for j in range(len(dp)-1, -1, -1):
	
	if (j % k) not in seen:
		seen.add(j % k)
		cumul += max(dp[j])

print cumul

Solution (Python):
def rsp(t):
  t=t.replace('r','P')
  t=t.replace('p','S')
  t=t.replace('s','R')
  return t
 
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
T_rev=list(rsp(T))
 
if N<K:
  print(T_rev.count('R')*R+T_rev.count('S')*S+T_rev.count('P')*P)
else:
  for i in range(K,N):
    if T_rev[i]==T_rev[i-K]:
      T_rev[i]='x'
  print(T_rev.count('R')*R+T_rev.count('S')*S+T_rev.count('P')*P)

Solution (Python):
def rsp(t):
  t=t.replace('r','P')
  t=t.replace('p','S')
  t=t.replace('s','R')
  return t

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

if N<K:
  print(T_rev.count('R')*R+T_rev.count('S')*S+T_rev.count('P')*P)
else:
  for i in range(K,N):
    if T_rev[i]==T_rev[i-K]:
      T_rev[i]='x'
  print(T_rev.count('R')*R+T_rev.count('S')*S+T_rev.count('P')*P)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
count = {"r": 0, "s": 0, "p": 0}
win = [-1 for i in range(N)]

for i in range(N):
    if i < K:
        count[T[i]] += 1
        win[i] = 1
    else:
        if T[i] == T[i-K] and win[i-K] == 1:
            win[i] = 0
        else:
            count[T[i]] += 1
            win[i] = 1

score = R*count["s"] + S*count["p"] + P*count["r"]
print(score)

Solution (Python):
import collections

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

ans = 0
q = collections.deque([-1]*k)
for i in input():
    prev = q.popleft()
    if i == 'r':
        if prev == 'r':
            q.append(-1)
        else:
            q.append(i)
            ans += p
    if i == 's':
        if prev == 's':
            q.append(-1)
        else:
            q.append(i)
            ans +=  r
    if i == 'p':
        if prev == 'p':
            q.append(-1)
        else:
            q.append(i)
            ans +=  s

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
cmds = ['']*n
for i, c in enumerate(t):
    if c == 'r':
        cmd = 'p'
        pt = p
    elif c == 's':
        cmd = 'r'
        pt = r
    elif c == 'p':
        cmd = 's'
        pt = s
    if i-k >= 0 and cmds[i-k] == cmd:
        cmd = ''
        pt = 0
    ans += pt
    cmds[i] = cmd

print(ans)

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

for i in range(k,n):
    if t[i]==t[i-k]:
        t[i]=''
ans = t.count('r')*p+t.count('s')*r+t.count('p')*s
print(ans)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()
r,s,p=nm()
t = [_ for _ in input()]
tt=[]
for ti in t:
  if ti == 'r':
    tt.append(0)
  elif ti == 's':
    tt.append(1)
  else:
    tt.append(2)

score=dict()
res=0
for i in range(k):
  last=False
  for j in range(i,n,k):
    if j>=k and tt[j-k]==tt[j] and last:
      last=False
    else:
      if tt[j]==0:
        res+=p
      elif tt[j]==1:
        res+=r
      else:
        res+=s
      
      last=True
print(res)

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


ans = 0
commands = [''] * N
for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

    if (i - K >= 0) and (commands[i -  K] == command):
        command = ''
        point = 0

    ans += point
    commands[i] = command

print(ans)

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

N,K = [int(x) for x in input().rstrip().split()]
R,S,P = [int(x) for x in input().rstrip().split()]
T = input().rstrip()

ans = 0
d = ['']*N
for i in range(N):
    if(T[i] == 's'):
        if(i-K >= 0):
            if(d[i-K] != T[i]):
                ans += R
                d[i] = 's'
        else:
            ans += R
            d[i] = 's'
    elif(T[i] == 'r'):
        if(i-K >= 0):
            if(d[i-K] != T[i]):
                ans += P
                d[i] = 'r'
        else:
            ans += P
            d[i] = 'r'
    else:
        if(i-K >= 0):
            if(d[i-K] != T[i]):
                ans += S
                d[i] = 'p'
        else:
            ans += S
            d[i] = 'p'

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
l=[1]*n
z=1
d={'r':0,'s':0,'p':0}
for i in range(n):
    if i-k>=0 and l[i]:
        if t[i]==t[i-k]:
            if i+k<n:
                l[i+k]=0
                continue
        else:
            d[t[i]]+=1
    else:
        d[t[i]]+=1   
score=d['r']*p+d['s']*r+d['p']*s
print(score)

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

my_hand = []
ans = 0
point = {'r':P, 's':R, 'p':S}
win = {'r':'p', 's':'r', 'p':'s'}

for i, t in enumerate(T):
    if i >= K:
        if my_hand[i - K] == win[t]:
            my_hand.append('z')
        else:
            ans += point[t]
            my_hand.append(win[t])
    else:
        ans += point[t]
        my_hand.append(win[t])
print(ans)

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


ans = 0
commands = [''] * N
for i, t in enumerate(T):
    if t == 'r':
        command = 'p'
        point = P

    elif t == 's':
        command = 'r'
        point = R

    elif t == 'p':
        command = 's'
        point = S

    if (i - K >= 0) and (commands[i -  K] == command):
        command = ''
        point = 0

    ans += point
    commands[i] = command

print(ans)