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())

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

wins = []

ans = 0
for i, pose in enumerate(T):
    if i < K:
        wins.append('win')
        ans += pose2point[pose]
    else:
        pre_pose = T[i-K]
        if wins[i-K] == 'win':
            if pose == pre_pose:
                wins.append('lose')
            else:
                wins.append('win')
                ans += pose2point[pose]
        else:
            wins.append('win')
            ans += pose2point[pose]
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
def point(hand):
  if(hand == "r"):
    return P
  elif(hand == "s"):
    return R
  else:
    return S
ans = 0
lose = [0] * N
for i in range(K):
  ans += point(T[i])
for i in range(K,N):
  if((T[i] != T[i-K]) or (lose[i-K] == 1)):
    ans += point(T[i])
  else:
    lose[i] = 1
print(ans)

Solution (Python):
n,k=list(map(int,input().split()))
r,s,p=list(map(int,input().split()))
t=input()
vic=""
ans=0
for i in t:
    if i=="r":
        vic+="p"
    if i=="p":
        vic+="s"
    if i=="s":
        vic+="r"

for i in vic[:k]:
    if i=="r":
        ans+=r
    elif i=="p":
        ans+=p
    elif i=="s":
        ans+=s


for i in range(k,n):
    
    if vic[i]==vic[i-k]:
        
        if not i==n-1:
            vic=vic[:i]+'n'+vic[i+1:]
    else:
        
        if vic[i]=="r":
            ans+=r
        elif vic[i]=="p":
            ans+=p
        elif vic[i]=="s":
            ans+=s
        else:
            pass
            

    

print(ans)

Solution (Python):
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = list(input())
sc = 0
for i in range(n):
  if i+k < n and t[i] == t[i+k]:
    t[i+k] = 'x'
print(t.count('r')*p + t.count('p')*s + t.count('s')*r)

Solution (Python):
[n,k]=input().split()
[r,s,p]=input().split()
t=input()
n=int(n)
k=int(k)
r=int(r)
s=int(s)
p=int(p)
r_miss=0
s_miss=0
p_miss=0
wl='w'
for i in range(1,k):
    wl=wl+'w'

for i in range(k,n):
    if t[i]==t[i-k] and wl[i-k]=='w':
        wl=wl+'l'
        if t[i]=='r':
            r_miss=r_miss+1
        elif t[i]=='s':
            s_miss=s_miss+1
        elif t[i]=='p':
            p_miss=p_miss+1
    else:
        wl=wl+'w'
            
score=(t.count('r')-r_miss)*p+(t.count('s')-s_miss)*r+(t.count('p')-p_miss)*s
print(score)

Solution (Python):
n, k = map(int, input().split())
scores = list(map(int, input().split()))
t = [0 if i=="r" else (1 if i=="s" else 2) for i in input()]

score = 0
no = [-1 for i in t]

for i, x in enumerate(t):
	if no[i] != x:
		score += scores[x-1]
		try:	
			no[i+k] = x
		except:
			pass
	
print(score)

Solution (Python):
def solve():
    from sys import stdin
    f_i = stdin
    
    N, K = map(int, f_i.readline().split())
    R, S, P = map(int, f_i.readline().split())
    T = f_i.readline().rstrip()
    
    pair = {'r': 0, 's': 1, 'p': 2}
    point = {1: R, 2: S, 0: P}
    
    score = sum(point[pair[c]] for c in T) 
    
    for k in range(K):
        H = [pair[h] for h in T[k::K]] + [None]
        pre_h = None
        for h, next_h in zip(H, H[1:]):
            if h == pre_h:
                score -= point[h]
                h = (h + 1) % 3
                if h == next_h:
                    h = (h + 1) % 3
            pre_h = h
    
    print(score)

solve()

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

total = 0

for i in range(n):
    if i < k:
        if t[i] == "r":
            total += p
        elif t[i] == "s":
            total += r
        else:
            total += s
    else: 
        if t[i-k] == t[i]:
            t[i] = "."
        else:
            if t[i] == "r":
                total += p
            elif t[i] == "s":
                total += r
            else:
                total += s

print(total)

Solution (Python):
def hand_to_win(hand):
    if hand == 'r':
        return 'p'
    elif hand == 'p':
        return 's'
    elif hand == 's':
        return 'r'


