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())
T=input().replace("s","R").replace("p","S").replace("r","P")

ans=0

def win(type):
  if type=="R":
    return R
  elif type=="S":
    return S
  elif type=="P":
    return P
  return 0

yaku=set(["R","S","P"])
def rest(a,b):
  re=yaku-set([a,b])
  for c in re:
    return c

def restRand(a):
  re=yaku-set([a])
  for c in re:
    return c

me=[""]*len(T)
for i in range(len(T)):
  if i-K<0:
    ans+=win(T[i])
    me[i]=T[i]
  else:
    if me[i-K]==T[i]:
      
      
      if i+K<len(T):
        me[i]=rest(T[i],T[i+K])
      else:
        me[i]=restRand(T[i])
    else:
      
      ans+=win(T[i])
      me[i]=T[i]

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,K = map(int,readline().split())
R,S,P = map(int,readline().split())
T = read().rstrip().decode('utf-8')

k_to_word = [[] for _ in range(K)]
for i,x in enumerate(T):
    k = i % K
    k_to_word[k].append(x)

def solve(A):
    
    r,s,p = 0,0,0 
    for x in A:
        if x == 'r':
            r,s,p = max(s,p),max(r,p),max(r,s)+P
        elif x == 's':
            r,s,p = max(s,p)+R,max(r,p),max(r,s)
        elif x == 'p':
            r,s,p = max(s,p),max(r,p)+S,max(r,s)
    return max(r,s,p)

answer = sum(solve(A) for A in k_to_word)
print(answer)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

N,K = LI()
R,S,P = LI()
T = list(input())
dp = [[0]*3 for _ in range(N+1)]

def main():
    for i in range(1,N+1):
        if i<K:
            if T[i-1] == 's':
                dp[i] = [R,0,0]
            if T[i-1] == 'p':
                dp[i] = [0,S,0]
            if T[i-1] == 'r':
                dp[i] = [0,0,P]
        else:
            dp[i][0] = max(dp[i-K][1],dp[i-K][2])
            dp[i][1] = max(dp[i-K][0],dp[i-K][2])
            dp[i][2] = max(dp[i-K][0],dp[i-K][1])
            if T[i-1] == 's':
                dp[i][0] += R
            if T[i-1] == 'p':
                dp[i][1] += S
            if T[i-1] == 'r':
                dp[i][2] += P
    ans = 0
    for i in range(K):
        ans += max(dp[N-i])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

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

def count(a1, a2, a3, n, t, k):
    ans = ['a'] * n

    if a1 == 'r':
        win = 's'
        lose = 'p'
    elif a1 == 's':
        win = 'p'
        lose = 'r'
    elif a1 == 'p':
        win = 'r'
        lose = 's'

    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a1
        elif i >= k:
            if t[i] == win and ans[i-k] != a1:
                ans[i] = a1

    if a2 == 'r':
        win = 's'
        lose = 'p'
    elif a2 == 's':
        win = 'p'
        lose = 'r'
    elif a2 == 'p':
        win = 'r'
        lose = 's'
    
    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a2
        elif i >= k:
            if t[i] == win and ans[i-k] != a2:
                ans[i] = a2

    if a3 == 'r':
        win = 's'
        lose = 'p'
    elif a3 == 's':
        win = 'p'
        lose = 'r'
    elif a3 == 'p':
        win = 'r'
        lose = 's'

    for i in range(n):
        if k > i :
            if t[i] == win:
                ans[i] = a3
        elif i >= k:
            if t[i] == win and ans[i-k] != a3:
                ans[i] = a3
    return ans


b1 = count('r', 's', 'p', n, t, k)
b2 = count('r', 'p', 's', n, t, k)

b3 = count('s', 'r', 'p', n, t, k)
b4 = count('s', 'p', 'r', n, t, k)

b5 = count('p', 's', 'r', n, t, k)
b6 = count('p', 'r', 's', n, t, k)

