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(lambda x: int(x), input().split())
r, s, p = map(lambda x: int(x), input().split())
t = input()

rsp = [[r, 's'], [s, 'p'], [p, 'r']]
rsp.sort()
rsp.reverse()

sum = 0
t_list = [t[i] for i in range(len(t))]
temp_list = [t[i] for i in range(len(t))]
for hand in rsp:
  for i, _ in enumerate(t_list):
    if hand[1] == _:
      if i >= k and temp_list[i-k] != hand[1]:
        sum += hand[0]
      elif i < k:
        sum += hand[0]
      else:
        temp_list[i] = 'a'
print(sum)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
A = []
ans = 0
for j in range(k):
    A.append([])
for i in range(n):
    A[i%k].append(t[i])
for l in range(k):
    for t in range(1,len(A[l])):
        if A[l][t] == A[l][t-1]:
            A[l][t] = 0
    for t in range(len(A[l])):
        if A[l][t] == "r":
            ans += p
        elif A[l][t] == "s":
            ans += r
        elif A[l][t] == "p":
            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(k,n):
  if t[i]==t[i-k]:
    t[i]="a"
print((t.count("r")*p)+(t.count("s")*r)+(t.count("p")*s))

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
point=0
l=[]
point_list=[r,s,p]
kind=['r','s','p','r']
for i in range(n):
  for j in range(len(kind)-1):
    if t[i]==kind[j]:
      if i-k>=0 and t[i]==kind[l[i-k]+1]:
        if i+k<n and t[i+k]==kind[j+1]:
          if j==2:j=-1
          l.append(j+1)
        else:
          l.append(j)
      else:
        point+=point_list[j-1]
        if j==0:j=3
        l.append(j-1)
      break
print(point)

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

score = {"r": P, "s": R, "p": S}
for i in range(K):
      result += score[T[i]]
for i in range(K, N):
      if T[i] == T[i-K]:
            T[i] = ""
            continue
      else:
            result += score[T[i]]

print(result)

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

for i in T:
  if i == "r":
    my_T.append("p")
  elif i == "s":
    my_T.append("r")
  elif i == "p":
    my_T.append("s")

for i in range(K, N):
      if my_T[i] == my_T[i-K]:
            if i+K < N:
                  if my_T[i+K] == "r":
                        my_T[i] = "s"
                  elif my_T[i+K] == "s":
                        my_T[i] = "p"
                  elif my_T[i+K] == "p":
                        my_T[i] = "r"
            else:
                  if my_T[i-K] == "r":
                        my_T[i] = "s"
                  elif my_T[i-K] == "s":
                        my_T[i] = "p"
                  elif my_T[i-K] == "p":
                        my_T[i] = "r"
      else:
        if my_T[i] == "r":
              result += R
        elif my_T[i] == "s":
              result += S
        elif my_T[i] == "p":
              result += P

for i in range(K):
      if my_T[i] == "r":
            result += R
      elif my_T[i] == "s":
            result += S
      elif my_T[i] == "p":
            result += P


print(result)

Solution (Python):
def getWinPoint(c):
    if(c == "r"):
        return P
    elif(c == "p"):
        return S
    elif(c == "s"):
        return R
    else:
        return 0
    return 0

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

ans = 0
hands = ""
for n in range(N):
    if(K <= n and T[n] == hands[n-K]):
        hands += "_"
        continue    
    ans += getWinPoint(T[n])
    hands += T[n]
print(ans)

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

hands = "rsp"

def f(t):
    n = len(t)
    if n == 0:
        return 0
    dp = [[-1 for hand in hands] for i in range(n)]
    for i in range(n):
        for j in range(len(hands)):
            point = RSP[j] if hands[(j + 1) % 3] == t[i] else 0
            if i == 0:
                dp[i][j] = point
            else:
                dp[i][j] = point + max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3])
    return max(dp[n - 1])

print(sum(map(lambda i: f(T[i::K]), range(K))))

Solution (Python):
import copy
n,k = map(int,input().split())
rsp = list(map(int,input().split()))
t = str(input())
count = 0
def win(h):
  if h == "r":
    return 2
  if h == "s":
    return 0
  if h == "p":
    return 1
