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())
dic = {'r': P, 's': R, 'p': S}
ans_dic = {'r': 'p', 's': 'r', 'p': 's'}
T = list(input())

result = 0
ans = []

for t in range(N) :
  if t < K :
    result += dic[T[t]]
    ans.append(ans_dic[T[t]])
  else :
    if ans[t-K] == ans_dic[T[t]] :
      if t+K < N :
        ans.append(T[t+K])
      else :
        continue
    else :
      result += dic[T[t]]
      ans.append(ans_dic[T[t]])

print(result)

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

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

total = 0

my_result = [] 

for i in range(K):
  
  total += win_point[T[i]]
  my_result.append(1)


for i in range(K, N):
  
  win_te = win_result[T[i]]
  
  if T[i-K] != T[i]:
    total += point[win_te]
    my_result.append(1)
  
  elif my_result[i-K] == 0:
    total += point[win_te]
    my_result.append(1)
  else:
    my_result.append(0)

print(total)

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

t = str(input())

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

Solution (Python):
[N, K] = list(map(int,input().split()))
[R, S, P] = list(map(int,input().split()))
T = input()
def calc(s):
    tmp = [0,0,0]
    if s[0] == 'r':
        tmp[2] += 1
    elif s[0] == 's':
        tmp[0] += 1
    else:
        tmp[1] += 1
    old = s[0]
    for i in range(1,len(s)):
        new = s[i]
        if old != new:
            if  new == 'r':
                tmp[2] += 1
            elif new  == 's':
                tmp[0] += 1
            else:
                tmp[1] += 1
        if old == new:
            old = 'a'
        else:
            old = new
    re = R * tmp[0] + S * tmp[1] + P * tmp[2]
    return re
ans = 0
for i in range(K):
    s = ''
    idx = i
    while idx <= N - 1:
        s += T[idx]
        idx += K
    ans += calc(s)
print(ans)

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

ans = 0
for a in range(k):
    mae = ''
    for i in range(a, n, k):
        if (mae != t[i]):
            mae = t[i]
            
            if(t[i] == 'r'):
                ans += p
            elif(t[i] == 's'):
                ans += r
            else:
                ans += s
        else:
            
            mae = ''
print(ans)

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

hands = [None] * N
d1 = {'r': 'p', 's': 'r', 'p': 's'}
d2 = {'r': P, 's': R, 'p': S}
result = 0
for i in range(len(T)):
    if i >= K and d1[T[i]] == hands[i - K]:
        if i + K < N:
            hands[i] = set('rps') - set(T[i + K]) - set(T[i])
        else:
            hands[i] = T[i]
    else:
        hands[i] = d1[T[i]]
        result += d2[T[i]]
print(result)

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

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



Rc = T.count('r')
Sc = T.count('s')
Pc = T.count('p')

count = P * Rc + S * Pc + R * Sc
print(count)

Solution (Python):
N, K = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
W = {'r': 'p', 's': 'r', 'p': 's'}
H = ['r', 's', 'p']
T = [i for i in list(input())]
dp = [[0, 0, 0] for _ in range(N+10)]
hl = []

for i in range(N):
    flag = True
    f2 = False
    for j in range(3):
        if f2:
            break
        for k in range(3):
            if W[T[i]] == H[k]:
                if i >= K:
                    if hl[i-K] == k:
                        hl.append(4)
                        dp[i+1] = [max(dp[i])]*3
                        f2 = True
                        break
                
                if flag:
                    hl.append(k)
                    flag = False
                dp[i+1][k] = max(dp[i+1][k], dp[i][j] + P[k])

print(max(dp[N]))

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

A = N*[-1]

for i in range(N):
    if T[i] == 'r':
        if i >= K:
            if A[i-K] == 5:
                pass
            else:
                score += P
                A[i] = 5
        else:
            score += P
            A[i] = 5

    elif T[i] == "s":
        if i >= K:
            if A[i-K] == 0:
                pass
            else:
                score += R
                A[i] = 0
        else:
            score += R
            A[i] = 0
    else:
        if i >= K:
            if A[i-K] == 2:
                pass
            else:
                score += S
                A[i] = 2
        else:
            score += S
            A[i] = 2

