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())
s,p,r = map(int ,input().split())
t=list(input())
ans=0
for i in range(n):
    if i<k:
        if t[i]=="r":
            ans+=r
        elif t[i]=="s":
            ans+=s
        elif t[i]=="p":
            ans+=p
    else:
        if t[i]==t[i-k]:
            t[i]="a"
        else:
            if t[i]=="r":
                ans+=r
            elif t[i]=="s":
                ans+=s
            elif t[i]=="p":
                ans+=p
print(ans)

Solution (Python):
N, K = map(int, input().split())
RSP = list(map(int, input().split()))
d = {'r': 0, 's': 1, 'p': 2}
T = input()

ans = 0
flags = [True] * K
for n in range(N):
    if flags[n % K]:
        ans += RSP[(d[T[n]] - 1) % 3]
        if n + K < N:
            if T[n] == T[n + K]:
                flags[n % K] = False
    else:
        flags[n % K] = True
print(ans)

Solution (Python):
args=input().split()
N=int(args[0])
K=int(args[1])
args=input().split()
R=int(args[0])
S=int(args[1])
P=int(args[2])
T=input()

resSS=[]
for i in T:
  if i=="r":
    resSS.append(P)
  elif i=="s":
    resSS.append(R)
  else:
    resSS.append(S)

if N>=K:
  for i in range(N-K):
    if T[i+K]==T[i] and resSS[i+K]==resSS[i]:
      resSS[i+K]=0
res=0
for i in resSS:
  res+=i
print(res)

Solution (Python):
N, K = (int(i) for i in input().split())  
R, S, P = (int(i) for i in input().split())  
T = str(input())
T = list(T)
max_w = max(R,S,P)*N
dp = [0] * (N+1)
dp_flg = [0] * (N+1)
def junk_win(x):
    if x == 'r':
        return('p')
    elif x == 's':
        return('r')
    else:
        return('s')
T_win = []
for i in range(N):
    T_win.append(junk_win(T[i]))
def get_w(x):
    if x == 'r':
        return(R)
    elif x == 's':
        return(S)
    else:
        return(P)
T_win_w = []
for i in range(N):
    T_win_w.append(get_w(T_win[i]))
for i in range(K):
    
    dp[i] = dp[i-1] + T_win_w[i]
    dp_flg[i] = 1
    
for i in range(K,N):
    
    if T_win[i]==T_win[i-K] and dp_flg[i-K]==1:
        dp[i] = dp[i-1]
    else:
        dp[i] = dp[i-1] + T_win_w[i]
        dp_flg[i] = 1
print(dp[-2])

Solution (Python):
from itertools import groupby
n,k = [int(x) for x in input().split()]
r,s,p = [int(x) for x in input().split()]
T = input()
a = (n+k-1)//k
klist=[[""]*a for _ in range(k)]
for i in range(k):
  m=0
  klist[i] = T[i::k]
ans = 0
for i in range(k):
  t = list(klist[i])
  for key, value in groupby(t):
    if key=="r":
      ans+=(len(list(value))+1)//2*p
    if key=="s":
      ans+=(len(list(value))+1)//2*r
    if key=="p":
      ans+=(len(list(value))+1)//2*s
print(ans)

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

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

log = ["" for _ in range(k)] 
ans = 0
for i, play in enumerate(t):
    if play == log[i % k]:
        log[i % k] = ""
        continue
    log[i % k] = play
    ans += point[play]
print(ans)

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

