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):
r_p_s_sum, K = map(int, input().split())
R,S,P = map(int, input().split())
enemy = input()
enemy_list = list(enemy)


max_points = enemy.count('r')*P + enemy.count('p') * S + enemy.count('s') * R

for i in range(K,len(enemy_list),1):
    if (enemy_list[i] == enemy_list[i-K]):
        if enemy_list[i] == 'r':
            max_points -= P
        elif enemy_list[i] == 'p':
            max_points -= S
        else:
            max_points -= R
        enemy_list[i] = 'NULL'
print(max_points)

Solution (Python):
n,k=(int(i) for i in input().split())
r,s,p=(int(i) for i in input().split( ))
T=input()
hand=[]
point=0

for i in range(n):
	if k-1<i:
		if hand[i-k]!=T[i]:
			if T[i]=='r':
				point+=p
			elif T[i]=='s':
				point+=r
			else:
				point+=s
			hand.append(T[i])
			
		else:
			hand.append('atcoder')
	else:
		if T[i]=='r':
			point+=p
		elif T[i]=='s':
			point+=r
		else:
			point+=s
		hand.append(T[i])

print(point)

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

mine = []
score = 0
for n in range(1, N + 1):
    if n <= K:
        if T[n - 1] == "r":
            score += P
            mine.append("p")

        elif T[n - 1] == "s":
            score += R
            mine.append("r")

        else:
            score += S
            mine.append("s")

    else:
        if T[n - 1] == "r":
            if mine[n - 1 - K] == "p":
                mine.append("any")
                continue
            else:
                score += P
                mine.append("p")

        elif T[n - 1] == "s":
            if mine[n - 1 - K] == "r":
                mine.append("any")
                continue
            else:
                score += R
                mine.append("r")

        else:
            if mine[n - 1 - K] == "s":
               mine.append("any")
               continue
            else:
               score += S
               mine.append("s")


print(score)

Solution (Python):
import sys

n, k = map(int, sys.stdin.readline().split())
r, s, p = map(int, sys.stdin.readline().split())
t = sys.stdin.readline().rstrip()
point = dict([('r', r), ('s', s), ('p', p)])

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

def main():
    res = 0
    for i in range(k):
        cur = hand(t[i])
        res += point[cur]
        prev = cur
        for j in range(i+k, n, k):
            cur = hand(t[j])
            if cur == prev:
                cur = 'any'
            else:
                res += point[cur]
            prev = cur
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

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

score = 0
result = ""
def judge(i):
    if i >= k and t[i] == t[i-k] and result[i-k] == "w":
        return False
    else:
        return True

for i in range(n):
    if judge(i):
        result += "w"
        if t[i] == "r":
            score += p
        elif t[i] == "s":
            score += r
        else:
            score += s
    else:
        result += "l"
print(score)

Solution (Python):
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
make = {"r": "s", "s": "p", "p": "r"}
_t = input()
patterns = ["p" if x == "r" else "r" if x == "s" else "s" for x in _t]
scores = [p if x == "r" else r if x == "s" else s for x in _t]
for i in range(n):
    if i > k - 1:
        if patterns[i - k] == patterns[i]:
            if scores[i - k] == 0:
                patterns[i - k] = make[_t[i - k]]
            else:
                patterns[i] = _t[i]
                scores[i] = 0
print(sum(scores))

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

score = 0
flag = [0] * N

for i in range(N):
    if i < K:
        if T[i] == 'r':
            score = score + P
            flag[i] = 1
        elif T[i] == 's':
            score = score + R
            flag[i] = 1
        else:
            score = score + S
            flag[i] = 1
    elif i >= K:
        if T[i] == T[i - K] and flag[i - K] == 1:
            score = score
        elif T[i] == 'r':
            score = score + P
            flag[i] = 1
        elif T[i] == 's':
            score = score + R
            flag[i] = 1
        else:
            score = score + S
            flag[i] = 1

print(score)

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