print(score)

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

T = [c for c in input()]
score = 0

for i in range(N):
    if i >= K and T[i] == T[i-K]:
        T[i] = "a"
        continue
    if T[i] == "r":
        score += P
    elif T[i] == "s":
        score += R
    else:
        score += S

print(score)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    pre = []

    def win_point(a):
        if a == 'r':
            pre.append('r')
            return p
        elif a == 's':
            pre.append('s')
            return r
        else:
            pre.append('p')
            return s

    ans = 0
    for i, j in enumerate(t):
        if i < k:
            ans += win_point(j)
        elif j != pre[i-k]:
            ans += win_point(j)
        else:
            pre.append('xx')
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
l = {'r':'p','s':'r','p':'s'}
L = ['' for i in range(n)]
tmp = ['r','s','p']
for i in range(n):
    x = t[i]
    if (i - k) >= 0 and L[i-k] == l[x]:
        tmp.remove(L[i-k])
        if (i + k) <= (n-1) and l[t[i+k]] != l[x]:
            tmp.remove(l[t[i+k]])
        L[i] = tmp[0]
        tmp = ['r','s','p']
    else:
        L[i] = l[x]

score = 0
sc = {'r':r,'s':s,'p':p}
for i in range(n):
    if l[t[i]] == L[i]:
        score += sc[L[i]]

print(score)

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

hand_table = [[0, 0, P],[R, 0, 0],[0, S, 0]]

T_int = [0 if c == 'r' else 1 if c == 's' else 2 for c in T]

DP = []
for i in range(K):
    innerDP = [[hand_table[T_int[i]][0],hand_table[T_int[i]][1],hand_table[T_int[i]][2]]]
    DP.append(innerDP)

for i in range(K, N):
    offset = i % K
    loopnum = i // K
    DP[offset].append([max(DP[offset][loopnum - 1][1] + hand_table[T_int[i]][0], \
                           DP[offset][loopnum - 1][2] + hand_table[T_int[i]][0]), \
                       max(DP[offset][loopnum - 1][0] + hand_table[T_int[i]][1],DP[offset][loopnum - 1][2] + hand_table[T_int[i]][1]), \
                       max(DP[offset][loopnum - 1][0] + hand_table[T_int[i]][2],DP[offset][loopnum - 1][1] + hand_table[T_int[i]][2])])

ans = 0

for i in range(N - K, N):
    offset = i % K
    loopnum = i // K
    ans += max(DP[offset][loopnum])

print(ans)

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

score = 0
for k in range(K):
    if T[k]=='r':
        score += P
    elif T[k]=='s':
        score += R
    else:
        score += S
    odd = False
    for n in range(K+k, N, K):
        if T[n-K] == T[n] and not odd:
            odd = True
        else:
            if T[n]=='r':
                score += P
            elif T[n]=='s':
                score += R
            else:
                score += S
            odd = False

print(score)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
sp_T = []
for i in range(K):
    sp_T.append(T[i::K])
ans = 0
win_d = {"s": "r", "r": "p", "p": "s"}
point = {"r": R, "s": S, "p": P}
for t in sp_T:
    sakki = ""
    for i in range(len(t)):
        if win_d[t[i]] != sakki:
            my_hand = win_d[t[i]]
            ans += point[my_hand]
            sakki = my_hand
        else:
            if i == len(t) - 1:
                pass
            else:
                next_want_hand = win_d[t[i + 1]]
                if sakki == next_want_hand:
                    sakki = "X"
                else:
                    sakki = "X"
print(ans)

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

dp = [0]
kl = [-1 for i in range(N)]
for i in range(N):
    tmp = dp[i]
    k = -1
    if kl[N-K+i] != 2 and T[i] == "r":
        tmp += P
        k = 2
    elif kl[N-K+i] != 0 and T[i] == "s":
        tmp += R
        k = 0
    elif kl[N-K+i] != 1 and T[i] == "p":
        tmp += S
        k = 1
    dp.append(tmp)
    kl.append(k)

