Problem:
 Score : 400 points    Problem Statement At an arcade, Takahashi is playing a game called RPS Battle , which is played as follows:   The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)  Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):  R points for winning with Rock;  S points for winning with Scissors;  P points for winning with Paper.    However, in the i -th round, the player cannot use the hand he/she used in the (i-K) -th round. (In the first K rounds, the player can use any hand.)   Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.  The information Takahashi obtained is given as a string T . If the i -th character of T  (1 \leq i \leq N) is r , the machine will play Rock in the i -th round. Similarly, p and s stand for Paper and Scissors, respectively.  What is the maximum total score earned in the game by adequately choosing the hand to play in each round?      Notes In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.   If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;  if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;  if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;  if both players play the same hand, it is a draw.       Constraints  2 \leq N \leq 10^5  1 \leq K \leq N-1  1 \leq R,S,P \leq 10^4  N,K,R,S, and P are all integers.  |T| = N  T consists of r , p , and s .         Input Input is given from Standard Input in the following format:  N  K  R  S  P  T       Output Print the maximum total score earned in the game.        Sample Input 1 5 2
8 7 6
rsrpr      Sample Output 1 27  The machine will play {Rock, Scissors, Rock, Paper, Rock}.  We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27 .       Sample Input 2 7 1
100 10 1
ssssppr      Sample Output 2 211       Sample Input 3 30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr      Sample Output 3 4996

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def cnt(str):
    r_num = str.count('r') - str.count('rr')
    s_num = str.count('s') - str.count('ss')
    p_num = str.count('p') - str.count('pp')
    return P*r_num + R*s_num + S*p_num
