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):
def main():
    N, K = (int(i) for i in input().split())
    R, S, P = (int(i) for i in input().split())
    T = input()
    pre = []
    win = {"r": "p", "s": "r", "p": "s"}
    ans = 0
    for i, t in enumerate(T):
        if K <= i and win[t] == pre[i-K]:
            pre.append("")
        else:
            if t == "r":
                ans += P
                pre.append("p")
            elif t == "s":
                ans += R
                pre.append("r")
            elif t == "p":
                ans += S
                pre.append("s")

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from random import choice,randint
inp=sys.stdin.readline
out=sys.stdout.write
flsh=sys.stdout.flush
 
sys.setrecursionlimit(10**9)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def MI(): return map(int, inp().strip().split())
def LI(): return list(map(int, inp().strip().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]
def LI_(): return [int(x)-1 for x in inp().strip().split()]
def LF(): return [float(x) for x in inp().strip().split()]
def LS(): return inp().strip().split()
def I(): return int(inp().strip())
def F(): return float(inp().strip())
def S(): return inp().strip()
def pf(s): return out(s+'\n')
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n,k=MI()
    r,s,p=MI()
    t=S()
    ans=0
    ns=""
    for i in range(k):
        if t[i]=="r":
            ans+=p
            ns+="p"
        if t[i]=="p":
            ans+=s
            ns+="s"
        if t[i]=="s":
            ans+=r
            ns+="r"
    for i in range(k,n):
        if t[i]=="r" and ns[i-k]=="p":
            ns+="1"
            ans+=0
        if t[i]=="r" and ns[i-k]!="p":
            ns+="p"
            ans+=p
        if t[i]=="p" and ns[i-k]=="s":
            ns+="1"
            ans+=0
        if t[i]=="p" and ns[i-k]!="s":
            ns+="s"
            ans+=s
        if t[i]=="s" and ns[i-k]=="r":
            ns+="1"
            ans+=0
        if t[i]=="s" and ns[i-k]!="r":
            ns+="r"
            ans+=r
    print(ans)


if __name__ == "__main__":
    main()

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


def set_hand(l, k):
    if l + k < n:
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "s"}:
            return "p"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "p"}:
            return "s"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"p", "s"}:
            return "r"
        return t[l]
    else:
        return t[l]


for i in range(n):
    for l in range(i, n, k):
        if d2.get(l) is not None:
            break
        if l - k >= 0:
            if d2[l - k] == d.get(t[l]):
                d2[l] = set_hand(l, k)
            else:
                d2[l] = d.get(t[l])
                r += score[d2[l]]
        if l - k < 0:
            d2[l] = d.get(t[l])
            r += score[d2[l]]
print(r)

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

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

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

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

result = [''] * n
result_num = 0
for j in range(n):
  if j >= k and result[j - k] == win_map[t[j]]:
    if j + k < n and t[j] == win_map[t[j + k]]:
      result[j] = lose_map[t[j]]
    else:
      result[j] = t[j]
  else:
    result[j] = win_map[t[j]]
    result_num += score[win_map[t[j]]]


print(result_num)

Solution (Python):
n, k = map(int, input().split())
r,s,p = map(int, input().split())
point = {}
can_win = {}
point["r"] = r
point["s"] = s
point["p"] = p
can_win["r"] = "p"
can_win["s"] = "r"
can_win["p"] = "s"
t = input()
acc = [0 for i in range(n + k + 10)]
ans = 0
pattern = ""
for i in range(n):
    pattern += (can_win[t[i]])
ans = 0
pattern = list(pattern)
for i in range(n):
    if i - k < 0:
        ans += point[pattern[i]]
    else:
        if pattern[i] == pattern[i-k]:
            pattern[i] = "q"
            continue
        else:
            ans += point[pattern[i]]
print(ans)