ans = 0
for i in [b1, b2, b3, b4, b5, b6]:
    tmp = 0
    for j in range(n):
        if i[j] == 'r' and t[j] == 's':
            tmp += r
        elif i[j] == 's' and t[j] == 'p':
            tmp += s
        elif i[j] == 'p' and t[j] == 'r':
            tmp += p
    ans = max(tmp, ans)
print(ans)

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

ans = 0
buf = ""
for i in range(N):
    t = T[i]

    if t == "r":
        if i >= K:
            if buf[i - K] == "p":
                buf = buf + " "
                continue
        ans += P
        buf = buf + "p"
    elif t == "p":
        if i >= K:
            if buf[i - K] == "s":
                buf = buf + " "
                continue
        ans += S
        buf = buf + "s"
    elif t == "s":
        if i >= K:
            if buf[i - K] == "r":
                buf = buf + " "
                continue
        ans += R
        buf = buf + "r"

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(4100000)

n,k= [int(i) for i in input().split()]
pts = {}
pts["s"],pts["p"],pts["r"] = [int(i) for i in input().split()]
t = input()
take,notake, chose = [0 for _ in range(n)], [0 for _ in range(n)], ["w" for _ in range(n)]
for i in range(len(t)):
    if i == 0:
        take[0] = pts[t[i]]
        notake[0] = 0
        chose[0] = t[i]
    else:
        if t[i] == chose[i-k]:
            take[i] = max(take[i-1],notake[i-1])
            notake[i] = max(take[i-1],notake[i-1])
        else:
            take[i] = take[i-1] + pts[t[i]]
            notake[i] = max(take[i-1],notake[i-1])
            chose[i] = t[i]


print(max(take[-1],notake[-1]))

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


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

dp = [[0, 0, 0] for _ in range(N)]

for i in range(K):
    Q = T[i]
    if Q == "s":
        dp[i][0] = R
    elif Q == "p":
        dp[i][1] = S
    else:
        dp[i][2] = P

for i in range(K, N):
    Q = T[i]
    if Q == "s":
        dp[i][0] = max(dp[i-K][1], dp[i-K][2]) + R
        dp[i][1] = max(dp[i-K][0], dp[i-K][2])
        dp[i][2] = max(dp[i-K][0], dp[i-K][1])
    elif Q == "p":
        dp[i][0] = max(dp[i-K][1], dp[i-K][2])
        dp[i][1] = max(dp[i-K][0], dp[i-K][2]) + S
        dp[i][2] = max(dp[i-K][0], dp[i-K][1])
    else:
        dp[i][0] = max(dp[i-K][1], dp[i-K][2])
        dp[i][1] = max(dp[i-K][0], dp[i-K][2])
        dp[i][2] = max(dp[i-K][0], dp[i-K][1]) + P

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

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

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

for i in range(n):
    if i >= k and t[i] == t[i-k] and dp[i+1-k][1] == 1:
        dp[i+1][0] = dp[i][0]
        dp[i+1][1] = 0
    elif i >= k and t[i] == t[i-k]:
        dp[i+1][0] = dp[i][0] + dict[t[i]]
        dp[i+1][1] = 1
    else:
        dp[i+1][0] = dp[i][0] + dict[t[i]]
        dp[i+1][1] = 1

print(dp[n][0])

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


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]

    
def main():
    N, K = read_values()
    R, S, P = read_values()
    T = input().strip()

    res = 0
    F = [True] * N
    for i, t in enumerate(T):
        if i >= K:
            if T[i - K] == T[i] and F[i - K]:
                F[i] = False
                continue
        
        if t == "r":
            res += P
        elif t == "s":
            res += R
        elif t == "p":
            res += S
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
l=list(input())
ans=0
v=[0]*n
for i in range(n):
    if i>=k and l[i]==l[i-k] and v[i-k]==1:
            continue
    else:
        v[i]=1
        if l[i]=='s':
            ans+=r
        elif l[i]=='r':
            ans+=p
        else:
            ans+=s
