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 = list(input())
r =[0]*100001
s=[0]*100001
p=[0]*100001
for i in range(N):
  if T[i]=='r':
    r[i]=1
    if i>=K:
      if r[i-K]==1:
        r[i]=0
  elif T[i] =='s':
    s[i]=1
    if i>=K:
      if s[i-K]==1:
        s[i]=0
  elif T[i]=='p':
    p[i]=1
    if i>=K:
      if p[i-K]==1:
        p[i]=0
count=0
for i in range(N):
  if r[i]==1:
    count+=P
  elif s[i]==1:
    count+=R
  elif p[i]==1:
    count+=S
print(count)

Solution (Python):
def solve():
  N, K = map(int, input().split())
  R, S, P = map(int, input().split())
  dic = {'r':P,'s':R,'p':S}
  T = input()
  point = [0]*N
  for i in range(N):
    if i<K:
      point[i] = dic[T[i]]
    else:
      if T[i]!=T[i-K] or point[i-K]==0:
        point[i]=dic[T[i]]
  ans = sum(point)
  return ans
print(solve())

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
li = [[""]*((n+k)//k) for i in range(k)]
for i in range(n):
    li[i%k][i//k] = t[i]
ans = 0
for i in li:
    P,R,S = 0,0,0
    hand_o = ""
    for j in i:
        if j == "r" and hand_o != "r":
            P += p
        elif j == "s" and hand_o != "s":
            R += r
        elif j == "p" and hand_o != "p":
            S += s
        else:
            hand_o = ""
            continue
        hand_o = j
    ans += (P+R+S)
print(ans)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
points = {'r': P, 's': R, 'p': S}
is_changed = [False] * K + [False] * (N - K)
score = 0
for i in range(K):
    score += points[T[i]]

for i in range(K, N):
    if T[i] == T[i - K] and not is_changed[i - K]:
        is_changed[i] = True
    else:
        score += points[T[i]]

print(score)

Solution (Python):
n,k = map(int,input().split())
li = list(map(int,input().split()))
t = list(input())
ans = []
for i in range(len(t)):
    if t[i] == 'r':
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[2])
    elif t[i] == 's':
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[0])
    else:
        if k <= i and t[i] == t[i-k]:
            t[i] = ''
            ans.append(0)
        else:
            ans.append(li[1])
print(sum(ans))

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

dict = {'s': r, 'p': s, 'r': p}
cnt, ans = set(), 0
for i, ti in enumerate(t):
    if (i - k in cnt) and ti == t[i - k]:
        continue
    ans += dict[ti]
    cnt.add(i)
print(ans)

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

cnt, ans = set(), 0
for i, ti in enumerate(t):
    if (i - k in cnt) and ti == t[i - k]:
        continue
    ans += r * (ti == 's')
    ans += s * (ti == 'p')
    ans += p * (ti == 'r')
    cnt.add(i)
print(ans)

Solution (Python):




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

ans = 0
memo = []
for i in range(n):
    if i < k or t[i] != memo[i-k]:
        memo.append(t[i])
        if t[i] == 'r': ans += p
        elif t[i] == 's': ans += r
        else: ans += s

    else:
        memo.append('-1')

print(ans)

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

ans = 0
memo = []
for i in range(n):
    if i < k or t[i] != memo[i-k]:
        memo.append(t[i])
        if t[i] == 'r': ans += p
        elif t[i] == 's': ans += r
        else: ans += s

    else:
        memo.append('-1')

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
R, S, P = mapint()
T = list(input())

ans = 0
for i in range(N):
    t = T[i]
    if i<K:
        if t=='r':
            ans += P
        elif t=='s':
            ans += R
        else:
            ans += S
    else:
        if T[i-K]==T[i]:
            T[i] = '0'
            continue
        else:
            if t=='r':
                ans += P
            elif t=='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()
l=["" for i in range(n)]

ans=0
for i in range(k):
  if t[i]=="r":
    l[i]="p"
    ans+=p
  elif t[i]=="p":
    l[i]="s"
    ans+=s
  else:
    l[i]="r"
    ans+=r

for i in range(k,n):
  if t[i]=="r":
    if l[i-k]!="p":
      l[i]="p"
      ans+=p

  elif t[i]=="p":
    if l[i-k]!="s":
      ans+=s
      l[i]="s"

  else:
    if t[i]=="s":
      if l[i-k]!="r":
        ans+=r
        l[i]="r"

print(ans)

Solution (Python):

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

ctr = {"r": P, "s": R, "p": S}
dp = [""] * N
ans = 0
for i in range(N):
    if i >= K and dp[i - K] == T[i]:
        continue

    ans += ctr[T[i]]
    dp[i] = T[i]

print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
V = {"s":R, "p":S, "r":P}
ans = 0
for i in range(K):
    check = T[i]
    ans += V[T[i]]
    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):