def main():
    """
    r,s,pからスコアの大きい順に手を決めていく。
    kだけ前の値がかぶったらk後の手を邪魔しない手を選ぶ。
    """
    n, k = map(int, input().split(' '))
    r, s, p = map(int, input().split(' '))
    t = (input())

    score_map = {'r': r, 's': s, 'p': p}
    history = []

    score = 0

    for i, hand in enumerate(t):
        tobe_win = hand_to_win(hand)
        if 0 <= i - k and history[i - k] == tobe_win:
            h = set(['r', 'p', 's']) - set([tobe_win])
            if i + k <= n - 1:
                h -= set([hand_to_win(t[i + k])])
            history.append(h.pop())
            continue
        history.append(tobe_win)
        score += score_map[tobe_win]
    print(score)


main()

Solution (Python):

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

ans = 0
my_hand = list() 

for i in range(N):
    
    if i >= K:
        
        
        if T[i] != my_hand[i-K]:
            if T[i] == "r":
                ans += P
                my_hand.append("r")
            elif T[i] == "p":
                ans += S
                my_hand.append("p")
            elif T[i] == "s":
                ans += R
                my_hand.append("s")
        else:
            my_hand.append("?")
            
    else:
        
        if T[i] == "r":
            ans += P
            my_hand.append("r")
        elif T[i] == "p":
            ans += S
            my_hand.append("p")
        elif T[i] == "s":
            ans += R
            my_hand.append("s")
        
print(ans)

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

score = {'r': p, 's': r, 'p': s, 'l': 0}

ans = 0
for i in range(n):
    ans += score[t[i]]
    if i + k < n and t[i] == t[i + k]:
        t[i + k] = 'l'

print(ans)

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

dic = dict([('s',r),('p',s),('r',p)])


ans = 0
for i in range(k):
    A = [t[j] for j in range(i,n,k)]
    flag = A[0]
    ans += dic[flag]
    for j in range(1,len(A)):
        if A[j]==flag:
            if j!=len(A)-1:
                flag = (set(['r','s','p'])-set(A[j+1])).pop()
        else:
            ans += dic[A[j]]
            flag = A[j]
print(ans)

Solution (Python):
x,y=map(int,input().split())
a,b,c=map(int,input().split())
n=list(input())
s=0
for i in range(y):
    if n[i]=="r":
        s+=c
    elif n[i]=="s":
        s+=a
    else:
        s+=b
for i in range(y,x):
    if n[i-y]==n[i]:
        n[i]="!"
    else:
        if n[i]=="r":
            s+=c
        elif n[i]=="s":
            s+=a
        elif n[i]=="p":
            s+=b
    
print(s)

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

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

for i in range(N):
    if i<K:
        ans += score[T[i]]
    else:
        if T[i]!=T[i-K]:
            ans += score[T[i]]
        else:
            T[i] = '?'
            
print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 100)

def dfs(i, type_):
    if i >= N:
        return 0
    if dp[i][type_] != -1:
        return dp[i][type_]
    ret = 0
    for t in range(3):
        if t == type_:
            continue
        ret = max(ret, dfs(i + K, t))
    dp[i][type_] = ret + ((type_ + 1) % 3 == T[i]) * score[type_]
    return dp[i][type_]


N, K = map(int, input().split())
score = [int(x) for x in input().split()]
dic = {"r": 0, "s": 1, "p": 2}
T = [dic[x] for x in input()]
dp = [[-1] * 3 for _ in range(N)]
ans = 0
for i in range(K):
    ans += max(dfs(i, t) for t in range(3))
print(ans)

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

def point(op):
    if op == 'r':
        return p
    elif op == 's':
        return r
    else:
        return s

res = 0
for mod in range(k):
    len = (n-mod+k-1) // k
    dp_free = [0] * len
    dp_win = [0] * len
    dp_win[0] = point(t[mod])
    for i in range(1, len):
        dp_free[i] = max(dp_win[i-1], dp_free[i-1])
        dp_win[i] = dp_free[i-1] + point(t[mod+k*i])
        if t[mod+k*i] != t[mod+k*(i-1)]:
            dp_win[i] = max(dp_win[i], dp_win[i-1] + point(t[mod+k*i]))
    res += max(dp_win[-1], dp_free[-1])