print(dp[N])

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = list(str(input()))
Te=[]
for t in T:
    if t=='r':
        Te.append(P)
    elif t=='s':
        Te.append(R)
    else:
        Te.append(S)
te=['r','s','p']
ans=0
for i in range(N):
    if K>i:
        ans+=Te[i]
    else:
        if T[i-K]==T[i]:
            
            if i+K>=N:
                T[i]='null'
            else:
            
                te.remove(T[i])
                try:
                    te.remove(T[i+K])
                except:
                    pass
                T[i]=te[0]
                te=['r','s','p']
        else:
            ans+=Te[i]
print(ans)

Solution (Python):
import math


def main():
    n, k = [int(x) for x in input().split()]
    r, s, p = [int(x) for x in input().split()]
    t = input()
    win = {"r": "p", "s": "r", "p": "s"}
    lose = {"r": "s", "s": "p", "p": "r"}
    score = {"r": r, "s": s, "p": p}
    machine = list(t)
    man = [win[x] for x in machine]

    changed = set()
    for i in range(n - k):
        ki = i + k
        if man[i] == man[ki]:
            if i in changed:
                man[i] = lose[machine[i]]
            elif score[man[i]] < score[man[ki]]:
                man[i] = machine[i]
            else:
                man[ki] = machine[ki]
                changed.add(ki)

    total = 0
    for i in range(n):
        if man[i] == win[machine[i]]:
            total += score[man[i]]

    print(total)


if __name__ == "__main__":
    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 = input()

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

TT = ""
for c in T:
    if c == "r":
        TT+="p"
    elif c == "s":
        TT+="r"
    else:
        TT+="s"

T = T.replace('r', 's')
flag = [False] * N
ans = 0
for idx, c in enumerate(TT):
    if idx - K < 0:
        ans += score[c]
        flag[idx] = True
    else:
        if c == TT[idx-K]:
            if flag[idx-K] == False:
                ans += score[c]
                flag[idx] = True
            else:
                pass
        else:
            ans += score[c]
            flag[idx] = True
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
from itertools import groupby
from collections import Counter
import math
ans = 0
c={'r':0,'s':0,'p':0}
for i in range(K):
  x = [[a,len(list(g))] for a,g in groupby(T[i::K])]
  for a in x:
    c[a[0]]+=math.ceil(a[1]/2)
r = c['r']*P + c['s']*R + c['p']*S
print(r)

Solution (Python):
(N, K), *D, = [s.split() for s in open(0)]
points = 0
dd = {r: p for p, r in zip(list(map(int, D[0])), ["s", "p", "r"])}
records = []
for i in range(len(D[1][0])):
    if i < int(K):
        points += dd[D[1][0][i]]
        records.append(D[1][0][i])
    else:
        if D[1][0][i] == D[1][0][i-int(K)] and records[i-int(K)] == "pass":
            points += dd[D[1][0][i]]
            records.append(D[1][0][i])
        elif D[1][0][i] == D[1][0][i-int(K)] and records[i-int(K)] != "pass":
             records.append("pass")
        else:
            points += dd[D[1][0][i]]
            records.append(D[1][0][i])
            
print(points)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
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] = "d"

t = "".join(t)
t = t.replace("r", "{}+".format(p)).replace("p", "{}+".format(s)).replace("s", "{}+".format(r)).replace("d", "0+") + "0"
print(eval(t))

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()[:-1]
    flag = [False]*n
    
    

    def tokuten(aite):
        if aite == 'r':
            return p
        if aite == 's':
            return r
        if aite == 'p':
            return s

    ans = t.count('r')*tokuten('r')+t.count('s') * \
        tokuten('s')+t.count('p')*tokuten('p')
    for i in range(n):
        if i - k >= 0 and t[i] == t[i - k] and not flag[i-k]:
            ans -= tokuten(t[i])
            flag[i] = True
    print(ans)