for i in range(k):
  j = 0
  pre = [0,0,0]
  new = [0,0,0]
  while i + j*k < n:
    h = win(t[i+j*k])
    new[0] = max(pre[1],pre[2])
    new[1] = max(pre[0],pre[2])
    new[2] = max(pre[0],pre[1])
    new[h] += rsp[h]
    pre = copy.deepcopy(new)
    j += 1
  count += max(pre)
print(count)

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

for i in range(len(t)):
  if t[i] == 'r':
    if (t[i-k] != 'r') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += p
    else:
      ans_list[i] = 0
      ans += 0
  if t[i] == 's':
    if (t[i-k] != 's') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += r
    else:
      ans_list[i] = 0
      ans += 0
  if t[i] == 'p':
    if (t[i-k] != 'p') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += s
    else:
      ans_list[i] = 0
      ans += 0
      
print(ans)

Solution (Python):
n,k=map(int,input().split())
a,b,c=map(int,input().split())
t=input()
d={"r":c,"s":a,"p":b}
ans=0
for i in range(n):
    if i<k:
        ans+=d[t[i]]
    elif t[i]!=t[i-k]:
        ans+=d[t[i]]
    else:
        t=t[:i]+"z"+t[i+1:]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int,input().split())
RSP = ["r", "s", "p"]
RSP_Point = list(map(int,input().split()))
T = list(input().rstrip())
li = [-1]*K
sum = 0
for i in range(N):
    player = (RSP.index(T[i])+2)%3
    if li[i%K] == player:
        if i+K < N:
            li[i%K] = (RSP.index(T[i+K])+1)%3
    else:
        sum += RSP_Point[player]
        li[i%K] = player
print(sum)

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

for i in range(k):
    u = t[i::k]
    m = len(u)
    dp = [[0] * 3 for j in range(m+1)]
    for j in range(m):
        dp[j+1][0] = max(dp[j][1], dp[j][2]) + (score[u[j]] if u[j] == "r" else 0)
        dp[j+1][1] = max(dp[j][0], dp[j][2]) + (score[u[j]] if u[j] == "s" else 0)
        dp[j+1][2] = max(dp[j][0], dp[j][1]) + (score[u[j]] if u[j] == "p" else 0)
    ans += max(dp[m])

print(ans)

Solution (Python):


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

ans_list = [ 0 for i in range(n)]
ans_list2 = [0 for i in range(n)] 
ans_list3 = ['' for i in range(n)] 

for i in range(len(t)):
    if t[i] == 'r':
        if (t[i-k] != 'r') | (ans_list2[i-k] == 0):
            ans_list[i] = p
            ans_list2[i] = 1
            ans_list3[i] = 'p'
        else:
            ans_list2[i] = 0
            
    elif t[i] == 's':
        if (t[i-k] != 's') | (ans_list2[i-k] == 0):        
            ans_list[i] = r
            ans_list2[i] = 1
            ans_list3[i] = 'r'
        else:
            ans_list2[i] = 0

    else: 
        if (t[i-k] != 'p') | (ans_list2[i-k] == 0):        
            ans_list[i] = s
            ans_list2[i] = 1
            ans_list3[i] = 's'
        else:
            ans_list2[i] = 0

print(sum(ans_list))

Solution (Python):
n,k = [int(i) for i in input().split(" ")]
r,s,p = [int(i) for i in input().split(" ")]
points = {"r":p, "s":r, "p":s}
hand = input()
check = [False]*n
totalpoint = 0

for i in range(n):
	
    
    if check[i]:
        continue
    
    now = i
    which = 1
    point = [points[hand[i]],0]
    
    while now+k<n and hand[now]==hand[now+k]:
        
        check[now+k] = True
        point[which]+=points[hand[now+k]]        
        now += k
        which = (which+1)%2
    
    totalpoint += max(point)   

print(totalpoint)

Solution (Python):
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())
    sub_ts = [[] for i in range(K)]
    for index, t in enumerate(T):
        sub_ts[index % K].append(t)

    total = 0
    for sub_t in sub_ts:
        total += sub(sub_t, R, S, P)
    print(total)

