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


def main():
    N, K = map( int, input().split())
    rsp = list(map( int, input().split()))
    T = input()
    TmodK = [[] for _ in range(K)]
    for i in range(N):
        if T[i] == 'r':
            TmodK[i%K].append(0)
        elif T[i] == 's':
            TmodK[i%K].append(1)
        elif T[i] == 'p':
            TmodK[i%K].append(2)
    ans = 0
    cnt = N//K
    red = N%K
    s = 0
    for i in range(K):
        if i < red:
            s = 1
        else:
            s = 0
        dp = [[0,0,0] for _ in range(cnt+s)]
        dp[0][(TmodK[i][0] + 2)%3] = rsp[(TmodK[i][0] + 2)%3]
        for j in range(1, cnt+s):
            t = TmodK[i][j]

            win = (t+2)%3
            
            for q in range(3):
                if q == win:
                    dp[j][q] = max(dp[j-1][(q+1)%3] + rsp[win], dp[j-1][(q+2)%3] + rsp[win])
                else:
                    dp[j][q] = max(dp[j-1][(q+1)%3], dp[j-1][(q+2)%3])

        ans += max(dp[cnt+s-1])
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
A=[]
for i in range(N):
  if T[i]=='r':
    A.append('p')
  elif T[i]=='p':
    A.append('s')
  else:
    A.append('r')
for i in range(K,N):
  if A[i]==A[i-K]:
    A[i]='x'
score=0
for i in A:
  if i=='r':
    score+=R
  elif i=='s':
    score+=S
  elif i=='p':
    score+=P
print(score)

Solution (Python):
N, K=map(int, input().split())
R, S, P=map(int, input().split())
T=input()
dp=[[0 for _ in range(N)] for _ in range(3)]
for i in range(N):
  if i>=K:
    dp[0][i]+=max(dp[1][i-K], dp[2][i-K])
    dp[1][i]+=max(dp[0][i-K], dp[2][i-K])
    dp[2][i]+=max(dp[0][i-K], dp[1][i-K])
  dp[0][i]+=R if T[i]=='s' else 0
  dp[1][i]+=S if T[i]=='p' else 0
  dp[2][i]+=P if T[i]=='r' else 0
ans=0
for i in range(N-K, N):
  ans+=max(dp[0][i], dp[1][i], dp[2][i])
print(ans)

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

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

vD = ["X",]*K
res = 0
for i,s in enumerate(T):
	np = vD[i]
	if s == "r":
		p, pt = "P", P
	if s == "s":
		p, pt = "R", R
	if s == "p":
		p, pt = "S", S

	if p != np:
		res += pt
	else:
		p = "X"
	vD.append(p)

print(res)

Solution (Python):
n, k = [ int(v) for v in input().split() ]
r, s, p = [ int(v) for v in input().split() ]
dp_list = [ [0, 0, 0] for i in range(n+1) ]
t = input()
hand = "rsp"
win = [r, s, p]

t_list = [t[i::k] for i in range(k)]
ans = 0
for z in t_list:
    dp_list = [ [0, 0, 0] for i in range(len(z)+1) ]
    for i, v in enumerate(z):
        a = (hand.index(v)-1)%3
        for j in range(3):
            dp_list[i+1][j] = max(dp_list[i][(j+1)%3], dp_list[i][(j-1)%3])
        dp_list[i+1][a] += win[a]

    ans += max(dp_list[-1])

print(ans)

Solution (Python):


import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    R, S, P = i2nn()
    table = {'r': P, 's': R, 'p': S}
    T = list(i2s())
    score = 0
    for i in range(K):
        score += table[T[i]]
    for i in range(K, len(T)):
        if T[i] == T[i - K]:
            T[i] = 'x'
        else:
            score += table[T[i]]
    print(score)
    return

main()

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

def get_point(computer, player):
    if computer == 'r' and player == 'p':
        return P
    if computer == 's' and player == 'r':
        return R
    if computer == 'p' and player == 's':
        return S
    return 0

ans = 0

