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 A():
    temp = input().split()
    print(''.join(temp[::-1]))

def B():
    temp = [int(n) for n in input().split()]
    A = temp[0]
    B = temp[1]
    K = temp[2]
    to_minus = min(A, K)
    A -= to_minus
    K -= to_minus
    if K > 0:
        B -= min(B, K)
    print (A, B)

import math
def C():
    X = int(input())
    if X <= 2:
        print(2)
        return

    def isPrime(n):
        if n <= 3:
            return True
        if(n % 2 == 0 or n % 3 == 0): 
            return False
      
        for i in range(5,int(math.sqrt(n) + 1), 6):  
            if(n%i == 0 or n%(i + 2) == 0): 
                return False
        
        return True
    
    while True:
        if isPrime(X):
            print(X)
            return
        X += 1

def D():
    temp = [int(n) for n in input().split()]
    N = temp[0]
    K = temp[1]

    temp = [int(n) for n in input().split()]
    point_dict = {'r': temp[0], 's': temp[1], 'p': temp[2]}

    T = input()

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

    ans = 0
    last_choice = ['' for i in range(K)]
    for i, c in enumerate(T):
        if i<K or last_choice[i%K] != winning_rule[c]:
            last_choice[i%K] = winning_rule[c]
            ans += point_dict[winning_rule[c]]
        else:
            last_choice[i%K] = None
    print(ans)

D()

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

dic = {"r":"p" , "s":"r" , "p":"s"}
ANS = []

for i in range(N):
    ANS.append(dic[T[i]])
    
ans = "".join(ANS)

for i in range(K , N):
    if ans[i] == ans[i-K]:
        ans = ans[:i] + "X" + ans[i+1:]
        
point = ans.count("r")*R + ans.count("s")*S + ans.count("p")*P

print(point)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
d = {"r":r,"s":s,"p":p}
d_ = {"r":"p","s":"r","p":"s"}
t = [d_[i] for i in t]
l = [[] for _ in range(k)]
for i,j in enumerate(t):
    l[i%k].append(j)

def solve(l):
    score = 0
    k_ = {}
    for i,j in enumerate(l):
        if i == 0:
            score += d[j]
            k_[j] = 1
        elif l[i-1] != j:
            score += d[j]
            k_ = {}
            k_[j] = 1
        else:
            if k_[j] == -1:
                score += d[j]
            k_[j] *= -1
    return score
score = 0
for i in l:
    score += solve(i)

print(score)

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

h = [''] * n
c = {'r': 'p', 's': 'r', 'p': 's'}
points = 0

for i, ti in enumerate(t):
    ci = c[ti]
    if i < k or h[i - k] != c[ti]:
        h[i] = ci
        points += eval(ci)
    else:
        
        
        
        
        
        
        h[i] = '*'


print(points)

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

h = [''] * n
c = {'r': 'p', 's': 'r', 'p': 's'}
points = 0

for i, ti in enumerate(t):
    ci = c[ti]
    if i < k or h[i - k] != c[ti]:
        h[i] = ci
        points += eval(ci)
    else:
        p1, p2 = eval(ci), eval(h[i - k])
        if p1 > p2:
            h[i] = ci
            points -= p2
            points += p1
        else:
            h[i] = '*'


print(points)

Solution (Python):

n, k = map(int, input().split())
r, s, p = map(int, input().split())  
t = input()
hand = [True for i in range(len(t))]  
result = 0
for i in range(0, k):
    for j in range(i, len(t), k):
        if j >= k and t[j] == t[j - k] and hand[j - k] is True:
            hand[j] = False
            continue
        if t[j] == "r":
            result += p
        elif t[j] == "s":
            result += r
        elif t[j] == "p":
            result += s
print(result)

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

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


for j in range(N):
  if j-K>=0:
    if kachite[j]==kachite[j-K] and kachite[j-K]!='':
      
      kachite[j] = '' 

rpoint = kachite.count('r')*R
spoint = kachite.count('s')*S
ppoint = kachite.count('p')*P
print(rpoint+spoint+ppoint)

Solution (Python):
n,k=map(int, input().split())
r,s,p=map(int,input().split())
T=input()
score=0
A=[]
for i in range(k):
    if T[i]=='r':
        score+=p
        A.append('p')
    if T[i]=='s':
        score+=r
        A.append('r')
    if T[i]=='p':
        score+=s
        A.append('s')