ans=0
jibun=""
for i in range(n):
  if i<k:
    if t[i]=="s":
      ans+=r
      jibun+="r"
    elif t[i]=="p":
      ans+=s
      jibun+="s"
    else:
      ans+=p
      jibun+="p"
  else:
    if t[i]==t[i-k]:
      if (t[i]=="s" and jibun[i-k]=="r") or (t[i]=="p" and jibun[i-k]=="s") or (t[i]=="r" and jibun[i-k]=="p"):
        jibun+="n"
      else:
        if t[i]=="s":
          ans+=r
          jibun+="r"
        elif t[i]=="p":
          ans+=s
          jibun+="s"
        else:
          ans+=p
          jibun+="p"
    else:
        if t[i]=="s":
          ans+=r
          jibun+="r"
        elif t[i]=="p":
          ans+=s
          jibun+="s"
        else:
          ans+=p
          jibun+="p"
print(ans)

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

def score(t):
    if t == "r": return p
    if t == "s": return r
    if t == "p": return s

hist = [False] * n

ans = 0
for i in range(n):
    if i < k or (t[i] != t[i-k] or not hist[i-k]): 
        ans += score(t[i])
        hist[i] = True

print(ans)

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

results = []
score = 0
for idx, t in enumerate(T):
    if idx - K < 0:
        if t == "r":
            results.append("p")
            score += P
        elif t == "s":
            results.append("r")
            score += R
        elif t == "p":
            results.append("s")
            score += S
    else:
        if t == "r" and results[idx - K] == "p":
            results.append("?")
        elif t == "r":
            results.append("p")
            score += P
        if t == "s" and results[idx - K] == "r":
            results.append("?")
        elif t == "s":
            results.append("r")
            score += R
        if t == "p" and results[idx - K] == "s":
            results.append("?")
        elif t == "p":
            results.append("s")
            score += S
print(score)

Solution (Python):
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = list(input())
MAX = 0
for i in T:
    if i == "r":
        MAX+=P
    elif i == "s":
        MAX+=R
    else:
        MAX+=S
ans = MAX
for i in range(K):
    index = i+K
    tmp = T[i]
    while index<=len(T)-1:
        if tmp == T[index]:
            if tmp == "r":
                ans-=P
            elif tmp == "s":
                ans-=R
            else:
                ans-=S
            tmp = None
        else:
            tmp = T[index]
        index+=K

print(ans)

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

win={"r":"p","s":"r","p":"s"}
score={"r":r,"s":s,"p":p}
total=0
already=[]
for i in range(k):
    win_hand=win[tl[i]]
    already.append(win_hand)
    total+=score[win_hand]
temp=["r","s","p"]
for j in range(k,n):
    temp=["r","s","p"]
    win_hand=win[tl[j]]
    
    if already[j-k]==win_hand:
        temp.remove(win_hand)
        if j+k <= n-1:
            keep=win[tl[j+k]]
            if not keep in temp:
                already.append(temp[0])
            else:
                temp.remove(keep)
                already.append(temp[0])
        else:
            already.append(temp[0])
        pass
    else:
        already.append(win_hand)
        total+=score[win_hand]
print(total)

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

points = {'rr': 0, 'rs': R, 'rp': 0, 'sr': 0, 'ss': 0, 'sp': S, 'pr': P, 'ps': 0, 'pp': 0}
hands = ['r', 's', 'p']
ans = 0
for i in range(K):
    subT = T[i::K]
    n = len(subT)
    dp = [{h: 0 for h in hands} for _ in range(n)]
    for j, t in enumerate(subT):
        for hand in hands:
            dp[j][hand] = max(dp[j-1][h] for h in set(hands) - set([hand])) + points[hand + t]
    ans += max(dp[-1][h] for h in hands)
print(ans)

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

def score_p(t):
    if t == 'r':
        return P
    if t == 's':
        return R
    if t == 'p':
        return S

