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())
r=0
s=0
p=0
a=list(input())
A=[]
for i in range(K):
    if a[i]=="r": 
        A.append(P)
    elif a[i]=="s":
        A.append(R)
    else:
        A.append(S)
for i in range(K,N):
    if a[i]==a[i-K] and A[i-K]!=0:
        A.append(0)
    elif a[i]=="r": 
        A.append(P)
    elif a[i]=="s":
        A.append(R)
    else:
        A.append(S)
print(sum(A))

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

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

tb = {'r': p, 's': r, 'p': s}
ans = 0
for i in range(len(t)):
    if i < k:
        ans += tb[t[i]]
    else:
        if t[i - k] == t[i]:
            t[i] = '-'
        else:
            ans += tb[t[i]]
print(ans)

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

data = {"r": P, "s": R, "p": S}
ans = 0

for i in range(K):
    hand = T[i]
    ans += data[hand]

    for j in range(i + K, N, K):
        if T[j] == hand:
            hand = "other"
        else:
            hand = T[j]
            ans += data[hand]

print(ans)

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

point = {"r" : P, "s" : R, "p" : S}
ans = 0
flag = 0
for i in range(K) :
    flag = T[i]
    ans += point[T[i]]
    while True :
        i += K
        if i >= N :
            break
        if T[i] != flag :
            ans += point[T[i]]
            flag = T[i]
        else :
            flag = 0

print(ans)

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

for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

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

ans = 0
select = [False] * N
for i in range(N):
    if i >= K:
        if T[i-K] == T[i]:
            if select[i-K]:
                continue
    if T[i] == "r":
        ans += P
    elif T[i] == "s":
        ans += R
    else:
        ans += S
    select[i] = True
print(ans)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = input()
dp = [[0]*n for _ in range(3)]
for i in range(k):
    if t[i] == 'r':
        dp[2][i] = a[2]
    if t[i] == 's':
        dp[0][i] = a[0]
    if t[i] == 'p':
        dp[1][i] = a[1]
for i in range(k,n):
    id = -1
    if t[i] == 'r':
        id = 2
    if t[i] == 's':
        id = 0
    if t[i] == 'p':
        id = 1
    for x in range(3):
        for y in range(3):
            if x == y:
                continue
            if x == id:
                dp[x][i] = max(dp[x][i], dp[y][i-k] + a[x])
            else:
                dp[x][i] = max(dp[x][i], dp[y][i-k])

ans = 0
for i in range(k):
    ans += max(dp[0][n-1-i], dp[1][n-1-i], dp[2][n-1-i])
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000)
def conv(r,s,p,j):
    if j == 'r':
        return p
    if j == 's':
        return r
    if j == 'p':
        return s
def listint():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a
def perse(t,k,num):
    stri = []
    for i in range(k):
        stri.append([])
    for i in range(num):
        stri[i%k].append(t[i])
    return stri
def main(q):
    i = 0
    ans = 0
    while i < len(q)-1:
        if q[i] == q[i+1]:
            ans = ans + conv(r,s,p,q[i])
            i = i+2
        else:
            ans = ans + conv(r,s,p,q[i])
            i = i+1
    if i == len(q)-1:
        ans = ans + conv(r,s,p,q[i])
    return ans
num,k = listint()
r,s,p = listint()
t = input()
afterperse = perse(t,k,num)
ans = 0
for i in range(k):
    ans = ans + main(afterperse[i])
print(ans)

Solution (Python):
NK = input().split()
N = int(NK[0])
K = int(NK[1])

RSP = input().split()
R = int(RSP[0])
S = int(RSP[1])
P = int(RSP[2])
T = input()

hands = {'r':'p', 's':'r', 'p':'s', 'e':'e'}
points = {'r':R, 's': S, 'p': P}
hist = {}
point = 0
for i in range(len(T)):
  if (i-K >= 0) and (hist[i-K] == hands[T[i]]):
    hist[i] = hands['e']
  else:
    hist[i] = hands[T[i]]
    point += points[hist[i]]
print(point)