for i in range(k,n):
    if T[i]=='r':
        if A[i-k]!='p':
            score+=p
            A.append('p')
        else:
            A.append('o')
    if T[i]=='s':
        if A[i-k]!='r':
            score+=r
            A.append('r')
        else:
            A.append('o')
    if T[i]=='p':
        if A[i-k]!='s':
            score+=s
            A.append('s')
        else:
            A.append('o')
print(score)

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

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

for i in range(k):
    li = []
    idx = i
    while idx < n:
        li.append(t[idx])
        idx += k

    pre = "x"
    cnt = 1
    for c in li:
        if c == pre:
            cnt += 1
        if c != pre and cnt > 1:
            cnt //= 2
            if pre == "r":
                score -= p * cnt
            elif pre == "s":
                score -= r * cnt
            else:
                score -= s * cnt
            cnt = 1
        pre = c
    else:
        if cnt > 1:
            cnt //= 2
            if pre == "r":
                score -= p * cnt
            elif pre == "s":
                score -= r * cnt
            else:
                score -= s * cnt
print(score)

Solution (Python):
*I,t=open(0).read().split()
n,k,r,s,p=map(int,I)

d={'r':p,'s':r,'p':s}
b=['']*n
a=0

for i,c in enumerate(t[:n]):
    if c!=b[i-k]:
        b[i]=c
        a+=d[c]

print(a)

Solution (Python):
*n,t=open(0).read().split()
n,k,r,s,p=map(int,n)
d={'r':p,'s':r,'p':s}
b=['']*n*2
a=0
for i,c in enumerate(t[:n]):
  if c!=b[i-k]:
    b[i]=c
    a+=d[c]
print(a)

Solution (Python):
*n,t=open(0).read().split()
n,k,r,s,p=map(int,n)
d={'r':p,'s':r,'p':s}
b=['']*n*2
a=0
for i,c in enumerate(t[:n]):
  if c!=b[i-k]:
    b[i]=c
    a+=d[c]
print(a)

Solution (Python):
n, k=list(map(int, input().split()))
R, S, P= list(map(int, input().split()))
A = str(input())
U = {'r':0,'s':0,'p':0}
W = [0]*n
for i in range(k):
    U[A[i]]=U[A[i]]+1
    W[i]=1
for i in range(k, n):
    if A[i] == A[i-k]:
        if W[i-k] == 0:
            U[A[i]]=U[A[i]]+1
            W[i]=1
    else:
        U[A[i]]=U[A[i]]+1
        W[i] = 1
print(U['r']*P + U['s']*R + U['p']*S)

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(len(T)):
    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())
st=list(input())
for i in range(n-k):
    if st[i] == st[i+k]:
        st[i+k] = 'no'
win = r*st.count('s')+s*st.count('p')+p*st.count('r')
print(win)