sum = 0
for i in range(K):
  str = ''
  for j in range(N//K+1):
    if i+K*j < N:
      str = str + T[i+K*j]
  sum = sum + cnt(str)
print(sum)

Solution (Python):
n, k = map(int, input().split())
rsp = list(map(int, input().split()))
t = input()
dp = [[0]*(n+1) for _ in range(3)]
d = {"r":0, "s":1, "p":2}
for i in range(1, n+1):
    now = d[t[i-1]]
    if i <= k:
        for j in range(3):
            if (now - j)%3 == 1:
                dp[j][i] = rsp[j]
    else:
        for j in range(3):
            c = 0
            for l in range(3):
                if l != j:
                    c = max(c, dp[l][i-k])
            dp[j][i] += c
            if (now - j)%3 == 1:
                dp[j][i] += rsp[j]
ans = 0
for i in range(n-k+1, n+1):
    c = 0
    for j in range(3):
        c = max(c, dp[j][i])
    ans += c
print(ans)

Solution (Python):
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = [0,0,0]
if N%K != 0:
  for _ in range(K - N%K):
    T = T + "W"
A = len(T)//K
r = [[] for _ in range(K)]
for i in range(K):
  for j in range(A):
    r[i].append(T[i + j*K])
for i in range(K):
  f = r[i]
  for j in range(len(f)-1):
    if f[j+1] == f[j]:
      f[j+1] = "W"
  for h in range(len(f)):
    if f[h] == "r":
      ans[2] += 1
    elif f[h] == "s":
      ans[0] += 1
    elif f[h] == "p":
      ans[1] += 1
a = ans[0]*R + ans[1]*S + ans[2]*P
print(a)

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

kanri=[]
for i in range(N):
   kanri.append(1)

score=0
for i in range(N):
   if i<K:
      if T[i]=="r":
         score+=P
      elif T[i]=="s":
         score+=R
      else:
         score+=S

   elif i>=K:
      if T[i]==T[i-K] and kanri[i-K]==1:
         kanri[i]=0
      elif T[i]==T[i-K] and kanri[i-K]==0:
         if T[i]=="r":
            score+=P
         elif T[i]=="s":
            score+=R
         else:
            score+=S 
      else:
         if T[i]=="r":
            score+=P
         elif T[i]=="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()
ans = [0] * n
for i, c in enumerate(t):
    if i >= k and c == t[i - k] and ans[i - k] > 0:
        continue
    if c == 'r':
        ans[i] = p
    elif c == 's':
        ans[i] = r
    else:
        ans[i] = s
print(sum(ans))

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

score = [[0, 0, 0] for i in range(N + 1)]

my_dict = {}

for i, your_hand in enumerate(T):
    i += 1

    if i <= K:
        if your_hand == "s":
            score[i][0] = R + max(score[i-1])
            score[i][1] = max(score[i-1])
            score[i][2] = max(score[i-1])
            my_dict[i % K] = "r"
        elif your_hand == "p":
            score[i][0] = max(score[i-1])
            score[i][1] = S + max(score[i-1])
            score[i][2] = max(score[i-1])
            my_dict[i % K] = "s"
        else:
            score[i][0] = max(score[i-1])
            score[i][1] = max(score[i-1])
            score[i][2] = P + max(score[i-1])
            my_dict[i % K] = "p"

    else:
        if your_hand == "s":
            if my_dict[i % K] == "r":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = R + max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = "r"
        elif your_hand == "p":
            if my_dict[i % K] == "s":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = max(score[i-1])
                score[i][1] = S + max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = "s"
        else:
            if my_dict[i % K] == "p":
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = max(score[i-1])
                my_dict[i % K] = ""
            else:
                score[i][0] = max(score[i-1])
                score[i][1] = max(score[i-1])
                score[i][2] = P + max(score[i-1])
                my_dict[i % K] = "p"

ans = max(score[N])
print(ans)

Solution (Python):
class Hand:

    def __init__(self):
        self.flag_r = False
        self.flag_s = False
        self.flag_p = False

    def set_flag(self,str,bool):

        if str == "r":
            self.flag_r = bool

        elif str == "s":
            self.flag_s = bool

        elif str == "p":
            self.flag_p = bool

    def get_flag(self,str):

        if str == "r":
            return self.flag_r

        elif str == "s":
            return self.flag_s

        elif str == "p":
            return self.flag_p

class Point:

    def __init__(self,r,s,p):
        self._R = r
        self._S = s
        self._P = p

    def get_point(self,str):

        if str == "r":
            return self._P

        elif str == "s":
            return self._R

        elif str == "p":
            return self._S


if __name__ == '__main__':

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


    Hands = [Hand() for i in range(len(T))]
    Point = Point(R,S,P)
    ans = 0

    for i in range(len(T)):

        
        if Hands[i].get_flag(T[i]) == False:
            
            ans += Point.get_point(T[i])

            
            try:
                Hands[i+K].set_flag(T[i],True)
            except:
                pass

        else:
            continue


    print(ans)

Solution (Python):


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

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

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

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

    print(s)

if __name__ == "__main__":
    resolve()

Solution (Python):

n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
dic = {'r':p, 's':r, 'p':s}
t1 = ['']*n
score = 0
for i in range(k):
    score += dic[t[i]]
    t1[i] = t[i]
for i in range(k,n):
    if t[i] != t1[i-k]:
        score += dic[t[i]]
        t1[i] = t[i]
    else:
        t1[i] = 'x'
print(score)

Solution (Python):
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = list(input())
for i in range(K,N,1) :
    if T[i]==T[i-K]:
        T[i] = "NG"
        
R2=T.count("r")
S2=T.count("s")
P2=T.count("p")
print(P*R2+R*S2+S*P2)

Solution (Python):



test = ""

"""
5 2
8 7 6
rsrpr
ans 27
"""

"""
7 1
100 10 1
ssssppr
ans 211
"""

"""
30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr
ans 4996
"""

test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()

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

rsp = {"r", "s", "p"}

for i in range(k, n-1):
    if t[i] == t[i-k]:
        t[i] = rsp - {t[i], t[i+1]}
        
if t[n-1] == t[n-1-k]:
    t[n-1] = rsp - {t[n-1]}

ans = (t.count("r") * p +
       t.count("s") * r +
       t.count("p") * s)

print(ans)

Solution (Python):
temp = input().split()
n, k = int(temp[0]), int(temp[1])
temp = input().split()
r, s, p = int(temp[0]), int(temp[1]), int(temp[2])

t = input()
ans = ""
for i in range(k):
    l = [t[j] for j in range(i, len(t), k)]
    
    txt = ''.join(l)
    txt = txt.replace("rr", "r*")
    txt = txt.replace("ss", "s*")
    txt = txt.replace("pp", "p*")
    ans += txt
    

print(p*ans.count('r') + r*ans.count('s') + s*ans.count('p'))

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

a1 = a.replace("r","P")
a1 = a1.replace("p","S")
a1 = a1.replace("s","R")
  
for j, i in enumerate(a1):
  if j < K:
    pass
  else:
    if i == a1[j-K]:
      a1 = a1[:j] + "n" + a1[j+1:]
    else:
      pass
    
point1 = a1.count("P") * P
point2 = a1.count("R") * R
point3 = a1.count("S") * S

ans =  point1 + point2 + point3

print(ans)

Solution (Python):

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
F = [True] * len(T)
point = {"r": P, "s":R, "p":S}
ans = 0
for i in range(N):
    if i < K:
        ans += point[T[i]]
        F[i] = False
    
    else:
        if T[i] == T[i-K]:
            if F[i-K]:
                ans += point[T[i]]
                F[i] = False
            
            
        else:
            ans += point[T[i]]
            F[i] = False
    
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):
    if i<k:
        if t[i] =='r':
            ans+=p
        elif t[i] =='s':
            ans+=r
        else :
            ans+=s
    else:
        if t[i] =='r' and t[i-k]!='r':
            ans+=p
        elif t[i] =='s' and t[i-k]!='s':
            ans+=r
        elif t[i] =='p' and t[i-k]!='p':
            ans+=s
        else:
            t[i]='a'
            continue
