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

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

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

ans = 0
for l in ll:
    x = ""
    for v in l:
        if x != v:
            if v == "r":
                ans += p
            elif v == "s":
                ans += r
            else:
                ans += s
            x = v
        else:
            x = ""
            
print(ans)

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()

    score = {'r': P, 's': R, 'p': S}
    ans = 0
    for i in range(K):
        prev = ''
        while i <= N - 1:
            if T[i] != prev:
                ans += score[T[i]]
                prev = T[i]
            else:
                prev = ''
            i += K
    print(ans)


main()

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

def score(s):
  if s == 'r':
    output = P
  elif s == 's':
    output = R
  else:
    output = S
  return output

k_list = [0]*K 
answer = 0
for i in range(K):
  s = T[i]
  ind = i%K
  k_list[ind] = s
  answer += score(s)
for j in range(K,N):
  s = T[j]
  ind = j%K
  if s == T[j-K] and k_list[ind] == s:
    k_list[ind] = 0
  elif s == T[j-K] and k_list[ind] != s:
    k_list[ind] = s
    answer += score(s)
  else:
    k_list[ind] = s
    answer += score(s)
print(answer)

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

st = ''
for i in t:
  if i == 'r':
    st += 'p'
  elif i == 'p':
    st += 's'
  else:
    st += 'r'

stt = st[:k]

for i in range(k,n):
  if st[i] != stt[i-k]:
    stt += st[i]
  else:
    stt += 'x'
    
sum = 0
for i in stt:
  if i == 'r':
    sum += r
  elif i == 's':
    sum += s
  elif i == 'p':
    sum += p
  else:
    pass
print(sum)

Solution (Python):


import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

def win(m):
    if m == 0:
        return 2
    if m == 1:
        return  0
    if m == 2:
        return 1


N, K = MI()
RSP = LI()
RSP += [0]
T = input()

M = []
for t in T:
    if t == 'r':
        M.append(0)
    if t == 's':
        M.append(1)
    if t == 'p':
        M.append(2)

P = [win(m) for m in M]

for i in range(K, N):
    if P[i] == P[i - K]:
        P[i] = 3

PP = [RSP[p] for p in P]
print(sum(PP))

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
raw = input()
new = []
for i in range(n):
  if raw[i] == 'r':
    new.append(0)
  elif raw[i] == 's':
    new.append(1)
  else:
    new.append(2)

ans = 0
for i in range(n):
  if i - k >= 0:
    if new[i] == new[i-k]:
      new[i] = 3
      
      continue
  if new[i] == 0:
    ans += p
  elif new[i] == 1:
    ans += r
  else:
    ans += s
  
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=str(input())
li=[]
ans=0
for i in range(len(t)):
    li.append(t[i])
    if i<k:
        if li[i]=="r":
            ans+=p
        elif li[i]=="s":
            ans+=r 
        else:
            ans+=s 
    else:
        if li[i]==li[i-k]:
            li[i]=0
            ans+=0
        elif li[i]=="r":
            ans+=p
        elif li[i]=="s":
            ans+=r 
        else:
            ans+=s 
print(ans)

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[i] == T[i+K]:
        T[i+K] = "@"
dic1 = {"r":P,"s":R,"p":S,"@":0}
points = 0
for i in range(N):
    points += dic1[T[i]]