import collections as cl
import sys


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 battle(enemy, r, s, p):
    if enemy == "r":
        return p
    if enemy == "s":
        return r
    if enemy == "p":
        return s


def main():
    N, K = MI()
    r, s, p = MI()
    t = input()
    splited_t = [[] for i in range(K)]

    for i in range(N):
        amari = i % K
        splited_t[amari].append(t[i])

    ans = 0
    for i in range(K):
        targets = splited_t[i]
        score = battle(targets[0], r, s, p)
        ans += score
        won = True
        for j in range(1, len(targets)):
            if targets[j] == targets[j - 1] and won:
                won = False
                continue
            score = battle(targets[j], r, s, p)
            won = True
            ans += score
    print(ans)


main()

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, k = MAP()
R, S, P = MAP()
t = input()
s = ''

ans = 0
for i in range(len(t)):
    if t[i] == 'r':
        if i >= k and s[i-k] == 'p':
            s += 'x'
        else:
            ans += P
            s += 'p'
    elif t[i] == 's':
        if i >= k and s[i-k] == 'r':
            s += 'x'
        else:
            ans += R
            s += 'r'
    else:
        if i >= k and s[i-k] == 's':
            s += 'x'
        else:
            ans += S
            s += 's'
print(ans)

Solution (Python):
from collections import Counter


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

T = list(input())
T_ =T.copy()




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


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

T_point = [0]*N
for n in range(N):
    T_point[n]=point[T[n]]
    
for k in range(K,N):
    check_out = set()
    if T[k] == T[k-K]: 
        check_out.add(T[k])
        if k+K < N:
            check_out.add(T[k+K])
            
        check_out=check-check_out
        
        T[k]=list(check_out)[0]
         
ans=0

for t in range(len(T)):
    if T[t] == T_[t]:
        ans += point[T[t]]
print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
dp = [0]*n
fl = [True]*k
for i in range(n):
    if t[i] == "r":
        h = p
    elif  t[i] == "s":
        h = r
    else:
        h = s
    if i < k:
        if i == 0:
            dp[i] = h
            fl[i%k-1] = False
        else:
            dp[i] = h + dp[i-1]
            fl[i%k-1] = False
    else:
        if t[i] == t[i-k]:
            if fl[i%k-1] == True:
                dp[i] = dp[i-1]+h
                fl[i%k-1] = False
            else:
                dp[i] = dp[i-1]
                fl[i%k-1] = True
        else:
            dp[i] = dp[i-1] + h
            fl[i%k-1] = False    
print(dp[-1])

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

q = deque([])
points = 0
for i in range(n):
    if i <= k-1:
        if t[i] == 'r':
            points += p
            q.append('p')
        elif t[i] == 's':
            points += r
            q.append('r')
        elif t[i] == 'p':
            points += s
            q.append('s')
    else:
        ban = q.popleft()
        if t[i] == 'r':
            if ban != 'p':
              points += p
              q.append('p')
            else:
                q.append('x')
        elif t[i] == 's':
            if ban != 'r':
              points += r
              q.append('r')
            else:
                q.append('x')
        elif t[i] == 'p':
            if ban != 's':
              points += s
              q.append('s')
            else:
                q.append('x')
                