def sub(T, R, S, P):
    point = 0
    last_te = None
    for t in T:
        if last_te is None or can_win(last_te, t):
            te, p = win(t, R, S, P)
            last_te = te
            point += p
        else:
            last_te = 'o'
    return point

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

def can_win(last_te, enemy_t):
    win_t = None
    if enemy_t == 'r':
        win_t = 'p'
    elif enemy_t == 's':
        win_t = 'r'
    else:
        win_t = 's'
    return last_te != win_t

main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
dic={}
dic["r"]=p
dic["s"]=r
dic["p"]=s
li=[[] for i in range(k)]
for i in range(n):
    li[i%k].append(t[i])
ans=0
for i in range(k):
    tmp=""
    for j in range(len(li[i])):
        
        if tmp==li[i][j]:
            tmp=""
            continue
        else:
            ans+=dic[li[i][j]]
            tmp=li[i][j]
        
print(ans)

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

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

for i in range(k):
    pre = 'n'
    for j in range(n//k+1):
        l = i+j*k
        if l < n:
            if t[l] != pre:
                total += JSC(t[l])
                pre = t[l]
            else:
                pre = 'n'
print(total)

Solution (Python):
import sys

def main():

    

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

    score = 0

    for i in range(n):
        score += make_score(t[i], r, s, p)

    flag = []

    for i in range(k):
        flag.append(False)

    for i in range(k, n):
        if t[i] == t[i - k] and not flag[i - k]:
            score -= make_score(t[i], r, s, p)
            flag.append(True)

        else:
            flag.append(False)

    print(score)


def make_score(value, r, s, p):
    if value == 'r':
        return p
    elif value == 's':
        return r
    elif value == 'p':
        return s


if __name__ == '__main__':
    main()

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

def rsp(hand: str) -> int:
  if hand == 'r':
    return p
  elif hand == 's':
    return r
  else:
    return s

for i in range(k):
  score += rsp(t[i])
  twice = True
  for j in range(i+k, n, k):
    if t[j] == t[j-k]:
      if twice:
        twice = False
      else:
        twice = True
        score += rsp(t[j])
    else:
      twice = True
      score += rsp(t[j])
      
print(score)

Solution (Python):




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

def opt(seg):
    
    dp = [ [-1, -1, -1] for _ in range(len(seg))]

    dp[0][0] = (R if seg[0] == 's' else 0)
    dp[0][1] = (P if seg[0] == 'r' else 0)
    dp[0][2] = (S if seg[0] == 'p' else 0)

    for i in range(1, len(seg)):
        dp[i][0] = (R if seg[i] == 's' else 0) + max(dp[i-1][1], dp[i-1][2])
        dp[i][1] = (P if seg[i] == 'r' else 0) + max(dp[i-1][0], dp[i-1][2])
        dp[i][2] = (S if seg[i] == 'p' else 0) + max(dp[i-1][0], dp[i-1][1])

    return max(dp[-1])

segs = list()

for K_idx in range(K):
    segs.append( T[K_idx::K] )

print('{}'.format(sum(map(opt, segs))))

Solution (Python):



def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    dp = [[0 for _ in range(n + 1)] for _ in range(3)]

    for index, ti in enumerate(t, 1):
        if index <= k:
            if ti == 'r':
                dp[2][index] = p
            elif ti == 's':
                dp[0][index] = r
            elif ti == 'p':
                dp[1][index] = s
        else:
            if ti == 'r' and dp[2][index - k] == 0:
                dp[2][index] = p
            elif ti == 's' and dp[0][index - k] == 0:
                dp[0][index] = r
            elif ti == 'p' and dp[1][index - k] == 0:
                dp[1][index] = s

    ans = 0

    for d in dp:
        ans += sum(d)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):






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


t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])


def calc_score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    else:
        return 0

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

def get_best_hand(j, hands, my_hand_prev):
    hand = hands[j]
    win_hand = get_win_hand(hand)
    
    candidates = ['r', 's', 'p']
    
    if j == 0 or j != 0 and win_hand != my_hand_prev:
        candidates = [win_hand]
    else:
        
        candidates.remove(my_hand_prev)
        
        if j == len(hands) - 1:
            candidates.pop()
        else:
            hand_next = hands[j+1]
            win_hand_next = get_win_hand(hand_next)
            
            if win_hand_next == my_hand_prev:
                candidates.pop()
            else:
                candidates.remove(win_hand_next)
    return candidates[0]

