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-----
5 2
8 7 6
rsrpr

-----Sample Output-----
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.

Difficulty: interview

Starter code:

Solutions:
###Prediction and Restriction
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=input()
win=[False]*n
def janken(char):
    if char=='r':
        return p
    elif char=='s':
        return r
    else:
        return s
cnt=0
for i in range(n):
    if i-k<0:
        point=janken(t[i])
        cnt+=point
        win[i]=True
    else:
        if t[i]!=t[i-k]:
            point=janken(t[i])
            cnt+=point
            win[i]=True
        else:
            if not win[i-k]:
                point=janken(t[i])
                cnt+=point
                win[i]=True

print(cnt)


#!/usr/bin/env python3
def main():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = input()

    command = [''] * N
    ans = 0
    for i, t in enumerate(T):
        if t == 'r':
            point = P
            command_candidate = 'p'
        elif t == 's':
            point = R
            command_candidate = 'r'
        else:
            point = S
            command_candidate = 's'

        if i >= K and command[i - K] == command_candidate:
            point = 0
            command_candidate = ''
        ans += point
        command[i] = command_candidate
    print(ans)


def __starting_point():
    main()

__starting_point()

# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, K = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])
#     if temp!=1:
#         arr.append([temp, 1])
#     if arr==[]:
#         arr.append([n, 1])
#     return arr

R, S, P = map(int, input().split())
T = input()
win = {"r": ["p", P], "s": ["r", R], "p": ["s", S]}

hands = []
tot = 0
for i, c in enumerate(T):
    if i >= K and win[c][0] == hands[i - K]:
        hands.append("anything")
        continue
    hands.append(win[c][0])
    tot += win[c][1]
        
print(tot)

n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = list(input())
ans = 0
dic = {'p': s, 's': r, 'r': p}
for i in range(k):
  ans += dic[t[i]]
dic_ls = []
for i in range(k, n):
  if t[i] != t[i - k]:
    ans += dic[t[i]]
  else:
    t[i] = 'j'
print(ans)

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

def janken(n):
    if n == "r":
        return P
    elif n == "s":
        return R
    else:
        return S

ans = 0

for i in range(K):
    h = T[i::K]
    l = len(h)
    f = 1
    a = ""
    for j in range(l):
        if a != h[j]:
            a = h[j]
            f = 0
            ans += janken(h[j])
        else:
            ans += f * janken(h[j])
            f = - (f - 1)

print(ans)

import copy

N, K = list(map(int, input().split()))
my_R, my_S, my_P = list(map(int, input().split()))
hand_score = {'r': my_P, 's': my_R, 'p': my_S}
T = input()
ans = 0

for i in range(K):
    cnt = (N - i - 1) // K + 1
    dp = {'R': 0, 'S': 0, 'P': 0}
    new_dp = {'R': 0, 'S': 0, 'P': 0}
    for j in range(cnt):
        check = {'r': 0, 's': 0, 'p': 0}
        check[T[(j * K) + i]] = hand_score[T[(j * K) + i]]

        new_dp['R'] = max(dp['S'] + check['s'], dp['P'] + check['p'])
        new_dp['S'] = max(dp['P'] + check['p'], dp['R'] + check['r'])
        new_dp['P'] = max(dp['R'] + check['r'], dp['S'] + check['s'])

        dp = copy.copy(new_dp)
    ans += max(dp.values())
print(ans)


from collections import deque

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

d = {"s":r, "p":s, "r":p}
before = deque([])
ans = 0
for i in range(n):
    if i<k:
        before.append(t[i])
        ans += d[t[i]]
    else:
        b = before.popleft()
        if t[i]!=b:
            before.append(t[i])
            ans += d[t[i]]
        else:
            before.append("n")
print(ans)

N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
ans = 0
check = [0] * N
for i in range(N):
  j = T[i]
  if i >= K:
    if j == check[i-K]: continue
  if j == "r": ans += P
  elif j == "s": ans += R
  else: ans += S
  check[i] = j
 
print(ans)

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

moi=[['x'] for i in range(K)]

for i in range(N):
    moi[i%K].append(T[i])

for i in range(K):
    moi[i].pop(0)

moji=''

for i in range(K):
    cnt=0
    for j in range(len(moi[i])):
        if j==0:
            moji+=moi[i][j]
        else:
            if moi[i][j-1]!=moi[i][j]:
                moji+=moi[i][j]
                cnt=0
            if moi[i][j-1]==moi[i][j] and cnt==1:
                moji+=moi[i][j]
                cnt=0
            elif moi[i][j-1]==moi[i][j] and cnt==0:
                cnt+=1

ans=0
for i in range(len(moji)):
    if moji[i]=='r':
        ans+=P
    elif moji[i]=='s':
        ans+=R
    else:
        ans+=S

print(ans)

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = list(map(int, readline().split()))
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    ans = 0
    for i in range(K):
        vec = T[i::K]
        M = len(vec)

        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == vec[j]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


def __starting_point():
    main()

__starting_point()

n, k = map(int, input().split())
scores = {k: v for k, v in zip('spr', map(int, input().split()))}
zero_score_index = set()
t = input()
total = sum(scores[tt] for tt in t)
for i in range(n):
  if i < k:
    continue
  if t[i] == t[i - k] and i - k not in zero_score_index:
    zero_score_index.add(i)
    total -= scores[t[i]]
print(total)

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

w1=''
w2=''
ans=0
for i in range(N):
    if T[i]=='r':
        w1+='p'
    elif T[i]=='p':
        w1+='s'
    else:
        w1+='r'

for i in range(0,K):
    if T[i]=='r':
        w2+='p'
        ans+=P
    elif T[i]=='p':
        w2+='s'
        ans+=S
    else:
        w2+='r'
        ans+=R

if N>=2:
    for i in range(K,N):
        if w1[i]==w2[i-K]:
            w2+='_'
        else:
            if w1[i]=='p':
                w2+='p'
                ans+=P
            elif w1[i]=='s':
                w2+='s'
                ans+=S
            else:
                w2+='r'
                ans+=R
print(ans)


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

rsp = {"r":p, "s":r, "p":s}
ans = 0
for i in range(n):
  if i < k: ans += rsp[t[i]]
  else:
    if t[i] != t[i-k]: ans += rsp[t[i]]
    else: t[i] = "a"
print(ans)