if __name__ == '__main__':
    main()

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=input()
dic={'r':p,'s':r,'p':s}
dic2={'r':'p','s':'r','p':'s'}
dic3={'r':'s','s':'p','p':'r'}
h=''

c=0
for i in range(k):
  c+=dic[t[i]]
  h+=dic2[t[i]]
for i in range(k,n):
  if h[i-k]!=dic2[t[i]]:
      c+=dic[t[i]]
      h+=dic2[t[i]]
  else:
    if i+k<n:
      if dic2[t[i+k]]!=t[i]:
        h+=t[i]
      else:
        h+=dic3[t[i]]
    else:
      h+=t[i]
print(c)

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


v = [0]*n
for idt in range(n):
    if idt >= k:
        if t[idt] == t[idt-k] and v[idt-k] != 0:
            v[idt] = 0
            continue
    if t[idt] == 'r':
        v[idt] = p
    elif t[idt] == 's':
        v[idt] = r
    else:
        v[idt] = s
        

ans = sum(v)
print(ans)

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

counter = 0
flag = -1

for i in range(k):
    flag = -1
    for j in range(i,n,k):
        if flag != 0 and t[j] == "r":
            flag = 0
            counter += p
            

        elif flag != 1 and t[j] == "s":
            flag = 1
            counter += r
            
            

        elif flag != 2 and t[j] == "p":
            flag = 2
            counter += s
            

        else:
            flag = -1

print(counter)

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

ans=0
for o in range(K):
    pre = 'X'
    for i in range(o,N,K):
        h=T[i]
        if h!=pre:
            ans+=score[h]
            pre=h
        else:
            pre='X'

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
dp = [[0]*3 for _ in range(n)]
for i, t in enumerate(T):
    if t == "r":
        if i < k:
            dp[i][0] += 0
            dp[i][1] += 0
            dp[i][2] += p
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2])
            dp[i][1] = max(dp[i-k][0], dp[i-k][2])
            dp[i][2] = max(dp[i-k][0], dp[i-k][1]) + p
    elif t == "s":
        if i < k:
            dp[i][0] += r
            dp[i][1] += 0
            dp[i][2] += 0
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2]) + r
            dp[i][1] = max(dp[i-k][0], dp[i-k][2])
            dp[i][2] = max(dp[i-k][0], dp[i-k][1])
    else:
        if i < k:
            dp[i][0] += 0
            dp[i][1] += s
            dp[i][2] += 0
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2])
            dp[i][1] = max(dp[i-k][0], dp[i-k][2]) + s
            dp[i][2] = max(dp[i-k][0], dp[i-k][1])
ans = 0
for i in range(k):
    ans += max(dp[n-i-1])
print(ans)

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

Solution (Python):
n, k = map(int, input().split(" "))
r, s, p = map(int, input().split(" "))
t = input()
t = list(t)
ans = 0
key = []

for i in range(k):
    key.append(t[i::k])
m = len(key)

for i in range(m):
    flag = 0
    for j,tem in enumerate(key[i]):
        if j == 0:
            if tem == 'r':
                ans += p
            elif tem == 's':
                ans += r
            else:
                ans += s
        elif j < len(key[i])-1:
            if (flag == 0)and(tem == key[i][j-1]):
                ans = ans
                flag = 1
            elif (flag == 1) and (tem == key[i][j-1]):
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s 
                flag = 0
            else:    
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s
                if tem == key[i][j+1]:
                    flag = 0
        else:
            if (flag == 0)and(tem == key[i][j-1]):
                ans = ans
                flag = 1
            elif (flag == 1) and (tem == key[i][j-1]):
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s 
                flag = 0
            else:    
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    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 = input()

outputs = ""
score = 0