A = [None] * N
for k in range(K):
    for i in range(k, N, K):
        if i == k:
            A[i] = score_p(T[i])
        else:
            if T[i] == T[i-K]:
                if A[i-K] > 0:
                    A[i] = 0
                else:
                    A[i] = score_p(T[i])
            else:
                A[i] = score_p(T[i])

print(sum(A))

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
t += "e"*k
lst = ["r","s","p"]
lst2 = [p,r,s]
score  = 0
for i in range(k):
    now = i
    s = ""
    while now < n:
        s += t[now]
        now += k
    l = []
    count = 1
    for j in range(len(s)-1):
        if s[j] != s[j+1]:
            l.append([s[j],count])
            count = 1
        else:
            count += 1
    l.append([s[len(s)-1],count])
    for arr in l:
        score += lst2[lst.index(arr[0])]*((arr[1]+1)//2)
print(score)

Solution (Python):
n,k = (int(i) for i in input().split())
r,s,p = (int(i) for i in input().split())
S = input()

ans_li = []
ans = 0
for t in S:
    if t == 'r':
        ans_li.append('p')
        ans += p

    elif t == 'p':
        ans_li.append('s')
        ans += s
    
    else:
        ans_li.append('r')
        ans += r

for i in range(k,n):
    
    if ans_li[i] == ans_li[i-k]:
       
       if ans_li[i] == 'r': ans -= r
       elif ans_li[i] == 'p': ans -= p
       else: ans -= s

       ans_li[i] = 'x'

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
t=t.replace('r','P')
t=t.replace('s','R')
t=t.replace('p','S')
t=t.lower()
ans=r*t.count('r')+s*t.count('s')+p*t.count('p')

l=int(n/k)
for i in range(k):
  j=i
  while j+k<=n-1:
    if t[j]==t[j+k]:
      ans-=r*(t[j+k]=='r')+s*(t[j+k]=='s')+p*(t[j+k]=='p')
      t=t[:j+k]+'x'+t[j+k+1:]
    j+=k
      



print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = input()
out = 0
L = [0 for i in range(n)]

def score(a):
  if a == "r":
    ret = p
  elif a == "s":
    ret = r
  else:
    ret = s
  return (ret)

for i in range(n):
  if i < k:
    out += score(T[i])
  else:
    if T[i] != T[i-k]:
      out += score(T[i])
    elif L[i-k] == 1:
      out += score(T[i])
    else:
      L[i] = 1
  
print(out)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t=input()
text = {"r":"p","s":"r","p":"s"}
tokutenn = 0
def dasu(te):
  if te=="r":
    return r
  if te=="s":
    return s
  if te=="p":
    return p
nons = list(range(n))
for i in range(n):
  non = "" if (i < k or nons[i-k][1]=="2") else nons[i-k][0]
  tei = text[t[i]]
  ts=dasu(tei)
  if k==i+1:
    tokutenn+=ts
    nons[i]=text[t[i]]+"0"
  else:
    if tei!=non:
      tokutenn+=ts
      nons[i]=text[t[i]]+"1"
    else:
      nons[i]=text[t[i]]+"2"
print(tokutenn)

Solution (Python):

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

from collections import deque
last_hand_queue = deque("a" * K)
last_result_queue = deque("a" * K)

def get_winning_hand(opponents_hand):
    if opponents_hand == "r":
        return("p")
    elif opponents_hand == "s":
        return("r")
    elif opponents_hand == "p":
        return("s")

ans = 0
for i in range(N):
    last_k_hand = last_hand_queue.pop()
    last_k_result = last_result_queue.pop()
    if get_winning_hand(T[i]) == last_k_hand and last_k_result == "w":
        last_hand_queue.appendleft(T[i])
        last_result_queue.appendleft("e")
    else:
        last_hand_queue.appendleft(get_winning_hand(T[i]))
        last_result_queue.appendleft("w")
        ans += win_point_dict[get_winning_hand(T[i])]

print(ans)

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

Solution (Python):
n,k = (int(i) for i in input().split())
r,s,p = (int(i) for i in input().split())
S = input()

ans_li = []
ans = 0
for t in S:
    if t == 'r':
        ans_li.append('p')
        ans += p

    elif t == 'p':
        ans_li.append('s')
        ans += s
    
    else:
        ans_li.append('r')
        ans += r

for i in range(k,n):
    
    if ans_li[i] == ans_li[i-k]:
       
       if ans_li[i] == 'r': ans -= r
       elif ans_li[i] == 'p': ans -= p
       else: ans -= s

       ans_li[i] = 'x'

print(ans)

Solution (Python):
n,k = map(int, input().strip().split())
r,s,p = map(int, input().strip().split())
T = input().strip()
resp=[0]*n
score=0

for i,t in enumerate(T):
    if t=="r":
        if i<k or resp[i-k]!="p":
            score+=p
            resp[i]="p"
    if t=="s":
        if i<k or resp[i-k]!="r":
            score+=r
            resp[i]="r"
    if t=="p":
        if i<k or resp[i-k]!="s":
            score+=s
            resp[i]="s"
print(score)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
key = [False]*n
for i in range(n):
    if i >= k:
        if t[i] == t[i-k] and key[i-k] == False:
            key[i] = True
            continue
    if t[i] == "r":
        ans += p
    if t[i] == "s":
        ans += r
    if t[i] == "p":
        ans += s
print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
R, S, P = MAP()
T = list(input())

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

ans = 0
for k in range(K):
    dp = list2d(len(T2[k])+1, 3, 0)
    for i, t in enumerate(T2[k]):
        for j in range(3):
            for l in range(3):
                if i != 0 and j == l:
                    continue
                nxt = 0
                if t == 'r' and l == 2:
                    nxt = P
                elif t == 's' and l == 0:
                    nxt = R
                elif t == 'p' and l == 1:
                    nxt = S
                dp[i+1][l] = max(dp[i+1][l], dp[i][j] + nxt)
    ans += max(dp[len(T2[k])])
print(ans)

Solution (Python):
n, k = map(int, input().split())
R, S, P = map(int, input().split())
score = [P, R, S]
t = input()
INF = 10**9

ans = 0
for i in range(k):
    t2 = ""
    j = 0
    while i + j * k <= n-1:
        t2 += t[i+j*k]
        j += 1
    m = len(t2)
    dp = [[-INF, -INF, -INF] for _ in range(m+1)]
    for j in range(3):
        dp[0][j] = 0
    for x in range(m):
        for y in range(3):
            for z in range(3):
                if y != z:
                    if z == 0 and t2[x] == 'r':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    if z == 1 and t2[x] == 's':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    if z == 2 and t2[x] == 'p':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    dp[x+1][z] = max(dp[x+1][z], dp[x][y])
    num = 0
    for z in range(3):
        num = max(num, dp[m][z])
    ans += num

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N,K = LMIIS()
    R,S,P = LMIIS()
    T = input()
    A = [False]*N
    ans = 0
    
    
    
    score = {
        'r':P,
        's':R,
        'p':S
    }
    for i in range(K):
        
        ans += score[T[i]]
        A[i] = True
    for i in range(K,N):
        if T[i] != T[i-K]:
            
            ans += score[T[i]]
            A[i] = True
        else:
            if A[i-K] != True:
                ans += score[T[i]]
                A[i] = True
            
    print(ans)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    





if __name__ == '__main__':
    main()

Solution (Python):
def solve(n, k, r, s, p, t) -> int:
    convert = {opp: me for opp, me in zip('rsp', 'prs')}
    score = {'r': r, 's': s, 'p': p}
    my_hands = tuple(convert[_t] for _t in t)

    ret = 0
    for i in range(k):
        w = my_hands[i::k]
        flg = False
        for j, (_w1, _w2) in enumerate(zip(w[:-1], w[1:])):
            if flg:
                flg = False
                continue

            ret += score[_w1]
            if _w1 == _w2:
                flg = True

        if not flg:
            ret += score[w[-1]]

    return ret


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


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
a=[[]for i  in range(k)]
for i in range(n):
    a[i%k].append(t[i])
s1=0
for i in range(k):
    for j in range(len(a[i])):
        if j==0 or a[i][j]!=a[i][j-1]:
            if a[i][j]=='r':
                s1+=p
            elif a[i][j]=='s':
                s1+=r
            else:
                s1+=s
        else:
            a[i][j]='k'
print(s1)

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

        else:
            if L[i-K] != "s":
                ans += S
                L.append("s")
            else:
                if i+K<=(N - 1):
                    if T[i + K] == "r":
                        L.append("r")
                    else:
                        L.append("p")
                else:
                    L.append("p")
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
points = 0
for i in range(K, N):
  if T[i] == T[i-K]:
    T = T[:i] + '
for i in range(N):
  if T[i] == 'r':
    points += P
  elif T[i] == 's':
    points += R
  elif T[i] == 'p':
    points += S
print(points)

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-K):
    if T[i] == "s":
        if T[i+K] == "s":
            T[i+K] = "x"
            ans += R
        else:
            ans += R
    elif T[i] == "p":
        if T[i+K] == "p":
            T[i+K] = "x"
            ans += S
        else:
            ans += S
    elif T[i] == "r":
        if T[i+K] == "r":
            T[i+K] = "x"
            ans += P
        else:
            ans += P
    else:
        pass

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

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