print(points)

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 = ['x'] * n

    ans = 0
    for i, j in enumerate(t):
        if i < k or j != pre[i-k]:
            pre[i] = j
            if j == 'r':
                ans += p
            elif j == 's':
                ans += r
            else:
                ans += s
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()[:-1]

    res = 0
    A = {'r': P, 's': R, 'p': S}
    for k in range(K):
        tmp = A[T[k]]
        cnt = 1
        for i in range(k,N-K,K):
            
            if i+K>=N:
                break
            if T[i]==T[i+K]:
                cnt += 1
            else:
                res += tmp * ((cnt + 1) // 2)
                tmp = A[T[i+K]]
                cnt = 1
            
        res += tmp * ((cnt + 1) // 2)

    print(res)    


if __name__ == '__main__':
    main()

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

for i in range(n):
  
  if i < k:
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  elif i >= k and t[i] != t[i-k]:
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  elif i >= k and t[i] == t[i-k] and w[i-k] =='l':
    if t[i] == 'r':
      sc += p
      w += 'w'
    if t[i] == 's':
      sc += r
      w += 'w'
    if t[i] == 'p':
      sc += s
      w += 'w'
  else:
    w += 'l'
    
print(sc)

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

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

for i in range(0,n-k):
  if t[i]==t[i+k]:
    t[i+k]="M"
    if t[i]=="r":
      ans-=p
    elif t[i]=="s":
      ans-=r
    elif t[i]=="p":
      ans-=s   
print(ans)

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

ja=[[] for i in range(k)]

for i in range(n):
    ja[i%k].append(t[i])

sum=0
for i in range(k):
    for j in range(len(ja[i])):
        if j!=0 and ja[i][j-1]==ja[i][j]:
            c+=1
        else:
            c=1
        if c%2 ==1:
            if ja[i][j]=='r':
                sum+=p
            elif ja[i][j]=='s':
                sum+=r
            else:
                sum+=s

print(sum)

Solution (Python):
def get_reward(x, r, s, p):
    if x == "r":
        return p
    if x == "s":
        return r
    if x == "p":
        return s


def rotate(x):
    if x == "r":
        return "s"
    if x == "s":
        return "p"
    if x == "p":
        return "r"


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

total = 0

for i, x in enumerate(T):
    if i <= K-1:
        total += get_reward(x, R, S, P)
        continue
    if T[i-K] != x:
        total += get_reward(x, R, S, P)
        continue
    if N > i+K and T[i+K] == x:
        T[i] = rotate(x)


print(total)

Solution (Python):

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


res =[]
answer = T.count('s')*S+T.count('p')*P+T.count('r')*R

l = [0]*N
for i in range(K,N):
    if T[i] == T[i-K]:
        if T[i]==('s'):
            answer -= S
            l[i]=1
        elif T[i]==('p'):
            answer -= P
            l[i]=1
        elif T[i]==('r'):
            answer -= R
            l[i]=1

for j in range(K*2,N):
    if T[j] == T[j-K] and T[j] == T[j-K*2] and l[j-K]==1 and l[j-K*2]==0:
        if T[j]==('s'):
            answer += S
            l[j] = 0
        elif T[j]==('p'):
            answer += P
            l[j] = 0
        elif T[j]==('r'):
            answer += R
            l[j] = 0
print(answer)

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

d = {"s":r, "p":s, "r":p}
score = 0
for idx in range(k):
    List = []
    for i in range(n):
        if idx+i*k > n-1:
            break
        else:
           List.append(t[idx+i*k])
    not_liset = True
    tmp = List[0]
    score += d[tmp]
    for i in range(1, len(List)):
        if tmp == List[i] and not_liset:
            not_liset = False
        else:
            score += d[List[i]]
            not_liset = True
        tmp = List[i]
print(score)

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

T = input()

rCount = T.count('r')
sCount = T.count('s')
pCount = T.count('p')
maxValue = rCount*P + sCount*R + pCount*S

if N <= K:
    print(maxValue)
else:
    A = []
    for i in range(K):
        A.append(T[i::K])
    
    for a in A:
        minus = {'r':0, 's':0, 'p':0}
        listA = list(a)
        for i in range(len(listA)-1):
            if listA[i] == listA[i + 1]:
                minus[listA[i]] += score[listA[i]]
                listA[i+1] = 'chenge'
        maxValue -= (minus['r'] + minus['s'] + minus['p'])

print(maxValue)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=[[] for i in range(k)]
for i in range(n):
    ans[i%k]+=[t[i]]
cnt=0

for i in range(k):
    if ans[i][0]=="r":
        cnt+=p
        ans[i][0]="R"
    elif ans[i][0]=="s":
        cnt+=r
        ans[i][0]="S"
    elif ans[i][0]=="p":
        cnt+=s
        ans[i][0]="P"
    for j in range(1,len(ans[i])):
        if ans[i][j]=="r" and ans[i][j-1]!="R":
            cnt+=p
            ans[i][j]="R"
        elif ans[i][j]=="s" and ans[i][j-1]!="S":
            cnt+=r
            ans[i][j]="S"
        elif ans[i][j]=="p" and ans[i][j-1]!="P":
            cnt+=s 
            ans[i][j]="P"

print(cnt)

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

Solution (Python):
def main():
    n,k=map(int,input().split())
    r,s,p=map(int,input().split())
    t=input()
    cnt = 0
    check=[0]*n
    for i in range(0,k,1):
        for j in range(i+k,n,k):
            if t[j] == t[j-k] and check[j-k] == 0 and check[j]==0:
                check[j] = 1


    i=0
    for tmp in t:
        if check[i] == 1:
            i+=1
            continue
        if tmp == 'r' :
            cnt += p
        elif tmp == 's':
            cnt+=r
        elif tmp == 'p':
            cnt+=s
        i+=1
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

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

Solution (Python):
import sys
import math
from collections import deque
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()
N, K = IL()
r, s, p = IL()
t = S()

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

ans = 0
for i in range(N):
  ans += judge[t[i]]

for n in range(K):
  tmp = ''
  cnt = 0
  for i in range(n,N,K):
    if t[i] == tmp:
      cnt += 1
    else:
      if tmp != '':
        ans -= judge[tmp] * (cnt//2)
      tmp = t[i]
      cnt = 1
  else:
    if tmp != '':
      ans -= judge[tmp] * (cnt//2)

print(ans)

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

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

for i in range(K, N):
    if t[i] == t[i-K]:
        t[i] = 'A'
        cnt[i] = 0
ans = sum(cnt)
print(ans)

Solution (Python):
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = input()
P = {'r':P, 's':R, 'p':S}
score = [-1 for _ in range(N)]

for i,t in enumerate(T):
	if i < K:
		score[i] = P[t]
	else:
		if t == T[i-K] and score[i-K] != 0:
			score[i] = 0
		else:
			score[i] = P[t]
			
print(sum(score))

Solution (Python):
n, k = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()] 
t = list(input())

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

score = 0

for i in range(n):
  if (0 <= i-k) and (t[i] == t[i-k]):
    t[i] = 'x'
    continue
  score += addscoremap[t[i]]

print(score)

Solution (Python):
def p_d():
    N, K = map(int, input().split())
    *rsp, = map(int, input().split())
    li = {
        "r": 0,
        "s": 1,
        "p": 2
    }
    t = input()
    ans = 0
    for k in range(K):
        dp = [0] * 3
        for i in range(k, N, K):
            x = li[t[i]]
            NEXT = [0] * 3
            for p in range(3):
                NEXT[p] = max(dp[(p + 1) % 3], dp[(p - 1) % 3])
                if (p + 1) % 3 == x:
                    NEXT[p] += rsp[p]
            dp = NEXT
        ans += max(dp)
    print(ans)
    
p_d()

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

score = {'r':P,'s':R,'p':S}
ans = 0
for i in range(N):
    if i+K<=N-1:
        if a[i]==0 and T[i]==T[i+K]:
            a[i+K]=1
    if a[i]==0:
        ans+=score[T[i]]
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
d=[]
ct=0
for i in range(k):
    
     
    for j in range(i,n,k):
        d.append(t[j])
    for l in range(len(d)-1):
        if d[l]==d[l+1]:
            d[l+1]='out'
   
    ct+=d.count('r')*p+d.count('s')*r+d.count('p')*s
    d=[]
    
    

print(ct)

Solution (Python):
n,k=map(int, input().split())
point = [int(i) for i in input().split()]
janken = input()

for i in range(k,n):
  if janken[i] == janken[i-k]:
    janken= janken[0:i]+'0'+janken[i+1:n]

count=0

for i in range(n):
  if janken[i] == 'r':
    count += point[2]
  elif janken[i] == 's':
    count += point[0]
  elif janken[i] == 'p':
    count += point[1]

print(count)

Solution (Python):
def points(i):
    if i=="r":
        point=p
    elif i=="s":
        point=r
    else:
        point=s
    return point
    
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()

ans=0
L=[]
for j in range(n):
    if j<k:
        ans+=points(t[j])
        L.append(t[j])
    else:
        if t[j]==L[j-k]:
            L.append("b")
            continue
        else:
            ans+=points(t[j])
            L.append(t[j])
print(ans)

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

m=0
ll=[]
for i in range(len(l)):
    if k<=i:
        if l[i]=="r" and ll[i%k]!=l[i]:
            m+=p
            ll[i%k]="r"
        elif l[i]=="r" and ll[i%k]==l[i]:
            ll[i%k]="."

        elif l[i]=="s" and ll[i%k]!=l[i]:
            m+=r
            ll[i%k]=("s")
        elif l[i]=="s" and ll[i%k]==l[i]:
            ll[i%k]=(".")
            
        elif l[i]=="p" and ll[i%k]!=l[i]:
            m+=s
            ll[i%k]=("p")
        elif l[i]=="p" and ll[i%k]==l[i]:
            ll[i%k]=(".")

    else:
        if l[i]=="r":
            m+=p
            ll.append("r")
        elif l[i]=="s":
            m+=r
            ll.append("s")
        elif l[i]=="p":
            m+=s
            ll.append("p")

print(m)

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

dp = [[0] * 3 for i in range(N+1)]
for i in range(1, N+1):
    

    if T[i-1] == '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]+P, dp[i-K][1]+P)

    elif T[i-1] == '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]+R, 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]+S, 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])
print(ans)

Solution (Python):
def ints():
  return [int(x) for x in input().split()]
def ii():
  return int(input())

n, k = ints()
R, S, P = ints()
cpu = input()
jibun = [0] * n

w = ['r', 's', 'p']

for i in range(n):
  c = cpu[i]
  if c=='r':
    t = ['p']
  elif c=='s':
    t = ['r']
  else:
    t = ['s']

  j = i-k
  if j>=0:
    o = jibun[j]
    te = t[0]
    if te in o:
      if len(o)==1:
        ww = w.copy()
        ww.remove(te)
        t = ww
      else:
        o.remove(te)

  jibun[i] = t

s = 0
for i in range(n):
  w = jibun[i][0]+cpu[i]
  if w=='rs':
    s += R
  elif w=='sp':
    s += S
  elif w=='pr':
    s += P
print(s)

Solution (Python):
import math

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


def point(i, hand):
    if i >= N: return 0
    t = T[i]
    if t == 'r': return P if hand == 2 else 0
    if t == 's': return R if hand == 0 else 0
    if t == 'p': return S if hand == 1 else 0
    return 0


def max_value(head_index):
    l = math.ceil(N / K)
    dp = [[0, 0, 0] for i in range(l)]
    dp[0] = [
      point(head_index, 0),
      point(head_index, 1),
      point(head_index, 2)
    ]
    for i in range(1, l):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + point(head_index + i * K, 0)
        dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + point(head_index + i * K, 1)
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + point(head_index + i * K, 2)
    return max(dp[l - 1])


def solve():
    ans = 0

    for i in range(K):
        ans += max_value(i)

    return ans


print(solve())

Solution (Python):
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T =input()
ans = 0
chk_list = [0] * len(T)
for i in range(len(T)):
    if i < K:
        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        else:
            ans += S
    else:
        if T[i] == T[i-K]:
            if chk_list[i-K] == 0:
                chk_list[i] =1
                continue
        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        elif T[i] == "p":
            ans += S
print(ans)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
li=[]
pt=0
for i in range(n):
  if i<k and t[i] == "r":
    li.append("p")
    pt+=p
  elif i<k and t[i] == "s":
    li.append("r")
    pt += r
  elif i<k and t[i] == "p":
    li.append("s")
    pt += s
  elif i>=k and t[i] == "r" and li[i-k] != "p":
    li.append("p")
    pt+=p
  elif i>=k and t[i]=="r" and li[i-k] =="p":
    li.append(0)
  elif i>=k and t[i] == "s" and li[i-k] != "r":
    li.append("r")
    pt += r
  elif i>= k and t[i]=="s" and li[i-k] == "r":
    li.append(0)
  elif i>=k and t[i] == "p" and li[i-k] != "s":
    li.append("s")
    pt += s
  elif i>=k and t[i] == "p" and li[i-k] == "s":
    li.append(0)
print(pt)

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

def opt(c):
    if c=='r':
        return 'p'
    if c=='s':
        return 'r'
    if c=='p':
        return 's'

def sec(c,x):
    if x!=opt(c):
        return opt(c)
    else:
        return 'x'
    
def cnt(ans):
    c = 0
    for i in range(len(ans)):
        if ans[i]=='r':
            c+=r
        if ans[i]=='s':
            c+=s
        if ans[i]=='p':
            c+=p
    return c


ans = ""
for i in range(len(t)):
    if i-k>=0:
        ans+=sec(t[i],ans[i-k])
    else:
        ans+=opt(t[i])
        
print(cnt(ans))

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
t=list(reversed(t))
r_p, p_p, s_p = 0, 0, 0
r_c, p_c, s_c = None, None, None
for i in range(len(t)):
    if t[i] == 'r':
        if r_c is None:
            r_c = i
        if i+k < len(t) and t[i+k] == 'r':
            t[i+k] = 'x' 
        r_p += p
        r_c = i
    if t[i] == 'p':
        if p_c is None:
            p_c = i
        if i+k < len(t) and  t[i+k] == 'p':
            t[i+k] = 'x' 
        p_p += s
        p_c = i
    if t[i] == 's':
        if s_c is None:
            s_c = i
        if i+k < len(t) and t[i+k] == 's':
            t[i+k] = 'x' 
        s_p += r
        s_c = i
    

print(r_p + p_p + s_p)

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

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input().strip()
D = deque(T)
D.appendleft('none')
T = list(D)

score = 0
count = 0
before = 'a'

for i in range(1, K+1):
    for j in range(i, N+1, K):
        now = T[j]
        if now == before:
            count += 1
        else:
            if now == 'r':
                score += P
            elif now == 's':
                score += R
            elif now == 'p':
                score += S
            if count >= 2:
                if before == 'r':
                    score += P*((count-1)//2)
                elif before == 's':
                    score += R*((count-1)//2)
                elif before == 'p':
                    score += S*((count-1)//2)
            count = 1
        before = now
    if count >= 2:
        if now == 'r':
            score += P*((count-1)//2)
        elif now == 's':
            score += R*((count-1)//2)
        elif now == 'p':
            score += S*((count-1)//2)
    before = 'a'
        
print(score)

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

for i in range(1, N+1):
  flag = True
  if K < i:
    if te[i-1] == te[i-1-K]:
      te[i-1] = "a"
      flag = False
  if flag:
    if te[i-1] == "r":
      point += P
    elif te[i-1] == "s":
      point += R
    else:
      point += S
print(point)

Solution (Python):
n, k = map(int,input().split())
R, S, P = map(int,input().split())
t = str(input())
dic_rsp = {'r':P, 's':R, 'p':S}
ans = 0
for ik in range(k):
    tmp = 'o'
    for ii in range(ik,n,k):
        if ii==0:
            ans+=dic_rsp[t[ii]]
            tmp = t[ii]
        else:
            if t[ii]!=tmp:
                ans+=dic_rsp[t[ii]]
                tmp = t[ii]    
            else:
                tmp = 'o'                    
print(ans)

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

history = []
ans = 0
for i in range(N):
    if not (i > K-1):
        if T[i] == "r":
            ans += P
            history.append([T[i], True])
        elif T[i] == "s":
            ans += R
            history.append([T[i], True])
        else:
            ans += S
            history.append([T[i], True])
        
    else:
        if T[i] != T[i-K] or (T[i] == T[i-K] and history[i-K][1] == False):
            if T[i] == "r":
                ans += P
                history.append([T[i], True])
            elif T[i] == "s":
                ans += R
                history.append([T[i], True])
            else:
                ans += S
                history.append([T[i], True])
            
        else:
            history.append([T[i], False])
    
print(ans)

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

te = ['r', 's', 'p']

def win(s):
	if s == 'r':
		return 2
	elif s == 's':
		return 0
	else:
		return 1

taka = []
cnt = 0

for i in range(n):
	winner = win(t[i])
	if i < k:
		taka.append(te[winner])
		cnt += rsp[winner]
	else:
		if taka[i-k] == te[winner]:
			if i < n-k:
				next = win(t[i+k])
				can = ['r', 's', 'p']
				del can[winner]
				if te[next] in can:
					for j in range(len(can)):
						if can[j] != te[next]:
							taka.append(can[j])
				else:
					taka.append(can[0])
			else:
				can = ['r', 's', 'p']
				del can[winner]
				taka.append(can[0])
		else:
			taka.append(te[winner])
			cnt += rsp[winner]
print(cnt)

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

ans = [0] * n
def janken(te):
  if te == 'r':
    return p
  elif te == 's':
    return r
  else:
    return s
    
for i in range(n):
  konkai = janken(t[i])
  if i < k:
    ans[i] = konkai
  elif ans[i-k] != konkai:
    ans[i] = konkai
  else:
    if t[i-k] == t[i]:
      ans[i] = 0
    else:
      ans[i] = konkai
print(sum(ans))

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

x = [0 for i in range(n)]
y = [None for i in range(n)]

t = list(t)

def hoge(my_hand, target_char, value, x):
  for i in range(n):
    if t[i] == target_char:
      if i - k < 0:
        x[i] = value
        y[i] = my_hand
      else:
        if y[i - k] != my_hand:
          x[i] = value
          y[i] = my_hand

rr = [r, 'r', 's']
pp = [p, 'p', 'r']
ss = [s, 's', 'p']

fuga = [rr, pp, ss]

fuga.sort(key=lambda x: x[0], reverse=True)

for f in fuga:
  hoge(f[1], f[2], f[0], x)

print(sum(x))

Solution (Python):



n,k=map(int,input().split())
point=list(map(int,input().split()))
t=input()

rsp={'r':0,'s':1,'p':2}

ls=[[] for _ in range(k)]

for i in range(n):
    ls[i%k].append(t[i])    
ans=0
for e in ls:
    cnt=1
    for i in range(1,len(e)):
        if e[i-1]==e[i]:
            cnt+=1
        else:
            pp=(rsp[e[i-1]]-1+3)%3
            ans+=-(-cnt//2)*point[pp]
            cnt=1
    pp=(rsp[e[-1]]-1+3)%3
    ans+=-(-cnt//2)*point[pp]


print(ans)

Solution (Python):

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

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

arr = list(input())
point = 0
for x,y in enumerate(arr):
    if x+k < n:
        if arr[x+k] == arr[x]:
            arr[x+k]=None

for i in arr:
    if i == "r":
        point+=p
    elif i =="s":
        point+=r
    elif i =="p":
        point+=s

print(point)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
List = [[] for k in range(K)]
for n in range(N):
    List[n % K].append(T[n])

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

score = 0
for k in range(K):
    L = List[k]
    count = 1
    length = len(L)
    for i in range(length):
        try:
            if L[i] == L[i+1]:
                count += 1
            else:
                score += point[L[i]]*((count+1)//2)
                count = 1
        except:
            score += point[L[i]]*((count+1)//2)
print(score)

Solution (Python):
import sys
from collections import defaultdict
input = sys.stdin.readline
n, k = [int(x) for x in input().strip().split()]
p = {a:b for a, b in zip(['s', 'p', 'r'], [int(x) for x in input().strip().split()])}
p['b'] = 0
t = input().strip()
pre_ = ['b'] * k
cnt = [0] * k
ans = 0
for i, tt in enumerate(t, start=1):
    if tt != pre_[i%k-1]:
        ans += p[pre_[i%k-1]] * ((cnt[i%k-1] + 1) // 2)
        cnt[i%k-1] = 1
        pre_[i%k-1] = tt
    else:
        cnt[i%k-1] += 1
else:
    for i in range(k):
        ans += p[pre_[i%k-1]] * ((cnt[i%k-1] + 1) // 2)
print(ans)

Solution (Python):
import re

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


ans = 0

for i in range(k):
    tmp = t[i::k]
    tmp = re.sub('rr','rz',tmp)
    tmp = re.sub('ss','sz',tmp)
    tmp = re.sub('pp','pz',tmp)
    ans += tmp.count('r') * p + tmp.count('s') * r + tmp.count('p') * s
    
print(ans)

Solution (Python):

n, k = map(int, input().split())
r, s, p = map(int, input().split())
hand = sorted([(r, 'r'), (s, 's'), (p, 'p')])
c = input()
ans = list('*'*n)
op = {'r':'p', 's':'r', 'p':'s'}
summ = 0
for i in range(n):
    if i-k >= 0:
        if ans[i-k] != op[c[i]]:
            ans[i] = op[c[i]]
            
            if op[c[i]] == 'r':
                summ += r
            elif op[c[i]] == 's':
                summ += s
            else:
                summ += p
    else:
        ans[i] = op[c[i]]

        if op[c[i]] == 'r':
            summ += r
        elif op[c[i]] == 's':
            summ += s
        else:
            summ += p
print(summ)