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):
from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


def main():
    n,k = mi()
    r,s,p = mi()
    tmp = list(input())
    t = [0]
    t+=tmp

    ans = 0

    for i in range(1,n+1):
        if t[max(0,i-k)] == t[i]:
            t[i] = 0
            continue
        if t[i] == 'r':
            ans+=p
        elif t[i] == 'p':
            ans+=s
        else:
            ans+=r

    print(ans)



    


    
main()

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

line=[ t[i::k] for i in range(k)]

rr=0
ss=0
pp=0
for x in line:
    

    combo=1
    al=x[0]
    for i in x[1:]:
        if al == i:
            combo+=1
            next
        else:
            if al == 'r':
                pp+=(combo+1)//2
            elif al == 's':
                rr+=(combo+1)//2
            else:
                ss+=(combo+1)//2
            combo=1
            al=i

    if al == 'r':
        pp+=(combo+1)//2
    elif al == 's':
        rr+=(combo+1)//2
    else:
        ss+=(combo+1)//2



print(pp*p+rr*r+ss*s)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
for i in range(K):
    current_list = []
    k = i
    while k < len(T):
        current_list.append(T[k])
        k += K
    current_value = current_list[0]
    c = 1
    for j in range(1,len(current_list)):
        if current_value == current_list[j]:
            c += 1
        else:
            if current_value == 'r':
                ans += ((c+1)//2)*P
            elif current_value == 's':
                ans += ((c+1)//2)*R
            else:
                ans += ((c+1)//2)*S
            c = 1
            current_value = current_list[j]
    if current_value == 'r':
        ans += ((c+1)//2)*P
    elif current_value == 's':
        ans += ((c+1)//2)*R
    else:
        ans += ((c+1)//2)*S
print(ans)

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

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

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

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

for i in range(K,N):
  if T[i-K] == T[i]:
    T[i] = ''
    continue
  ans += score[T[i]]

print(ans)

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

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


def scr(inp):
	if inp == 'r':
		return P
	elif inp == 'p':
		return S
	else:
		return R


Score = 0
for i in range(N):
	if i >= K and T[i] == T[i-K]:
		if T[i-K] == 'z':
			Score += scr(T[i])
		else:
			T = T[:i] + 'z' + T[i + 1:]
	else:
		Score += scr(T[i])

print(Score)

Solution (Python):
def getPt(h):
    if h == "s":
        return R
    elif h == "p":
        return S
    elif h == "r":
        return P

N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = list(input())
pt = 0
for i in range(N):
    if i < K:
        pt += getPt(T[i])
    else:
        if T[i] == T[i-K]:
            T[i] = ""
        else:
            pt += getPt(T[i])
print(pt)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 1 << 100
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
r, s, p = LI()
D = {'r': p, 'p': s, 's': r}
t = S()
flag = [1] * n
ans = 0
for i in range(n):
    if i <= k - 1:
        ans += D[t[i]]
    else:
        if t[i - k] == t[i] and flag[i - k]:
            flag[i] = 0
        else:
            ans += D[t[i]]



print(ans)

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

Solution (Python):
n, k = [int(i) for i in  input().split(' ')]
r, s, p = [int(i)for i in  input().split(' ')]
t = input()

su  =  p*t.count('r') + r*t.count('s')  + s*t.count('p') 

ls = []
for i in range(len(t)-k):
    if t[i] == t[i+k]:
        ls.append(t[i])
        t = t[:i+k] + 'a' + t[i+k+1:]  

su2 = p*ls.count('r') + r*ls.count('s')  + s*ls.count('p') 
print(su-su2)

Solution (Python):
input_line1 = input().split(' ')
input_line2 = input().split(' ')
stT = input()
numN = int(input_line1[0])
numK = int(input_line1[1])
numR = int(input_line2[0])
numS = int(input_line2[1])
numP = int(input_line2[2])

vioR = []
vioS = []
vioP = []
result = [0] * numN 

for i, c in enumerate(stT):
  if i + numK < numN and stT[i + numK] == c and result[i] == 0:
    result[i + numK] = -1
  if result[i] != -1:
    result[i] = c

score = 0
for i in result:
    if i == 'r':
        score += numP
    elif i == 's':
        score += numR
    elif i == 'p':
        score += numS
print(score)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
p = dict()
p['r'] = R
p['s'] = S
p['p'] = P
T = input()


def solve(me, you, memo):
    point = 0
    for i, t in enumerate(T):
        if t == you and (i < K or (i - K >= 0 and memo[i - K] != me)):
            memo[i] = me
            point += p[me]
    return point


m = ['' for _ in range(N)]
pri = [(R, 'r', 's'), (S, 's', 'p'), (P, 'p', 'r')]
pri.sort(reverse=True)
ans = 0
for _, i, u in pri:
    ans += solve(i, u, m)
print(ans)

Solution (Python):

def solve():
  n,k = (int(i) for i in input().split())
  r,s,p = (int(i) for i in input().split())
  t = input()
  score = 0
  times = 0
  possible = [False]*n
  for i in range(n):
    if i >= k:
      if t[i-k] == t[i] and possible[i-k]== False:
        possible[i] = True
        continue

    if t[i] == "r":
        score += p
    elif t[i] == "s":
        score += r
    elif t[i] == "p":
        score += s
  print(score)
solve()

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()
  
  my = T.replace("r", "hoge").replace("s", "r").replace("p", "s").replace("hoge", "p") 
  for i in range(k, n):
    if my[i] == my[i-k]:
      my = my[:i] + "l" + my[i+1:]  

  print(my.count("r")* r + my.count("s")*s + my.count("p")* p)
  
if __name__ == "__main__":
  main()

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


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

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

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

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

    ans += point
    commands[i] = command

print(ans)

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

n, k = map(int, input().split())
s, p, r = map(int, input().split())
t = input().rstrip().decode("utf-8")

cnt = 0
for i in range(k):
    f = 0
    pre = ""
    for j in range(i, n, k):
        if pre != t[j] or f == 1:
            cnt += eval(t[j])
            f = 0
        else:
            f = 1
        pre = t[j]
print(cnt)

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

cnt = 0
for i in range(k):
    f = 0
    pre = ""
    for j in range(i, n, k):
        if pre != t[j] or f == 1:
            cnt += eval(t[j])
            f = 0
        else:
            f = 1
        pre = t[j]
print(cnt)

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 math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res



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

hands = [[] for i in range(k)]
for i in range(n):
    hands[i%k].append(s[i])
ans = 0    
for i in hands:
    prev = 't'
    for j in i:
        if(prev==j):
            ans+=0
            prev = 't'
        else:
            if(j=='r'): ans+=p
            if(j=='s'): ans+=r
            if(j=='p'): ans+=ss
            prev = j
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
p_list=[]
score=0
for i in range(len(t)):
    if i<k:
        if t[i]=='r':
            p_list.append('p')
            score+=p
        elif t[i]=='s':
            p_list.append('r')
            score+=r
        else:
            p_list.append('s')
            score+=s
    else:
        if t[i]=='r':
            if p_list[i-k]=='p':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('p')
                score+=p
        elif t[i]=='s':
            if p_list[i-k]=='r':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('r')
                score+=r
        else:
            if p_list[i-k]=='s':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('s')
                score+=s
print(score)

Solution (Python):
from sys import stdin
from itertools import groupby
import math 

n,k = [int(x) for x in stdin.readline().rstrip().split()]
r,s,p = [int(x) for x in stdin.readline().rstrip().split()]
t = stdin.readline().rstrip()
t = [i for i in t]
point = 0

for i in range(k):
    li = t[i::k]
    lin = []
    for key, value in groupby(li):
        lin.append((key,len(list(value))))
    for j in lin:
        if j[0] == "r":
            point += math.ceil(j[1]/2)*p
        elif j[0] == "s":
            point += math.ceil(j[1]/2)*r
        else:
            point += math.ceil(j[1]/2)*s

print(point)

Solution (Python):

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

hist = [[0 for i in range(3)] for _ in range(k)]

for i,v in enumerate(t):
    md = i%k
    cur_0 = hist[md][0]
    cur_1 = hist[md][1]
    cur_2 = hist[md][2]
    cur_max = max(hist[md])
    hist[md]=[cur_max]*3
    if v=="r":
        hist[md]=[cur_max]*3
        hist[md][2]=max(cur_1,cur_0)+p
    elif v=="s":
        hist[md][0]=max(cur_1,cur_2)+r
    else:
        hist[md][1]=max(cur_0,cur_2)+s

print(sum([max(i) for i in hist]))

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

def rsp(x):
    if x=='r':
        return 'p'
    elif x=='s':
        return 'r'
    else:
        return 's'

Ban = ['']*k+[ rsp(t[i]) for i in range(n-k) ]
res = 0
for i in range(n):
    if rsp(t[i]) != Ban[i]:
        if rsp(t[i]) == 'r':
            res += r
        elif rsp(t[i]) == 's':
            res += s
        else:
            res += p
    elif i+k<n:
        Ban[i+k] = ''
print(res)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
d = {'r':'p', 'p':'s', 's':'r'}
ans = [d[i] for i in t]
for i in range(k, n):
    if ans[i-k] == 'x':
        continue
    if ans[i-k] == ans[i]:
        ans[i] = 'x'
total = 0
for i in ans:
    if i == 'r':
        total+=r
    elif i == 'p':
        total+=p
    elif i == 's':
        total+=s
print(total)

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


for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Solution (Python):
def fu(st):
    global r,s,p
    if(st=='r'):
        return p
    elif(st=='p'):
        return s
    return r
    

n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
t=list(input())


dp = [0]*(10**5+11)
res=0
for i in range(k):
    res+=fu(t[i])
    dp[i]=res

for i in range(k,len(t)):
    if(t[i-k]==t[i]):
        dp[i] = max(dp[i-k],dp[i-1])
        t[i]='0'
    else:
        dp[i] = dp[i-1]+fu(t[i])

print(dp[n-1])

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
score = 0
for i in range(0,K):
    prev = ""
    for j in range(i,N,K):
        if prev != T[j:j+1]:
            if T[j:j+1] == "r":
                score += P
            elif T[j:j+1] == "s":
                score += R
            else:
                score += S
            prev = T[j:j+1]
        else:
            prev = ""
print(score)

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

def point(x):
  if x == 'r':
    return(p)
  elif x == 's':
    return(r)
  else:
    return(s)

score = 0
for i in range(n):
  score += point(t[i])
  
for i in range(k):
  a = i
  b = 1
  while a+k < n:
    if t[a] == t[a+k]:
      b += 1
    else:
      score -= point(t[a])*(b//2)
      b = 1
    a += k
  else:
    score -= point(t[a])*(b//2)
print(score)

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

dasita_te = []

score = 0

for i, c in enumerate(t):
    if c == "r":
        if (i >= k and dasita_te[i - k] != "p") or i < k:
            score += p
            dasita_te.append("p")
        else:
            dasita_te.append("-")
    elif c == "s":
        if (i >= k and dasita_te[i - k] != "r") or i < k:
            score += r
            dasita_te.append("r")
        else:
            dasita_te.append("-")
    elif c == "p":
        if (i >= k and dasita_te[i - k] != "s") or i < k:
            score += s
            dasita_te.append("s")
        else:
            dasita_te.append("-")

print(score)

Solution (Python):
n = list(map(int,input().split()))
scores = list(map(int,input().split()))
cards = input()
cards = list(cards)
ans = 0

for i in range(n[0]):
    if i < n[1] or cards[i-n[1]] != cards[i]:
        if cards[i] == 'r':
            ans += scores[2]
        elif cards[i] == 's':
            ans += scores[0]
        else:
            ans += scores[1]
    else:
        cards[i] = 'k'

print(ans)

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

print(score)

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

ans = 0
hands = []
for i in range(N):
    t = T[i]
    if t == 'r':
        if i < K or hands[i-K] != 'p':
            ans += P
            hands.append('p')
        else:
            if i+K >= N:
                hands.append('r')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
    elif t == 's':
        if i < K or hands[i-K] != 'r':
            ans += R
            hands.append('r')
        else:
            if i+K >= N:
                hands.append('s')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
    elif t == 'p':
        if i < K or hands[i-K] != 's':
            ans += S
            hands.append('s')
        else:
            if i+K >= N:
                hands.append('p')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
X = {'r':P, 's':R, 'p':S}
win = [False]*K

ans = 0
for i in range(N):
    if i<K or T[i]!=T[i-K] or not win[i%K]:
        ans += X[T[i]]
        win[i%K]=True
    else:
        win[i%K]=False

print(ans)

Solution (Python):
n, k = map(int, input().split())
rsp = list(map(int, input().split()))
t = input()
ans = 0
d = {'r': 2, 's':0, 'p': 1}
for c in t:
  ans += rsp[d[c]]
for i in range(k):
  prev = t[i]
  seq = 1
  for j in range(i+k, n, k):
    if t[j] == prev:
      seq += 1
    else:
      ans -= rsp[d[prev]] * (seq // 2)
      prev = t[j]
      seq = 1
  ans -= rsp[d[prev]] * (seq // 2)
print(ans)

Solution (Python):
def main():
    N, K = map(int, input().split(" "))
    R, S, P = map(int, input().split(" "))
    strings = input()
    T = []
    for s in strings:
        T.append(s)
    score = {'r': P, 's': R, 'p': S}
    total = 0

    for i in range(K):
        total += score[T[i]]

    for j in range(K, N):
        if T[j] != T[j - K]:
            total += score[T[j]]
        else:
            T[j] = ' '
            continue

    print(total)

   
if __name__ == '__main__':
    main()

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

hands = ['r', 's', 'p']
for i,t in enumerate(T):
    if t == 'r':
        T[i] = 'p'
    elif t == 's':
        T[i] = 'r'
    else:
        T[i] = 's'
point = 0
for i,t in enumerate(T):
    if i+1 <= K:
        if t == 'r':
            point += R
        elif t == 's':
            point += S
        else:
            point += P
        continue
    if t == T[i-K]:
        a = 0
        if len(T)-1 < i+K:
            next_hand = T[i]
        else:
            next_hand = T[i+K]
        while T[i] == T[i-K] or T[i] == next_hand:
            T[i] = hands[a]
            a += 1
    else:
        if t == 'r':
            point += R
        elif t == 's':
            point += S
        else:
            point += P
print(point)

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] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: 
        point = 0
    ans += point
 
    if(T[i+K] == T[i]):
        T[i+K] = 'n'
 

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

Solution (Python):
import math
import sys

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





def maxp(s):
    vr = 0
    vs = 0
    vp = 0
    for i in range(0,len(s)):
        if s[i] == 'r':
            nvr = 0 + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = P + max(vr,vs)
        elif s[i] == 's':
            nvr = R + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = 0 + max(vr,vs)
        else:
            nvr = 0 + max(vs,vp)
            nvs = S + max(vp,vr)
            nvp = 0 + max(vr,vs)
        vr = nvr
        vs = nvs
        vp = nvp
        
    return(max(vr,vs,vp))
ans=0
for i in range(0,K):
    p = ""
    for k in range(i,len(T),K):
        p = p + T[k]
    
    ans  = ans + maxp(p)
print(ans)

Solution (Python):
def main():
  N,K = input().split()
  N = int(N)
  K = int(K)
  R,S,P = input().split()
  R = int(R)
  S = int(S)
  P = int(P)
  T = input()
  
  
  
  T = T.replace("r","a")
  T = T.replace("s","b")
  T = T.replace("p","c")
  T = T.replace("a","p")
  T = T.replace("b","r")
  T = T.replace("c","s")
  
  
  
  
  
  
  T = list(T)
  score=0
  
  for i in range(N-K):
    if(T[i]=="r"):
      score = score+R
    elif(T[i]=="s"):
      score = score+S
    elif(T[i]=="p"):
      score = score+P
    if(T[i]==T[i+K]):
      T[i+K]="n"
          
  for i in range(N-K,N,1):
    if(T[i]=="r"):
      score = score+R
    elif(T[i]=="s"):
      score = score+S
    elif(T[i]=="p"):
      score = score+P

  print(score)
  

if __name__=="__main__":
  main()

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
r,s,p = inm()
t = input().strip()
fail = [False]*n
acc = 0
for i in range(n):
    ddprint("i {}".format(i))
    if t[i]=='r':
        if not (i>=k and t[i-k]=='r') or fail[i-k]:
            acc += p
            ddprint("add {}".format(p))
        else:
            fail[i] = True
    elif t[i]=='s':
        if not (i>=k and t[i-k]=='s') or fail[i-k]:
            acc += r
            ddprint("add {}".format(r))
        else:
            fail[i] = True
    else:
        if not (i>=k and t[i-k]=='p') or fail[i-k]:
            acc += s
            ddprint("add {}".format(s))
        else:
            fail[i] = True
print(acc)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
string=input()
ans=0
for i in range(k):
    F=""
    for j in range(n):
        if i+j*k<=n-1:
            F+=string[i+j*k]
        else:
            break
    length=len(F)
    gu=[0 for i in range(length+1)]
    tyoki=[0 for i in range(length+1)]
    pa=[0 for i in range(length+1)]
    for index in range(1,length+1):
        gu[index]=max(tyoki[index-1],pa[index-1])
        tyoki[index]=max(gu[index-1],pa[index-1])
        pa[index]=max(gu[index-1],tyoki[index-1])
        if F[index-1]=="r":
            pa[index]+=P
        elif F[index-1]=="p":
            tyoki[index]+=S    
        elif F[index-1]=="s":
            gu[index]+=R
    ans+=max(tyoki[length],pa[length],gu[length])
print(ans)

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] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: 
        point = 0
    ans += point

    if(T[i+K] == T[i]):
        T[i+K] = 'n'


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

print(ans)

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

def score(R,S,P,T):
    if T == "r":
        return P
    elif T == "s":
        return R
    else:
        return S

sum = 0
flag = [0 for i in range(K)]
for i in range(N):
    if i < K:
        sum += score(R,S,P,T[i])
    else:
        if T[i] == T[i-K]:
            if not flag[i%K]:
                flag[i%K] = 1
            else:
                flag[i%K] = 0
                sum += score(R, S, P, T[i])
        else:
            flag[i % K] = 0
            sum += score(R, S, P, T[i])





print(sum)

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

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
hand = {"r" : p, "s" : r, "p" : s}
ans = 0
for i in range(n):
  temp= i-k
  if temp >=0 and t[i]==t[temp]:
    t[i]=" "
  else:
    ans+=hand[t[i]]
print(ans)

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

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

dp = [''] * n
a = 0
for i in range(k):
    a += m[t[i]]
    dp[i] = t[i]
    
for i in range(k, n):
    if t[i] != dp[i-k]:
        a += m[t[i]]
        dp[i] = t[i]

print(a)

Solution (Python):
def main():
	
	n,k = map(int,input().split())
	
	a = list(map(int,input().split()))
	rsp = list(str(input()))
	
	
	ans = 0
	jan = []
	for i in range(n):
		
		if i < k:
			if rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		elif rsp[i] == "s" and jan[i-k] == "r":
			jan.append("z")
		elif rsp[i] == "r" and jan[i-k] == "p":
			jan.append("z")
		elif rsp[i] == "p" and jan[i-k] == "s":
			jan.append("z")
		else:
			if rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		
		
	
	print(ans)

	
 
 
if __name__ == '__main__':
	main()

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

sum = 0
flag = [0 for i in range(N)]
countP, countR, countS = 0, 0, 0

for i in range(K):
    if T[i]=='r':
        countP += 1

    elif T[i]=='s':
        countR += 1

    elif T[i]=='p':
        countS += 1
for i in range(K,N):
    if T[i]=='r':
        if T[i-K]!='r':
            countP += 1
        else:
            if flag[i-K] == 1:
                countP += 1
                flag[i] = 0
            else:
                flag[i] = 1

    if T[i]=='s':
        if T[i-K]!='s':
            countR += 1
        else:
            if flag[i-K] == 1:
                countR += 1
                flag[i] = 0
            else:
                flag[i] = 1

    if T[i]=='p':
        if T[i-K]!='p':
            countS += 1
        else:
            if flag[i-K] == 1:
                countS += 1
                flag[i] = 0
            else:
                flag[i] = 1

sum = countR * R+ countP * P + countS * S
print(sum)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
syo = n//k 
total = 0
for i in range(k):
  te = ''
  score = 0
  for j in range(i, n, k):
    if score == 0 or t[j] != te:
      if t[j] == 'r':
        score = p
        total += p
        te = 'r'
      elif t[j] == 's':
        score = r
        total += r
        te = 's'
      else:
        score = s
        total += s
        te = 'p'
    else:
      score = 0
      
  
print(total)