can_use = []
score = 0

def get_score(forwin):
    if forwin == "r":
        score = R
    elif forwin == "s":
        score = S
    elif forwin == "p":
        score = P
    return score

def can_use_hand(forwin):
    if forwin == "r":
        hands = ["s", "p"]
    elif forwin == "s":
        hands = ["r", "p"]
    elif forwin == "p":
        hands = ["r", "s"]
    return hands

for i in range(N):
    if T[i] == "r":
        forwin = "p"
    elif T[i] == "s":
        forwin = "r"
    elif T[i] == "p":
        forwin = "s"
    
    if len(can_use) < K:
        score += get_score(forwin)
        can_use.append(can_use_hand(forwin))
    else:
        can_use_hands = can_use[-K]
        if forwin in can_use_hands:
            score += get_score(forwin)
            can_use.append(can_use_hand(forwin))
        else:
            score += 0
            can_use.append(["r","s","p"])
print(score)

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

dp = [False] * N

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

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

score=0
for k in range(K):
    temp_t = T[k::K]
    if len(temp_t)==1:
        if temp_t=='r':
            score += P
        elif temp_t=='s':
            score += R
        else:
            score += S
    else:
        prev = 'x'
        prev_count = 0
        for itr in range(len(temp_t)):
            if temp_t[itr] != prev:
                prev = temp_t[itr]
                prev_count = 1
                if temp_t[itr]=='r':
                    score += P
                elif temp_t[itr]=='s':
                    score += R
                else:
                    score += S
            else:
                prev_count += 1
                if prev_count % 2 == 1:
                    if temp_t[itr]=='r':
                        score += P
                    elif temp_t[itr]=='s':
                        score += R
                    else:
                        score += S