Solution (Python):
n,k = map(int,input().split())
R,S,P = map(int,input().split())
T = str(input())
t=[T[i] for i in range(n)]

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] == t[i-k]:
          t[i] = 'o'
          continue
     else:
          if t[i] == 'r':
               ans += P
          elif t[i] == 's':
               ans += R
          else:
               ans += S
print(ans)

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

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

score = 0

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

print(score)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

def win(s):
    if s == 'r':
        return 'p'
    elif s == 's':
        return 'r'
    elif s == 'p':
        return 's'
    else:
        return 'x'
        
def point(s):
    if s == 'r':
        return R
    elif s == 's':
        return S
    elif s == 'p':
        return P
    else:
        return 0

ans = 0
tmp = ''
for i in range(len(T)):
    if i < K:
        tmp += T[i]
    elif T[i] != tmp[i-K]:
        tmp += T[i]
    else:
        tmp += 'x'

for i in range(len(T)):
    ans += point(win(tmp[i]))


print(ans)

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

    for i in range(k):
        t2 = t[i::k]
        l = len(t2)
        dp = [0, 0, 0]
        for j in range(l):
            if t2[j] == "r":
                dp = [max(dp[1], dp[2]), max(dp[0], dp[2]),
                      max(dp[0]+p, dp[1]+p)]
            elif t2[j] == "s":
                dp = [max(dp[1]+r, dp[2]+r), max(dp[0], dp[2]),
                      max(dp[0], dp[1])]
            else:
                dp = [max(dp[1], dp[2]), max(dp[0]+s, dp[2]+s),
                      max(dp[0], dp[1])]
        ans += max(dp)
    print(ans)


main()

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

ans = 0
log = ["*"]*N
win = {"r":"p","s":"r","p":"s"}
score = {"r":P,"s":R,"p":S}
for i,t in enumerate(T):
    if log[i-K]==win[t]: continue
    log[i] = win[t]
    ans += score[t]
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()
ans = 0
d = {}
d["r"] = P
d["s"] = R
d["p"] = S
rens = [0] * N
for i in range(N):
    if i>=K and T[i-K] == T[i]:
        rens[i] = rens[i-K] + 1
    if rens[i] %2 == 0:
        ans += d[T[i]]
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] = 'x'

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

print(points)

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

def rps(hero, villain):
    hero = hand.index(hero)
    villain = hand.index(villain)
    result = hero - villain
    if result % 3 == 0 or result % 3 == 1:
        return 0
    else:
        return 1

if_rock, if_scissors, if_paper = ([0]*k for _ in range(3))
for i in range(n):
        tmp_r = rps('r', T[i]) * r + max(if_scissors[i%k], if_paper[i%k])
        tmp_s = rps('s', T[i]) * s + max(if_rock[i%k], if_paper[i%k])
        tmp_p = rps('p', T[i]) * p + max(if_rock[i%k], if_scissors[i%k])
        if_rock[i%k] = tmp_r
        if_scissors[i%k] = tmp_s
        if_paper[i%k] = tmp_p

maxpoint = 0
for i in range(k):
    maxpoint += max(if_rock[i], if_scissors[i], if_paper[i])
print(maxpoint)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
dic = {'r':p, 's':r, 'p':s}
b = [1]*n
ans = 0
for i in range(n):
    if i > (k-1) and t[i]==t[i-k]:
        b[i] -= b[i-k]
    ans += dic[t[i]] if b[i]==1 else 0

print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = str(input())
score = 0
lose = []
def janken(hand):
    if hand == 'r':
        return(p)
    if hand == 's':
        return(r)
    if hand == 'p':
        return(s)

for i in range(n):
    if i <= k - 1:
        score += janken(T[i])
    else:
        if T[i] == T[i - k]:
            if i - k not in lose:
                lose.append(i)
            else:
                score += janken(T[i])
        else:
            score += janken(T[i])
print(score)

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

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

for i in range(n):
    if T[i] == 'r':
        P.append('p')
    elif T[i] == 's':
        P.append('r')
    elif T[i] == 'p':
        P.append('s')