print(ans)

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

ans = 0

te = ''

for i in range(n):
    l = t[i]
    if i < k:
        if l == 'r':
            ans += p
            te += 'p'
        elif l == 'p':
            ans += s
            te += 's'
        else:
            ans += r
            te += 'r'
    else:
        if l == 'r':
            if te[i-k] != 'p':
                ans += p
                te += 'p'
            else:
                te += 'a'
        elif l == 'p':
            if te[i-k] != 's':
                ans += s
                te += 's'
            else:
                te += 'a'
        else:
            if te[i-k] != 'r':
                ans += r
                te += 'r'
            else:
                te += 'a'

print(ans)

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

    string = ''
    ans = 0
    for i, ti in enumerate(t):
        if i < k:
            if ti == 'r':
                ans += p
            elif ti == 's':
                ans += r
            else:
                ans += s
            string += ti
        else:
            if ti == string[i-k]:
                string += 'e'
            else:
                if ti == 'r':
                    ans += p
                elif ti == 's':
                    ans += r
                elif ti == 'p':
                    ans += s
                string += ti
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

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

    table = [False]*n

    ans = 0
    for index, i in enumerate(t[:k]):

        if i == 'r':
            ans += p
            table[index] = True
        elif i == 'p':
            ans += s
            table[index] = True
        elif i == 's':
            ans += r
            table[index] = True

    for index, (i, j) in enumerate(zip(t[k:], t)):
        if i == j:
            if table[index]:
                table[index+k] = False
                continue
        if i == 'r':
            ans += p
            table[index+k] = True
        elif i == 'p':
            ans += s
            table[index+k] = True
        elif i == 's':
            ans += r
            table[index+k] = True
    print(ans)

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

R = []

for i in range(K):
    if T[i]=='r':
        R.append('p')
    elif T[i]=='s':
        R.append('r')
    else:
        R.append('s')

for i in range(K, len(T)):
    if T[i]=='r':
        if R[i-K] == 'p':
            R.append('x')
        else:
            R.append('p')
    elif T[i]=='s':
        if R[i-K] == 'r':
            R.append('x')
        else:
            R.append('r')
    else:
        if R[i-K] == 's':
            R.append('x')
        else:
            R.append('s')

ans = 0
for r in R:
    if r=='p': ans += P
    elif r=='r': ans += R2
    elif r=='s': ans += S

print(ans)

Solution (Python):
n,k = map(int, input().split( ))
r,s,p = map(int, input().split( ))
t = input()
x = []
ans = 0
hand = {"r":"p", "s":"r", "p":"s"}
point = {"r":r,"s":s,"p":p}
for i in range(n):
    h = hand[t[i]]
    if i<k:
        x.append(h)
        ans += point[h]
    else:
        if h !=x[i-k]:
            x.append(h)
            ans += point[h]
        else:
            x.append("x")
print(ans)

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

ans = 0
flag = [True]*k

for i in range(1,n+1):
    if i-k <= 0:
        if t[i-1] == 'r':
            ans += p
        elif t[i-1] == 's':
            ans += r
        else:
            ans += s
    else:
        if t[i-1] == t[i-k-1]:
            if flag[(i-1)%k]:
                flag[(i-1)%k] = False
            else:
                flag[(i-1)%k] = True
                if t[i-1] == 'r':
                    ans += p
                elif t[i-1] == 's':
                    ans += r
                else:
                    ans += s
        else:
            flag[(i-1)%k] = True
            if t[i-1] == 'r':
                ans += p
            elif t[i-1] == 's':
                ans += r
            else:
                ans += s

print(ans)

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





ans = 0
mem = ['x']*N  
for i,t in enumerate(T):
  if i < K:
    ans += score_dict[t]
    mem[i] = t
  elif t != mem[i-K]:
    ans += score_dict[t]
    mem[i] = t

print(ans)