result = 0

for i in range(k):
    hands = groups[i]
    my_hand_prev = ''
    
    for j in range(len(hands)):
        my_hand = get_best_hand(j, hands, my_hand_prev)
        my_hand_prev = my_hand
        
        result += calc_score(my_hand, hands[j])

print(result)

Solution (Python):

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(K):
  ans += score[T[i]]

for i in range(K,N):
  if T[i-K] == T[i]:
    T[i] = '*' 
  else:
    ans += score[T[i]]

print(ans)

Solution (Python):






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


t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])


def score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    return 0

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

result = 0

for i in range(k):
    hands = groups[i]
    
    for j in range(len(hands)):
        hand = hands[j]
        win_hand = get_win_hand(hand)
        if j == 0:
            my_hand = win_hand
        else:
            candidates = ['r', 's', 'p']
            candidates.remove(my_hand_prev)
            if win_hand == my_hand_prev:
                if j == len(hands) - 1:
                    candidates.pop()
                    my_hand = candidates[0]
                else:
                    hand_next = hands[j+1]
                    win_hand_next = get_win_hand(hand_next)
                    if win_hand_next == my_hand_prev:
                        candidates.pop()
                    else:
                        candidates.remove(win_hand_next)
                my_hand = candidates[0]
            else:
                my_hand = win_hand
        result += score(my_hand, hand)
        my_hand_prev = my_hand

print(result)

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
Pt=[0,p,r,s]
Ans=[0]*k
Prev=[0]*k
for i in range(n):
  if t[i]=="r":
    cur=1
  elif t[i]=="s":
    cur=2
  else:
    cur=3
  ii=i%k
  if Prev[ii]==cur:
    Prev[ii]=0
  else:
    Ans[ii]+=Pt[cur]
    Prev[ii]=cur
print(sum(Ans))

Solution (Python):






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


t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])


def score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    return 0

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

result = 0

for i in range(k):
    hands = groups[i]
    
    for j in range(len(hands)):
        hand = hands[j]
        win_hand = get_win_hand(hand)
        if j == 0:
            my_hand = win_hand
            result += score(my_hand, hand)
            my_hand_prev = my_hand
        else:
            candidates = ['r', 's', 'p']
            candidates.remove(my_hand_prev)
            if win_hand == my_hand_prev:
                if j == len(hands) - 1:
                    candidates.pop()
                    my_hand = candidates[0]
                else:
                    hand_next = hands[j+1]
                    win_hand_next = get_win_hand(hand_next)
                    if win_hand_next == my_hand_prev:
                        candidates.pop()
                    else:
                        candidates.remove(win_hand_next)
                my_hand = candidates[0]
                result += score(my_hand, hand)
                my_hand_prev = my_hand
            else:
                my_hand = win_hand
                result += score(my_hand, hand)
                my_hand_prev = my_hand