print(score)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
next_r = [1] * N
next_s = [1] * N
next_p = [1] * N
ans = 0
for i in range(len(T)):
        if T[i] == 'r' and next_r[i - K]:
                ans += P
                next_r[i] = 0
        elif T[i] == 's' and next_s[i - K]:
                ans += R
                next_s[i] = 0
        elif T[i] == 'p' and next_p[i - K] :
                ans += S
                next_p[i] = 0
print(ans)

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

A=[[R,"r","s"],[S,"s","p"],[P,"p","r"]]
A.sort()

rec=[0]*N
for i in range(N):
  if T[i]==A[0][2]:
    if i<K:
      rec[i]=A[0][1]
    elif rec[i-K] !=A[0][1]:
      rec[i]=A[0][1]
for i in range(N):
  if T[i]==A[1][2]:
    if i<K:
      rec[i]=A[1][1]
    elif rec[i-K] !=A[1][1]:
      rec[i]=A[1][1]
for i in range(N):
  if T[i]==A[2][2]:
    if i<K:
      rec[i]=A[2][1]
    elif rec[i-K] !=A[2][1]:
      rec[i]=A[2][1]


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

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

for i in range(len(t)):
    if i < k:
        ans += dic[t[i]]
    else:
        if not t[i-k] == t[i]:
            ans += dic[t[i]]
        else:
            t[i] = "x"