Solution (Python):
def win_score(hand):
  global R,S,P,my
  if hand=="r":
    my+="P"
    return P
  elif hand=="s":
    my+="R"
    return R
  elif hand=="p":
    my+="S"
    return S
  return 0

nk=input().split(" ")
N,K=int(nk[0]),int(nk[1])
nk=input().split(" ")
R,S,P=int(nk[0]),int(nk[1]),int(nk[2])
T=input()
my=""

score=0
for i in range(N):
  if i<K:
    score+=win_score(T[i])
  elif T[i]!=T[i-K] or my[i-K]=="-":
    score+=win_score(T[i])
  else:
    my+="-"


print(score)

Solution (Python):
N,K = list(map(int,input().split()))

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

T = input()
ans = 0
for i in range(K):
    j = i
    count = 0
    DP = [[0]*3 for i in range((N-i-1)//K + 2)]
    
    while j<N:
        if T[j]=="r":
            DP[count+1][0] = max(DP[count][1],DP[count][2])
            DP[count+1][1] = max(DP[count][0],DP[count][2])
            DP[count+1][2] = max(DP[count][0],DP[count][1])+P
        elif T[j]=="s":
            DP[count+1][0] = max(DP[count][1],DP[count][2])+R
            DP[count+1][1] = max(DP[count][0],DP[count][2])
            DP[count+1][2] = max(DP[count][0],DP[count][1])
        elif T[j]=="p":
            DP[count+1][0] = max(DP[count][1],DP[count][2])
            DP[count+1][1] = max(DP[count][0],DP[count][2])+S
            DP[count+1][2] = max(DP[count][0],DP[count][1])
        j+=K
        count+=1
    ans += max(DP[count][0],DP[count][1],DP[count][2])

print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
dic = {'r': P, 's': R, 'p': S}
tmp = [''] * (N + 1)
for i in range(K):
    ans += dic[T[i]]
    tmp[i] = T[i]

for i in range(K, N):
    if T[i] != tmp[i - K]:
        ans += dic[T[i]]
        tmp[i] = T[i]
    else:
        tmp[i] = 't'
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
c=0
for i in range(k):
    q,d=t[i],1
    if i+k<n:
        for j in range(i+k,n,k):
            if q==t[j]:
                d+=1
            else:
                x=(d-1)//2+1
                if q=="r":
                    c+=x*p
                elif q=="s":
                    c+=x*r
                else:
                    c+=x*s
                q=t[j]
                d=1
    x=(d-1)//2+1
    if q=="r":
        c+=x*p
    elif q=="s":
        c+=x*r
    else:
        c+=x*s
print(c)

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

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

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

A=[0]*N

for i in range(N):
    if T[i]==T[i-K] and A[i-K]!=0:
        A[i]=0
    else:
        if T[i]=="r":
            A[i]=P
        elif T[i]=="s":
            A[i]=R
        else:
            A[i]=S

print(sum(A))

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

def score(c) :
  if c == 'r' : return P
  if c == 's' : return R
  if c == 'p' : return S

ret = 0
for fr in range(K) :
  U = list(T[fr::K])
  s = 0
  s += score(U[0])
  for i in range(1, len(U)) :
    if U[i] == U[i-1] :
      U[i] = '
    else :
      s += score(U[i])
  ret += s
  
print(ret)

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

ans = 0
hand = []
for i in range(K):
    if T[i] == "r":
        ans += P
        hand.append("p")
    elif T[i] == "s":
        ans += R
        hand.append("r")
    else:
        ans += S
        hand.append("s")

for i in range(K, N):
    if T[i] == "r" and hand[i-K] != "p":
        ans += P
        hand.append("p")
    elif T[i] == "s" and hand[i-K] != "r":
        ans += R
        hand.append("r")
    elif T[i] == "p" and hand[i-K] != "s":
        ans += S
        hand.append("s")
    else:
        hand.append("joker")
        
        
print(ans)

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

flag = [[0] * 3 for _ in range(N)]
score = 0

for i in range(K):
    t = T[i]
    if t == 'r':
        flag[i][0] = 1
        score += P
    elif t == 's':
        flag[i][1] = 1
        score += R
    elif t == 'p':
        flag[i][2] = 1
        score += S


for i in range(K,N):
    t = T[i]
    if t == 'r':
        if flag[i-K][0] != 1:
            flag[i][0] = 1
            score += P
    if t == 's':
        if flag[i-K][1] != 1:
            flag[i][1] = 1
            score += R
    if t == 'p':
        if flag[i-K][2] != 1:
            flag[i][2] = 1
            score += S

print(score)

Solution (Python):
import sys
from collections import defaultdict as dd

N, K = map(int,input().split())
R, S, P = map(int,input().split())
rsp = [R,S,P]
T = input()
res = 0
tb = dd(list)
for i in range(N):
    tb[i%K].append(T[i])
for key in tb.keys():
    dic = tb[key]
    lg = len(dic)+1
    dp = [[0,0,0] for _ in range(lg)]
    for i in range(1,lg):
        for j in range(3):
            rsp2 = [0,0,0]
            if dic[i-1] == 'r':
                rsp2[2] = P
            elif dic[i-1] == 's':
                rsp2[0] = R
            else:
                rsp2[1] = S
            dp[i][j] = max([dp[i-1][(j+1)%3]+rsp2[(j+1)%3],dp[i-1][(j+2)%3]+rsp2[(j+2)%3]])
    res += max(dp[lg-1])
print(res)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

C = [0 for i in range(N)]
D = {"r": 0, "s": 0, "p": 0}

for i in range(N):
    if i <= K-1:
        C[i] = 1
        D[T[i]] += 1
    else:
        if T[i] == T[i-K] and C[i-K] == 1:
            continue
        else:
            C[i] = 1
            D[T[i]] += 1

print(D["r"]*P + D["s"]*R + D["p"]*S)

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

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

ans=0
for i in range(K):
    dp=[[0,0,0]for a in range(len(L[i])+1)]
    for x in range(1,len(dp)):
        dp[x][0]=max(dp[x-1][1],dp[x-1][2])
        dp[x][1]=max(dp[x-1][0],dp[x-1][2])
        dp[x][2]=max(dp[x-1][0],dp[x-1][1])
        if L[i][x-1]=="r":
            dp[x][2]+=P
        elif L[i][x-1]=="s":
            dp[x][0]+=R
        else:
            dp[x][1]+=S
    
    ans+=max(dp[-1])
print(ans)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time
from fractions import gcd



def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    N, K = MI()
    R, S, P = MI()
    T = input()
    
    r = 'r'
    
    s = 's'
    
    p = 'p'
    m = {
        'r': P,
        's': R,
        'p': S
    }
    ms = {
        'r': 'p',
        's': 'r',
        'p': 's'
    }
    ans = 0
    records = []

    for i in range(len(T)):
        if len(records) < K:
            ans += m[T[i]]
            records.append(ms[T[i]])
        else:
            if records[i - K] == ms[T[i]]:
                records.append('empty')
            else:
                ans += m[T[i]]
                records.append(ms[T[i]])

    print(ans)


if __name__ == '__main__':
    main()

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

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

Solution (Python):

import sys
sys.setrecursionlimit(4100000)

n,k= [int(i) for i in input().split()]
pts = {}
pts["s"],pts["p"],pts["r"] = [int(i) for i in input().split()]
t = input()
take,notake, chose = [0 for _ in range(n)], [0 for _ in range(n)], ["w" for _ in range(n)]
for i in range(len(t)):
    if i == 0:
        take[0] = pts[t[i]]
        notake[0] = 0
        chose[0] = t[i]
    else:
        if t[i] == chose[i-k]:
            take[i] = max(take[i-1],notake[i-1])
            notake[i] = max(take[i-1],notake[i-1])
        else:
            take[i] = take[i-1] + pts[t[i]]
            notake[i] = max(take[i-1],notake[i-1])
            chose[i] = t[i]


print(max(take[-1],notake[-1]))

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
dic={"s":R,"p":S,"r":P}
t=input()
ans=0
l=[t[i::k] for i in range(k)]
for j in l:
  V=[]
  unit=0
  for i in j:
    if len(V)==0 or V[-1][0]!=i:
      V.append([i,1])
    else:
      V[-1][1] += 1
  for i in V:
    ans+=dic[i[0]]*((i[1]+1)//2)
print(ans)

Solution (Python):
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
S, P, R = MAP()
t = input()

lis = [[] for i in range(k)]
for i in range(n):
    lis[i%k].append(t[i])

res = 0
for i in range(k):
    for j in range(len(lis[i])):
        if j != 0 and lis[i][j] == lis[i][j-1]:
            lis[i][j] = 'n'
        if lis[i][j] == 'r':
            res += R
        if lis[i][j] == 's':
            res += S
        if lis[i][j] == 'p':
            res += P

print(res)

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

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

Solution (Python):
def main():

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

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

    ans = 0
    prev = ["" for _ in range(K)]
    for i in range(N):
        c = T[i]
        if opp[c] == prev[i%K]:
            prev[i%K] = ""
        else:
            ans += d[opp[c]]
            prev[i % K] = opp[c]
        

    return ans

if __name__ == '__main__':
    print(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}

prev = [None for _ in range(k)]
flag = [False for _ in range(k)]    

ind = 0
ans = 0
for char in t:
    if char == prev[ind]:
        if flag[ind]:
            flag[ind] = False
        else:
            ans += d[char]
            flag[ind] = True
    else:
        ans += d[char]
        flag[ind] = True

    prev[ind] = char
    ind = (ind + 1) % k


print(ans)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
N,M = m()
a,b,c = m()
s = input()
kkk = [0 for i in range(N)]
co = 0
for i in range(N):
    if i < M:
        if s[i] == "s":
            co += a
        elif s[i] == "p":
            co += b
        else:
            co += c
        kkk[i] = 1
    else:
        if s[i] != s[i-M] or (kkk[i-M] == 0 and s[i] == s[i-M]):
            if s[i] == "s":
                co += a
                kkk[i] = 1
            elif s[i] == "p":
                co += b
                kkk[i] = 1
            else:
                co += c
                kkk[i] = 1

print(co)

Solution (Python):


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

hero_rsp_list = [''] * K
ans = 0

for i in range(N):
    opponent_rsp = T[i]
    hero_last_rsp = hero_rsp_list[i % K]
    if opponent_rsp == 'r':
        if hero_last_rsp != 'p':
            hero_rsp_list[i % K] = 'p'
            ans += P
        else:
            hero_rsp_list[i % K] = ''
    elif opponent_rsp == 's':
        if hero_last_rsp != 'r':
            hero_rsp_list[i % K] = 'r'
            ans += R
        else:
            hero_rsp_list[i % K] = ''
    elif opponent_rsp == 'p':
        if hero_last_rsp != 's':
            hero_rsp_list[i % K] = 's'
            ans += S
        else:
            hero_rsp_list[i % K] = ''

print(ans)

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

C = [''] * N
ans = 0
for i, t in enumerate(T):
    if t == 'r':
        c = 'p'
        win = P

    if t == 's':
        c = 'r'
        win = R

    if t == 'p':
        c = 's'
        win = S

    if (i - K >= 0) and (C[i - K] == c):
        win = 0

    if win != 0:
        C[i] = c
    ans += win

print(ans)

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

def func(string):
    if string == "r":
        return P
    if string == "s":
        return R
    if string == "p":
        return S

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

boolean_lis = [True] * N


for n in range(K, N):
    if T[n - K] != T[n]:
        ans += func(T[n])
    elif T[n - K] == T[n]:
        if not boolean_lis[n - K]:
            ans += func(T[n])
        else:
            boolean_lis[n] = False
    
print(ans)