print(points)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.append(0)
s=input()
ans=0
for i in range(k,n):
  if s[i]==s[i-k]:
    s=s[:i]+'z'+s[i+1:]
for i in range(n):
  ans+=a['sprz'.index(s[i])]
print(ans)

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n,k = readInts()
r,s,p = readInts()
t = list(input())

his = ["-1"]
ans = 0
for i in range(len(t)):
  if t[i]=="r":
    me = "p"
    point = p
  elif t[i]=="s":
    me = "r"
    point = r
  else:
    me = "s"
    point = s
  if his[max(0,i-k+1)]==me:
    his.append(i)
  else:
    ans+=point
    his.append(me)

print(ans)

Solution (Python):
MOD = 10**9+7
def resolve():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = list(input())
    
    score = 0
    hands = {
        "r": ("p", P),
        "s": ("r", R),
        "p": ("s", S)
    }
    hist = []
    for i, t in enumerate(T):
        hand, point = hands[t]
        if i >= K and hand == hist[-K]:
            hist.append("*")
            continue
        score += point
        hist.append(hand)
    
    print(score)
    

if '__main__' == __name__:
    resolve()

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

POINT = tuple(map(int,input().split()))

T = input()
m = {'r':0,'s':1,'p':2}
T = [m[h] for h in T]

def helper(T):
    dp = [0]*3
    for t in T:
        wh = (t-1)%3
        op = max(dp)
        dp[wh] = max(v for i,v in enumerate(dp) if i != wh)+POINT[wh]
        for h in range(3):
            if h != wh:
                dp[h] = op
    return max(dp)


res = sum(helper(T[i::K]) for i in range(min(K,N)))

print(res)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = {"r":P, "s":R, "p":S} 
m = {"r":"p", "s":"r", "p":"s"}
command= []
mm = []
ans = 0


for i, t in enumerate(T):
    if t == "r":
      c = "p"
      point = P
    elif t == "s":
      c ="r" 
      point = R
    elif t == "p":
      c = "s"
      point = S
    
    if (i - K >= 0) and (command[i - K] == c):
        c = ""
        point = 0
    
    ans += point
    command.append(c)
print(ans)

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):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
L = [0]*N
dic = dict()
dic["r"] = P
dic["s"] = R
dic["p"] = S
dic["0"] = 0
res = 0
for i in range(N):
    if i < N-K and T[i] == T[i+K] and L[i] == 0:
        L[i+K] = 1
    if L[i] == 0:
        res += dic[T[i]]
print(res)

Solution (Python):

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = [i for i in input()]
b = [0] * n
for i in range(n - k):
    if t[i] == t[i + k] and b[i] == 0:
        if t[i] == 's':
            b[i + k] -= r
        elif t[i] == 'p':
            b[i + k] -= s
        else:
            b[i + k] -= p
print(t.count('s') * r + t.count('p') * s + t.count('r') * p + sum(b))

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

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

com = [0]*N

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