# D - Prediction and Restriction
N,K = map(int,input().split())
score = list(map(int,input().split()))
T = input()

# dp[i][j]: i回目のじゃんけんで j を出したときの最大の合計点数
# グー: 0, チョキ: 1, パー: 2
dp = [[0]*3 for _ in range(N+1)]

def judge(a,b):
    # パーで勝つとき
    if a == 'r' and b == 2:
        return score[2]
    # グーで勝つとき
    elif a == 's' and b == 0:
        return score[0]
    # チョキで勝つとき
    elif a == 'p' and b == 1:
        return score[1]
    return 0
    
# i 回目のじゃんけん
for i in range(1,N+1):
    # i回目のじゃんけんで j を出したとき
    for j in range(3):
        # i-K 回目のじゃんけんで k を出したとき
        for k in range(3):
            # i = K 回以上のとき
            if i > K:
                # K 回前と同じ手を出したらスルー
                if j == k:
                    continue
            # i < K 回のとき
            else:
                # 勝ち続けるのが最適
                dp[i][j] = judge(T[i-1],j)
                continue
            # K 回前に k を出したときの得点に、j を出して得た得点を加点
            dp[i][j] = max(dp[i][j],dp[i-K][k] + judge(T[i-1],j))

ans = 0
for a in dp[-K:]:
    ans += max(a)
print(ans)

def rle(a):
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append([l,a[i]])
            l=1
    ll.append([l,a[-1]])
    return ll
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=input()
ans=0
w=[]
for i in range(k):
    j=i
    b=""
    while j<n:
        b+=t[j]
        j+=k
    w.append(b)
for i in t:
    if i=="r":
        ans+=p
    elif i=="s":
        ans+=r
    else:
        ans+=s