print(ans)

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

prev = []
ans = 0
for _t in t:
    _p = ""
    if len(prev) >= k:
        _p = prev.pop(0)

    if _t == "r" and _p != "p":
        ans += p
        prev.append("p")
    elif _t == "s" and _p != "r":
        ans += r
        prev.append("r")
    elif _t == "p" and _p != "s":
        ans += s
        prev.append("s")
    else:
        prev.append("")

print(ans)

Solution (Python):





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

T=T.replace('r','P')
T=T.replace('s','R')
T=T.replace('p','S')

T_list = list(T)

for i in range(K,N):
    if T_list[i] == T_list[i-K]:
        T_list[i] = int(0)

for i in range(0,N):
    if T_list[i] == 'R':
        T_list[i] = R
    if T_list[i] == 'S':
        T_list[i] = S
    if T_list[i] == 'P':
        T_list[i] = P

print(sum(T_list))

Solution (Python):


N,K=map(int,input().split())
r,s,p=map(int,input().split())
t=[i for i in input()]
tmp=0
for i in range(K):
    if t[i]=='s':
        tmp+=r
    elif t[i]=='p':
        tmp+=s
    else:
        tmp+=p   
    
    
flg=0
for i in range(K,N):
    if t[i]!=t[i-K]:
        if t[i]=='s':
            tmp+=r
            
        elif t[i]=='p':
            tmp+=s
            
        else:
            tmp+=p
            
    else:
        t[i]='w'
        
        
            
    

print(tmp)

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

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

d = {"r":"p", "s":"r", "p":"s"}
r_d = {"p":"r", "r":"s", "s":"p"}

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

t = [None]*len(T)
for i in range(len(T)):
    t[i] = d[T[i]]

pp = 0
c = {"r":1, "s": 1, "p": 1}
cont = False
a = list()
for i in range(len(t)):
    if i-K>=0:
        if t[i]==t[i-K]:
            if a[i-K]==t[i]:
                a.append("")
                continue
    a.append(t[i])
    pp+=p_d[t[i]]

print(pp)

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

    ans_dict = {"r":0, "s":0, "p":0}
    ix_list = [i for i in range(K)]

    for i in range(N):
        if i < K:
            ans_dict[T[i]] += 1
            
        else:
            if T[i] == T[i-K]:
                T[i] = "t"
            else:
                ans_dict[T[i]] += 1
                
    ans = (ans_dict["r"]*P) + (ans_dict["s"]*R) + (ans_dict["p"]*S)
    return ans

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

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