for i in range(N):
    if (i >= K) and (T[i - K] == T[i]) and (outputs[i - K] == "w"):
        score += 0
        outputs += "l"
        continue

    if T[i] == "r":
        score += P
        outputs += "w"
    elif T[i] == "s":
        score += R
        outputs += "w"
    elif T[i] == "p":
        score += S
        outputs += "w"
    

print(score)

Solution (Python):
def points(me, you, R, S, P):
    hand_map = {
        'r':'p',
        's':'r',
        'p':'s'
    }
    if hand_map[you] == me:
        if me == 'r':
            return R
        elif me =='p':
            return P
        elif me =='s':
            return S
    else:
        return 0
    
def main():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = input()
    Ts = list(T)

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

    memo = []
    cum = 0
    for i, t in enumerate(Ts):
        hand = hand_map[t]
        
        if i >= K:
            if hand == memo[i - K]:
                hand = 'na'
            

        memo.append(hand)
        cum += points(hand, t, R, S, P)
        


    print(cum)

if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
r_num = t.count("r")
s_num = t.count("s")
p_num = t.count("p")
for i in range(n-k):
    if t[i] == t[i+k]:
        if t[i] == "r":
            r_num -= 1
        elif t[i] == "s":
            s_num -= 1
        else:
            p_num -= 1
        t[i+k] = "a"
print(p*r_num + r*s_num + s*p_num)

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

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


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

l = [-1] * n

for i in range(n):
    if l[i] >= 0:
        continue

    if i > n - k - 1:
        l[i] = get_score(t[i])
        continue

    if t[i] == t[i + k]:
        l[i] = get_score(t[i])
        l[i + k] = 0
    else:
        l[i] = get_score(t[i])


print(sum(l))

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

def get_score(te):
    if te == "r":
        return p
    elif te == "s":
        return r
    elif te == "p":
        return s
    else:
        return 0

ans = 0
for i in range(k):
    tmp = list(T[i::k])
    
    for j in range(len(tmp) -1):
        if tmp[j] == tmp[j+1]:
            tmp[j+1] = "x"
    
    for j in range(len(tmp)):
        ans += get_score(tmp[j])
    

print(ans)

Solution (Python):
import numpy as np
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
act = np.array(list(t.replace('r','P').replace('s','R').replace('p','S')))
for a in range(k, len(act)):
    if act[a] == act[a-k]:
        act[a] = 'Z'
score_dict = {'R':r, 'S':s, 'P':p, 'Z':0}
score = np.array([score_dict[i] for i in act])
print(score.sum())

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

def get_winpos(dealer):
  if dealer == 'r':
    return 'p'
  elif dealer == 's':
    return 'r'
  else:
    return 's'
def calc_point(rsp):
  if rsp == 'r':
    return R
  elif rsp == 's':
    return S
  else:
    return P
  
winpos = [get_winpos(s) for s in T]
point = 0
check = 'any'
for i, pos in enumerate(winpos):
  if i-K >= 0:
    check = winpos[i-K]
  if winpos[i] == check:
    winpos[i] = 'any'
  else:
    point += calc_point(winpos[i])
print(point)

Solution (Python):
n,k=map(int,input().split())
R,S,P=map(int,input().split())
t=input()

ans=0
win=[]
for tt in t:
 if tt=='r':
  win+='p'
  ans+=P
 elif tt=='s':
  win+='r'
  ans+=R
 else:
  win+='s'
  ans+=S

for i in range(k):
 l=win[i::k]

 for j in range(1,len(l)):
  if l[j]==l[j-1]:
   if l[j]=='p':
    ans-=P
   elif l[j]=='r':
    ans-=R
   else:
    ans-=S
   l[j]='z'

print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
x=[]
count=0
for i in range(k):
  x.append(t[i::k])

for j in range(k):
  for i in range(len(x[j])):
    if i==0:
      if x[j][i]=='r':
        count+=p
      elif x[j][i]=='s':
        count+=r
      else:
        count+=s
    elif x[j][i]==x[j][i-1]:
      x[j][i]='z'
    else:
      if x[j][i]=='r':
        count+=p
      elif x[j][i]=='s':
        count+=r
      else:
        count+=s