for x in w:
    st=rle(x)
    for i,j in st:
        if j=="r":
            ans-=(i//2)*p
        elif j=="s":
            ans-=(i//2)*r
        else:
            ans-=(i//2)*s
print(ans)


import math

def main():
	N, K = [int(n) for n in input().split(" ")]
	R, S, P = [int(p) for p in input().split(" ")]
	T = list(input())
	T.reverse()
	cnt = 0
	for i in range(K):
		tmp = []
		for j in range(math.ceil((N - i) / K)):
			if len(tmp) == 0 or T[i + j * K] != tmp[-1][0]:
				if T[i + j * K] == "r":
					tmp.append(["r", P, 1])
				elif T[i + j * K] == "s":
					tmp.append(["s", R, 1])
				elif T[i + j * K] == "p":
					tmp.append(["p", S, 1])
			else:
				tmp[-1][2] += 1
		for l in range(len(tmp)):
			cnt += tmp[l][1] * math.ceil(tmp[l][2] / 2)

	print(cnt)


main()

n,k=map(int,input().split())
p=list(map(int,input().split()))
t=input()
s='rsp'
dp=[[-10**10]*(n+k) for _ in range(3)]
for i in range(k):
  for j in range(3):
    dp[j][i]=0
for i in range(n):
  for j in range(3):
    for l in range(3):
      if j==l:
        continue
      res=((s.index(t[i])-l+3)%3==1)*p[l]
      if dp[l][i+k]<dp[j][i]+res:
        dp[l][i+k]=dp[j][i]+res
ans=0
for i in range(n,n+k):
  ans+=max(dp[0][i],dp[1][i],dp[2][i])
print(ans)

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

for n in range(N-K):
  if T[n+K]==T[n]:
    T[n+K]=""
    
print(P*T.count("r")+R*T.count("s")+S*T.count("p"))

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

T_split = [[] for i in range(K)]
for i in range(N):
    T_split[i%K].append(T[i])
#print(T_split)

ans = 0
flag = 0

for i in range(len(T_split)):
    for j in range(len(T_split[i])):
        if flag == 1:
            flag = 0
            continue
        if T_split[i][j] == 'r':
            point = P
        elif T_split[i][j] == 's':
            point = R
        elif T_split[i][j] == 'p':
            point = S
        ans += point
        if j != len(T_split[i]) - 1:
            if T_split[i][j] == T_split[i][j+1]:
                flag = 1
print(ans)


def jan(x,a,b,c):
    if x == "s":
        return a
    elif x == "r":
        return c
    else:
        return b

n,k = map(int,input().split())
r,s,p = map(int,input().split())
r = int(r)
s = int(s)
p = int(p)
t = list(input())
m = [0]*n
ans = 0

for i in range(n):
    if i>=k:
        if t[i] == t[i-k]:
            if m[i-k] == 1:
                ans += jan(t[i],r,s,p)
            else:
                m[i] = 1
        else:
            ans += jan(t[i],r,s,p)
    else:
        ans += jan(t[i],r,s,p)

print(ans)

n, k = map(int ,input().split())
R, S, P= map(int ,input().split())
t = input()

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

ans = 0

for i in range(k):
    check = t[i]
    ans += v[check]
    while True:
        i+= k
        if i >= n:
            break
        if check == t[i]:
            check ='-1'
        else :
            ans += v[t[i]]
            check = t[i]


print(ans)

#!/usr/bin/env python3

#import
#import math
#import numpy as np
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = list(input())

ans = 0

for i in range(K):
    last = ""
    for j in range(i, N, K):
        if T[j] == last:
            last = ""
            continue

        if T[j] == "r":
            ans += P
        elif T[j] == "s":
            ans += R
        else:
            ans += S

        last = T[j]

print(ans)


N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()+'*'*K
h=['']*N
f,n='',''
ans=0
for i in range(N):
    if K<=i:f=h[i-K]
    n=T[i+K]
    if T[i]=='r':
        if f=='p':h[i]=n
        else:ans+=P;h[i]='p'
    if T[i]=='p':
        if f=='s':h[i]=n
        else:ans+=S;h[i]='s'
    if T[i]=='s':
        if f=='r':h[i]=n
        else:ans+=R;h[i]='r'
print(ans)

from collections import deque


def calc(t, r, s, p):
    if t == "r":
        return p
    elif t == "s":
        return r
    else:
        return s


n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
T = input()
d = deque()
ans = 0
for i, t in enumerate(T):
    if i < k:
        ans += calc(t, r, s, p)
        d.append(t)
    else:
        x = d.popleft()
        if x == t:
            d.append(0)
        else:
            ans += calc(t, r, s, p)
            d.append(t)
print(ans)


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

me = ""

ans = 0

for i in range(n):
    if t[i] == "r":
        if i < k:
            me += "p"
            ans += p
        elif me[i-k] != "p":
            me += "p"
            ans += p
        else:
            if t[i-k] == "p":
                me += "p"
                ans += p
            else:
                me += "r"

    elif t[i] == "s":
        if i < k:
            me += "r"
            ans += r
        elif me[i-k] != "r":
            me += "r"
            ans += r
        else:
            if t[i-k] == "r":
                me += "r"
                ans += r
            else:
                me += "s"

    else:
        if i < k:
            me += "s"
            ans += s
        elif me[i-k] != "s":
            me += "s"
            ans += s
        else:
            if t[i-k] == "s":
                me += "s"
                ans += s
            else:
                me += "p"

print(ans)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = [ti for ti in input()]
score = {"z": 0, "r": p, "s": r, "p": s}
for i in range(k, n):
    if t[i] == t[i-k]: t[i] = "z"
print(sum([score[ti] for ti in t]))

def main():
    n,k = list(map(int,input().split()))
    r,s,p = list(map(int,input().split()))
    t = input()
    dp = [0]*n
    hnd = [0]*n
    point = {'r':p,'s':r,'p':s}
    h = {'r':'p','s':'r','p':'s'}
    dp[0]=point[t[0]]
    hnd[0] = h[t[0]]
    for i in range(1,k):
        dp[i] = dp[i-1]+point[t[i]]
        hnd[i] = h[t[i]]
    for i in range(k,n):
        if hnd[i-k]!=h[t[i]]:
            hnd[i] = h[t[i]]
            dp[i] += dp[i-1]+point[t[i]]
        else:
            dp[i]=dp[i-1]
    print((dp[-1]))

def __starting_point():
    main()

__starting_point()


N,K=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))

S=input()
li=[1]*(K)
ans=[0,0,0]

for i in range(N-K):
    if S[i]==S[i+K]:
        li[i%K]+=1
    else:
        if S[i]=="r":
            ans[0]+=(li[i%K]+1)//2
        if S[i]=="s":
            ans[1]+=(li[i%K]+1)//2
        if S[i]=="p":
            ans[2]+=(li[i%K]+1)//2
        li[i%K]=1


for j in range(N-K,N):
    if S[j]=="r":
            ans[0]+=(li[j%K]+1)//2
    if S[j]=="s":
            ans[1]+=(li[j%K]+1)//2
    if S[j]=="p":
            ans[2]+=(li[j%K]+1)//2
    
    
print((p*ans[0]+r*ans[1]+s*ans[2]))


def rle(a):
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append([l,a[i]])
            l=1
    ll.append([l,a[-1]])
    return ll
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
w=[]
for i in range(k):
    j=i
    b=""
    while j<n:
        b+=t[j]
        j+=k
    w.append(b)
for i in t:
    if i=="r":
        ans+=p
    elif i=="s":
        ans+=r
    else:
        ans+=s
for x in w:
    st=rle(x)
    for i,j in st:
        if j=="r":
            ans-=(i//2)*p
        elif j=="s":
            ans-=(i//2)*r
        else:
            ans-=(i//2)*s
print(ans)

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

rsp = {"r":p, "s":r, "p":s}
ans = 0
for i in range(n):
  if i < k: ans += rsp[t[i]]
  else:
    if t[i] != t[i-k]: ans += rsp[t[i]]
    else: t[i] = "a"
print(ans)

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

res = 0
log = []
for i in range(len(read_data)):
    # k = 2
    if len(log) < k:
        if read_data[i] == 'r':
            res+=p
        if read_data[i] == 's':
            res+=r
        if read_data[i] == 'p':
            res+=s

        log.append(read_data[i])

    else:
        tmp = log.pop(0)
        if tmp != read_data[i]:
            if read_data[i] == 'r':
                res+=p
            if read_data[i] == 's':
                res+=r
            if read_data[i] == 'p':
                res+=s
            log.append(read_data[i])
        else:
            log.append('-')
    
    
print(res)

from collections import deque
n,k = map(int, input().split())
r,s,p = map(int, input().split())

point = {}
point["r"]=r
point["s"]=s
point["p"]=p

t = input()
# グー:r
# チョキ:s
# パー:p

def janken(opp, rireki, point, k):
    if opp=="r":
        rireki.append("p")
        return point["p"]
    elif opp=="s":
        rireki.append("r")
        return point["r"]
    elif opp=="p":
        rireki.append("s")
        return point["s"]
    
        
ans = 0
rireki = deque()
for i in range(n):
    hand = t[i]
    ans += janken(hand, rireki, point, k)
for i in range(k,n):
    if rireki[i]==rireki[i-k]:
        ans -= point[rireki[i]]
        rireki[i]="else"
print(ans)

N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T=input()
l=[]
for i in range(K):
    l.append([T[K-i-1]])
for j in range(K,N):
    if l[K-1-j%K][j//K-1]!=T[j]:
        l[K-1-j%K].append(T[j])
    else:
        l[K-1-j%K].append("t")
ans=0
for k in l:
    ans+=k.count("r")*P+k.count("s")*R+k.count("p")*S
print(ans)
    
    




        




    









        

    









    

        




    















    



    





    






        






    
















        


























            
    


    


    
    














    


    














n, k = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

for i in range(n-k):
    if T[i] == T[i+k]:
        T[i+k] = 0
        
ans = [0] * n
for i in range(n):
    if T[i] == "r":
        ans[i] = P
    if T[i] == "s":
        ans[i] = R
    if T[i] == "p":
        ans[i] = S

print(sum(ans))

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

c = [''] * n
ans = 0
for i in range(n):
    if t[i] == 'r':
        rsp = 'p'
        point = p
    elif t[i] == 's':
        rsp = 'r'
        point = r
    elif t[i] == 'p':
        rsp = 's'
        point = s
    
    if i >= k and c[i-k] == rsp:
        rsp = ''
        point = 0
    
    c[i] = rsp
    ans += point
print(ans)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
dp = [0] * (len(t) + 1)
dp[0] = 0

def pon(d):
 if d  == 'r':
    return p
 elif d == 's':
    return r
 else:
    return s

m = []
for i,j in enumerate(t):
    m.append(j)
    if i >= k:
        if m[i-k] == j:
            dp[i+1] = dp[i]
            m[i] = 'skip'
        else:
            dp[i+1] = dp[i]+pon(j)
    else:
        dp[i+1] = dp[i] + pon(j)
print(dp[-1])

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines

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

    dp = [[0] * 3 for _ in range(n + k)]
    for i1 in range(k):
        i2 = i1 + k
        while i2 - k < n:
            dp[i2][0] = max(dp[i2-k][1], dp[i2-k][2])
            dp[i2][0] += r if t[i2-k] == 's' else 0
            dp[i2][1] = max(dp[i2-k][0], dp[i2-k][2])
            dp[i2][1] += s if t[i2-k] == 'p' else 0
            dp[i2][2] = max(dp[i2-k][0], dp[i2-k][1])
            dp[i2][2] += p if t[i2-k] == 'r' else 0
            i2 += k
    r = 0
    for j1 in range(-k, 0, 1):
        r += max(dp[j1])
    print(r)

def __starting_point():
    main()
__starting_point()

n,k = (int(x) for x in input().split())
r,s,p = (int(x) for x in input().split())
t = input()
a = []
ans = 0

for i in range(0,n):
    a.append(t[i])

for j in range(0,n):
    if j - k >= 0 and a[j - k] == a[j]:
           a[j] = "o"
    p
    if a[j] == "r":
        ans = ans + p
    if a[j] == "s":
        ans = ans + r
    if a[j] == "p":
        ans = ans + s


print(ans)

import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    RSP_score = LI()
    rsp_num = {'r': 0, 's': 1, 'p': 2}
    T = [rsp_num[i] for i in S()]

    # dp[i][j]: i回目にjを出したときの合計点数
    # Kのmod毎に独立
    dp = [[0] * 3 for _ in range(N)]
    for i in range(N):
        for j in range(3):
            # グーを出す場合、K回前までにグー以外出したときの最大得点に加えて、今回相手がチョキだったら勝点Rを足す
            if i - K >= 0:
                dp[i][j] += max([dp[i-K][k] for k in range(3) if k != j])
            if (j + 1) % 3 == T[i]:
                dp[i][j] += RSP_score[j]

    # for i in dp:
    #     print(i)
    ans = sum([max(i) for i in dp[N-K:]])
    print(ans)

def __starting_point():
    resolve()
__starting_point()

import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
 
        if x == y:
            return
 
        if self.parents[x] > self.parents[y]:
            x, y = y, x
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
 
    def size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
 
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
mod = 10**9 + 7
sigma = lambda x : x*(x+1)//2

#--maincode----------------

n,k = inm()
r,s,p = inm()
t = input()
d = {'r':p,'s':r,'p':s}
l = []
ans = 0
for i in range(k):
    ans += d[t[i]]
for i in range(k,n):
    if t[i-k] == t[i]:
        t = t[:i] + 'n' + t[i+1:]
        continue
    ans += d[t[i]]
print(ans)

n,k=map(int,input().split())
B=list(map(int,input().split()))
S=list(input())
A=[[] for i in range(k)]
for i in range(n):
  if S[i]=="s":
    A[i%k].append(0)
  elif S[i]=="p":
    A[i%k].append(1)
  else:
    A[i%k].append(2)
ans=0
for i in range(k):
  f=0
  for j in range(len(A[i])):
    if f==0:
      ans=ans+B[A[i][j]]
      if j!=len(A[i])-1:
        if A[i][j]==A[i][j+1]:
          f=1
    else:
      f=0
print(ans)

n, k = map(int, input().split())

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

t = input()

li = list(t[:k])

cnt = 0

for itr, val in enumerate(t):

  if itr >= k and val == li[itr % k]:

    li[itr % k] = "x"

  else:

    if val == "r":

      cnt += p

    elif val == "s":

      cnt += r

    else:

      cnt += s

    li[itr % k] = val

print(cnt)

N, K = map(int, input().split())
R, S, P = map(int, input().split())
p = {"r":P, "s":R, "p":S}

T = [s for s in input()]
ans = 0
for i in range(N):
  if i >= K and T[i] == T[i-K]:
    T[i] = "."
    continue
  #print(p[T[i]])
  ans += p[T[i]]
print(ans)

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

scr = 0
#k回目までの処理
for i in range(k):
    if t[i] == "r":
        scr += p
    elif t[i] == "s":
        scr += r
    elif t[i] == "p":
        scr += s
#print(scr)

#k＋１回目以降の処理
#前に出した手のせいで勝てない場合は、次の手の邪魔にならないような手を出す
for i in range(k, n):
    if t[i] == t[i - k]:
        scr += 0
        t[i] = "q"
    else:
        if t[i] == "r":
            scr += p
        elif t[i] == "s":
            scr += r
        elif t[i] == "p":
            scr += s

print(scr)


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

score_list = []

ans = 0
for i in range(K):
    pre_word = "dummy"
    for j in range(i,N,K):
        if(T[j] != pre_word):
            if(T[j] == "r"):
                ans += P
                pre_word = "r"
            elif(T[j] == "s"):
                ans += R
                pre_word = "s"
            else:
                ans += S
                pre_word = "p"
        else:
            pre_word = "dummy"
        

print(ans)

N,K = map(int,input().split())
S,P,R = map(int,input().split())
point = {'s':S,'p':P,'r':R}
T = str(input())
max_point = 0
for i in range(K):
  while i < N:
    if i+K < N and T[i] == T[i+K]:
      max_point += point[T[i]]
      i = i+2*K
    else:
      max_point += point[T[i]]
      i = i+K
print(max_point)

N,K=list(map(int,input().split()))
R,S,P=list(map(int,input().split()))
T=input()
cnt=[0 for _ in range(N)]
score={'r':P,'s':R,'p':S}
for i in range(N):
    if i<K:
        cnt[i]=score[T[i]]
    else:
        if T[i-K]!=T[i]:
            cnt[i]=score[T[i]]
        else:
            if cnt[i-K]==0:
                cnt[i]=score[T[i]]
print((sum(cnt)))


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

def score(hand):
    if hand == 's':
        return r
    if hand == 'p':
        return s
    if hand == 'r':
        return p

ans = 0
changed = [0] * n

for i in range(n):
    if i >= k and t[i] == t[i - k] and changed[i - k] == 0:
        changed[i] = 1
        continue    
    ans += score(t[i])

print(ans)


n, k = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

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

hand = []
for t in T:
    hand.append(dict[t])

for i in range(n - k):
    if hand[i] == hand[i + k]:
        hand[i + k] = 'x'

ans = 0
for t in hand:
    if t == 'r':
        ans += R
    elif t == 's':
        ans += S
    elif t == 'p':
        ans += P
print(ans)

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

tt = list(t)
d = {'r':p, 's': r, 'p':s}
dp = [d[t[i]] for i in range(n)]

for i in range(n-k):
    if tt[i]==tt[i+k]:
        dp[i+k] = 0
        tt[i+k] = 'x'

print((sum(dp)))



def main():
    N,K = list(map(int,input().split()))
    R,S,P = list(map(int,input().split()))
    T = input()
    dict = {'r':P,'s':R, 'p':S}
    check = []
    ans = 0
    for i in range(N):
        if i-K < 0:
            ans += dict[ T[i] ]
            check.append(1)
        else:
            if T[i-K] != T[i]:
                ans += dict[ T[i] ]
                check.append(1)
            elif T[i-K] == T[i]:
                if i-2*K < 0:
                    ans += 0
                    check.append(0)
                else:
                    if T[i-2*K] == T[i]:
                        if check[i-K] == 1:
                            ans += 0
                            check.append(0)
                        else:
                            ans += dict[ T[i] ]
                            check.append(1)
                    else:
                        ans += 0
                        check.append(0)
    return ans

print((main()))


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

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

a = [trans[t] for t in T]

for i in range(K, N):
    if a[i] == a[i-K]:
        a[i] = 't'
print(sum([score[s] for s in a]))

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
dic = {'r':P,'s':R,'p':S}
ans  = 0
for i in range(K):
    ans += dic[T[i]]
for i in range(K,N):
    if T[i-K] != T[i]:
        ans += dic[T[i]]
    else:
        T[i] = 'e'
print(ans)

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
 
for n in range(N-K):
  if T[n+K]==T[n]:
    T[n+K]=""
    
print(P*T.count("r")+R*T.count("s")+S*T.count("p"))

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = list(map(int, readline().split()))
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    ans = 0
    for i in range(K):
        M = (N - i + K - 1) // K
        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == T[i + j * K]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


def __starting_point():
    main()

__starting_point()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = list(map(int, readline().split()))
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    hand = [0] * N
    ans = 0
    for i, h in enumerate(T):
        win = (h - 1) % 3
        if i >= K and win == hand[i - K]:
            hand[i] = -1
        else:
            hand[i] = win
            ans += point[win]

    print(ans)
    return


def __starting_point():
    main()

__starting_point()

n, k = map(int, input().split())
s, p, r = map(int, input().split()) 
dic = {"p":p, "r":r, "s":s}
t = input()
ans = 0
for i in range(k):
    bef = "a"
    for j in range(i, n, k):

        if bef == t[j]:
            bef = "a"
        else:
            ans += dic[t[j]]
            bef = t[j]
print(ans)

#!/usr/bin/env python3

def main():
    n, k = list(map(int, input().split()))
    r, s, p = list(map(int, input().split()))
    t = input()
    c = ""
    ans = 0

    for i in range(n):
        if t[i] == "r":
            if i >= k and c[i-k] == 'p':
                c += 'x'
            else:
                c += "p"
                ans += p
        elif t[i] == "s":
            if i >= k and c[i-k] == 'r':
                c += 'x'
            else:
                c += "r"
                ans += r
        else:
            if i >= k and c[i-k] == 's':
                c += 'x'
            else:
                c += "s"
                ans += s
    print(ans)


def __starting_point():
    main()

__starting_point()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-08-30 13:56:28 +0900
# LastModified: 2020-08-30 14:22:16 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from collections import Counter


def main():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = input()
    takahashi = ''
    for t in T:
        if t == 's':
            takahashi += 'r'
        elif t == 'p':
            takahashi += 's'
        else:
            takahashi += 'p'

    for i in range(K, N):
        if takahashi[i-K] == takahashi[i]:
            takahashi = takahashi[:i] + '_' + takahashi[i+1:]
    ans = takahashi.count('r')*R + takahashi.count('s')*S + takahashi.count('p')*P
    print(ans)

def __starting_point():
    main()

__starting_point()

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


point = {'r':P, 's':R, 'p':S}
win = [False] * N
total = 0
for i in range(N):
    if i < K or T[i] != T[i - K] or win[i - K] == False:
        total += point[T[i]]
        win[i] = True

print(total)

from math import ceil
def point(x,y,r,s,p):
    if y == 'r':
        y = p
    if y == 's':
        y = r
    if y == 'p':
        y = s
    return ceil(x/2) * y
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
ans = 0
for _ in range(k):
    l = t[_::k]
    x = 1
    y = l[0]
    for i in l[1:]:
        if i != y:
            ans += point(x,y,r,s,p)
            x = 1
            y = i
        else:
            x += 1
    ans += point(x,y,r,s,p)
print(ans)

import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return list(map(int, input().split()))
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

import string

def main():
    n,k = i_map()
    r,s,p = i_map()
    dic = {"r":p,"s":r,"p":s}
    t = list(s_input())
    ans = 0

    for i,j in enumerate(t):
        if i < k:
            ans += dic[j]
        else:
            if j != t[i-k]:
                ans += dic[j]
            else:
                t[i] = "z"
    print(ans)



def __starting_point():
    main()

__starting_point()

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

t_list = [ c for c in t ]
for i in range( k, n ):
    if t_list[ i ] == t_list[ i - k ]:
        t_list[ i ] = "x"

score = 0
for hand in t_list:
    if hand == "r":
        score += p
    elif hand == "s":
        score += r
    elif hand == "p":
        score += s

print( score )

n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
T = input()
record = [False for _ in range(n)]
ans = 0


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


for i, t in enumerate(T):
    if i < k:
        ans += win(t)
        record[i] = t

    else:
        f = record[i - k]
        if f:
            if f == t:
                continue
            else:
                ans += win(t)
                record[i] = t
        else:
            ans += win(t)
            record[i] = t
print(ans)


def inp(r, s, p, jan):
  if jan == 's':
    return r
  elif jan == 'p':
    return s
  else:
    return p

n, k = map(int, input().split(" "))
r,s,p = map(int, input().split(" "))
t = list(input())
total = 0
for i in range(n):
  if i < k:
    #print(2)
    total += inp(r,s,p,t[i])
  else:
    if t[i] != t[i-k]:
      #print("OK")
      total += inp(r,s,p,t[i])
    else:
      #print("NG")
      t[i] = 'x'
print(total)

n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
for i in range(k):
  R=0
  S=0
  P=0
  cnt=0
  while i+cnt*k<n:
    if i+cnt*k-k<0:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    elif i+cnt*k-k>=0 and t[i+cnt*k]==t[i+cnt*k-k]:
      if t[i+cnt*k]=='r' and P==0:
        ans+=p
        P=1
      elif t[i+cnt*k]=='r' and P==1:
        P=0
      elif t[i+cnt*k]=='s' and R==0:
        ans+=r
        R=1
      elif t[i+cnt*k]=='s' and R==1:
        R=0
      elif t[i+cnt*k]=='p' and S==0:
        ans+=s
        S=1
      elif t[i+cnt*k]=='p' and S==1:
        S=0
    elif i+cnt*k-k>=0 and t[i+cnt*k]!=t[i+cnt*k-k]:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    cnt+=1
print(ans)

def main():
  n,k=map(int,input().split())
  p=list(map(int,input().split()))
  t=input()
  s='rsp'
  dp=[[-10**10]*(n+k) for _ in range(3)]
  for i in range(k):
    for j in range(3):
      dp[j][i]=0
  for i in range(n):
    for j in range(3):
      for l in range(3):
        if j==l:
          continue
        res=((s.index(t[i])-l+3)%3==1)*p[l]
        if dp[l][i+k]<dp[j][i]+res:
          dp[l][i+k]=dp[j][i]+res
  ans=0
  for i in range(n,n+k):
    ans+=max(dp[0][i],dp[1][i],dp[2][i])
  print(ans)
main()

import sys
input = sys.stdin.readline



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

    D = [[] for _ in range(K)]
    for i, t in enumerate(T):
        D[i%K].append(t)

    ans = 0
    for List in D:
        pre = -1
        for i, p in enumerate(List):
            if p == "r":
                s = P
            elif p == "s":
                s = R
            else:
                s = S
            if pre != p:
                ans += s
                pre = p
            else:
                pre = -1
    print(ans)
            

def __starting_point():
    main()
__starting_point()

N, K = map(int,input().split())
R, S, P = map(int,input().split())
d = {'r': P, 's': R, 'p': S}
T = input()

ans = 0

for i in range(min(N, K)):
    flag = 0
    tmp = T[i::K]
    for j in range(len(tmp) - 1):
        if tmp[j] != tmp[j+1] or flag:
            ans += d[tmp[j]]
            flag = 0
        else:
            flag = 1
    ans += d[tmp[-1]]

print(ans)

import math
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
t = input()
#r<-p, s<-r, p<-s
dic = {'r': p, 's': r, 'p': s}
point = 0
lose = False

for j in range(k):
    point += dic[t[j]]
    for i in range(j+k, n, k):
        if t[i-k] != t[i] or lose:
            point += dic[t[i]]
            lose = False
        else:
            lose = True
    lose = False
print(point)


N, K = list(map(int, input().split()))
pt = list(map(int, input().split()))
T = input()

pc_hand = [0]*N
my_hand = [0]*N

for i in range(N):
    if T[i] == 'r':
        pc_hand[i] = 0
    elif T[i] == 's':
        pc_hand[i] = 1
    else:
        pc_hand[i] = 2

point = 0

for i in range(N):
    if i >= K and T[i] == T[i - K] and (my_hand[i - K] + 1) % 3 == pc_hand[i - K]:
        my_hand[i] = pc_hand[i]
    else:
        my_hand[i] = (pc_hand[i] - 1) % 3
        point += pt[my_hand[i]]

print(point)

n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
x=list(t)
ans=0
for i in range(n):
  if x[i]=="r":x[i]="p"
  elif x[i]=="p":x[i]="s"
  else:x[i]="r"
for i in range(k):
  if x[i]=="r":ans+=r
  elif x[i]=="p":ans+=p
  else:ans+=s
for i in range(k,n):
  if x[i]==x[i-k]:
    x[i]="-"
    continue
  if x[i]=="r":ans+=r
  elif x[i]=="p":ans+=p
  else:ans+=s
print(ans)

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

#dp[i][j]:i回目にjを出した時のそれまでの合計点の最大値。j:0グー1チョキ2パー
dp = [[0 for j in range(3)] for i in range(n)]
for i in range(0,k):
    if T[i] == 'r':
        dp[i][2] = p 
    elif T[i] == 's':
        dp[i][0] = r 
    else:
        dp[i][1] = s 
    for j in range(k,n-i,k):
        if T[i+j] == 'r':
            dp[i+j][2] = max(dp[i+j-k][0]+p,dp[i+j-k][1]+p)
            dp[i+j][1] = max(dp[i+j-k][0],dp[i+j-k][2])
            dp[i+j][0] = max(dp[i+j-k][1],dp[i+j-k][2])
        elif T[i+j] == 's':
            dp[i+j][0] = max(dp[i+j-k][1]+r,dp[i+j-k][2]+r)
            dp[i+j][1] = max(dp[i+j-k][0],dp[i+j-k][2])
            dp[i+j][2] = max(dp[i+j-k][0],dp[i+j-k][1])
        else:
            dp[i+j][1] = max(dp[i+j-k][2]+s,dp[i+j-k][0]+s)
            dp[i+j][0] = max(dp[i+j-k][1],dp[i+j-k][2])
            dp[i+j][2] = max(dp[i+j-k][0],dp[i+j-k][1])
    
total = 0
#print(dp)
for i in range(n-1,max(0,n-1-k),-1):
    total += max(dp[i])
print(total)
        
    



def decide_hand2(T, hand, i, K, N):
    if T[i] == "r":
        if i <= K - 1:
            hand[i] = "p"
        else:
            if hand[i - K] == "p":
                hand[i] = "-"
            else:
                hand[i] = "p"

    elif T[i] == "p":
        if i <= K - 1:
            hand[i] = "s"
        else:
            if hand[i - K] == "s":
                hand[i] = "-"
            else:
                hand[i] = "s"

    elif T[i] == "s":
        if i <= K - 1:
            hand[i] = "r"
        else:
            if hand[i - K] == "r":
                hand[i] = "-"
            else:
                hand[i] = "r"

def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    hand = ["" for _ in range(N)]

    for i in range(N):
        decide_hand2(T, hand, i, K, N)

    ans = 0
    for i in range(N):
        if hand[i] == "r":
            ans += R
        elif hand[i] == "s":
            ans += S
        elif hand[i] == "p":
            ans += P

    print(ans)
    
def __starting_point():
    main()
__starting_point()

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

ans = 0
check = [0] * N

for i in range(N):
  j = T[i]
  if i >= K:
    if j == check[i-K]: continue
  if j == "r": ans += P
  elif j == "s": ans += R
  else: ans += S
  check[i] = j

print(ans)

n, k = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

for i in range(n-k):
    if T[i] == T[i+k]:
        T[i+k] = ""

ans = 0
for i in range(n):
    if T[i] == "r":
        ans += P
    if T[i] == "s":
        ans += R
    if T[i] == "p":
        ans += S

print(ans)

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
d = []
for i in range(N):
    if T[i] == "r":
        if i < K or d[i-K] != "p":
            d.append("p")
        else:
            d.append("a")
    elif T[i] == "s":
        if i < K or d[i-K] != "r":
            d.append("r")
        else:
            d.append("a")
    else:
        if i < K or d[i-K] != "s":
            d.append("s")
        else:
            d.append("a")
ans = d.count("r") * R + d.count("s") * S + d.count("p") * P
print(ans)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
l = [0] * n

for i in range(n):
    if t[i] == 'r':
        l[i] = p
    elif t[i] == 's':
        l[i] = r
    else:
        l[i] =s

ans = sum(l)
for i in range(k, n):
    if t[i] == t[i - k]:
        ans -= l[i]
        t[i] = 'o'

print(ans)

# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 22:36:36 2020

@author: liang
"""

N, K = list(map(int,input().split()))
R, S, P = list(map(int,input().split()))
T = input()
hand = {"r":P, "s":R, "p":S}
ans = 0
for i in range(K):
    s = str()
    tmp = i
    while tmp < N:
        s += T[tmp]
        tmp += K
    #print(s)
    count = 1
    prev = str()
    for j in range(len(s)):
        if s[j] == prev:
            count += 1
        else:
            count = 1
        if count%2 == 0:
            continue
        prev = s[j]
        """
        if s[j] == "r":
            ans += P
        elif s[j] == "s":
            ans += R
        else:
            ans += S
        """
        ans += hand[s[j]]
print(ans)


n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = [_ for _ in str(input())]
ans = 0
for i in range(n):
  if  i - k < 0:
    if t[i] == "r":
      ans += p
    elif t[i] == "s":
      ans += r
    elif t[i] == "p":
      ans += s
  else:
    if t[i] == "r" and t[i-k] != "r":
      ans += p
    elif t[i] == "s" and t[i-k] != "s":
      ans += r
    elif t[i] == "p" and t[i-k] != "p":
      ans += s
    else:
      t[i] = "a"
print(ans)

n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
rsp={"r":p,"s":r,"p":s}
sumt=0
chk=[1]*n
for i in range(len(t)):
  sumt+=rsp[t[i]]
  if i>=k and t[i]==t[i-k]:
    chk[i]=chk[i-k]+1
    chk[i-k]=1
ans=sumt
for i in range(len(chk)):
  if chk[i]!=1:
    chg=chk[i]//2
    ans-=(rsp[t[i]])*chg
print(ans)

N, K = map(int, input().split())
dic = {}
dic['s'], dic['p'], dic['r'] = map(int, input().split())
T = input()
score = [0]*N
for i in range(N):
  if i>=K and T[i]==T[i-K] and score[i-K]>0:
    continue
  score[i] = dic[T[i]]
print(sum(score))

n,k=map(int,input().split())
r,s,p=map(int,input().split())
d={'r':p,'s':r,'p':s}
t=input()
ans=0
for i in range(k):
  x=t[i]
  ans+=d[x]
  for j in range(i+k,n,k):
    if t[j]!=x:
      x=t[j]
      ans+=d[x]
    else:
      x='z'
print(ans)

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

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

score = []
for i in range(N):
    if i < K:
        score.append(s[T[i]])
        continue
    if T[i] == T[i-K] and score[i-K] != 0:
        score.append(0)
    else:
        score.append(s[T[i]])

print((sum(score)))


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

    ans = 0
    U = []
    for i, t in enumerate(T):
        if t == 'r':
            if i-K >= 0:
                if U[i-K] == 'p':
                    U.append('rs')
                    continue
            U.append('p')
            ans += P
        elif t == 's':
            if i-K >= 0:
                if U[i-K] == 'r':
                    U.append('sp')
                    continue
            U.append('r')
            ans += R
        else:
            if i-K >= 0:
                if U[i-K] == 's':
                    U.append('rp')
                    continue
            U.append('s')
            ans += S
    print(ans)

def __starting_point():
    main()

__starting_point()

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

point = {"r":(p,"p"), "s":(r,"r"), "p":(s,"s")}
ans = 0

hand = ""
for i in range(n):
  if i < k:
    ans += point[t[i]][0]
    hand += point[t[i]][1]
  else:
    if (t[i-k] != t[i]):
      ans += point[t[i]][0]
      hand += point[t[i]][1]
    else:
      if hand[i-k] != point[t[i]][1]:
        ans += point[t[i]][0]
        hand += point[t[i]][1]
      else:
        if (i+k) < n:
          hand += "rsp".replace(point[t[i+k]][1],"").replace(hand[i-k],"")[0]
        else:
          hand += "rsp".replace(hand[i-k],"")[0]
  
      
print(ans)

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

ans=0
chk=[0]*N
for n in range(N):
  c=T[n]
  if n>=K and T[n-K]==c and chk[n-K]==0:
    chk[n]=1 # not used
    continue
  if c=='r':
    ans+=P
  elif c=='s':
    ans+=R
  else:
    ans+=S

print(ans)

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

ans = 0
for i in range(0, K):
    rsp_K = T[i::K]
    rsp_before = ''
    for rsp in rsp_K:
        if rsp == 'r':
            if rsp_before != 'r':
                ans += P
                rsp_before = 'r'
            else:
                rsp_before = ''
        elif rsp == 's':
            if rsp_before != 's':
                ans += R
                rsp_before = 's'
            else:
                rsp_before = ''
        elif rsp == 'p':
            if rsp_before != 'p':
                ans += S
                rsp_before = 'p'
            else:
                rsp_before = ''

print(ans)


N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
G=[[] for n in range(K)]

for n in range(N):
  G[n%K].append(T[n])

cnt=0
for g in G:
  r,s,p=True,True,True
  for g2 in g:
    if g2=='r':
      if p:
        p=False
        r,s=True,True
        cnt+=P
      else:
        r,s,p=True,True,True
    if g2=='s':
      if r:
        r=False
        p,s=True,True
        cnt+=R
      else:
        r,s,p=True,True,True
    if g2=='p':
      if s:
        s=False
        r,p=True,True
        cnt+=S
      else:
        r,s,p=True,True,True

print(cnt)

from collections import deque
N,K = map(int,input().split())
R,S,P = map(int,input().split())
Tlist = input()
Answer = 0
def po(x):
    if x == 's':
        return R
    elif x == 'r':
        return P
    else:
        return S
for i in range(K):
    TK = list(Tlist[i::K]).copy()
    TK.append('')
    T2 = TK[0]
    sig = 2
    for i in range(1,len(TK)):
        T1 = T2
        T2 = TK[i]
        if T1 == T2 and i != len(TK)-1:
            sig += 1
        else:
            Answer += po(T1)*(sig//2)
            sig = 2
print(Answer)

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

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

score = 0
human = [0] * N

for i in range(K):
    human[i] = cnv[T[i]]
    score += p[human[i]]

for i in range(K, N):
    #human[i] = cnv[T[i]]
    if human[i - K] == cnv[T[i]]:
        #human[i] = T[i]
        continue
    else:
        human[i] = cnv[T[i]]
        score += p[human[i]]

print(score)


n,k=map(int,input().split())
rp,sp,pp=map(int,input().split())
p={"r":pp,"s":rp,"p":sp}
t=[s for s in input()]
ans=0
for i in range(n):
    if i>=k and t[i]==t[i-k]:
        t[i]="."
        continue
    ans+=p[t[i]]
print(ans)

def main():
    N,K = map(int,input().split())
    R,S,P = map(int,input().split())
    T = input()
    dict = {'r':P,'s':R, 'p':S}
    check = []
    ans = 0
    for i in range(N):
        if i-K < 0:
            ans += dict[ T[i] ]
            check.append(1)
        else:
            if T[i-K] != T[i]:
                ans += dict[ T[i] ]
                check.append(1)
            elif T[i-K] == T[i]:
                if i-2*K < 0:
                    ans += 0
                    check.append(0)
                else:
                    if T[i-2*K] == T[i]:
                        if check[i-K] == 1:
                            ans += 0
                            check.append(0)
                        else:
                            ans += dict[ T[i] ]
                            check.append(1)
                    else:
                        ans += 0
                        check.append(0)
    return ans
 
print(main())

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

# point = {'r':P, 's':R, 'p':S}
# skip = {'r':[], 's':[], 'p':[]}

# ans = 0
# cnt = 0
# for i in range(N):
# 	cnt += 1
# 	spell = T[i]
# 	if i < K:
# 		ans += point[spell]
# 		continue
# 	if T[i - K] == spell:
# 		if i - K in skip[spell]:
# 			ans += point[spell]
# 		else:
# 			skip[spell].extend([i])
# 	else:
# 		ans += point[spell]
# print(ans)

ans = [''] * N
point = 0

for i, t in enumerate(T):
	if t == 'r':
		ans_tmp = 'p'
		point_tmp = P
	elif t == 's':
		ans_tmp = 'r'
		point_tmp = R
	elif t == 'p':
		ans_tmp = 's'
		point_tmp = S

	if i - K >= 0:
		if ans[i - K] == ans_tmp:
			ans_tmp = ''
			point_tmp = 0

	ans[i] = ans_tmp
	point += point_tmp

print(point)



n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = 0
for i in range(k):
    l = "z"
    m = 0
    for c in t[i::k]:
        if c == l:
            m += 1
        else:
            if l == "r":
                a += p*(m//2)
            elif l == "s":
                a += r*(m//2)
            elif l == "p":
                a += s*(m//2)
            l = c
            m = 2
    else:
        if l == "r":
            a += p*(m//2)
        elif l == "s":
            a += r*(m//2)
        elif l == "p":
            a += s*(m//2)
print(a)

# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 22:36:36 2020

@author: liang
"""

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

ans = 0
for i in range(0,K):
    s = str()
    tmp = i
    while tmp < N:
        s += T[tmp]
        tmp += K
    #print(s)
    count = 1
    prev = str()
    for j in range(len(s)):
        if s[j] == prev:
            count += 1
        else:
            count = 1
        if count%2 == 0:
            continue
        prev = s[j]
        if s[j] == "r":
            ans += P
        elif s[j] == "s":
            ans += R
        else:
            ans += S
print(ans)


N,K = map(int,input().split())
RSP = list(map(int,input().split()))
T = input()

# dp[i][j]: i 回目に j を出したときの得点
# 0: グー, 1: チョキ, 2: パー
dp = [[0]*3 for _ in range(N+1)]

def judge(a,b):
    # パーで勝ったとき
    if a == 'r' and b == 2:
        return RSP[2]
    # グーで勝ったとき
    elif a == 's' and b == 0:
        return RSP[0]
    # チョキで勝ったとき
    elif a == 'p' and b == 1:
        return RSP[1]
    return 0

# i 回目のじゃんけん
for i in range(1,N+1):
    # i 回目に出した手
    for j in range(3):
        # i-K 回目に出した手
        for k in range(3):
            # K 回前と同じ手を出したとき
            if j == k:
                continue
            # j を出したとき
            dp[i][j] = max(dp[i][j],dp[i-K][k] + judge(T[i-1],j))
            
ans = 0
for k in range(K):
    ans += max(dp[N-k])
print(ans)

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

# Mod Kでは、直前に出した手を出せない制約
# それぞれ最善手を求めて、得点を合計する
ans = 0
for i in range(k):
    R,S,P = 0,0,0
    for hand in t[i::k]:
        if hand == 'r':
            R,S,P = max(S,P), max(R,P), max(R,S) + p
        elif hand == 's':
            R,S,P = max(S,P) + r, max(R,P), max(R,S)
        elif hand == 'p':
            R,S,P = max(S,P), max(R,P) + s, max(R,S)
    ans += max(R,S,P)

print(ans)

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

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