print(res)

Solution (Python):
def main():
    N,K = map(int,input().split())
    R,S,P = map(int,input().split())
    dic = {"r":P,"s":R,"p":S}
    T = list(input())
    ans = 0
    for i in range(K):   
        for j in range(i,N,K):
            if j == i:
                ans += dic[T[j]]
            else:
                if T[j-K]==T[j]:
                    T[j]="x"
                else:
                    ans += dic[T[j]]
    print(ans)
        
if __name__ == '__main__':
    main()

Solution (Python):
def main():
  N, K = [int(i) for i in input().split()]
  R, S, P = [int(i) for i in input().split()]
  T = input()
  point = {"r":P, "s":R, "p":S}
  ans = 0
  
  for i in range(K):
    last = ''
    j = i
    while j<N:
      aite = T[j]
      if aite!=last:
        ans = ans+point[aite]
        last = aite
      else:
        last = ''
      j = j+K
   
  print(ans)
  
if __name__ == "__main__":
  main()

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

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

pt={"r":P,"s":R,"p":S}
for i in range(N):
    idx[T[i]].append(i)
idxlst={"r":set(idx["r"]),"s":set(idx["s"]),"p":set(idx["p"])}

value=0
for t in idx:
    for i in range(len(idx[t])-1,-1,-1):
        if idx[t][i] in idxlst[t]:
            a=idx[t][i]-K
            if a in idxlst[t]:
                idxlst[t].remove(a)
    value+=pt[t]*len(idxlst[t])
print(value)

Solution (Python):



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

point = 0

judge = {"r": R, "s": S, "p": P}
skip = [False] * N

for i in range(N):
  if i < K:
    if T[i] == "r":
      point += judge["p"]
    elif T[i] == "s":
      point += judge["r"]
    elif T[i] == "p":
      point += judge["s"]
  else:
    
    if T[i-K] != T[i] or skip[i-K]:
      if T[i] == "r":
        point += judge["p"]
      elif T[i] == "s":
        point += judge["r"]
      elif T[i] == "p":
        point += judge["s"]
    else:
      skip[i] = True
      
print(point)

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

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

ans_str = list(ans_str)
for i in range(k, n):
    if ans_str[i] == ans_str[i-k]:
        ans_str[i] = 'z'

pnt = 0
for i in range(n):
    if ans_str[i] == 'r':
        pnt += R
    elif ans_str[i] == 's':
        pnt += S
    elif ans_str[i] == 'p':
        pnt += P

print(pnt)

Solution (Python):
N,K=map(int,input().split())
p=[int(x) for x in input().split()]
T=[-1]*K+[0 if x=='r' else (1 if x=='s' else 2) for x in input()]

s=0
for i in range(K,N+K):
  if T[i]==T[i-K]:
    T[i]=-1
  else:
    s+=p[(T[i]+2)%3]

print(s)

Solution (Python):
n=list(map(int,input().split()))
score=list(map(int,input().split()))
s=input()

def rscore(ss):
  if ss=='r':
    return score[2]
  elif ss=='s':
    return score[0]
  elif ss=='p':
    return score[1]

result=0
flg=[]
for i in range(n[1]):
  result+=rscore(s[i])
  flg.append(True)
for i in range(n[1],len(s)):
  if(s[i-n[1]]==s[i] and flg[i-n[1]]):
    flg.append(False)
    continue
  else:
    flg.append(True)
    result+=rscore(s[i])
print(result)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
d = {"r":p, "s":r, "p":s}
t = list(input())
dp = [0]*n
for i in range(n):
  if i>=k and t[i]==t[i-k]:
    dp[i] = 0
    t[i] = "x"
  else:
    dp[i] = d[t[i]]
print(sum(dp))

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

ans = 0

for i in range(N):
    if i < K:
        ans += d[T[i]]
    else:
        if T[i] != T[i - K]:
            ans += d[T[i]]
        else:
            T[i] = 'z'

print(ans)

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

B = list(map(int, input().split()))

T = list(map(lambda c: 0 if c == 'r' else 1 if c == 's' else 2, list(input())))





dp = [[0 for c in range(3)] for i in range(N + 1)]