for i in range(n-k):
    if P[i] == P[i+k]:
        P[i+k] = 0

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

print(count)

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

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
n,k = map(int,input().split())
r,s,p = map(int,input().split())
q = deque(input())
score = 0
memory = [0]*k
for i in range(n):
    a = q.popleft()
    if a == 'r': 
        if memory[i % k] != 'r':
            score += p 
            memory[i % k] = 'r'
        else:
            memory[i % k] = 0
    if a == 's': 
        if memory[i % k] != 's':
            score += r 
            memory[i % k] = 's'
        else:
            memory[i % k] = 0    
    if a == 'p': 
        if memory[i % k] != 'p':
            score += s 
            memory[i % k] = 'p'
        else:
            memory[i % k] = 0
print(score)

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

def solve(t, r, s, p):
    ret = 0
    pre = ""
    for c in t:
        if c == "r":
            if pre != "p":
                ret += p
                pre = "p"
            else:
                pre = ""
        elif c == "p":
            if pre != "s":
                ret += s
                pre = "s"
            else:
                pre = ""
        else:
            if pre != "r":
                ret += r
                pre = "r"
            else:
                pre = ""
    return ret


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

    ans = 0
    for i in range(k):
        j = i
        tmp = []
        while j < N:
            tmp.append(t[j])
            j += k
        ans += solve(tmp, r, s, p)
    print(ans)

if __name__ == "__main__":
    main()

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

for i in range(n):
  u[i%k]+=t[i]

for i in range(k):
  l=len(u[i])
  for j in range(1,l):
    if u[i][j]==u[i][j-1]:
      u[i][j]='o'
  
  R+=u[i].count('s')
  S+=u[i].count('p')
  P+=u[i].count('r')

print(r*R+s*S+p*P)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
point=0
double=0
def put(what):
    if what=="s":
        return R
    if what=="r":
        return P
    if what=="p":
        return S