import math
for j in range(K):
    computer = [T[i] for i in range(j, N, K)]
    C = len(computer)
    dp = [{'r': 0, 's': 0, 'p': 0} for _ in range(C)]

    for player in ('r', 's', 'p'):
        dp[0][player] = get_point(computer[0], player)

    for i in range(1, C):
        dp[i]['r'] = max(dp[i-1]['s'], dp[i-1]['p']) + get_point(computer[i], 'r')
        dp[i]['s'] = max(dp[i-1]['r'], dp[i-1]['p']) + get_point(computer[i], 's')
        dp[i]['p'] = max(dp[i-1]['s'], dp[i-1]['r']) + get_point(computer[i], 'p')

    ans += max(dp[C-1]['r'], dp[C-1]['s'], dp[C-1]['p'])

print(ans)

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



r=0;s=1;p=2;m=3


dp=[[0 for j in range(N)] for i in range(4)]
for j in range(K):
    if T[j]=="r":
        dp[p][j]=P
    elif T[j]=="s":
        dp[r][j]=R
    else:
        dp[s][j]=S
    dp[m][j]=max(dp[r][j],dp[s][j],dp[p][j])
for j in range(K,N):
    for i in range(3):
        dp[i][j]=max(dp[(i+1)%3][j-K],dp[(i+2)%3][j-K])
    if T[j]=="r":
        dp[p][j]+=P
    elif T[j]=="s":
        dp[r][j]+=R
    else:
        dp[s][j]+=S
    dp[m][j]=max(dp[r][j],dp[s][j],dp[p][j])
ans=0
for i in range(K):
    ans+=dp[m][-i-1]
print(ans)

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

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

N, K = rl()
R, S, P = rl()
T = list(rs())
dict = {'r': P, 's': R, 'p': S}
def count(X):
    ret = 0
    cur = None
    for a in X:
        if a != cur:
            ret += dict[a]
            cur = a
        else:
            cur = None
    
    return ret

answer = 0
for i in range(1, K+1):
    x = T[i-1::K]
    answer += count(x)

print(answer)

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


def win(c):
	if c == 'r': return p
	elif c == 's': return r
	elif c == 'p': return s
	return 0

for i, c in enumerate(t):
	tt[i%k].append(c)


score = 0
for ttt in tt:
	preC = 'X'
	for i, c in enumerate(ttt):
		if c != preC:
			score += win(c)
			preC = c
		else:
			preC = 'X'
print(score)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
janken={"r":p,"s":r,"p":s}
aiko=[]
for i in range(n):
    if i<k:
        ans+=janken[t[i]]
        aiko.append(False)
    else:
        if t[i-k]!=t[i] or (t[i-k]==t[i] and aiko[i-k]):
            ans+=janken[t[i]]
            aiko.append(False)
        else: aiko.append(True)
print(ans)

Solution (Python):


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

ten = {'r':p, 's':r, 'p':s}
ans = 0
add = []
for i in range(n):
    if i + 1 > k and t[i - k] == t[i] and add[i - k]:
        add.append(False)
    else:
        ans += ten[t[i]]
        add.append(True)
print(ans)

Solution (Python):
def run(N, K, R, S, P, T):
    ans = 0
    main = ''
    for i in range(len(T)):
        t = T[i]
        if i < K:
            if t == 'r':
                ans += P
                main += 'p'
            elif t == 's':
                ans += R
                main += 'r'
            elif t == 'p':
                ans += S
                main += 's'
        else:
            pre_t = main[i - K]
            if t == 'r' and pre_t != 'p':
                ans += P
                main += 'p'
            elif t == 's' and pre_t != 'r':
                ans += R
                main += 'r'
            elif t == 'p' and pre_t != 's':
                ans += S
                main += 's'
            else:
                main += ' '
    return ans


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


if __name__ == '__main__':
    main()

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

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().rstrip()
point = dict([('r', r), ('s', s), ('p', p)])

def hand(opponent):
    if opponent == 'r':
        return 'p'
    elif opponent == 's':
        return 'r'
    elif opponent == 'p':
        return 's'

def main():
    res = 0
    for i in range(k):
        cur = hand(t[i])
        res += point[cur]
        prev = cur
        for j in range(i+k, n, k):
            cur = hand(t[j])
            if cur == prev:
                cur = 'any'
            else:
                res += point[cur]
            prev = cur
    return res

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

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

r_flg, s_flg, p_flg = [0]*k,[0]*k,[0]*k