for i in range(1,N+1):
    for c in range(3):
        bonus = B[c] if (T[i - 1] - c) % 3 == 1 else 0

        if i <= K:
            dp[i][c] = bonus
        else:
            dp[i][c] = bonus + max(dp[i - K][(c + 1) % 3], dp[i - K][(c + 2) % 3])

print(sum([max(dp[i]) for i in range(N - K + 1, N + 1)]))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K
memo = [{} for _ in range(K)]

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

def okjudge(m, ss, p):
    if p in memo[m]:
        return memo[m][p]

    lss = len(ss)
    if p == lss:
        ret =  0
        memo[m][p] = ret
        return ret

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            ret = P
        elif c == 's':
            ret =  R
        else:
            ret =  S
        memo[m][p] = ret
        return ret

    if ss[p + 1] == ss[p]:
        if c == 'r':
            ret =  max(okjudge(m, ss, p + 1), P + okjudge(m, ss, p + 2))
        elif c == 's':
            ret =  max(okjudge(m, ss, p + 1), R + okjudge(m, ss, p + 2))
        else:
            ret =  max(okjudge(m, ss, p + 1), S + okjudge(m, ss, p + 2))
    elif c == 'r':
        ret =  P + okjudge(m, ss, p + 1)
    elif c == 's':
        ret =  R + okjudge(m, ss, p + 1)
    else:
        ret =  S + okjudge(m, ss, p + 1)

    memo[m][p] = ret
    return ret

print(sum([okjudge(mi, v, 0) for mi,v in enumerate(l)]))

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

dic = {'r':p, 's':r, 'p':s}
score = 0
for i in range(k):
    flg = True
    for j in range(i, n-k, k):
        if t[j]==t[j+k] and flg:
            score -= dic[t[j]]
            flg = False
        else:
            flg = True

for i, j in dic.items():
    score += t.count(i)*j
print(score)

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

score = 0
back = 0

for fst in range(K):
  index = fst
  while True:
    ch = T[index]
    if ch != back:
      back = ch
      if ch == "r":
        score += P
      elif ch == "p":
        score += S
      else:
        score += R
    else:
        back = 0
    
    index += K
    if index >= N:
      break
  back = 0
  
print(score)

Solution (Python):
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=input()
ans=0
for i in range(k):
  u=t[i::k]
  u=u.replace("rr", "rx")
  u=u.replace("pp", "px")
  u=u.replace("ss", "sx")
  ans+=u.count("s")*r
  ans+=u.count("p")*s
  ans+=u.count("r")*p
print(ans)

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

win_hand = {"r":"p", "s":"r", "p":"s"}
lose_hand = {"r":"s", "s":"p", "p":"r"}
score_hand = {"r":R, "s":S, "p":P}

score = 0

for k in range(K):
  n = k
  preb_hand = ""
  while n < N:
    if win_hand[T[n]] != preb_hand:
      score = score + score_hand[win_hand[T[n]]]
      preb_hand = win_hand[T[n]]
    else:
      if (n + K) < N:
        preb_hand = lose_hand[T[n + K]]
      else:
        preb_hand = ""
    n = n + K

print(score)

Solution (Python):
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p'               
print(s)

Solution (Python):
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1


score=0

for i in range(K):
        mem=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                score+=P
                                mem ='r'
                        elif n[j-K]=='r' and mem=='r': mem =""
                        else:
                                score+=P
                                mem ='r'
                elif n[j] =='s':
                        if j < K:
                                score+=R
                                mem ='s'
                        elif n[j-K]=='s' and mem=='s': mem =""
                        else:
                                score+=R
                                mem ='s'
                elif n[j] =='p':
                        if j < K:
                                score+=S
                                mem ='p'
                        elif n[j-K]=='p' and mem=='p': mem =""
                        else:
                                score+=S
                                mem ='p'
                        
print(score)

Solution (Python):
n, k = (int(x) for x in input().split())
r, s, p = (int(x) for x in input().split())
t = input()
list = []
for i in range(k):
    i = int(i)
    list.append(t[i::k])
    cr = 0
    cs = 0
    cp = 0