print(ans)

Solution (Python):
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N,K=LI()
SC=LI()
T=input()

DP=[[[0]*3 for _ in range(K)] for _ in range(N//K + (1 if N%K else 0))]

for i in range(N):
    cur = i//K
    rest = i%K
    v = 0 if T[i] == 'r' else (1 if T[i] == 's' else 2)
    if cur > 0:
        
        DP[cur][rest][v] = max(DP[cur-1][rest][(v+1)%3], DP[cur-1][rest][(v+2)%3])
        DP[cur][rest][(v+1)%3] = max(DP[cur-1][rest][v], DP[cur-1][rest][(v+2)%3])
        
        DP[cur][rest][(v+2)%3] = max(DP[cur-1][rest][v], DP[cur-1][rest][(v+1)%3]) + SC[(v+2)%3]
    else:
        
        DP[cur][rest][v] = 0
        DP[cur][rest][(v+1)%3] = 0
        
        DP[cur][rest][(v+2)%3] = SC[(v+2)%3]

res = 0

for i in range(N-K, N):
    cur = i//K
    rest = i%K
    
    res += max(DP[cur][rest])
print(res)

Solution (Python):
import collections as c
ip = lambda : map(int, input().split())


N,K = ip()
R,S,P = ip()
T = input()
ans = 0

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

for i in range(K):
    ans += p[T[i]]
    

for i in range(K,N):
    
    if T[i] == T[i-K]:
        T = T[:i] + 'c' + T[i+1:]
    else:
        ans += p[T[i]]
    
print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
cnt=0
t=[[0]for i in range(K)]
for i in range(N):
    if T[i]=="r":
        if t[i%K]!="p":
            cnt+=P
            t[i%K]="p"
        else:
            t[i%K]="0"
    if T[i]=="p":
        if t[i%K]!="s":
            cnt+=S
            t[i%K]="s"
        else:
            t[i%K]="0"
    if T[i]=="s":
        if t[i%K]!="r":
            cnt+=R
            t[i%K]="r"
        else:
            t[i%K]="0"
    

print(cnt)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
i=0
ans=0
hand_dic={'r':'p','s':'r','p':'s'}
point_dic={'r':R,'s':S,'p':P}

for i in range(K):
  _T=T[i::K]
  before=''
  _ans=0
  for t in _T:
    if before != hand_dic[t]:
      _ans+=point_dic[hand_dic[t]]
      before=hand_dic[t]
    else:
      before=''
  ans+=_ans
print(ans)

Solution (Python):
n,k=map(int,input().split())
point=list(map(int,input().split()))
t=input()
t+="x"*k
l=[[t[i],1] for i in range(k)]
ans=0
for i in range(k,n+k):
    if l[i%k][0]==t[i]:
        l[i%k][1]+=1
    else:
        ans+=point["spr".index(l[i%k][0])]*((l[i%k][1]+1)//2)
        l[i%k][0]=t[i]
        l[i%k][1]=1
print(ans)

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

ans = 0

r_ = p
s_ = r
p_ = s

for i in range(k):
    if t[i] == 'r':
        ans += r_
    if t[i] == 's':
        ans += s_
    if t[i] == 'p':
        ans += p_

for i in range(k,n):
    if t[i] == t[i-k]:
        t[i] = 'z'
    else:
        if t[i] == 'r':
            ans += r_
        if t[i] == 's':
            ans += s_
        if t[i] == 'p':
            ans += p_

print(ans)