selected  = []
get_points = []

def win_method(b):
  if b == 'r':
    return 'p'
  elif b == 's':
    return 'r'
  elif b == 'p':
    return 's'

for a, t in enumerate(tt):
  win_this = win_method(t)
  selectable = {'r': r, 's': s, 'p': p}
  
  if len(selected) >= k:
    bf = selected[-k]
    del selectable[bf]
  else:
    bf = ''
  
  if a + k < n:
    nx = win_method(tt[a + k])
    if win_this != nx and bf != nx:
      del selectable[nx]
  if win_this in selectable:
    selected.append(win_this)
    get_points.append(selectable[win_this])
  else:
    selected.append(next(iter(selectable)))
    get_points.append(0)
  
  
  
    
print(sum(get_points))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n,k=MI()
    r,s,p=MI()
    t=input()
    ans=0
    for m in range(k):
        pc="a"
        for c in t[m::k]:
            if c==pc:
                pc="a"
                continue
            if c=="r":ans+=p
            if c=="s":ans+=r
            if c=="p":ans+=s
            pc=c
    print(ans)

main()

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

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

    for i in range(K):
        prev = ''
        for j in range(N//K+1):
            idx = j*K+i
            if idx >= N:
                break
            current = T[idx]
            if current != prev:
                score += scores[current]
                prev = current
            else:
                prev = ''
    print(score)


if __name__ == '__main__':
    main()

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


def point(hand):
    if hand == "r":
        return p
    elif hand == "s":
        return r
    elif hand == "p":
        return s


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

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
scores = inpl()
T = input()
ctoi = {'r':0, 's':1, 'p':2}
ans = 0
for i in range(K):
    points = [0,0,0]
    j = i
    while j < N:
        en = ctoi[T[j]]
        points = [max(points[k] for k in ((c+1)%3,(c+2)%3)) + (scores[c] if (en-c) % 3 == 1 else 0) for c in range(3)]
        j += K
    ans += max(points)
print(ans)

Solution (Python):
n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = input()
cnt = 0
for i in range(k):
    lst = [t[i]]
    cont_cnt = 1
    for j in range(i+k, n, k):
        if t[j] == t[j-k]:
            cont_cnt += 1
        else:
            lst.append(cont_cnt)
            lst.append(t[j])
            cont_cnt = 1
    lst.append(cont_cnt)
    for j in range(0,len(lst), 2):
        if lst[j] == "r":
            cnt += (lst[j+1]+1) // 2 * p
        elif lst[j] == "s":
            cnt += (lst[j+1]+1) // 2 * r
        elif lst[j] == "p":
            cnt += (lst[j+1]+1) // 2 * s
print(cnt)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
tt=[]
ans=0
def score(c):
    if c=='r':
        return p
    if c=='s':
        return r
    if c=='p':
        return s


for i in range(n):
    if i-k>=0 and i+k>=n:
        if t[i]==t[i-k]:
            ans+=0
        else:
            ans+=score(t[i])
    elif i-k>=0:
        if t[i]==t[i-k]:
            if t[i]=='r':
                if t[i+k]!='s':
                    t=t[:i]+'s'+t[i+1:]
                else:
                    t=t[:i]+'p'+t[i+1:]
            elif t[i]=='s':
                if t[i+k]!='p':
                    t=t[:i]+'p'+t[i+1:]
                else:
                    t=t[:i]+'r'+t[i+1:]
            elif t[i]=='p':
                if t[i+k]!='r':
                    t=t[:i]+'r'+t[i+1:]
                else:
                    t=t[:i]+'s'+t[i+1:]
        else:
            ans+=score(t[i])
        
    else:
        ans+=score(t[i])

print(ans)

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

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
t2 = [''] * n
a = 0
for i in range(k):
    if t[i] == 'p':
        a += s
        t2[i] = 's'
    elif t[i] == 'r':
        a += p
        t2[i] = 'p'
    elif t[i] == 's':
        a += r
        t2[i] = 'r'
for i in range(k, n):
    if t[i] == 'p' and t2[i-k] != 's':
        a += s
        t2[i] = 's'
    elif t[i] == 'r' and t2[i-k] != 'p':
        a += p
        t2[i] = 'p'
    elif t[i] == 's' and t2[i-k] != 'r':
        a += r
        t2[i] = 'r'
print(a)

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

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


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

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

for i in range(len(T)):
  if T[i] == "r":
    points.append(P)
  if T[i] == "s":
    points.append(R)
  if T[i] == "p":
    points.append(S)
  
  if i >= K and T[i] == T[i-K]:
    if points[i] > points[i-K]:
      points[i-K] = 0
    else:
      points[i] = 0

print(sum(points))

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



m={'r':'R','s':'S','p':'P'}
rot = {'R':'p','S':'r','P':'s'}
ans = t.translate(str.maketrans(m))
ans = ans.translate(str.maketrans(rot))



val = 0
for i in range(k):
    
    tmp = (ans[i::k].replace('pp','pX').replace('ss','sX').replace('rr','rX'))
    val += tmp.count('r')*r + tmp.count('s')*s + tmp.count('p')*p




print(val)

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

if r >= s and s >= p:
    strong_list = ["r", "s", "p"]
    opp_list = ["s", "p", "r"]
    strong_list_num = [r, s, p]
elif r >= p and p >= s:
    strong_list = ["r", "p", "s"]
    opp_list = ["s", "r", "p"]
    strong_list_num = [r, p, s]
elif s >= p and p >= r:
    strong_list = ["s", "p", "r"]
    opp_list = ["p", "r", "s"]
    strong_list_num = [s, p, r]
elif s >= r and r >= p:
    strong_list = ["s", "r", "p"]
    opp_list = ["p", "s", "r"]
    strong_list_num = [s, r, p]
elif p >= r and r >= s:
    strong_list = ["p", "r", "s"]
    opp_list = ["r", "s", "p"]
    strong_list_num = [p, r, s]
else:
    strong_list = ["p", "s", "r"]
    opp_list = ["r", "p", "s"]
    strong_list_num = [p, s, r]

my_hands = [0] * n
sum = 0
for ct in range(3):
    my_current_hand = strong_list[ct]
    opp_current_hand = opp_list[ct]
    for i in range(n):
        if not t[i] == opp_current_hand:
            continue
        elif i-k < 0:
            my_hands[i] = my_current_hand
            sum += strong_list_num[ct]
        elif not my_hands[i-k] == my_current_hand:
            my_hands[i] = my_current_hand
            sum += strong_list_num[ct]
print(sum)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n, k = LI()
    rsp = LI()
    t = S()
    dp = [[0] * 3 for i in range(n + k)]
    j = ["r", "s", "p"]
    for i in range(n):
        ti = t[i]
        for l in range(3):
            dp[i + k][l] = max(dp[i][(l - 1) % 3], dp[i][(l + 1) % 3]) + (rsp[l] if (j.index(ti) - 1) % 3 == l else 0)

    ans = 0
    for i in range(k):
        ans += max(dp[n + i])
    print(ans)

    return



if __name__ == '__main__':
    solve()

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

ans = 0
check = {i:0 for i in range(N)}
for i in range(N):
    if i <= K-1:
        if T[i] == 'r':
            ans += P
        elif T[i] == 's':
            ans += R
        else:
            ans += S
    else:
        if T[i] != T[i-K]:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R
            else:
                ans += S
        elif T[i] == T[i-K] and check[i-K] == 1:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R   
            else:
                ans += S
        else:
            check[i] = 1

print(ans)

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

ten = [0] * N
for i in range(N):
    if (T[i] == 'r'):
        ten[i] = P
    elif(T[i] == 's'):
        ten[i] = R
    else:
        ten[i] = S
 

for i in range(N-K):
    if(T[i] == T[i+K]):
        ten[i+K] = 0
        T[i+K] = 'o'


print(sum(ten))

Solution (Python):
n,k=map(int,input().split())
ro,se,pe=map(int,input().split())
t=input()
cnt=0
x=[]
for i in range(n):
    if(i>=k):
        if(t[i]=='r' and x[i-k]!='p'):
            cnt=cnt+pe
            x.append('p')
        elif(t[i]=='s' and x[i-k]!='r'):
            cnt=cnt+ro
            x.append('r')
        elif(t[i]=='p' and x[i-k]!='s'):
            cnt=cnt+se
            x.append('s')
        else:
            x.append('n')
    else:
        if(t[i]=='r'):
            cnt=cnt+pe
            x.append('p')
        elif(t[i]=='s'):
            cnt=cnt+ro
            x.append('r')
        elif(t[i]=='p'):
            cnt=cnt+se
            x.append('s')
        else:
            x.append('n')
print(cnt)

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

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

total = 0
enable = [True for _ in range(n)]
for i in range(n):
    if enable[i]:
        
        total += point(t[i])
        if i+k < n and t[i] == t[i+k]:
            enable[i+k] = False

print(total)

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

for i in t:
  if i == "s":
    T.append(r)
  if i == "p":
    T.append(s)
  if i == "r":
    T.append(p)

ans = 0
for i in range(n):
  if i+k < n:
    if t[i] == t[i+k]:
      T[i+k] = 0
      t[i+k] = "q"
      ans += T[i]
    else:
      ans += T[i]
  else:
    ans += T[i]
print(ans)

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n,k=nm()
r,s,p=nm()
t=si()
l=[[] for i in range(k)]
ans=0

for i in range(n):
    j=(i+1)%k
    l[j].append(t[i:i+1])

same_flg=False
for ll in l:
    cur_str=''
    for lll in ll:
        if cur_str=='':
            if lll=='p':
                ans+=s
            elif lll=='r':
                ans+=p
            elif lll=='s':
                ans+=r 
            cur_str=lll
            same_flg=True
            continue

        if lll==cur_str:
            
            cur_str=lll
            if same_flg==True:
                same_flg=False
            elif same_flg==False:
                if lll=='p':
                    ans+=s
                elif lll=='r':
                    ans+=p
                elif lll=='s':
                    ans+=r 
                same_flg=True
            pass
        else:
            if lll=='p':
                ans+=s
            elif lll=='r':
                ans+=p
            elif lll=='s':
                ans+=r
            cur_str=lll
            same_flg=True
print(ans)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
get=[P,R,S]
t=list(input())
info=[]
for i in range(1,k+1): 
    a=[]
    num=i-1
    count=0
    while num<=(n-1):
        if t[num]=="r":
            b=0
        elif t[num]=="s":
            b=1
        else:
            b=2
        a.append(b)
        count+=1
        num+=k
    a.insert(0,count)
    info.append(a)

score=0
for i in range(k):
    if info[i][0]==1: 
        score+=get[info[i][1]]
        continue
    rensa=1
    now=info[i][1]
    for j in range(2,info[i][0]+1):
        if now==info[i][j]:
            rensa+=1
        else:
            if rensa%2==0: 
                score+=((rensa//2)*get[now])
            else:
                score+=((rensa//2+1)*get[now])
            now=info[i][j]
            rensa=1
    if rensa==1:
        score+=get[now]
    else:
        if rensa%2==0: 
                score+=((rensa//2)*get[now])
        else:
            score+=((rensa//2+1)*get[now])
print(score)

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

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

count = 0

for i in range(N):
  count += point[T[i]]

for i in range(K):
  S = [T[j] for j in range(i, N, K)]
  
  same = 1
  for j in range(1, len(S)):
    
    if S[j-1] == S[j]:
      same += 1
    else:
      t = point[S[j-1]] * (same // 2)
      
      count -= t
      same = 1
  if same >= 2:
    t = point[S[j-1]] * (same // 2)
    
    count -= t
    same = 1

print(count)

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

ans=0
ans+=order.count("r")*p
ans+=order.count("s")*r
ans+=order.count("p")*s

for i in range(k):
    part=order[i::k]
    check=re.findall(r'r+r', part)
    if check:
        for hand in check:
            ans-=(len(hand)//2)*p
    check=re.findall(r's+s', part)
    if check:
        for hand in check:
            ans-=(len(hand)//2)*r
    check=re.findall(r'p+p', part)
    if check:
        for hand in check:
            ans-=(len(hand)//2)*s

print(ans)