print(result)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T =  input()
ans = 0
for k in range(K):
  for i in range(k,N,K):
    if i ==k:
      prv_t = T[i]
      prv_t_num = 1
    else:
      if prv_t != T[i]:
        if prv_t_num == 1:
          ans += R if prv_t=='s' else S if prv_t=='p' else P
        else:
          ans += R*(prv_t_num//2+prv_t_num%2) if prv_t=='s' else S*(prv_t_num//2+prv_t_num%2) if prv_t=='p' else P*(prv_t_num//2+prv_t_num%2)
        prv_t = T[i]
        prv_t_num = 1
      else:
        prv_t_num += 1
     
  if prv_t_num == 1:
    ans += R if prv_t=='s' else S if prv_t=='p' else P
  else:
    ans += R*(prv_t_num//2+prv_t_num%2) if prv_t=='s' else S*(prv_t_num//2+prv_t_num%2) if prv_t=='p' else P*(prv_t_num//2+prv_t_num%2)
print(ans)

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


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

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

t = list(map(str, input().strip().split()[0]))
ans = 0
mp = {'r': p, 'p': s, 's': r}


for i, x in enumerate(t):
    if i >= k and t[i-k] == x:
        t[i] = 'o'
    else:
        ans += mp[x]

print(ans)

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

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

T = input()

result = 0
for i in range(K):
    before = ''

    for t in T[i:N:K]:
        if t != before:
            result += point[t]
            before = t
        else:
            before = ' '

print(result)

Solution (Python):
i=input()
n=int(i.split()[0])
k=int(i.split()[1])

rsp=input()
r=int(rsp.split()[0])
s=int(rsp.split()[1])
p=int(rsp.split()[2])

T=input()
rel=[]
for i in range(len(T)):
  temp=0
  if T[i]=="r":
    temp=p
  elif T[i]=="s":
    temp=r
  elif T[i]=="p":
    temp=s

  if i >= k and T[i]==T[i-k] and rel[i-k]!=0:
    temp=0
  rel.append(temp)

print(sum(rel))

Solution (Python):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(str(input()))
cntr = t.count("r")
cnts = t.count("s")
cntp = t.count("p")
cntr0 = 0
cnts0 = 0
cntp0 = 0
ans = 0

for i in range(n-k):
    if t[i] == "r"and t[i] == t[i+k]:
        cntr0 += 1
        t[i+k] = "q"
    elif t[i] == "s"and t[i] == t[i+k]:
        cnts0 += 1
        t[i+k] = "q"
    elif t[i] == "p"and t[i] == t[i+k]:
        cntp0 += 1
        t[i+k] = "q"

ans = p*(cntr - cntr0) + r*(cnts - cnts0) + s*(cntp - cntp0)
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):
    if i >= k:
        if T[i] == T[i - k]:
            T[i] = '-'


psum = 0
for i in range(n):
    if T[i] == 'r':
        psum = psum + p
    elif T[i] == 'p':
        psum = psum + s
    elif T[i] == 's':
        psum = psum + r
print(psum)

Solution (Python):
"""
References
----------
https://atcoder.jp/contests/abc149/submissions/9219652
"""
def solve(n, k, r, s, p, t):
    score = 0
    mapping = {"r": p, "s": r, "p": s}
    t = list(t)
    for i in range(k):
        score += mapping[t[i]]
    for i in range(k, n):
        if t[i] == t[i-k]:
            t[i] = "x"
        else:
            score += mapping[t[i]]
    return score

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

Solution (Python):
import numpy as np
def solve(n, k, r, s, p, t):
    dp = np.zeros((n, 3), dtype=int)
    win_hand_mapping = {
        "r": "p",
        "s": "r",
        "p": "s"
    }
    score_mapping = {
        "r": r,
        "s": s,
        "p": p
    }
    hand_index_mapping = {
        "r": 0,
        "s": 1,
        "p": 2
    }
    legal_hands_mapping = {
        i: [j for j in range(3) if i!=j] for i in range(3)
    }
    for i0 in range(k):
        for i in range(i0, n, k):
            win_hand = win_hand_mapping[t[i]]
            win_hand_index = hand_index_mapping[win_hand]
            win_hand_score = score_mapping[win_hand]
            for current in range(3):
                point = (current == win_hand_index) * win_hand_score
                if i < k:
                    dp[i][current] = max(dp[i][current], point)
                else:
                    for before in legal_hands_mapping[current]:
                        dp[i][current] = max(dp[i][current], dp[i-k][before] + point)
    return np.max(dp, axis=1)[-k:].sum()

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

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

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

score = 0
for i in range(0, K):
  a, b = scoreMap[T[i]], 0
  for j in range(i+K, N, K):
    na = b + scoreMap[T[j]]
    if T[j] != T[j-K]:
      na = max(na, a + scoreMap[T[j]])
    nb = max(a, b)
    a, b = na, nb
  score += max(a, b)
print(score)

Solution (Python):
'''input
3 5
1 2 1
2 3 0
'''

from sys import stdin, setrecursionlimit
from bisect import bisect_left
import sys, threading



def get_cost(m, h):
    if m == 'r':
        if h == 'p':
            return paper
    elif m == 's':
        if h == 'r':
            return rock
    else:
        if h == 's':
            return scissor
    return 0


def make(first, second):
    return str(first) + ' ' + str(second)


def brute(string, index, last, dp):
    if index == len(string):
        return 0
    x = -float('inf'); y = -float('inf'); z = -float('inf')
    if make(index, last) in dp:
        return dp[make(index, last)]
    if index != 0:
        if last != 'p':
            x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        if last != 's':
            y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        if last != 'r':
            z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)

    else:
        x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)
    dp[make(index, last)] = max(x, y, z)
    return max(x, y, z)




