print(count)

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

for num,i in enumerate(t):
    if num<k:
        continue  
    if i==t[num-k]:
        t[num]=0

rl=[]
sl=[]
pl=[]
count=0
for num,i in enumerate(t):
    if i=="r":
        count+=p
        
    elif i=="s":
        count+=r
        
    elif i=="p":
        count+=s
        

print(count)

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

for i in range(N):
  if i<=K-1:
    if T[i]=="r":
      score+=P
      hand.append("p")
    elif T[i]=="s":
      score+=R
      hand.append("r")
    else:
      score+=S
      hand.append("s")
  elif i<=N-K-1:
    if T[i]=="r":
      if hand[i-K]=="p":
        if T[i+K]=="r":
          hand.append("r")
        elif T[i+K]=="s":
          hand.append("s")
        elif T[i+K]=="p":
          hand.append("r")
      else:
        score+=P
        hand.append("p")
    if T[i]=="s":
      if hand[i-K]=="r":
        if T[i+K]=="r":
          hand.append("s")
        elif T[i+K]=="s":
          hand.append("s")
        elif T[i+K]=="p":
          hand.append("p")
      else:
        score+=R
        hand.append("r")
    if T[i]=="p":
      if hand[i-K]=="s":
        if T[i+K]=="r":
          hand.append("r")
        elif T[i+K]=="s":
          hand.append("p")
        elif T[i+K]=="p":
          hand.append("p")
      else:
        score+=S
        hand.append("s")
  else:
    if T[i]=="r" and hand[i-K]!="p":
      score+=P
    if T[i]=="s" and hand[i-K]!="r":
      score+=R
    if T[i]=="p" and hand[i-K]!="s":
      score+=S

print(score)

Solution (Python):

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    s = ['b']*(N)
    
    l = [
        ['s', S],
        ['r', R],
        ['p', P]
    ]
    l = sorted(l, key=lambda x:-x[1])

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

    for i in range(N):
        s[i] = d[T[i]]


    for a, b in l:
        for i in range(N):
            if s[i] == a and i >= K and s[i-K] == a:
                s[i] = 'b'

    ans = 0
    for i in range(N):
        if s[i] == 'r' and T[i] == 's':
            ans += R
        elif s[i] == 's' and T[i] == 'p':
            ans += S
        elif s[i] == 'p' and T[i] == 'r':
            ans += P
    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()


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

flag = [False for i in range(N)]
ans = 0
for i in range(N):
    if (flag[i]):
        pass
    else:
        ans += score[T[i]]
        if (N-i-1 >= K):
            if (T[i] == T[i+K]):
                flag[i+K] = True

print(ans)

Solution (Python):
N,K = map(lambda x: int(x),input().split())
r,s,p = map(lambda x: int(x),input().split())
T = input()
dic={}
for i in range(K):
  txt=""
  idx=i
  while idx<N:
    txt = txt+T[idx]
    idx += K
  dic[i] = txt
toWin = {"r":"p","s":"r","p":"s"}
scoretable = {"r":{"r":0,"s":0,"p":p},
              "s":{"r":r,"s":0,"p":0},
              "p":{"r":0,"s":s,"p":0},
             }
def katite(txt):
  best = txt.translate(str.maketrans({"r":toWin["r"],"s":toWin["s"],"p":toWin["p"]}))
  return best

def Score(txt):
  first = txt[0]
  prev_score = scoretable[first]
  nexthand = {"r":["s","p"],"s":["r","p"],"p":["s","r"]}
  
  for c in txt[1:]:
    next_score = {k:v for k,v in prev_score.items()}
    score = scoretable[c]
    for prevhand in prev_score.keys():
      for nh in nexthand[prevhand]:
        s = score[nh]
        next_score[nh] = max(prev_score[prevhand]+s,next_score[nh])
    prev_score = next_score
  return prev_score
    
    
    
ans=0    

for i in range(K):
  
  ans += max(Score(dic[i]).values())

print(ans)