for rem in range(k):
    dp = [[0]*3 for _ in range(n//k + int(n%k > rem))]
    ind = 0
    for i in range(rem,n,k):
        dp[ind][0] = max(dp[ind-1][1], dp[ind-1][2]) + int(t[i] == 's') * r
        dp[ind][1] = max(dp[ind-1][2], dp[ind-1][0]) + int(t[i] == 'p') * s
        dp[ind][2] = max(dp[ind-1][0], dp[ind-1][1]) + int(t[i] == 'r') * p

        ind += 1
    ind -= 1
    ans += max(dp[ind][0], dp[ind][1], dp[ind][2])
print(ans)

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

point = {"r": P, "s": R, "p": S}
total = 0




before = ["*" for _ in range(K)]

for i,now in enumerate(T):
  
  
  if before[i % K] != now:
    total += point[now]
    before[i % K] = now
  else:
    before[i % K] = "*"

print(total)

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

d = dict()
ans = 0
for i, move in enumerate(t):
    if move == "r" and d.get(i, 0) != "r":
        ans += p
        d[i+k] = d.get(i+k, "r")
    elif move == "s" and d.get(i, 0) != "s":
        ans += r
        d[i+k] = d.get(i+k, "s")
    elif move == "p" and d.get(i, 0) != "t":
        ans += s
        d[i+k] = d.get(i+k, "t")
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()

ans = 0
prev = ["
for i, move in enumerate(t):
    if prev[i%k] != move:
        ans += d[move]
        prev[i%k] = move
    else:
        prev[i%k] = "
print(ans)

Solution (Python):
from collections import deque

n,k = map(int,input().split())
s = list(map(int,input().split()))
score = {"r":s[0], "s":s[1],"p":s[2], "n":0}
stronger = {"r":"p", "s":"r","p":"s"}
rsp = input()


d = deque()
[d.append("n") for i in range(k)]

ans = 0
for i in range(n):
    bk = d.popleft()
    st = stronger[rsp[i]]
    if st  != bk:
        ans += score[st]
        d.append(st)
    else:
        d.append("n")

print(ans)

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

def win(a):
  if a == "r":
    return P
  elif a == "s":
    return R
  else:
    return S

flag = [0 for i in range(N)]

for i in range(N):
  if i < K or T[i] != T[i-K]:
    flag[i] = win(T[i])
  elif flag[i-K] == 0:
    flag[i] = win(T[i])

print(sum(flag))

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    
    N, K = Scanner.map_int()
    R, S, P = Scanner.map_int()
    T = Scanner.string()
    ans = 0
    M = {"r": P, "s": R, "p": S}
    prevs = [""] * N
    for i in range(N):
        if 0 <= i - K:
            if T[i] == prevs[i-K]:
                continue
        ans += M[T[i]]
        prevs[i] = T[i]
    print(ans)
    return


if __name__ == "__main__":
    main()

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

for i, s in enumerate(input()):
    d[i%K].append(s)

ans = 0
score = {}
score["s"] = R
score["p"] = S
score["r"] = P

for l in d:
    prev_hand = "a"
    for s in l:
        if s!=prev_hand:
            ans += score[s]
            prev_hand = s
        else:
            prev_hand = "a"
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = str(input())
i = 0
point = 0
L = [""]*N
for i in range(N):
  if T[i] == "r": 
    if i >= K and L[i-K] == "p":
      pass
    else:
      point += P
      L[i] = "p"
  elif T[i] == "s":
    if i >= K and L[i-K] == "r":
      pass
    else:
      point += R
      L[i] = "r"
  elif T[i] == "p":
    if i >= K and L[i-K] == "s":
      pass
    else:
      point += S
      L[i] = "s"
  else:
    break
print(point)

Solution (Python):
from sys import stdin, stdout
rock = 0
scissor = 1
paper = 2

def win(i, j):
    if j == 'r' and i == paper:
        return True
    if j == 's' and i == rock:
        return True
    if j == 'p' and i == scissor:
        return True
    return False

N, K = map(int, stdin.readline().split())
prize = list(map(int, stdin.readline().split())) 

T = stdin.readline()
length = len(T)
dp = [[0 for _ in range(3)] for _ in range(length)]

ans = 0
for i in range(K):
    for j in range(i, length, K):
        for k in range(3):
            if j >= K:
                dp[j][k] = max(dp[j-K][(k+1)%3], dp[j-K][(k+2)%3])
            if win(k, T[j]):
                dp[j][k] = dp[j][k] + prize[k]
        if j+K >= length:
            ans = ans + max(dp[j][0], dp[j][1], dp[j][2])

print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
me = []
for i in range(N):
    if T[i] == "r":
        ans += P
        log = P
        me.append("p")
    elif T[i] == "s":
        ans += R
        log = R
        me.append("r")
    else:
        ans += S
        log = S
        me.append("s")
    if i>=K:
        if me[i] == me[i-K]:
            ans -= log
            if (i+K) < N:
                if T[i+K] == "r":
                    if me[i-K] != "s":
                        me[i] = "s"
                    else:
                        me[i] = "r"
                if T[i+K] == "s":
                    if me[i-K] != "p":
                        me[i] = "p"
                    else:
                        me[i] = "s"
                if T[i+K] == "p":
                    if me[i-K] != "r":
                        me[i] = "r"
                    else:
                        me[i] = "p"
print(ans)

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

ans = 0
win = [True for _ in range(n)]
for i in range(n):
    
    if i-k >= 0 and t[i-k] == t[i] and win[i-k] == True:
        win[i] = False
    else:
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        else:
            ans += s

print(ans)

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

n,k = map(int, input().split())
r,s,p = map(int, input().split())
p = {'s': r, 'p':s, 'r':p}
T = input()

kk = [[] for _ in range(k)]
for i in range(n):
	kk[i % k].append(T[i])
ans = 0
for i in range(len(kk)):
	ans += p[kk[i][0]]
	j = 1
	while j < len(kk[i]):
		pj = kk[i][j-1]
		if(kk[i][j]==pj):
			if(j+1 < len(kk[i])):
				nj = kk[i][j+1]
				ans += p[kk[i][j+1]]
				j+=2
			else : j+=1
		else:
			ans += p[kk[i][j]]
			j+=1

print(ans)

Solution (Python):
N,K = [int(i) for i in input().rstrip().split(' ')]
R,S,P = [int(i) for i in input().rstrip().split(' ')]
r = 0
T = input()
te = ["p","r","s"]
point = {"p":P,"r":R,"s":S}
Play = []
for i,t in enumerate(T):
    best = te[te.index(t)-1]
    if Play[i-K] != best if i >= K else True:
        Play += [best]
        r += point[best]
    else:
        Play += ["
print(r)

Solution (Python):

import sys
input = sys.stdin.readline

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

score_dict = {"r": p, "s":r, "p":s}
winning = []
ans = 0
for i, ch in enumerate(t):
    if i < k:
        winning.append(1)
        ans += score_dict[ch]
        continue
    if t[i-k] == ch and winning[i-k] == 1:
        winning.append(0)
    else:
        winning.append(1)
        ans += score_dict[ch]

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
}
point = [None for i in range(n)]
for i in range(len(t)):
    if point[i] == None:
        point[i] = d[t[i]]
        if i + k < len(t) and t[i] == t[i + k]:
            point[i + k] = 0
print(sum(point))

Solution (Python):
N,K=map(int,input().split())
RSP=list(map(int,input().split()))
RSP.append(0)
T=list(input())
A=[0]*N

for i in range(N):
  if T[i]=='r':
    A[i]=2
  elif T[i]=='s':
    A[i]=0
  else:
    A[i]=1

ans=0
for i in range(K):
  B=A[i::K]
  for j in range(1,len(B)):
    if B[j-1]==B[j]:
      B[j]=3
  for k in B:
    ans+=RSP[k]

print(ans)

Solution (Python):
from itertools import groupby as gb
n,k,g,t,p,s=map(lambda v:v if v[0] in "rsp" else int(v),open(0).read().split())
r,ss,pa=0,0,0
l=[""]*k
for x,i in enumerate(list(s)):
  if l[x%k]==i:l[x%k]="";continue
  if i=="r":pa+=1
  elif i=="s":r+=1
  else:ss+=1
  l[x%k]=i
print(r*g+ss*t+pa*p)

Solution (Python):
n,k = map(int,input().split())
point = list(map(int,input().split()))
t = str(input())
janken = ["s","p","r"]
ans = 0
iziwaru = [False for i in range(n)]
for i in range(n):
    if not iziwaru[i]:
        ans += point[janken.index(t[i])]
        if i + k < n:
            if t[i] == t[i+k]:iziwaru[i+k] = True
print(ans)

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

Solution (Python):
n, k = map(int, input().split())
score_dict = dict(zip("spr", map(int, input().split())))
t = list(input())

score = sum([score_dict[h] for h in t[:k]])
for i in range(k, n):
    if t[i] != t[i - k]:
        score += score_dict[t[i]]
    else:
        t[i] = None

print(score)

Solution (Python):
n, k = map(int, input().split())
s = dict(zip('spr', map(int, input().split())))
t = list(input())
r = sum(s[h] for h in t[0:k])
for i in range(k, n):
  if t[i-k] != t[i]:
    r += s[t[i]]
  else:
    t[i] = None
print(r)

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

for i in range(N):
    WAKE[i%K] += T[i]

ans = 0
for i in range(K):
    if len(WAKE[i])>=0:
        pre = WAKE[i][0]
        ans += win[pre]
        for j in range(1,len(WAKE[i])):
            now = WAKE[i][j]
            if pre != now:
                ans += win[now]
                pre = now
            else:
                pre = ''
print(ans)

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


hands = [True] * N

scores = 0

for i in range(len(T)):
  if i >= K:
    
    if T[i-K] == T[i] and hands[i-K]:
      hands[i] = False
      continue
      
  if T[i] == "r":
    scores += P
  elif T[i] == "s":
    scores += R
  elif T[i] == "p":
    scores += S
    
print(scores)

Solution (Python):
def jyanken_win(enemy, r, s, p):
    score = 0
    if enemy == 's':
            score += r
    elif enemy == 'p':
            score += s
    elif enemy == 'r':
            score += p
    
    return score


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

    mine = []
    final_score = 0

    for i in range(n):
        if i >= k and mine[i-k] != 'not_win' and t[i-k] == t[i]: 
            mine.append('not_win')
        
        else:
            final_score += jyanken_win(t[i],r,s,p)
            mine.append('win')

    print(final_score)

if __name__ == '__main__':
    main()

Solution (Python):

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

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

answer=0
oklist=[False]*K
for i in range(N):
    if oklist[i%K]:
        oklist[i%K]=False
        if T[i]=="r":
            answer+=P
        elif T[i]=="s":
            answer+=R
        else:
            answer+=S
        continue


    if i>=K and T[i]==T[i-K]:
        oklist[i%K]=True
        continue
    
    if T[i]=="r":
        answer+=P
    elif T[i]=="s":
        answer+=R
    else:
        answer+=S

print(answer)

Solution (Python):
import sys
n, k = map(int, sys.stdin.readline().split())
r, s, p = map(int, sys.stdin.readline().split())
t = sys.stdin.readline()
ans = 0
for i in range(k, n):
  if t[i] == t[i-k]:

    t = t[:i] + "a" + t[i+1:]
ans = t.count("r")*p + t.count("s")*r + t.count("p")*s

print(ans)

Solution (Python):
n,k = list(map(int,input().split(" ")))
s1,s2,s3 = list(map(int,input().split(" ")))
t = input()

scores = dict()
win = dict()
scores["r"] = s1
scores["s"] = s2
scores["p"] = s3

win["r"] = "p"
win["s"] = "r"
win["p"] = "s"

isWin = [False for _ in range(len(t))]

total = 0
for i in range(n):
    if i < k:
        total += scores[win[t[i]]]
        isWin[i] = True
    else:
        if t[i] != t[i-k]:
            total += scores[win[t[i]]]
            isWin[i] = True
        else:
            if not isWin[i-k]:
                total += scores[win[t[i]]]
                isWin[i] = True
            else:
                isWin[i] = False

print(total)

Solution (Python):

n, k = map(int, input().split())
rsp_score = list(map(int, input().split()))
rsp = input("")
rsp_order = list(rsp)
t_score = []
t_order = []

for i in range(n):
    if rsp_order[i] == "r":
        if i < k:
            t_score.append(rsp_score[2])
            t_order.append("p")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[2])
                t_order.append("p")

            else:
                t_score.append(0)
                t_order.append("c")

    elif rsp_order[i] == "s":
        if i < k:
            t_score.append(rsp_score[0])
            t_order.append("r")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[0])
                t_order.append("r")

            else:
                t_score.append(0)
                t_order.append("c")

    elif rsp_order[i] == "p":
        if i < k:
            t_score.append(rsp_score[1])
            t_order.append("s")

        elif i >= k:
            if rsp_order[i] != rsp_order[i - k] or t_order[i - k] == "c":
                t_score.append(rsp_score[1])
                t_order.append("s")

            else:
                t_score.append(0)
                t_order.append("c")

print(sum(t_score))

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
hand = {"r": "p", "s": "r", "p": "s"}
score = {"r": r, "s": s, "p": p}
other = "o"
t = input()
hist = [other] * k

result = 0
for i, v in enumerate(t):
    i %= k
    if hist[i] != hand[v]:
        hist[i] = hand[v]
        result += score[hand[v]]
    else:
        hist[i] = other
print(result)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  r,s,p=LI()
  t=S()

  dp=[[0 for _ in range(n+1)]for __ in range(3)]

  for i in range(n):
    hyoka=t[i]

    dp[0][i+1]=max(dp[0][i],dp[1][i],dp[2][i])
    dp[1][i+1]=max(dp[0][i],dp[1][i],dp[2][i])
    dp[2][i+1]=max(dp[0][i],dp[1][i],dp[2][i])

    if i-k<0:
      if hyoka=='s':
        dp[0][i+1]+=r
      elif hyoka=='p':
        dp[1][i+1]+=s
      else:
        dp[2][i+1]+=p
    else:
      if hyoka=='s':
        if not (dp[0][i+1-k]>dp[1][i+1-k] and dp[0][i+1-k]>dp[2][i+1-k]):
          dp[0][i+1]+=r
      elif hyoka=='p':
        if not (dp[1][i+1-k]>dp[2][i+1-k] and dp[1][i+1-k]>dp[0][i+1-k]):
          dp[1][i+1]+=s
      else:
        if not (dp[2][i+1-k]>dp[0][i+1-k] and dp[2][i+1-k]>dp[1][i+1-k]):
          dp[2][i+1]+=p

  
  
  
  return max(dp[0][n],dp[1][n],dp[2][n])


print(main())

Solution (Python):










N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = [c for c in input()]

score = dict(r=P, s=R, p=S)  
total = 0
for res in range(K):  
    i = res
    while i < N:
        lasthand = T[i]
        cont = 0
        while i < N and T[i] == lasthand:
            cont += 1
            i += K
        total += (cont+1)//2 * score[lasthand]

print(total)

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

ans = []
def get_ans(h):
    if h == 'r':
        return('p')
    elif h == 's':
        return('r')
    elif h == 'p':
        return('s')

for i in range(k):
    ans.append(get_ans(hand[i]))
for i in range(k,n):
    if (hand[i] =='r' and ans[i-k] != 'p') or (hand[i] =='p' and ans[i-k] != 's') or (hand[i] =='s' and ans[i-k] != 'r'):
        
        ans.append(get_ans(hand[i]))
    else:
        
        try:
            h = ['r', 's', 'p']
            h.remove(get_ans(hand[i-k]))
            h.remove(get_ans(hand[i+k]))
            ans.append(h[0])
        except:
            ans.append(h[0])
score = 0
for i in range(n):
    if ans[i] == 'r' and hand[i] == 's':
        score += r
    if ans[i] == 'p' and hand[i] == 'r':
        score += p
    if ans[i] == 's' and hand[i] == 'p':
        score += s
print(score)

Solution (Python):
if __name__ == '__main__':
    n, k = map(int, input().split())
    point_r, point_s, point_p = map(int, input().split())
    t = list(input())

    point = {
        'r': point_p,
        's': point_r,
        'p': point_s
    }

    score = 0
    for i in range(n):
        if i < k:
            score += point[t[i]]
        else:
            if t[i] != t[i - k]:
                score += point[t[i]]
            else:
                t[i] = 'x'

    print(score)

Solution (Python):


def pin(type=int):
    return map(type,input().split())
N,K =pin()
R,S,P =pin()
T, =pin(str)



winT = []
win = "rpsr"
for i in range(N):
    for j in range(3):
        if T[i] ==win[j]:
            winT.append(win[j+1])





for i in range (N-K):
    if winT[i] == winT[i+K]:
        winT[i+K] = "0"
        


ans=0
for i in range(N):
    if winT[i]=="p":
        ans+=P
    elif winT[i]=="r":
        ans+=R
    elif winT[i]=="s":
        ans+=S
print(ans)

Solution (Python):
import math
import bisect
N,K = list(map(int,input().split(" ")))
r,s,p = list(map(int,input().split(" ")))

di = {}
di["r"] = p
di["s"] = r
di["p"] = s
di["@"] = 0
T = input()
T = list(T)
for i in range(K,N):
    if T[i - K] == T[i]:
        T[i] = "@"
ans = 0
for temp in T:
    ans += di[temp]

print(ans)

Solution (Python):
from itertools import compress

def solve(RSP, T, K):
    w = [True] * K
    for i, (t, tt) in enumerate(zip(T, T[K:])):
        w.append(True if t != tt else not w[i])
    return sum(map(lambda t: RSP[t], compress(T, w)))

def main():
    _, K = list(map(int, input().split()))
    RSP = dict(zip('spr', map(int, input().split())))
    T = input()
    print(solve(RSP, T, K))

if __name__ == '__main__':
    main()

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

ans = 0
d = {'s':R , 'p':S, 'r':P}
for i,t in enumerate(T):
    if i<K:
        ans += d[t]
    else:
        if T[i-K]!=T[i]:
                ans += d[t]
        else:
            T[i]=''

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import floor, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
R, S, P = MAP()
T = input()

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

ideal = [win[x] for x in T]

ans = 0
for i in range(N):
  if i < K:
    ans += point[ideal[i]]
  elif ideal[i-K] == ideal[i]:  
    ideal[i] = None
  else:
    ans += point[ideal[i]]

print(ans)

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

commands = [''] * N
ans = 0
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()

score = 0

for i in range(k):
    aa = t[i::k]
    before = ''
    for ja in aa:
        if ja==before:
            before = ''
            continue
        else:
            if ja=='s':
                score += R
            elif ja=='p':
                score += S
            else:
                score += P
            before = ja

print(score)

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

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

total_point = 0
memory = [""] * k
for i in range(n):
  if t[i] != memory[i % k]:
    memory[i % k] = t[i]
    total_point += point(t[i])
  else:
    memory[i % k] = ""
print(total_point)

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

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

ans = 0
for i in range(min(n,k)):
    before = T[i]
    counter = 0
    for s in T[i::k]:
        if before == s:
            counter += 1
        else:
            ans += (counter+1)//2 * point[before]
            before = s
            counter = 1
    ans += (counter+1)//2 * point[before]
print(ans)

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

BAN = [""]*N
score = 0

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


for j in range(K,N):
    if T[j]=="r":
        if BAN[j-K]=="p" :
            continue
        else:
            score += P
            BAN[j] = "p"
    elif T[j] == "s":
        if BAN[j-K] == "r":
            continue
        else:
            score += R
            BAN[j] = "r"
    else:
        if BAN[j-K] == "s":
            continue
        else:
            score += S
            BAN[j] = "s"

print(score)