Solution (Python):
n,k=[int(j) for j in input().split()]
s,p,r=[int(j) for j in input().split()]
t=input()
ans=0
from itertools import*
for i in range(k):
    l=[]
    for j in range(-(-n//k)):
        tmp=i+k*j
        ss=""
        if tmp<n:
            l.append(t[tmp])
    for i,j in [(k,len(list(v))) for k,v in groupby(l)]:
       ans+=eval(i)*-(-j//2)

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
if  n%k!=0:
    leng=n//k+1
else:
    leng=n//k
data=[[0 for i in range(leng)] for j in range(k)]
ans=0
for i in range(0,k):
    for j in range(0,leng):
        if i+(k*j)<n:
            data[i][j]=t[i+(k*j)]
rz=0
sz=1
pz=2
for j in range(0,k):
    dp=[[0 for x in range(3)] for y in range(leng)]
    if data[j][0]=='r':
        dp[0][rz]=0
        dp[0][sz]=0
        dp[0][pz]=p
    elif data[j][0]=='s':
        dp[0][rz]=r
        dp[0][sz]=0
        dp[0][pz]=0
    elif data[j][0]=='p':
        dp[0][rz]=0
        dp[0][sz]=s
        dp[0][pz]=0

    for i in range(1,leng):
        if data[j][i]=='r':
            dp[i][rz]=dp[i][rz]+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+p+max(dp[i-1][sz],dp[i-1][rz])
        elif data[j][i]=='s':
            dp[i][rz]=dp[i][rz]+r+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+max(dp[i-1][sz],dp[i-1][rz])
        elif data[j][i]=='p':
            dp[i][rz]=dp[i][rz]+max(dp[i-1][sz],dp[i-1][pz])
            dp[i][sz]=dp[i][sz]+s+max(dp[i-1][rz],dp[i-1][pz])
            dp[i][pz]=dp[i][pz]+max(dp[i-1][sz],dp[i-1][rz])
        else:
            dp[i][rz]=dp[i-1][rz]
            dp[i][sz]=dp[i-1][sz]
            dp[i][pz]=dp[i-1][pz]
    ans+=max(dp[-1][rz],dp[-1][sz],dp[-1][pz])
print(ans)

Solution (Python):
n_k = list(map(int, input().split()))
R_S_P = list(map(int, input().split()))
"""
グーで勝った場合、 R点
チョキで勝った場合、 S点
パーで勝った場合、P点

r :グー
s :チョキ
p :パー
"""
n = input()
j = 0
a = ""
res = 0
dasita = []


for i in n:
    if j >= n_k[1]:
        a = dasita[j - n_k[1]]

    if i == "r":  
        if a == "p":
            dasita.append("N")
        else:
            res += R_S_P[2]
            dasita.append("p")
    elif i == "s":
        if a == "r":
            dasita.append("N")
        else:
            res += R_S_P[0]
            dasita.append("r")
    elif i == "p":
        if a == "s":
            dasita.append("N")
        else:
            res += R_S_P[1]
            dasita.append("s")
    j += 1
print(res)

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

point = 0
unPointed = []

for i in range(len(T)):
    ifAdd = True
    if i - K >= 0:
        if T[i] == T[i - K]:
            if unPointed[i - K] == 0:
                ifAdd = False
    if ifAdd is True:
        if T[i] == 'r':
            point += P
        elif T[i] == 's':
            point += R
        elif T[i] == 'p':
            point += S
        unPointed.append(0)
    else:
        unPointed.append(1)

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):
  if i>=k and t[i]==t[i-k]:
    t[i]=' '
  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()

def point(k):
    if k=="r": return P
    if k=="s": return R
    if k=="p": return S

p=0

ser=True



for i in range(0,K):
    p+=point(T[i])
    for j in range(i+K,N,K):
        if T[j-K]==T[j]:
            if ser==True:
                ser=False
            else:
                ser=True
                p+=point(T[j])
        else:
            p+=point(T[j])
            ser=True
    ser=True


print(p)

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


def pon(computer, mine):
    if computer == 'r' and mine == 2: return P
    if computer == 's' and mine == 0: return R
    if computer == 'p' and mine == 1: return S
    return 0


dp = [[0 for _ in range(3)] for _ in range(N+10)]

ans = 0
for st in range(K):
    for i in range(st, N, K):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                if i+1-K >= 0:
                    dp[i+1][k] = max(dp[i+1][k], dp[i+1-K][j] + pon(T[i], k))
                else:
                    dp[i+1][k] = max(dp[i+1][k], pon(T[i], k))
    ans += max(dp[i+1])
print(ans)

Solution (Python):
N,K=map(int, input().split())
R,S,P=map(int, input().split())
T=input()
score=0
hand = []
for i in range(len(T)):
  if T[i] == 'r':
    hand.append('p')
    if i >= K:
      if hand[i - K] == 'p':
        hand.pop()
        hand.append("z")
  elif T[i] == 's':
    hand.append('r')
    if i >= K:
      if hand[i - K] == 'r':
        hand.pop()
        hand.append("z")
  else:
    hand.append('s')
    if i >= K:
      if hand[i - K] == 's':
        hand.pop()
        hand.append("z")
for i in hand:
  if i == 'r':
    score+=R
  elif i == 's':
    score+=S
  elif i == 'p':
    score+=P
print(score)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
cnt = 0
score = {'r': p, 's': r, 'p': s}
for i in range(k):
  a, b = 0, score[t[i]]
  for j in range(i+k,n,k):
    a, b = b, b + score[t[j]] if t[j] != t[j-k] else a + score[t[j]]
  cnt += max(a, b)
print(cnt)

Solution (Python):
def f(x):
    if x == "r":
        return "p"
    elif x == "s":
        return "r"
    else:
        return "s"

def g(x):
    if x == "r":
        return r
    elif x == "s":
        return s
    else:
        return p 

n,k = map(int,input().split())

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

t = str(input())

m = n
a = 0
ss = [' '] * m

if m <= k:
    for i in range(m):
        a += g(f(t[i]))
    print(a)
else:
    for i in range(k):
        a += g(f(t[i]))
        ss[i] = f(t[i])
    for i in range(k,m):
        if t[i-k] != t[i]:
            a += g(f(t[i]))
            ss[i] = f(t[i])
        elif (t[i-k] == t[i]) and (ss[i-k] != f(t[i])):
            a += g(f(t[i]))
            ss[i] = f(t[i])
    print(a)

Solution (Python):

N,K = map(int,input().split())
r,s,p = map(int,input().split())
T = list(input())

def battle(i,b):
    st=['r','s','p']
    a = st[i]
    if a=='r':
        if b=='p':
            return p
        else:
            return 0
    elif a=='s':
        if b=='r':
            return r
        else:
            return 0
    elif a=='p':
        if b=='s':
            return s
        else:
            return 0





ans = 0
hand = ['n']*N
for i,t in enumerate(T):
    if i<K:
        if t=='r':
            ans += p
            hand[i] = 'p'
        elif t=='s':
            ans += r
            hand[i] = 'r'
        elif t=='p':
            ans += s
            hand[i] = 's'
    else:
        if t=='r' and hand[i-K]!='p':
                ans += p
                hand[i] = 'p'               
        elif t=='s' and hand[i-K]!='r':
            ans += r
            hand[i] = 'r'
        elif t=='p' and hand[i-K]!='s':
            ans += s
            hand[i] = 's'
print(ans)

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


def pon(computer, mine):
    if computer == 'r' and mine == 2: return P
    if computer == 's' and mine == 0: return R
    if computer == 'p' and mine == 1: return S
    return 0


ans = 0
for st in range(K):
    t = T[st::K]

    l = len(t)
    dp = [[0 for _ in range(3)] for _ in range(l+1)]

    for i in range(l):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                dp[i+1][k] = max(dp[i+1][k], dp[i][j] + pon(t[i], k))
    ans += max(dp[i+1])
print(ans)

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

win = {"r":P,"s":R,"p":S}
tmp = [1]*N
ans = 0

for i in range(N):
    if i<K:
        ans += win[T[i]]
        tmp[i] = 0
    else:
        if T[i]!=T[i-K] or tmp[i-K]:
            ans += win[T[i]]
            tmp[i] = 0

print(ans)

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


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

    Ts = ["" for _ in range(K)]
    for i, c in enumerate(T):
        Ts[i % K] += c

    ans = 0
    p = {"r": P, "s": R, "p": S}
    for t in Ts:
        c0 = ""
        for c in t:
            if c0 == c:
                c0 = ""
            else:
                ans += p[c]
                c0 = c
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
def resolve():
    n, k = map(int, input().split())
    s, p, r = map(int, input().split())
    point_dict = {'s': s, 'p': p, 'r': r}
    t = list(input())

    subs = [[] for _ in range(n)]
    for i in range(n):
        m = i % k
        subs[m].append(t[i])
    point = 0
    for sub in subs:
        i_b = None
        for i in sub:
            if i_b != i:
                point += point_dict[i]
                i_b = i
            else:
                i_b = None
    print(point)
    
resolve()

Solution (Python):
n,k=map(int,input().split())
p=[int(i) for i in input().split()]
d={"r":2,"s":0,"p":1}
T=[d[i] for i in input()]
flag=[True for _ in range(n)]

sum_=0
for i in range(n):
    if i >=k:
        if T[i] == T[i-k] and flag[i-k] :
            flag[i] = False
        else:
            sum_ += p[T[i]]
            
    else:
        sum_ += p[T[i]]
print(sum_)

Solution (Python):
H = list(map(int, input().split()))
A = list(map(int, input().split()))
T = input()
P=A[2]
R=A[0]
S=A[1]
k=H[1]
ans = 0
T = list(T)

for i in range(int(H[0])):
    a=i-k
    b=T[0]
    if(a<0):
        if(T[i]=="r"):
                ans+=P
                
        if(T[i]=="s"):
                ans+=R
                
        if(T[i]=="p"):
                ans+=S
                
    else:
        if(T[i]=="r"):
            if(T[a] !="r"):
                ans+=P
                
            else:
                T[i]="l"
                

        if(T[i]=="s"):
            if(T[a] !="s"):
                ans+=R
                
            else:
                T[i]="l"
                
        if(T[i]=="p"):
            if(T[a] !="p"):
                ans+=S
                
            else:
                T[i]="l"
                
print(ans)

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

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
d = {'r': 'p', 's': 'r', 'p': 's'}
a = {'r' : R, 's' : S, 'p' : P}
l = [d[s] for s in T]
cnt = 0
for i, s in enumerate(T):
    if i < K:
        cnt += a[l[i]]
        continue
    if s == 'r' and l[i-K] != 'p':
        cnt += P
    elif s == 'r' and l[i-K] == 'p':
        
        l[i] = 'a'

    if s == 's' and l[i-K] != 'r':
        cnt += R
    elif s == 's' and l[i-K] == 'r':
        
        l[i] = 'a'

    if s == 'p' and l[i-K] != 's':
        cnt += S
    elif s == 'p' and l[i-K] == 's':
        
        l[i] = 'a'
print(cnt)

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

TE = []
for i in range(K):
    TE.append([])

for i, ti in enumerate(T):
    TE[i%K].append(ti)


def f(te):
    if te == 'r':
        return P
    elif te == 's':
        return R
    elif te == 'p':
        return S
    else:
        return 0


ans = 0
for te in TE:
    for i in range(len(te)):
        if i == 0:
            continue
        elif te[i] == te[i-1]:
            te[i] = '!'



ans = 0
for te in TE:
    for ti in te:
        ans += f(ti)
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
score = 0
win = ["others" for i in range(len(t))]
for i in range(n):
	if t[i] == "r":
		if win[i - k] != "p":
			win[i] = "p"
		else:
			win[i] = "others"
	elif t[i] == "s":
		if win[i - k] != "r":
			win[i] = "r"
		else:
			win[i] = "others"
	elif t[i] == "p":
		if win[i - k] != "s":
			win[i] = "s"
		else:
			win[i] = "others"

for i in range(n):
	if win[i] == "r": score += r
	elif win[i] == "s": score += s
	elif win[i] == "p": score += p 
	else: continue
print(score)

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

a = [0] * N
ans = 0
for i in range(K):
    a[i] = h[T[i]]
    ans += t[a[i]]

for i in range(K, N):
    if h[T[i]] != a[i - K]:
        a[i] = h[T[i]]
        ans += t[a[i]]
    else:
        if i + K < N:
            a[i] = T[i+K]
        else:
            a[i] = T[i]
print(ans)

Solution (Python):

def solve(N,K,R,S,P,T):
    check = [0]*N
    ans = 0
    for i in range(N):
        if i < K or T[i] != T[i-K] or not check[i-K]:
            ans += P if T[i] == 'r' else R if T[i] == 's' else S
            check[i] = 1
    print(ans)

def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    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()
check = [0]*N
ans = 0
for i in range(N):
    if i < K or T[i] != T[i-K] or not check[i-K]:
        ans += P if T[i] == 'r' else R if T[i] == 's' else S
        check[i] = 1
print(ans)

Solution (Python):
N,K=[int(s) for s in input().split()]
point=[int(s) for s in input().split()]
point=point+point
kyotai=input()
kyo=[None]*N
for i in range(N):
  if kyotai[i]=='s':
    kyo[i]=0
  elif kyotai[i]=='p':
    kyo[i]=1
  else:
    kyo[i]=2
hand=[] 
score=0 

for i in range(K):
  hand.append(kyo[i])
  score+=point[kyo[i]]

for i in range(K,N-K):
  if hand[i-K]!=kyo[i]:
    hand.append(kyo[i])
    score+=point[kyo[i]]
  elif hand[i-K]==kyo[i+K]:
    hand.append((kyo[i]+1)%3)
  else:
    hand.append(3-kyo[i+K]-hand[i-K])

for i in range(N-K,N):
  if hand[i-K]!=kyo[i]:
    score+=point[kyo[i]]
print(score)

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

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