print(sum(com))

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
d={'r':p,'s':r,'p':s}
t=input()
ans=0
for i in range(k):
  a=t[i::k]
  flag=a[0]
  cnt=2
  for j in a[1:]:
    if j!=flag:
      ans+=d[flag]*(cnt//2)
      flag=j
      cnt=2
    else:cnt+=1
  ans+=d[flag]*(cnt//2)
print(ans)

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

kaburi = list(t)
for i in range(n-k):
    if kaburi[i] == kaburi[i+k]:
        kaburi[i+k] = 'x'
result = 0
for i in range(n):
    if kaburi[i] == 'r':
        result += p
    elif kaburi[i] == 's':
        result += r
    elif kaburi[i] == 'p':
        result += s
    else:
        pass


print(result)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
history = []
device2point = {'r' : p, 's': r, 'p': s}
device2hand = {'r': 'p', 's': 'r', 'p': 's'}
point = 0
for i in range(n):
    d = t[i]
    if i - k >= 0 and device2hand[d] == history[i-k]:
        history.append(None)
    else:
        point += device2point[d]
        history.append(device2hand[d])
print (point)

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

t_list = []

for i in range(k):
  t_list.append(t[i])

for i in range(k,n):
  if t[i] != t_list[i-k] or t_list[i-k] == 'all':
    t_list.append(t[i])
  else:
    t_list.append('all')

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    hand = ['r', 's', 'p']
    score = [p, r, s]
    dic_score = dict(zip(hand, score))
    win = [0] * n
    ans = 0
    for i in range(k):
        ans += dic_score[t[i]]
        win[i] = 1
    for i in range(k, n):
        if t[i] == t[i - k] and win[i - k]:
            continue
        ans += dic_score[t[i]]
        win[i] = 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
r,s,p=I()
t = X()
for i in range(n-k):
    if t[i+k]==t[i]:
        t[i+k]=0

print(t.count("r")*p+t.count("s")*r+t.count("p")*s)

Solution (Python):






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





points = {}
points["r"] = P
points["s"] = R
points["p"] = S

ans = 0
mylist = []
for i in range(N):
    mylist.append(T[i])
    if i >= K:
        if T[i] == mylist[-K-1]:
            mylist[-1] = "x"
        else:
            ans += points[T[i]]
    else:
        ans += points[T[i]]
print(ans)

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

hand=["" for _ in range(k)]
point=[0 for _ in range(k)]

for i in range(n):
    index=i%k
    
    if index==i:
        if t[i]=="r":
            hand[index]="p"
            point[index]+=p
        elif t[i]=="s":
            hand[index]="r"
            point[index]+=r
        else:
            hand[index]="s"
            point[index]+=s
    
    else:
        if t[i]=="r":
            if hand[index]=="p":
                hand[index]="x"
            else:
                hand[index]="p"
                point[index]+=p
        elif t[i]=="s":
            if hand[index]=="r":
                hand[index]="x"
            else:
                hand[index]="r"
                point[index]+=r
        else:
            if hand[index]=="s":
                hand[index]="x"
            else:
                hand[index]="s"
                point[index]+=s

print(sum(point))

Solution (Python):


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

X = [[] for _ in range(K)]

for i in range(N):
    X[i % K].append(T[i])

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

score = 0
for k in range(K):
    last = None
    for st in X[k]:
        if st != last:
            score += pt[st]
            last = st
        else:
            last = None

print(score)

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

for i in range(N - K):
  if T[K + i] == T[i]:
    T[K + i] = 0
    
print(T.count('r') * P + T.count('s') * R + T.count('p') * S)

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

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

ans = 0
for i in range(k): ans += rsp[t[i]]
for i in range(k,n):
  if t[i] != t[i-k]: ans += rsp[t[i]]
  else: t[i] = "a"
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = list(input())
res = 0
for i in range(N):
    if T[i] == 'r':
        res += P
        if i + K < N and T[i + K] == 'r':
            T[i + K] = 'x'
    if T[i] == 's':
        res += R
        if i + K < N and T[i + K] == 's':
            T[i + K] = 'x'
    if T[i] == 'p':
        res += S
        if i + K < N and T[i + K] == 'p':
            T[i + K] = 'x'
print(res)

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

ans = 0

def sc(st):
    if st == "p":  return s
    elif st == "r":  return p
    return r

for i in range(K):
    pre = T[i]
    cnt = i
    ans += sc(T[i])
    while(1):
        cnt += K
        if cnt >= N: break 
        if T[cnt] == pre:  pre = "
        else:  
            ans += sc(T[cnt])
            pre = T[cnt]
print(ans)

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

    res=0
    T_=[]
    for i in range(N):
        if T[i]=="r":
            res+=P
            T_.append("p")
        elif T[i]=="s":
            res+=R
            T_.append("r")
        else:
            res+=S
            T_.append("s")
    for i in range(N):
        if i>=K:
            if T_[i-K]==T_[i]:
                if T_[i]=="r":
                    res-=R
                elif T_[i]=="s":
                    res-=S
                else:
                    res-=P
                T_[i]="

    print(res)

if __name__=="__main__":
    main()

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

graph = {"r":P,"s":R,"p":S,"*":0}

for i,t in enumerate(T):
  if len(T)-i-K > 0:
    if t == T[i+K]:
      T[i+K] = "*"

ans = 0
for t in T:
  ans += graph[t]
print(ans)

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

score=0
kekka=[]

def Tensu(Aite):
  if Aite=="r":
    return P
  elif Aite=="s":
    return R
  else:
    return S


for i in range(N):
  if i<K:
    score+=Tensu(T[i])
    kekka.append(1)
  else:
    if T[i]!=T[i-K] or kekka[i-K]==0:
      score+=Tensu(T[i])
      kekka.append(1)
    else:
      score+=0
      kekka.append(0)
      
print(score)

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

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

flg = [False]*n
cnt = 0
for i in range(n):
    
    if i >= k and t[i] == t[i-k] and not flg[i-k]:
        flg[i] = True
        continue
    elif t[i] == 'r':
        cnt += p
    elif t[i] == 's':
        cnt += r
    elif t[i] == 'p':
        cnt += s
print(cnt)

Solution (Python):
from itertools import groupby
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()
    ans = 0
    for i in range(k):
        x = i
        li = []
        while x < n:
            li.append(t[x])
            x += k
        for key, value in groupby(li):
            if key=='r':
                a=p
            elif key=='s':
                a=r
            else:
                a=s

            ans += ((len(list(value))+1)//2)*a
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int,input().split())
r, s, p = map(int,input().split())
r_s_p_points = [r, s, p]
t = input()
my_hands = []
points = 0
r_s_p = [0, 1, 2]
t_num_list = []
for i in range(n):
    if t[i] == "r":
        t_num_list.append(0)
    elif t[i] == "s":
        t_num_list.append(1)
    else:
        t_num_list.append(2)

for i in range(k):
    if t_num_list[i] == 0:
        my_hands.append(2)
        points += p
    elif t_num_list[i] == 1:
        my_hands.append(0)
        points += r
    else:
        my_hands.append(1)
        points += s

for i in range(k, n):
    if r_s_p[(t_num_list[i] - 1) % 3] != my_hands[i - k]:
        my_hands.append(r_s_p[(t_num_list[i] - 1) % 3])
        points += r_s_p_points[r_s_p[(t_num_list[i] - 1) % 3]]
    else:
        if i + k <= n-1:
            for j in range(3):
                if j == r_s_p[(t_num_list[i] - 1) % 3]:
                    continue
                elif j == r_s_p[(t_num_list[i + k] - 1) % 3]:
                    continue
                else:
                    my_hands.append(j)
                    break
        else:
            my_hands.append(0)

print(points)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().rstrip()
d1 = {'r': ('p', p), 's': ('r', r), 'p': ('s', s)}
d2 = {'r': {'p': 'r', 's': 's', 'r': 's'},
      's': {'r': 's', 'p': 'p', 's': 's'},
      'p': {'r': 'r', 's': 'p', 'p': 'r'}}
ret = 0
me = []
for i, rival in enumerate(t):
    if i - k < 0 or me[i - k] != d1[rival][0]:
        me.append(d1[rival][0])
        ret += d1[rival][1]
    elif i - k >= 0 and me[i - k] == d1[rival][0] and i + k < n:
        me.append(d2[rival][t[i + k]])
print(ret)

Solution (Python):
n,k = list(map(int,input().split()))
point = list(map(int,input().split()))
rsp = ['r', 's', 'p']
t = input()
m = [rsp[rsp.index(i)-1] for i in t]
ans = 0
for i in range(n):
    if i<k :
        ans += point[rsp.index(m[i])]
    else:
        if m[i]!=m[i-k]:
            ans += point[rsp.index(m[i])]
        else:
            try:
                m[i] = rsp[rsp.index(m[i+k])-1]
            except:
                pass
    
print(ans)

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

win = {"r":P,"s":R,"p":S,"x":0}

ans = 0
for i in range(len(T)):
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += win[T[i]]

print(ans)

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

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

ans = 0
for i in range(K, len(T)):
  if T[i] == T[i - K]:
    T[i] = "x"
  else:
    ans += win[T[i]]

print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
D={"r":P,"s":R,"p":S}
T = input().strip()
A = {i:[] for i in range(K)}
for i in range(N):
    A[i%K].append(T[i])
cnt = 0
for k in range(K):
    if len(A[k])>0:
        cnt += D[A[k][0]]
        for i in range(1,len(A[k])):
            if A[k][i]==A[k][i-1]:
                A[k][i] = 0
            elif A[k][i]!=A[k][i-1]:
                cnt += D[A[k][i]]
print(cnt)

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

ans = 0
temp = [0]*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-k] == t[i]:
            if temp[i-k] == 0:
                temp[i] = -1
            else:
                if t[i] == 'r':
                    ans += p
                elif t[i] == 's':
                    ans += r
                else:
                    ans += s
                temp[i] = 0
        else:
            if t[i] == 'r':
                ans += p
            elif t[i] == 's':
                ans += r
            else:
                ans += s
            temp[i] = 0

    
print(ans)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,k = readInts()
r,s,p = readInts()
T = list(input())
ans = 0
for i in range(n):
    if k <= i:
        if T[i] == T[i-k]:
            T[i] = '0'
    if T[i] == 's':
        ans += r
    if T[i] == 'p':
        ans += s
    if T[i] == 'r':
        ans += p
print(ans)

Solution (Python):
n, k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
point = {"s":r, "p":s, "r":p}
x = {"s":"r", "p":"s", "r":"p"}
ans = []
P = 0
for i in t:
    if len(ans) < k or ans[-k] != x[i]:
        ans.append(x[i])
        P += point[i]
    else: ans.append("")
print(P)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ret = 0
for i in range(k):
    last = 'x'
    for c in t[i::k]:
        if last != c:
            ret += p if c == 'r' else r if c == 's' else s
            last = c
        else:
            last = 'x'
print(ret)

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


def win(t):
    if t == 'r':
        return P
    elif t == 's':
        return R
    else:
        return S


def janken(i):
    if i >= K:
        if T[i] != T[i - K] or own_win[i - K] is False:
            own_win[i] = True
            return win(T[i])
        else:
            return 0
    else:
        own_win[i] = True
        return win(T[i])


cnt = 0
for i in range(N):
    cnt += janken(i)

print(cnt)

Solution (Python):
n, k = list(map(int, input().split(' ')))
r, s, p = list(map(int, input().split(' ')))
d = dict(r=p,
         s=r,
         p=s)
ttt = input()
ans = 0
prevs = []
for i in range(n):
    if i < k or ttt[i] != prevs[i-k]:
        ans += d[ttt[i]]
        prevs.append(ttt[i])
    else:
        prevs.append('hoge')
print(ans)

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


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

for i in range(N):
  if i>=1:
    dp[i][0] += max(dp[i-1][0],dp[i-1][1],dp[i-1][2])
    dp[i][1] += max(dp[i-1][0],dp[i-1][1],dp[i-1][2])
    dp[i][2] += max(dp[i-1][0],dp[i-1][1],dp[i-1][2])
    
  if T[i] == "s":
    dp[i][0] += R
  if T[i] == "p":
    dp[i][1] += S
  if T[i] == "r":
    dp[i][2] += P
    
  if i-K >= 0:
    dp[i][0] += max(dp[i-K][1],dp[i-K][2]) - max(dp[i-K][0],dp[i-K][1],dp[i-K][2])
    dp[i][1] += max(dp[i-K][2],dp[i-K][0]) - max(dp[i-K][0],dp[i-K][1],dp[i-K][2])
    dp[i][2] += max(dp[i-K][0],dp[i-K][1]) - max(dp[i-K][0],dp[i-K][1],dp[i-K][2])
  
print(max(dp[-1][0],dp[-1][1],dp[-1][2]))