for i in range(k):
    i = int(i)
    list[i] = list[i].replace("rr","rx")
    list[i] = list[i].replace("ss","sx")
    list[i] = list[i].replace("pp","px")

    cr += list[i].count("r")
    cs += list[i].count("s")
    cp += list[i].count("p")

print(cr*p + cs*r + cp*s)

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

ans = 0
for i in range(K):
    scorer = 0
    prev = ''
    for j in range(i, N, K):
        if T[j] == "r":
            if prev != 'p':
                scorer += P
                prev = 'p'
            else:
                prev = ''
        elif T[j] == "s":
            if prev != 'r':
                scorer += R
                prev = 'r'
            else:
                prev = ''
        elif T[j] == "p":
            if prev != 's':
                scorer += S
                prev = 's'
            else:
                prev = ''
    ans += scorer

print(ans)

Solution (Python):
input1 = input()
input2 = input()
input3 = input()

N = int(input1.split()[0])
K = int(input1.split()[1])
R = int(input2.split()[0])
S = int(input2.split()[1])
P = int(input2.split()[2])
T = []
for i in range(N):
    T.append(input3[i])



for i in range(N):
    if T[i] == "r":
        T[i] = "P"
    if T[i] == "s":
        T[i] = "R"
    if T[i] == "p":
        T[i] = "S"



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

print(R*(T.count("R")) + S*(T.count("S")) + P*(T.count("P")))

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

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

bef = [-1 for i in range(k)]

ans = 0
for i in range(k):
    ans += score[txt[i]]
    bef[i] = txt[i]



for i in range(k,n):
    sup = i%k
    if bef[sup] != txt[i]:
        ans += score[txt[i]]
        bef[sup] = txt[i]
    else:
        bef[sup] = -1

print(ans)

Solution (Python):
def win(hand):
        if hand == 'r':
            return 'p'
        elif hand == 'p':
            return 's'
        else:
            return 'r'

def get_best_hand(hand, last_hand):
    won_hand = win(hand)
    if won_hand == last_hand:
        return 'draw'
    else:
        return won_hand
    
def get_point(hand, R, S, P):
        if hand == 'r':
            return R
        elif hand == 'p':
            return P
        elif hand == 's':
            return S
        else:
            return 0

K = int(input().split()[1])
R, S, P = map(int, input().split())
T = input()

last_hand = ''
history = []
point = 0

for t in T:
    last_hand = history[0] if len(history) >= K else ''
    best_hand = get_best_hand(t, last_hand)
    point += get_point(best_hand, R, S, P)
    history.append(best_hand)
    if len(history) > K:
        del history[0]

print(point)

Solution (Python):

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

l = [""]*k
for i in range(n):
    l[i%k] += t[i]

res = 0
for x in l:
    prev = ""
    score = 0
    for i in range(len(x)):
        if x[i]=="r":
            if prev!="p":
                score += p
                prev = "p"
            else:
                prev = ""
        elif x[i]=="s":
            if prev!="r":
                score += r
                prev = "r"
            else:
                prev = ""
        else:
            if prev!="s":
                score += s
                prev = "s"
            else:
                prev = ""
    res += score

print(res)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
janken = ['' for i in range(K)]
for i in range(N):
    amari = i % K
    janken[amari] = janken[amari] + T[i]

count = 0
for i in range(K):
    lenN = len(janken[i])
    now = 1
    jan = janken[i][0]
    for j in range(1,lenN):
        if janken[i][j-1] == janken[i][j]:
            now += 1
        elif janken[i][j-1] != janken[i][j]:
            sho = (now + 1) // 2
            if jan == 'r':
                count += sho * P
            elif jan == 's':
                count += sho * R
            elif jan == 'p':
                count += sho * S
            now = 1
            jan = janken[i][j]
    sho = (now + 1) // 2
    if jan == 'r':
        count += sho * P
    elif jan == 's':
        count += sho * R
    elif jan == 'p':
        count += sho * S
print(count)

Solution (Python):
n, k = map(int, input().split())
R, S, P = map(int, input().split())
t = input()
 
dp = [0] * n
dic = {'r': P, 's': R, 'p': S}
 
for i in range(n):
  if i >= k and (t[i-k] == t[i]) and dp[i-k] != 0:
    continue
  dp[i] = dic[t[i]]
 
print(sum(dp))

Solution (Python):

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