r_cnt,s_cnt,p_cnt = 0,0,0

for i in range(n):
	if t[i] == "r":
		if r_flg[i%k] == 0:
			r_cnt += 1
			r_flg[i%k] = 1
			s_flg[i%k] = 0
			p_flg[i%k] = 0
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0
	if t[i] == "s":
		if s_flg[i%k] == 0:
			s_cnt += 1
			r_flg[i%k] = 0
			s_flg[i%k] = 1
			p_flg[i%k] = 0
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0
	if t[i] == "p":
		if p_flg[i%k] == 0:
			p_cnt += 1
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 1
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0

ans = r_cnt*P + s_cnt*R + p_cnt*S
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
points = {'r': p, 's': r, 'p': s}
t = input()
ans = 0
for i in range(k):
  f = t[i::k]
  ans += points[f[0]]
  cnt = 1
  for j in range(1, len(f)):
    if f[j] != f[j-1]:
      ans += points[f[j]]
      cnt = 1
    else:
      cnt += 1
      if cnt % 2 == 1:
        ans += points[f[j]]
print(ans)

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

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

ans = 0
for i in range(K):
    tmp = 1
    for j in range(i+K, N, K):
        if T[j] == T[j-K]:
            tmp += 1
        else:
            tmp = -(-tmp // 2)
            if T[j-K] == "r":
                tmp *= P
            elif T[j-K] == "s":
                tmp *= R
            else:
                tmp *= S
            ans += tmp
            tmp = 1

        if j + K >= N:
            tmp = -(-tmp // 2)
            if T[j] == "r":
                tmp *= P
            elif T[j] == "s":
                tmp *= R
            else:
                tmp *= S
            ans += tmp
    if i + K >= N:
        if T[i] == "r":
            tmp *= P
        elif T[i] == "s":
            tmp *= R
        else:
            tmp *= S
        ans += tmp

print(ans)

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

ans = 0
for i in range(N):
 if(i <= K-1):
   if(T[i] == 'r'):   ans += P
   elif(T[i] == 's'): ans += R
   elif(T[i] == 'p'): ans += S
 else:
   if(T[i-K] != T[i]):
     
     if(T[i] == 'r'): ans += P
     elif(T[i] == 's'): ans += R
     elif(T[i] == 'p'): ans += S
   if(T[i-K] == T[i]):
     T[i] = 'A'
      
print(ans)

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

def reverse_rocks(s):
    if 'r' == s:
        return 'p'
    elif 's' == s:
        return 'r'
    elif 'p' == s:
        return 's'

def except_rocks(s):
    if 'r' == s:
        return ['s','p']
    elif 's' == s:
        return ['r','p']
    elif 'p' == s:
        return ['s','r']

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

sum_score = 0
chechs = []
for i in range(N):
    must_not_char = ''
    if i >= K: 
        flag = True
        for c in chechs[i-K]:
            if c != reverse_rocks(T[i]):
                sum_score += score(reverse_rocks(T[i]))
                chechs.append([reverse_rocks(T[i])])
                flag = False
                break
        if flag:
            chechs.append(except_rocks(reverse_rocks(T[i])))
        continue
            
    sum_score += score(reverse_rocks(T[i]))
    chechs.append([reverse_rocks(T[i])])
            
print(sum_score)

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

for i in range(k):
    t = tlist[i]
    if t == "r":
        score += p
    elif t == "s":
        score += r
    elif t == "p":
        score += s

for i in range(k,n):
    if tlist[i] != tlist[i-k]:
        if tlist[i] == "r":
            score += p
        elif tlist[i] == "s":
            score += r
        elif tlist[i] == "p":
            score += s
    else:
        tlist[i] = "n"

print(score)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

N,K = map(int,input().split(' '))
r,s,p = map(int,input().split(' '))
S = list(input())
ANS = [None]*N

ans = 0
for i in range(K):
    k = S[i]
    if 'r' == k:
        ans += p
        ANS[i] = 'p'
    elif 's' == k:
        ans += r
        ANS[i] = 'r'
    else:
        ans += s
        ANS[i] = 's'

for j in range(K,N):
    k = S[j]
    pre = ANS[j-K]
    if 'r' == k:
        if pre=='p':
            ANS[j] = 0
        else:
            ans += p
            ANS[j] = 'p'
    elif 's' == k:
        if pre=='r':
            ANS[j] = 0
        else:
            ans += r
            ANS[j] = 'r'
    else:
        if pre=='s':
            ANS[j] = 0
        else:
            ans += s
            ANS[j] = 's'
print(ans)

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

r_count = t.count('r') *p
s_count = t.count('s') *r
p_count = t.count('p') *s
tot = r_count + s_count + p_count

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

Solution (Python):
N, K = [int(x) for x in input().split()]
R, S, P = [int(x) for x in input().split()]
T = input()
d = {'r': P, 's': R, 'p': S}
ans = 0
m = set()
for i in range(N):
    t = T[i]
    if i < K:
        ans += d[t]
    else:
        if t == T[i-K]:
            if i-K in m:
                ans += d[t]
            else:
                m.add(i)
        else:
            ans += d[t]
print(ans)

Solution (Python):
def solve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())
    
    ans = 0
    for i in range(K):
        if T[i] == 'r':
            ans += P
        elif T[i] == 's':
            ans += R
        else:
            ans += S
    for i in range(K, N):
        if T[i-K] != T[i]:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R
            else:
                ans += S
        else:
            T[i] = 'n'
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
import collections
import numpy as np
input = sys.stdin.readline
import sys
import collections
import numpy as np
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
R, S, P = [int(x) for x in input().split()]
score = {}
score["r"] = P
score["s"] = R
score["p"] = S
T = list(input().rstrip())
ans = 0
for i in range(K):
    temp = T[i::K]
    ans += score[temp[0]]
    if len(temp) != 1:
        flag = 1
        for j in range(1, len(temp)):
            if temp[j] == temp[j-1]:
                if flag:
                    flag = 0
                else:
                    ans += score[temp[j]]
                    flag = 1
            else:
                ans += score[temp[j]]
                flag = 1
print(ans)

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
l = [[] for i in range(k)]
for i in range(n):
    l[i%k].append(t[i])
ans = 0
for i in range(k):
    a = l[i]
    count = 0
    mae = "a"
    for j in a:
        if mae == j:
            count += 1
        else:
            if count == 0:
                count += 1
            elif count == 1:
                if mae == "r":
                    ans += p
                elif mae == "s":
                    ans += r
                else:
                    ans += s
            else:
                if count % 2 == 0:
                    b = count//2
                else:
                    b = count//2 + 1
                if mae == "r":
                    ans += p*b
                elif mae == "s":
                    ans += r*b
                else:
                    ans += s*b
            mae = j
            count = 1
    if count == 0:
        count += 1
    elif count == 1:
        if mae == "r":
            ans += p
        elif mae == "s":
            ans += r
        else:
            ans += s
    else:
        if count % 2 == 0:
            b = count//2
        else:
            b = count//2 + 1
        if mae == "r":
            ans += p*b
        elif mae == "s":
            ans += r*b
        else:
            ans += s*b
print(ans)

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

katsu = {}
katsu["r"] = "p"
katsu["s"] = "r"
katsu["p"] = "s"

score = {}
score["r"] = r
score["s"] = s
score["p"] = p

jibuns = ""
out_score = 0

jibuns = [katsu[aite] for aite in t]
out_score = 0
for i, jibun in enumerate(jibuns):
    if i >= k and jibuns[i-k] == jibun:
        nokoris = ["r", "s", "p"]
        nokoris.remove(jibun)
        if i+k <= n-1:
            if jibuns[i+k] != jibun:
                nokoris.remove(jibuns[i+k])
        jibun = nokoris[0]
        jibuns[i] = jibun
    else:
        out_score += score[jibun]


print(out_score)

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

c = {'r': P, 's': R, 'p': S}
ans = 0
for j in range(K):
    for i in range(j, N, K):
        if i == j:
            con = 1
            ans += c[T[i]]
        elif T[i] == T[i - K]:
            con += 1
            if con % 2 == 1:
                ans += c[T[i]]
        else:
            con = 1
            ans += c[T[i]]

print(ans)

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

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
c=0
for i in range(k):
    z=groupby(y[i])
    l=len(z)
    for j in range(l):
        c+=(x[z[j][0]]*((z[j][1]+1)//2))
print(c)

Solution (Python):
def point(m):
    if m=="r":
        return p
    elif m=="s":
        return r
    else:
        return s

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = str(input())
q = [ [i] for i in range(k)]
for i in range(k,n):
    q[i%k].append(i)
ans = 0
for i in range(k):
    j = 0
    while j<len(q[i]):
        if j==len(q[i])-1:
            ans += point(t[q[i][j]])
            j+=1
        else:
            if t[q[i][j]] == t[q[i][j+1]]:
                ans += point(t[q[i][j]])
                j +=2
            else:
                ans += point(t[q[i][j]])
                j +=1
print(ans)

Solution (Python):
LOCAL = True and False


def dprint(*args):
  if LOCAL:
    print(*args)


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

dprint(t)

ans = 0

for i in range(k, n + k):
  x = t[i]
  dprint(i, x)
  j = i - k
  if x == 'r':
    dprint('t[j]', '=', t[j])
    dprint('x', '=', x)
    if t[j] != x:
      ans += p
    else:
      t = t[:i] + '*' + t[i + 1:]
  if x == 's':
    if t[j] != x:
      ans += r
    else:
      t = t[:i] + '*' + t[i + 1:]
  if x == 'p':
    if t[j] != x:
      ans += s
    else:
      t = t[:i] + '*' + t[i + 1:]
  dprint(t)
  dprint(ans)

print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
t=list(input())
q=['q']*K
t=t+q

s=0
for i in range(K,N+K):
    if t[-i-1] == t[-i-1+K]:
        t[-i-1]='q'
        continue
    elif t[-i-1] =='r':
        s+=P
    elif t[-i-1] =='s':
        s+=R
    elif t[-i-1] =='p':
        s+=S


print(s)

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

import numpy as np

score = np.array([0, R, S, P], dtype=int)
M = (1 + (N-1)//K)
addi = M * K - N
t = np.array([{'n':0, 's':1, 'p':2, 'r':3}[i] for i in T+'n'*addi], dtype=int)

t = t.reshape(M,K).T

for i in range(K):
    for j in range(M):
        if j!=0 and t[i,j] == t[i,j-1]:
            t[i,j] = 0

print(score[t].sum())

Solution (Python):
N, K=map(int, input().split())
R, S, P=map(int, input().split())
T=input()
dp=[[0 for _ in range(N)] for _ in range(3)]
for i in range(N):
  if i>=K:
    dp[0][i]+=max(dp[1][i-K], dp[2][i-K])
    dp[1][i]+=max(dp[0][i-K], dp[2][i-K])
    dp[2][i]+=max(dp[0][i-K], dp[1][i-K])
  dp[0][i]+=R if T[i]=='s' else 0
  dp[1][i]+=S if T[i]=='p' else 0
  dp[2][i]+=P if T[i]=='r' else 0
ans=0
for i in range(N-K, N):
  ans+=max(dp[0][i], dp[1][i], dp[2][i])
print(ans)

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

def cal(t):
    if t=="r":
        return (t,P)
    elif t=="s":
        return (t,R)
    elif t=="p":
        return (t,S)
point = 0
for i in range(K):
    count = i
    data = []
    while count < N:
        data.append(cal(T[count]))
        count += K
    n = "q"
    for j in data:
        if n != j[0]:
            point += j[1]
            n = j[0]
        else:
            n = "q"

print(point)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
A=[[] for i in range(k)]
for i in range(k):
  A[i]=[]
for i in range(n):
  A[i%k].append(t[i])
ans=0
for i in range(k):
  a=0
  for j in range(len(A[i])):
    if j==0:
      if A[i][j]=="r":
        ans=ans+p
      elif A[i][j]=="s":
        ans=ans+r
      else:
        ans=ans+s
    else:
      if A[i][j]!=A[i][j-1]:
        a=0
        if A[i][j]=="r":
          ans=ans+p
        elif A[i][j]=="s":
          ans=ans+r
        else:
          ans=ans+s
      else:
        a=a+1
        if a%2==0:
          if A[i][j]=="r":
            ans=ans+p
          elif A[i][j]=="s":
            ans=ans+r
          else:
            ans=ans+s
print(ans)

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

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

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

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

checks=[False for i in range(N)]
hand=[]

index=0
output = []
for i in range(N):
    if T[i]=="r":
        output.append("p")
    elif T[i]=="s":
        output.append("r")
    else:
        output.append("s")

sums =0
for i in range(len(output)):
    if i <K:
        sums += dicts[output[i]]
        checks[i]=True
    else:
        if output[i]==output[i-K]:
            if checks[i-K]!=True:
                sums += dicts[output[i]]
                checks[i]=True

        else:
            sums += dicts[output[i]]
            checks[i]=True


print(sums)

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

Solution (Python):
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):
    tt = t[i:n:k]    
    old="x"
    c=1
    for ttt in tt:
        if ttt!=old:
            ans+=score[ttt]
            c=1
        else:
            if c==1:
                c=-1
            else:
                c=1
                ans+=score[ttt]
        old = ttt
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
dicta = {'r':P,'s':R,'p':S}
T = input()
count = 0
 
for i in range(N):
    if i >= K:
        if T[i-K] == T[i]:
            T = T[:i] + 'z' + T[i+1:]
            
        else:
            count += dicta[T[i]]
    else:
        count += dicta[T[i]]
print(count)

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


def janken(x):
    if x == "r":
        return P
    elif x == "s":
        return R
    else:
        return S


ans = 0
for i in range(K):
    ans += janken(T[i])
flg = [True] * N
for i in range(K, N):
    if T[i - K] == T[i] and flg[i - K] == True:
        flg[i] = False
        continue
    else:
        ans += janken(T[i])
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for j in range(k):
    pre = 'no'
    for i in range(n - 1 - j, -1, -k):
        score = 0
        if t[i] == 'r' and pre != 'p':
            score = p
            pre = 'p'
        if t[i] == 's' and pre != 'r':
            score = r
            pre = 'r'
        if t[i] == 'p' and pre != 's':
            score = s
            pre = 's'
        if score == 0:
            pre = 'no'
        ans += score
print(ans)

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

ans = 0
te = ["" for _ in range(n)]
for i in range(n):
	if t[i] == "r" and te[i - k] != "p":
		ans += p
		te[i] = "p"
	elif t[i] == "s" and te[i - k] != "r":
		ans += r
		te[i] = "r"
	elif t[i] == "p" and te[i - k] != "s":
		ans += s
		te[i] = "s"

print(ans)

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

ans = ["" for _ in range(K)]
for i in range(N):
    ans[i%K] += T[i]

score = ""

for a in ans:
    for i in range(len(a)):
        if i == 0:
            score += a[i]
            continue
        else:
            if a[i] == score[-1]:
                score += "?"
            else:
                score += a[i]
    score += "?"
final = 0

for i in range(len(score)):
    if score[i] == "r":
        final += P
    if score[i] == "p":
        final += S
    if score[i] == "s":
        final += R

print(final)

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

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

ans = 0
for i in range(K):
    tmp = T[i::K]
    b_hand = T[i]
    s = 1
    cum = []

    for j in range(1,len(tmp)):
        if b_hand == tmp[j]:
            s += 1
        else:
            cum.append([s, b_hand])
            s = 1
            b_hand = tmp[j]
    cum.append([s, b_hand])

    for j in range(len(cum)):
        if cum[j][0] == 1:
            ans += win_score_each_op_hand[cum[j][1]]
        elif cum[j][0] % 2 == 1:
            ans += win_score_each_op_hand[cum[j][1]] * (-(-cum[j][0] // 2))
        else:
            ans += win_score_each_op_hand[cum[j][1]] * (cum[j][0] // 2)

print(ans)

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

def janken(s):
  if s == "r":
    return P
  if s == "s":
    return R
  if s == "p":
    return S

answer = 0
for i in range(K):
  j = 0
  flag = 0
  while i + K * j < N:
    if j == 0:
      answer += janken(T[i + K * j])
    else:
      if T[i + K * j] != T[i + K * (j - 1)] or flag == 1:
        answer += janken(T[i + K * j])
        flag = 0
      else:
        flag = 1
    
    j += 1
print(answer)

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

print(ans)