for i in range(K):
    check=[]
    for j in range(N//K+1):
          if i+K*j<N:
              check+=T[i+K*j]
    
    point+=put(T[i])
    for j in range(len(check)-1):
        if check[j]==check[j+1]:
            check[j+1]="n"
        else:
            point+=put(check[j+1])
    
print(point)

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
dp=[[0 for i in range(3)] for _ in range(N)] 
b=[]
cnt=0

def janken(machine,hand):
    p=0
    if ((machine=="r")and(hand==2)):
        p=p+P
    elif((machine=="s")and(hand==0)):
        p=p+R
    elif((machine=="p")and(hand==1)):
        p=p+S
    return p
c=0
for i in range(N):
    machine=T[i]
    if i>=K:
        dp[i][0]=janken(machine,0)+max(dp[i-K][1],dp[i-K][2])
        dp[i][1]=janken(machine,1)+max(dp[i-K][0],dp[i-K][2])
        dp[i][2]=janken(machine,2)+max(dp[i-K][0],dp[i-K][1])
    else:
        for j in range(3):
            dp[i][j]=(janken(T[i],j))
for i in range(K):
    cnt=cnt+max(dp[len(dp)-i-1])
print(cnt)

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

commands = ['']*n
score = 0

for i,c in enumerate(t):
    if c=='r':
        command = 'p'
        point = p
    elif c=='p':
        command = 's'
        point = s
    elif c=='s':
        command = 'r'
        point = r
        
    if (i-k>=0) and (commands[i-k]==command):
        command = ''
        point = 0
    
    score += point
    commands[i] = command

print(score)

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

l=[0]*n

for i in range(n):
    if i>=k:
        if t[i]==t[i-k]:
            if l[i-k]:
                ans+=d[t[i]]
            else:
                l[i]=1
        else:
            ans+=d[t[i]]
    else:
        ans+=d[t[i]]
print(ans)

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

c=0

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

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

for i in T:
    c+=jan(i)

print(c)

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

c=0

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

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

for i in range(1,K+1): 
    for j in range(N//K+1):
        if i+K*j>N:
            continue
        c+=jan(T[i+K*j-1])

print(c)

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] = "x"

answer = 0
for t in T:
    if t == "r":
        answer += p
    elif t == "s":
        answer += r
    elif t == "p":
        answer += s


print(answer)

Solution (Python):
N, K = [int(_) for _ in input().split()]
R, S, P = [int(_) for _ in input().split()]
T = input()
ans = 0
dp = {}
dp['r'] = []
dp['s'] = []
dp['p'] = []
for i in range(K):
    s = T[i::K]
    now = s[0]
    l = 1
    for d in s[1:]:
        if now == d:
            l += 1
        else:
            dp[now] += [l]
            now = d
            l = 1
    dp[now] += [l]
for r in dp['r']:
    ans += P * ((r + 1) // 2)
for s in dp['s']:
    ans += R * ((s + 1) // 2)
for p in dp['p']:
    ans += S * ((p + 1) // 2)
print(ans)

Solution (Python):
def hantei(a):
  if a == 'r':
    return p
  elif a == 's':
    return r
  else:
    return s

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

ans = 0

for i in range(k):
  flag = True
  last1 = 'a'
  last2 = 'b'
  
  for j in range(i, n, k):
    if last1 != t[j]:
      ans += hantei(t[j])
      
    elif last1 == last2 and last1 == t[j]:
      ans += hantei(t[j])
      last1 = 'y'
    
    last2 = last1
    last1 = t[j]
    
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

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


dp = [[0] * 3 for _ in range(K)]

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

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

d = {"r":P, "s":R, "p":S} 
ans = 0
wins = [False]*K
for i in range(len(T)):
  if not wins[i%K] or T[i-K] != T[i]:
    ans += d[T[i]]
    wins[i%K] = True
  else:
    wins[i%K] = False
print(ans)

Solution (Python):
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
ans = sum([dic[s] for s in t])
for i in range(k,len(t)):
    if t[i-k] == t[i]:
        ans -= dic[t[i]]
        t = t[:i] + 'a' + t[(i+1):]
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
result = 0
for i in range(n):
    if t[i] == "r":
        result += p
    elif t[i] == "p":
        result += s
    else:
        result += r
for i in range(n-k):
    if t[i] == t[i+k]:
        if t[i] == "r":
            result -= p
        elif t[i] == "p":
            result -= s
        else:
            result -= r  
        t[i+k] = "1"      

print(result)

Solution (Python):
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
 
def EarnScore(rsp, t):
    if t == 'r':
        return rsp[2]
    elif t == 's':
        return rsp[0]
    elif t == 'p':
        return rsp[1]
    else:
        return 0
 
RSP = [R, S, P]
Answer = 0
ListT = list(T)
for i in range(N):
    Answer = EarnScore(RSP, ListT[i]) + Answer
    if i + K < N:
        if ListT[i] == ListT[i+K]:
            ListT[i+K] = 'x'
 
print(Answer)

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





ans = 0
for i in range(n):
    if score_list[i] == "r":
        if score_list[i] != score_list[i-k] or i < k:
            ans += p
        else:
            score_list[i] = 0
    elif score_list[i] == "s":
        if score_list[i] != score_list[i-k] or i < k:
            ans += r
        else:
            score_list[i] = 0
    elif score_list[i] == "p":
        if score_list[i] != score_list[i-k] or i < k:
            ans += s
        else:
            score_list[i] = 0

print(ans)

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

dp = [[0]*3 for _ in range (N+1)]

winsum = 0

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


for Ts in Ti:
    dp = [[0]*3 for _ in range (len(Ts)+1)]
    for i in range(1,len(Ts)+1):
        for j in range(3): 
            for k in range(3): 
                
                if j == k:
                    continue
                elif j == 2 and Ts[i-1] == 'r':
                    dp[i][j] = max(dp[i][j], dp[i-1][k]+RSP[j])
                elif j == 0 and Ts[i-1] == 's':
                    dp[i][j] = max(dp[i][j], dp[i-1][k]+RSP[j])
                elif j == 1 and Ts[i-1] == 'p':
                    dp[i][j] = max(dp[i][j], dp[i-1][k]+RSP[j])
                else: 
                    dp[i][j] = max(dp[i][j], dp[i-1][k])
        
    winsum += max(dp[i])
print(winsum)

Solution (Python):

def win(n):
	if n=='r':
		return p
	elif n=='s':
		return r
	else:
		return s

n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
score=0
for i in t:
	score += win(i)

hands=list(t)

for i in range(n):
	if i+k<n:
		if hands[i]==hands[i+k]:
			score-=win(hands[i+k])
			hands[i+k]='x'
print(score)

Solution (Python):
import itertools
import sys
import math
import numpy as np
import fractions
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

def main():

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

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

    for i in range(K):
        X.append(T[i::K])

    ans = 0
    for x in X:
        
        dp = [[0] * 2 for _ in range(len(x)+1)]
        dp[0][True] = score(x[0])
        for i in range(1,len(x)):
            if x[i] == x[i-1]:
                dp[i][True] = dp[i-1][False] + score(x[i])
                dp[i][False] = dp[i-1][True]
            else:
                dp[i][True] = max(dp[i-1][True],dp[i-1][False]) + score(x[i]) 
                dp[i][False] = max(dp[i-1][True],dp[i-1][False])

        ans += max(dp[len(x)-1][True],dp[len(x)-1][False])
        

    print(ans)


if __name__ == "__main__":
  main()

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

ans = 0
M = []
for i in range(N):
	if i >= K and M[i-K] == T[i]:
		M.append('x')
		continue
	else:
		if T[i] == 'r':
			ans += P
			M.append('r')
		if T[i] == 's':
			ans += R
			M.append('s')
		if T[i] == 'p':
			ans += S
			M.append('p')
print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

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

hands = {'r':P, 's':R, 'p':S}
b = [1] * N
score = 0
for i in range(N):
    if i > K - 1 and T[i] == T[i-K]:
        b[i] = 1 - b[i-K]
    score += hands[T[i]] if b[i] == 1 else 0
print(score)

Solution (Python):
[N,K] = list(map(int,input().split()))
[R,S,P] = list(map(int,input().split()))
T = input()
H = []
output = 0
for i in range(N):
    if T[i]=="r":
        H.append("P")
        output += P
    elif T[i]=="s":
        H.append("R")
        output += R
    elif T[i]=="p":
        H.append("S")
        output += S
for i in range(0,N-K):
    if H[i]==H[i+K]:
        if H[i]=="R":
            output += -1*R
            H[i+K] = str(i)
        elif H[i]=="S":
            output += -1*S
            H[i+K] = str(i)
        elif H[i]=="P":
            output += -1*P
            H[i+K] = str(i)

print(output)

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

win = {"r": 2, "s": 0, "p": 1}
lost = [False] * n

ans = 0
for i in range(n):
    score = rsp[win[t[i]]]
    ans += score
    if i >= k and t[i - k] == t[i] and not lost[i - k]:
        lost[i] = True
        ans -= score

print(ans)

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

dp = [[0] * 3 for _ in range(N + 1)]


def point(hand, n):
    n -= 1
    if hand == T[n]:
        return 0
    elif hand == "r" and T[n] == "p" or hand == "s" and T[n] == "r" or hand == "p" and T[n] == "s":
        return 0
    elif T[n] == "r":
        return P
    elif T[n] == "s":
        return R
    elif T[n] == "p":
        return S


for n in range(1, N + 1):
    if n <= K:
        dp[n][0] = point("r", n)
        dp[n][1] = point("s", n)
        dp[n][2] = point("p", n)
    else:
        dp[n][0] = max(dp[n-K][1], dp[n-K][2]) + point("r", n)
        dp[n][1] = max(dp[n-K][2], dp[n-K][0]) + point("s", n)
        dp[n][2] = max(dp[n-K][1], dp[n-K][0]) + point("p", n)

MaxPoint = 0
for k in range(K):
    MaxPoint += max(dp[N-k])

print(MaxPoint)