tlist = [[] for _ in range(k)]
for i in range(k):
    j = i
    while j < n:
        tlist[i].append(t[j])
        j += k

ans = 0    
for l in tlist:
    pt = ""
    pr = True
    for i in l:
        if i == pt and pr == True:
            pr = False
        else:
            pr = True
            if i == "r": ans += p
            elif i == "s": ans += r
            elif i == "p": ans += s
        pt = i


print(ans)

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

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

  if k <= i:
    hantei = t[i-k]

  if t[i] != hantei:
    if t[i] == 'r':
      point += r
    elif t[i] == 's':
      point += s
    elif t[i] == 'p':
      point += p
  else:
    t[i] = 'x'

print(point)

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

def score(T):
    sa = 0
    x = 0
    for i in range(len(T)):
        if x == 1 and T[i] == T[i-1] :
            if T[i] == "r":
                sa += P
                x = 0
            elif T[i] == "s":
                sa += R
                x = 0
            elif T[i] == "p":
                sa += S
                x = 0
            
        elif i > 0 and T[i] == T[i-1] :
            x = 1
        elif T[i] == "r":
            sa += P
            x = 0
        elif T[i] == "s":
            sa += R
            x = 0
        elif T[i] == "p":
            sa += S
            x = 0
    return sa
ans = 0
for k in range(K):
    ans += score(T[k::K])

print(ans)

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

def takahashiKun(t_i, cnt):
    if cnt>=k and t[cnt-k]==t_i:
        t[cnt] = "X"
        return int(0)
    else:
        if   t_i=="p": return s
        elif t_i=="r": return p
        else         : return r

score = 0
for cnt, t_i in enumerate(t):
    score += takahashiKun(t_i, cnt)

print(score)

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

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

res = 0

for i in range(k):
    dp = [0,0,0]
    idx = i
    while idx < n:
        dp[0],dp[1],dp[2] = max(dp[1],dp[2]),max(dp[0],dp[2]),max(dp[0],dp[1])
        if t[idx] == 's':
            dp[0] += r
        elif t[idx] == 'p':
            dp[1] += s
        else:
            dp[2] += p
        idx += k
    res += max(dp)
print(res)

Solution (Python):
def main():
    n,k = list(map(int,input().split()))

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

    t = list(input())
    ans = 0
    for i in range(len(t)):
        if i < k:
            if t[i] == "r":

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

    print(ans)
if __name__ == "__main__":
    main()

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

point = 0
histry = []

for i in range(N):
    if i >= K and T[i] == histry[i-K]:
        
        histry.append(N)
        continue
    else:
        if T[i] == 'r':
            point += P
            histry.append('r')
            
        elif T[i] == 's':
            point += R
            histry.append('s')
            
        elif T[i] == 'p':
            point += S
            histry.append('p')
            

print(point)

Solution (Python):


def resolve():
    N,K=(int(i) for i in input().split())
    R,S,P=(int(i) for i in input().split())
    pdic={'r':P,'s':R,'p':S}
    T=list(input())
    points=sum([pdic[i] for i in T[:K]])
    for i in range(K):
        for j in range(i+K,N,K):
            if T[j]!=T[j-K]:
                points+=pdic[T[j]]
            else:
                T[j]='x'
    print(points)


if __name__ == "__main__":
    resolve()

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

ans = 0
win = [1] * N

for i in range(N):
  if T[i] == "r":
    ans += P
    x = P
  elif T[i] == "s":
    ans += R
    x = R
  else:
    ans += S
    x = S
  if i >= K:
    if (T[i] == T[i - K]) and (win[i - K] == 1):
      ans -= x
      win[i] = 0
  
print(ans)

Solution (Python):
n,k = map(int,input().split())
point = list(map(int,input().split()))
t = input().translate(str.maketrans('rsp','201'))
c = 0
for i in range(k):
  dp = [0]*3
  for j in range(i,n,k):
    pdp,dp = dp,[0]*3
    for l in range(3):
      if l == int(t[j]):
        dp[l] = max(pdp[(l+1)%3],pdp[(l+2)%3]) + point[l]
      else:
        dp[l] = max(pdp[(l+1)%3],pdp[(l+2)%3])
  c += max(dp)
print(c)