paper = 0
scissor = 0
rock = 0
def main():
    global paper, rock, scissor
    n, k = list(map(int, stdin.readline().split()))
    rock, scissor, paper = list(map(int, stdin.readline().split()))
    string = stdin.readline().strip()
    cost = 0
    for i in range(k):
        temp = []
        j = i
        while j < n:
            temp.append(string[j])
            j += k
        dp = dict()
        cost += brute(temp, 0, '$', dp)
    print(cost)

if __name__ == "__main__":
    sys.setrecursionlimit(200005)
    threading.stack_size(1 << 27)
    thread = threading.Thread(target=main)
    thread.start()

Solution (Python):
'''input
3 5
1 2 1
2 3 0
'''

from sys import stdin, setrecursionlimit
from bisect import bisect_left


def solve(string):
    dp = [[0 for x in range(3)] for y in range(len(string))]
    if string[0] == 'r':
        dp[0] = [0, 0, paper]
    elif string[0] == 's':
        dp[0] = [rock, 0, 0]
    else:
        dp[0] = [0, scissor, 0]

    for i in range(1, len(string)):
        if string[i] == 'r':
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + paper
        elif string[i] == 's':
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + rock
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
        else:
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + scissor
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
    
    return max(dp[-1])


n, k = list(map(int, stdin.readline().split()))
rock, scissor, paper = list(map(int, stdin.readline().split()))
string = stdin.readline().strip()
cost = 0
for i in range(k):
    temp = []
    j = i
    while j < n:
        temp.append(string[j])
        j += k
    cost += solve(temp)
print(cost)

Solution (Python):
def get_score(enemy_hand):
    return {"r": P, "s": R, "p": S}[enemy_hand]


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

enemy_hands = [[] for _ in range(K)]
for i in range(N):
    enemy_hands[i % K].append(T[i])

score = 0
for hands in enemy_hands:
    succession = {"r": [], "s": [], "p": []}
    current = hands[0]
    streak = 0
    for hand in hands:
        if hand == current:
            streak += 1
        else:
            succession[current].append(streak)
            streak = 1
            current = hand
    else:
        succession[current].append(streak)
    for k, v in succession.items():
        for streak in v:
            score += ((streak + 1) // 2) * get_score(k)

print(score)

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


def subtotal(sub_T):
    
    dp = [['non', 0]]
    sub_total_point = 0

    for i in range(0,len(sub_T)):
        hand = sub_T[i]
        if hand != dp[i][0]:
            if hand == 'r':
                sub_total_point += P
            elif hand == 's':
                sub_total_point += R
            elif hand == 'p':
                sub_total_point += S
            else:
                pass

        else:
            hand = 'n'

        dp.append([hand,sub_total_point])

    return sub_total_point

def main():
    total_point = 0

    if K==1:
        total_point += subtotal(T)

    else:
        for m in range(0, K):
            sub_T = []
            for n in range(0, int(N/K)+1):
                if n*K+m < N:
                    sub_T.append(T[n*K+m])
                else:
                    pass

            total_point += subtotal(sub_T)

    print(total_point)

if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
pre_jyanken = [0] * K  
ans = 0
for i in range(N):
    if pre_jyanken[i % K] == T[i]:
        pre_jyanken[i % K] = i
    else:
        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        else:
            ans += S
        pre_jyanken[i % K] = T[i]
print(ans)

Solution (Python):
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    score = {"r": P, "s": R, "p": S}
    T = input()
    skip = set()
    ans = 0
    for i in range(N):
        if i >= K and T[i] == T[i-K] and (i-K) not in skip:
            skip.add(i)
            continue
        ans += score[T[i]]
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

print(point)