Solution (Python):
num = input().split(' ')
point = input().split(' ')
point_int = [int(point[i]) for i in range(3)]
data = input()
count = 1
result = 0
judge = [0 for i in range(int(num[0]))]
for i in range(int(num[0])):
    if(data[i] == 'r'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'r'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[2]
                    judge[i] = 1
                else:
                    pass

            else:
                result += point_int[2]
                judge[i] = 1
        else:
            result += point_int[2]
            judge[i] = 1
    elif(data[i] == 's'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 's'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[0]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[0]
                judge[i] = 1
        else:
            result += point_int[0]
            judge[i] = 1

    else:
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'p'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[1]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[1]
                judge[i] = 1
        else:
            result += point_int[1]
            judge[i] = 1

print(result)

Solution (Python):
N_K = input().split()
N = int(N_K[0])
K = int(N_K[1])

R_S_P = input().split()
R = int(R_S_P[0])
S = int(R_S_P[1])
P = int(R_S_P[2])

T = input()

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

decision = ""
for index, value in enumerate(T):
    if (index >= K and win_strategy[value] == decision[index-K]):
        decision += "X"
    else:
        decision += win_strategy[value]

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

points = decision.count('r')*R + decision.count('s')*S + decision.count('p')*P
print(points)

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

Solution (Python):
line1 = input().split(' ')
line2 = input().split(' ')
N = int(line1[0])
K = int(line1[1])
R = int(line2[0])
S = int(line2[1])
P = int(line2[2])
T = input()
score = 0
mem = list(range(K))

for i in range(N):
  index = i % K

  if (mem[index] != T[i:i+1]):
    if (T[i:i+1] == 'r'):
      score = score + P
    elif (T[i:i+1] == 's'):
      score = score + R
    elif (T[i:i+1] == 'p'):
      score = score + S
    mem[index] = T[i:i+1]
  else:
    mem[index] = 0

print(score)

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

i = 1
A = ['t']*K
mp1 = {'r':'p','s':'r','p':'s'}
mp2 = {'r':'r','s':'s','p':'p'}
mp3 = {'r':R,'s':S,'p':P}
ans = 0
for janken in T:
    tmp = i%K
    if A[tmp] == mp1[janken]:
        A[tmp] = 't'
    else:
        A[tmp] = mp1[janken]
        ans += mp3[mp1[janken]]
    i += 1

print(ans)

Solution (Python):
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = list(input())
c = [[0 for i in range(3)] for _ in range(N)]
if T[0] == 'r':
    c[0][0] = 0
    c[0][1] = 0
    c[0][2] = P
elif T[0] == 's':
    c[0][0] = R
    c[0][1] = 0
    c[0][2] = 0
elif T[0] == 'p':
    c[0][0] = 0
    c[0][1] = S
    c[0][2] = 0






def change(s):
    l = []
    for i in range(len(s)):
        if s[i] == 'r':
            v = 'p'
        elif s[i] == 's':
            v = 'r'
        elif s[i] == 'p':
            v = 's'
        l.append(v)
    return l
new = change(T)
point = 0
for i in range(len(new)-K):
    if new[i] == new[i+K]:
        new[i+K] = 'stop'
for i in new:
    if i == 'p':
        point += P
    elif i == 'r':
        point += R
    elif i == 's':
        point += S
print(point)

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

Solution (Python):
def win_rsp(hand):
    if hand == 'r':
        return 'p'
    elif hand == 's':
        return 'r'
    else:  
        return 's'


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

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

player_hands = [''] * N
score = 0


















t = list(T)

for i in range(K):
    spl = t[i::K]
    win_l = list(map(win_rsp, spl))
    
    
    j = 0
    while j < len(win_l):
        if j == len(win_l) - 1:
            score += get_score[win_l[j]]
            
            
            break
        if win_l[j] != win_l[j + 1]:
            score += get_score[win_l[j]]
            
            
            j += 1
        else:
            score += get_score[win_l[j]]
            
            j += 2

    
    
    
    
    

    
    
    
    

    
    
    
    
    
    

    
    
    

print(score)

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

pattern = []
score = 0

for t in T:
    if t == 'r':
        pattern.append('p')
    elif t == 's':
        pattern.append('r')
    else:
        pattern.append('s')

double = []
single = []
for i in range(K, N - K):
    if pattern[i] == pattern[i - K] and pattern[i] == pattern[i + K]:
        pattern[i] = ''

for i in range(K, N):
    if pattern[i] == pattern[i - K]:
        pattern[i] = ''

for p in pattern:
    if p == 'r':
        score += R
    elif p == 's':
        score += S
    elif p == 'p':
        score += P

print(score)

Solution (Python):
n, k = [int(_) for _ in input().split()]
r_s, s_s, p_s = [int(_) for _ in input().split()]
t = input()

s = ''
for i in range(k):
    s += t[i]

for start in range(k):
    c = 0
    for i in range(start + k, n, k):
        if t[i] == t[i - k]:
            c += 1
            if c % 2 == 0:
                s += t[i]
        else:
            s += t[i]
            c = 0

ans = 0
for i in range(len(s)):
    if s[i] == 'r':
        ans += p_s
    elif s[i] == 's':
        ans += r_s
    else:
        ans += s_s
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N, K = MAP()
	R, S, P = MAP()
	T = input().rstrip()

	Tokuten = [R, S, P]
	Te = ['r', 's', 'p']

	A = [-1] * N
	for i in range(N):
		now = 0 if T[i]=='s' else 1 if T[i]=='p' else 2
		if i-K < 0:
			A[i] = now
		else:
			past = A[i-K]
			if now == past:
				if i+K < N:
					future = 0 if T[i+K]=='s' else 1 if T[i+K]=='p' else 2
					for j in range(3):
						if not j in [now, future]:
							A[i] = j
			else:
				A[i] = now
	
	

	ans = 0
	for i in range(N):
		te = A[i]
		if te==0 and T[i]=='s':
			ans += Tokuten[0]
		elif te==1 and T[i]=='p':
			ans += Tokuten[1]
		elif te==2 and T[i]=='r':
			ans += Tokuten[2]

	print(ans)

if __name__ == '__main__':
	main()

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

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
    elif T[i] == "s":
        ans += R
    elif T[i] == "p":
        ans += S

print(ans)

Solution (Python):
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t = input()
res = 0
te_log = []

def judg(te):
  if te == "r":
    return "p"
  if te == "s":
    return "r"
  if te == "p":
    return "s"

def score(te):
  if te == "r":
    return r
  if te == "s":
    return s
  if te == "p":
    return p
  
for i in range(n):
  if i+1 <= k:
    res += score(judg(t[i]))
    te_log.append(judg(t[i]))
    continue
  comp = t[i]
  no = te_log[i-k]
  me = judg(comp)
  if no == me:
    if i+1+k > n:
      te_log.append(comp)
    elif judg(t[i+k]) == comp:
      te_log.append(judg(judg(comp)))
    else:
      te_log.append(comp)
    continue
  res += score(me)
  te_log.append(me)

print(res)

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

stra = {"r":"p","s":"r","p":"s"}
point = {"r":R,"s":S,"p":P}
c = 0
result = []
for i in range(K):
    c += point[stra[T[i]]]
    result.append(stra[T[i]]) 
    

for i in range(K,N):
    before = result[i-K]
    if i <= N - K - 1:
        after = stra[T[i+K]]
    if stra[T[i]] == before:
        result.append("ok")
    else:
        c += point[stra[T[i]]]
        result.append(stra[T[i]])
        
print(c)

Solution (Python):
n, k = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]
t = input()
z = {'r': 'p', 's': 'r', 'p': 's'}
v = {'r': r, 's': s, 'p': p, 'z': 0}
x = []
for a in t:
    x.append(z[a])
for i in range(k):
    for j in range(k + i, n, k):
        if x[j] == x[j - k]:
            x[j] = 'z'
ans = 0
for i in range(n):
    ans += v[x[i]]
print(ans)

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

st = ''
for i in t:
    if i == 'r':
        st += 'p'
    elif i == 'p':
        st += 's'
    else:
        st += 'r'

stt = st[:k]

for i in range(k, n):
    if st[i] != stt[i - k]:
        stt += st[i]
    else:
        stt += 'x'

sum = 0
for i in stt:
    if i == 'r':
        sum += r
    elif i == 's':
        sum += s
    elif i == 'p':
        sum += p
    else:
        pass
print(sum)

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


T = input()

r_num = 0
s_num = 0
p_num = 0

mycurrent = ['a' for _ in range(K)]

for n in range(N):
    if T[n] == 'r':
        me = 'p'
    if T[n] == 's':
        me = 'r'
    if T[n] == 'p':
        me = 's'
    
    if mycurrent[n%K] != me:
        if me == 'r':
            r_num += 1
            mycurrent[n%K] = me
        if me == 's':
            s_num += 1
            mycurrent[n%K] = me
        if me == 'p':
            p_num += 1
            mycurrent[n%K] = me
            
    else:
        mycurrent[n%K] = 'a'
        
    
print(r_num* R + s_num* S + p_num*P)

Solution (Python):
[N, K] = list(map(int, input().split()))
[R, S, P] = list(map(int, input().split()))
T = str(input())
t = []
for i in range(K):
  t.append(T[i])
for i in range(K, N):
  if t[-K] != T[i]:
    t.append(T[i])
  else:
    t.append('Z')
R_s = R * len([r for r in t if r == 's'])
S_s = S * len([s for s in t if s == 'p'])
P_s = P * len([p for p in t if p == 'r'])
ans = int(R_s + S_s + P_s)
print(ans)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
for i in range(K,N):
    if T[i]==T[i-K] and T[i-K]!="x":
      T=(T[:i]+"x"+T[i+1:])
    else:
      continue
     
R_win=T.count("s")
S_win=T.count("p")
P_win=T.count("r")
point=R_win*R+S_win*S+P_win*P
print(point)

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

t_sep = []
for i in range(k):
    t_sep.append([])

for j in range(n):
    list_num = j%k
    t_sep[list_num].append(t[j])

ans = 0
for each_list in t_sep:
    if each_list[0] == 'r':
        ans += p
    elif each_list[0] == 's':
        ans += r
    else:
        ans += s


for each_list in t_sep:
    for i in range(len(each_list)-1):
        if each_list[i+1]==each_list[i]:
            if each_list[i + 1] != each_list[i-1] or i ==0:
                con = 1
            if  con ==1:
                ans += 0
            else:
                if each_list[i + 1] == 'r':
                    ans += p
                elif each_list[i + 1] == 's':
                    ans += r
                else:
                    ans += s
            con = con * (-1)


        else:
            if each_list[i+1] == 'r':
                ans += p
            elif each_list[i+1] == '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()





ans = [0] *len(T)
for i,x in enumerate(T):
  if x == 'r':
    ans[i] = P
  elif x == 's':
    ans[i] = R
  elif x == 'p':
    ans[i] = S
  
  if i >= K:
    if (T[i-K] == x) & (ans[i-K] == ans[i]):
      ans[i] = 0

print(sum(ans))

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = str(input())
janken = {"r":"p","s":"r","p":"s"}
point = {"r":P,"s":R,"p":S}
kekka = []
ans = 0

for i in range(K):
    kekka.append(janken[T[i]]) 
    ans += point[T[i]]

for i in range(K,N):
    if janken[T[i]] == kekka[i-K] and kekka[i-K] != T[i-K]: 
        kekka.append(T[i])
    else:
        kekka.append(janken[T[i]])
        ans += point[T[i]]

print(ans)

Solution (Python):

import sys


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    ans=0
    flag=[False]*N
    for i in range(N):
        if i<K:
            ans+=P if T[i]=='r' else S if T[i]=='p' else R
            flag[i]=True
    for i in range(K, N):
        if T[i]!=T[i-K] or (not flag[i-K]):
            ans+=P if T[i]=='r' else S if T[i]=='p' else R
            flag[i]=True
        
    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    R = int(next(tokens))  
    S = int(next(tokens))  
    P = int(next(tokens))  
    T = next(tokens)  
    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()


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


for i in range(k,n):    
    if hands[i] == hands[i-k]:
        c = 'rsp'
        c = c.replace(hands[i], '')
        if i+k < n:
            c = c.replace(hands[i+k], '')
        if len(c) == 1:
            hands = hands[:i] + c + hands[i+1:]
        else:
            hands = hands[:i] + t[i] + hands[i+1:]

cnt = 0
for i in range(n):
    if (hands[i]=='r')&(t[i]=='s'):
        cnt += r
    elif (hands[i]=='s')&(t[i]=='p'):
        cnt += s
    elif (hands[i]=='p')&(t[i]=='r'):
        cnt += p
print(cnt)

Solution (Python):

import sys

N,K=map(int, sys.stdin.readline().split())
R,S,P=map(int, sys.stdin.readline().split())
T=raw_input()

max_T=[]
for x in T:
    if x=="r": max_T.append("p")
    elif x=="s": max_T.append("r")
    elif x=="p": max_T.append("s")

for i,x in enumerate(max_T):
    if i+K<N:
        if max_T[i]==max_T[i+K]:
            max_T[i+K]=None

ans=0
for x in max_T:
    if x=="r": ans+=R
    elif x=="s": ans+=S
    elif x=="p": ans+=P
print ans

Solution (Python):
N , K = map(int,input().split())
R , S, P = map(int,input().split())
T = list(input())
Tstr = str(T)
r = Tstr.count('r')
s = Tstr.count('s')
p = Tstr.count('p')

for i in range(N-K):
    if T[K+i] == T[i] == 'r':
        T[K+i] = 'x'
    elif T[K+i] == T[i] == 's':
        T[K+i] = 'x'
    elif T[K+i] == T[i] == 'p':
        T[K+i] = 'x'
    else:
        r = r
Tstr = str(T)
r2 = Tstr.count('r')
s2 = Tstr.count('s')
p2 = Tstr.count('p')
    
print(r2*P+s2*R+p2*S)

Solution (Python):
import copy

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

total = 0
choice = {'r':0,'s':1,'p':2}
lst = [P,R,S]

for i in range(K):
    total += lst[choice[T[i]]]
    
for i in range(K,N):
    if T[i] != T[i-K]:
        total += lst[choice[T[i]]]
    else:
        T[i] = ''

print(total)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
line,last=divmod(n,k)
for i in range(last):
  lr,ls,lp=0,0,0
  for j in range(line+1):
    x=t[j*k+i]
    if x=="r":
      tr=max(ls,lp)
      ts=max(lp,lr)
      tp=max(lr,ls)+p
      lr,ls,lp=tr,ts,tp
    elif x=="s":
      tr=max(ls,lp)+r
      ts=max(lp,lr)
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
    else:
      tr=max(ls,lp)
      ts=max(lp,lr)+s
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
  ans+=max(lr,ls,lp)
for i in range(last,k):
  lr,ls,lp=0,0,0
  for j in range(line):
    x=t[j*k+i]
    if x=="r":
      tr=max(ls,lp)
      ts=max(lp,lr)
      tp=max(lr,ls)+p
      lr,ls,lp=tr,ts,tp
    elif x=="s":
      tr=max(ls,lp)+r
      ts=max(lp,lr)
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
    else:
      tr=max(ls,lp)
      ts=max(lp,lr)+s
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
  ans+=max(lr,ls,lp)
print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
best = ''
for ti in t:
    if ti == 's':
        best += 'r'
    elif ti == 'p':
        best += 's'
    else:
        best += 'p'
lis = [[] for _ in range(k)]

final_st = ''
for bi,b in enumerate(best):
    lis[bi%k].append(b)
for each_lis in lis:
    each_st = ''
    for e_i,e_lis in enumerate(each_lis):
        if e_i == len(each_lis) - 1:
            if e_i == 0:
                each_st += each_lis[e_i]
            else:
                if each_lis[e_i] != each_lis[e_i - 1]:
                    each_st += each_lis[e_i]
            break
        else:
            if each_lis[e_i] != each_lis[e_i + 1]:
                each_st += each_lis[e_i]
            else:
                each_st += each_lis[e_i]
                each_lis[e_i + 1] = 'x'
    
    final_st += each_st


final_sum = 0
for f in final_st:
    if f == 'r':
        final_sum += r
    elif f == 's':
        final_sum += s
    elif f == 'p':
        final_sum += p
print(final_sum)

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

ans = 0
a = []
for i, t in enumerate(T):
  if i < K:
    ans += d[t]
    a.append(t)
  else:
    if t == a[i-K]:
      a.append('a')
    else:
      ans += d[t]
      a.append(t)

print(ans)

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

  set_rsp = set(['r', 's', 'p'])

  def vict_output(c):
    if c == 'r':
        output = 'p'
        point = P
    elif c == 's':
        output = 'r'
        point = R
    else:
        output = 's'
        point = S
    return output, point

  history = []
  score = 0
  for i in range(len(T)):
      
      
      output, point = vict_output(T[i])
      if i >= K:
          
          pre_K_output = history[i - K]
          
          if output == pre_K_output:
              
              point = 0
              
              if (i + K) <= (len(T) - 1):
                  
                  output, _ = vict_output(T[i + K])
                  if output == pre_K_output:
                        
                        
                        
                        
                        
                        
                        o1, p1 = vict_output(pre_K_output)
                        o2, p2 = vict_output(o1)
                        
                        
                        if (p1 > p2):
                            output = o1
                            
                        else:
                            output = o2
                            
                  else:
                        output, _ = vict_output(output)
                        output, _ = vict_output(output)
              else:
                  
                  output, _ = vict_output(list(set_rsp - set(pre_K_output))[0])
                  

      
      score += point
      history.append(output)

  
  print(score)

if __name__ == '__main__':
    main()

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

row=[]
for i in range(K):
  row.append(T[i::K])
  
score=0
for r in row:
  pre="n"
  for i, hand in enumerate(r):
    if hand==pre:
      pre=0
    else:
      if hand=="r":
        score+=P
        pre="r"
      elif hand=="s":
        score+=R
        pre="s"
      else:
        score+=S
        pre="p"
        
print(score)

Solution (Python):
import sys
import math


N,K=map(int,input().split())
r,s,p=map(int,input().split()) 
t=input()
ans=0
for i in range(K):
    j=i
    tmp=[0,0,0]
    while j<N:
        if t[j]=='r':
            tmp=[max(tmp[1],tmp[2]),max(tmp[0],tmp[2]),max(tmp[0],tmp[1])+p]
        elif t[j]=='s':
            tmp=[max(tmp[1],tmp[2])+r,max(tmp[0],tmp[2]),max(tmp[0],tmp[1])]
        else:
            tmp=[max(tmp[1],tmp[2]),max(tmp[0],tmp[2])+s,max(tmp[0],tmp[1])]
        j+=K
        
    ans+=max(tmp)

print(ans)

Solution (Python):
def winList(t, r, s, p):
    if t == "r":
        return [0, 0, p]
    elif t == "s":
        return [r, 0, 0]
    else:
        return [0, s, 0]

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

dp = []
for i in range(N):
    wList = winList(T[i], R, S, P)
    if i <= K-1:
        if i == 0:
            dp.append(wList)
        else:
            dp.append([wList[0] + max(dp[i - 1]),
                       wList[1] + max(dp[i - 1]),
                       wList[2] + max(dp[i - 1])])
    else:
        wKList = winList(T[i - K], R, S, P)
        tmpR = 0
        tmpS = 0
        tmpP = 0
        if dp[i - K][0] > dp[i - K][1] and dp[i - K][0] > dp[i - K][2]:
            tmpR = wList[0] + max(dp[i - 1]) - wKList[0]
        else:
            tmpR = wList[0] + max(dp[i - 1])

        if dp[i - K][1] > dp[i - K][0] and dp[i - K][1] > dp[i - K][2]:
            tmpS = wList[1] + max(dp[i - 1]) - wKList[1]
        else:
            tmpS = wList[1] + max(dp[i - 1])

        if dp[i - K][2] > dp[i - K][0] and dp[i - K][2] > dp[i - K][1]:
            tmpP = wList[2] + max(dp[i - 1]) - wKList[2]
        else:
            tmpP = wList[2] + max(dp[i - 1])
        dp.append([tmpR, tmpS, tmpP])

print(max(dp[N-1]))

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

print(ans)

Solution (Python):
import sys
from pprint import pprint
import math

def solve(N, k, r, s, p, t):
    score = [r, s, p]
    ans = 0
    for i in range(k):
        new_t = []
        
        
        
        for j in range(math.ceil(N / k)):
            
            if j * k + i >= len(t):
                break
            v = t[j * k + i]
            if v == "r":
                v_tmp = 0
            elif v == "s":
                v_tmp = 1
            else:
                v_tmp = 2
            new_t.append(v_tmp)
        
        
        len_nt = len(new_t)
        
        dp = []
        for _ in range(len_nt + 1):
            dp.append([0] * 3)
        for l in range(len_nt):
            for m in range(3):
                
                for n in range(3):
                    
                    if m == n:
                        continue

                    
                    
                    
                    if new_t[l] - n == 1 or n - new_t[l] == 2:
                        
                        dp[l+1][n] = max(dp[l+1][n], dp[l][m] + score[n])
                    
                    else:
                        dp[l+1][n] = max(dp[l+1][n], dp[l][m])
        
        
        ans += max(dp[len_nt])
    
    print(ans)  

if __name__ == '__main__':
    n, k = map(int, sys.stdin.readline().strip().split(" "))
    r, s, p = map(int, sys.stdin.readline().strip().split(" "))
    t = list(sys.stdin.readline().strip())
    
    
    solve(n, k, r, s, p, t)