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 = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()

d = {'R': R, 'S': S, 'P': P}

win = T.replace('r', 'P').replace('s', 'R').replace('p', 'S')

SUM = 0
flag = [True] * N
for i in range(N):
  if i < K:
    SUM += d[win[i]]
  else:
    if i >= K and win[i] == win[i-K]:
      if flag[i-K]:
        flag[i] = False
      else:
        SUM += d[win[i]]
    else:
      SUM += d[win[i]]

print(SUM)

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

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

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

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

def getScore(t):
    if t == "r":
        return P
    elif t == "s":
        return R
    elif t == "p":
        return S

score=0
mycard = []
for i in range(N):
    if i < K:
        score += getScore(T[i])
        mycard.append(1)
    else:
        if mycard[i-K] == 0:
            score += getScore(T[i])
            mycard.append(1)
        else:
            if T[i] != T[i-K]:
                score += getScore(T[i])
                mycard.append(1)
            else:
                mycard.append(0)
print(score)

Solution (Python):
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
temp = [False for _ in range(N)]

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

ans = 0
for i in range(N):
    ans += a[T[i]]

for i in range(N):
    if 0 <= (i - K):
        if T[i] == T[i - K]:
            if not temp[i-K]:
                ans -= a[T[i]]
                temp[i] = True

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

hands = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': R, 's': S, 'p': P}
max_score = 0
memo = [None for _ in range(N)]

for i, t in enumerate(T):
    if i < K:
        memo[i] = hands[t]
        max_score += score[hands[t]]
    else:
        if memo[i-K] == hands[t]:
            memo[i] = [k for k in hands.keys() if k not in (t, hands[t])]
        else:
            memo[i] = hands[t]
            max_score += score[hands[t]]
            
print(max_score)

Solution (Python):
def func(u):
    if u=="r":
        return "p"
    elif u =="s":
        return "r"
    else:
        return "s"

def func1(memo):
    rcount = memo.count("r")
    pcount = memo.count("p")
    scount = memo.count("s")
    if rcount ==0:
        return "r"
    elif pcount == 0:
        return "p"
    else:
        return "s"

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
score = 0
memory = []
for i in range(0,N):
    te = func(T[i])
    memory1 = []
    memory1.append(te)
    hantei = True
    if i>=K:
        if te == memory[i-K]:
            if i+K<=N-1:
                te = T[i]
                memory1.append(te)
                te1 = func(T[i+K])
                if te1==te:
                    te = func1(memory1)
            hantei = False

    if hantei == True:
        if te == "r":
            score+=R
        elif te == "s":
            score+=S
        else:
            score+=P
    memory.append(te)

print(score)

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

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


flag = []
alls = 0
for i in range(N) :
    if i < K :
       flag.append(0)
       if T[i]=="r" : 
          alls += P
       elif T[i]=="s" :  
          alls += R
       else :  
          alls += S
    elif T[i]!=T[i-K] or ( T[i]==T[i-K] and flag[i%K] == 1 ):
       flag[i%K] = 0
       if T[i]=="r" : 
          alls += P
       elif T[i]=="s" :  
          alls += R
       else :  
          alls += S
    elif T[i]==T[i-K]:
       flag[i%K] = 1

print( alls )

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().rstrip()
ret = 0
me = []
for i, rival in enumerate(t):
    if rival == 'r':
        if i - k >= 0 and me[i - k] == 'p':
            if i + k < n:
                if t[i + k] == 'p':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('s')
                elif t[i + k] == 'r':
                    me.append('s')
        else:
            me.append('p')
            ret += p
    elif rival == 's':
        if i - k >= 0 and me[i - k] == 'r':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('s')
                elif t[i + k] == 'p':
                    me.append('p')
                elif t[i + k] == 's':
                    me.append('s')
        else:
            me.append('r')
            ret += r
    elif rival == 'p':
        if i - k >= 0 and me[i - k] == 's':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('p')
                elif t[i + k] == 'p':
                    me.append('r')
        else:
            me.append('s')
            ret += s
print(ret)

Solution (Python):
import math

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

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

ans=0
for i in range(k):
    l = [t[j] for j in range(i,n,k)]
    
    m = len(l)
    tmp = 0
    cnt=1
    for j in range(m-1):
        if l[j]==l[j+1]:
            cnt+=1
        else:
            tmp+=math.ceil(cnt/2)*h(l[j])
            cnt=1
    tmp+=math.ceil(cnt/2)*h(l[m-1])
    
    ans+=tmp
print(ans)

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

r_point = t.count('s') * r
s_point = t.count('p') * s
p_point = t.count('r') * p

sum_point = r_point+s_point+p_point

for i, moji in enumerate(t):
    if i+k >= len(t):
        break
    if t[i] == 'r' and t[i+k] == 'r':
        sum_point -= p
        t[i+k] = 'x'
    if t[i] == 's' and t[i+k] == 's':
        sum_point -= r
        t[i+k] = 'x'
    if t[i] == 'p' and t[i+k] == 'p':
        sum_point -= s
        t[i+k] = 'x'

print(sum_point)

Solution (Python):
def ten(arr,R,S,P):
  ans = 0
  num = 0
  length = len(arr)
  for i in range(length):
    if i == 0:
      if arr[i] == "r":
        ans = ans + P
        num = i
      elif arr[i] == "s":
        ans = ans + R
        num = i
      else:
        ans = ans + S
        num = i

    elif i > 0 and arr[i] != arr[i-1]:
      if arr[i] == "r":
        ans = ans + P
        num = i
      elif arr[i] == "s":
        ans = ans + R
        num = i
      else:
        ans = ans + S
        num = i
        
    elif i > 0 and arr[i] == arr[i-1] and int(num+1) != i:
      if arr[i] == "r":
        ans = ans + P
        num = i
      elif arr[i] == "s":
        ans = ans + R
        num = i
      else:
        ans = ans + S
        num = i
    
  return ans

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

T = list(T)
arr = []

for i in range(K):
  a = T[i::K]
  arr.append(a)

ans = 0

for i in arr:
  ans = ans + ten(i,R,S,P)

print(ans)

Solution (Python):



def cal(mozi, r, s, p):
    if mozi == "r":
        return r
    if mozi == "s":
        return s
    if mozi == "p":
        return p

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
dasu_list = []
for i in range(0, k):
    dasu_list.append([])
for i in range(0, len(t)):
    j = i % k
    if t[i] == "r":
        dasu_list[j].append("p")
    if t[i] == "s":
        dasu_list[j].append("r")
    if t[i] == "p":
        dasu_list[j].append("s")
score = 0
for i in range(0, k):
    check_list = dasu_list[i]
    score += cal(check_list[0], r, s, p)
    for j in range(1, len(check_list) - 1):
        if check_list[j - 1] == check_list[j]:
            if check_list[j - 1] == "r":
                if check_list[j + 1] == "r":
                    check_list[j] = "s"
                if check_list[j + 1] == "s":
                    check_list[j] = "p"
                if check_list[j + 1] == "p":
                    check_list[j] = "s"
            if check_list[j - 1] == "s":
                if check_list[j + 1] == "r":
                    check_list[j] = "p"
                if check_list[j + 1] == "s":
                    check_list[j] = "p"
                if check_list[j + 1] == "p":
                    check_list[j] = "r"
            if check_list[j - 1] == "p":
                if check_list[j + 1] == "r":
                    check_list[j] = "s"
                if check_list[j + 1] == "s":
                    check_list[j] = "r"
                if check_list[j + 1] == "p":
                    check_list[j] = "r"
        else:
            score += cal(check_list[j], r, s, p)
    if check_list[len(check_list) - 2] != check_list[len(check_list) - 1]:
        score += cal(check_list[len(check_list) - 1], r, s, p)
print(score)

Solution (Python):
def main():
    n, k = [int(x) for x in input().split()]
    r, s, p = [int(x) for x in input().split()]
    points = {"r":p, "s":r, "p":s}
    flags = [True] * k
    t = input()
    score = 0
    
    
    for i in range(n):
        score += points[t[i]]
        
        if not flags[i % k]:
            if t[i] == t[i - k]:
                flags[i % k] = True
                score -= points[t[i]]
                
            
            
        else:
            flags[i % k] = False
            
        
        
    
    
    print(score)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
li = [int(i) for i in input().split()]
R = li[0]
S = li[1]
P = li[2]
Ta = input()
T = ['a'] * N
for i in range(N) :
    T[i] = Ta[i]
sum = 0
for i in range(N) :
    if i < N - K :
        if T[i] == "r" :
            if T[i+K] == "r" :
                T[i+K] = "k"
            sum+=P
        elif T[i] == "s" :
            if T[i+K] == "s" :
                T[i+K] = "k"
            sum+=R
        elif T[i] == "p" :
            if T[i+K] == "p" :
                T[i+K] = "k"
            sum+=S
        else :
            sum=sum
    else :
        if T[i] == "r" :
            sum+= P
        elif T[i] == "s" :
            sum+= R
        elif T[i] == "p" :
            sum+= S
        else :
            sum +=0
print(sum)

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

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

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

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

for i in range(k):
    ans += d[t[i]]

for i in range(1, len(t)//k+1):
    if i == len(t)//k :
        for j in range(len(t)%k):
            if t[i*k+j] != t[(i-1)*k+j]:
                ans += d[t[i*k+j]]
                flag[j] = 0
            elif t[i*k+j] == t[(i-1)*k+j] and flag[j] == 0:
                flag[j] = 1
            elif t[i*k+j] == t[(i-1)*k+j] and flag[j] == 1:
                ans += d[t[i*k+j]]
                flag[j] = 0
    else:
        for j in range(k):
            if t[i*k+j] != t[(i-1)*k+j]:
                ans += d[t[i*k+j]]
                flag[j] = 0
            elif t[i*k+j] == t[(i-1)*k+j] and flag[j] == 0:
                flag[j] = 1
            elif t[i*k+j] == t[(i-1)*k+j] and flag[j] == 1:
                ans += d[t[i*k+j]]
                flag[j] = 0

print(ans)

Solution (Python):

def solve(N, K, R, S, P, T):
    hands = [None] * N
    score = {"r":P, "s":R, "p":S}

    for s in range(K):
        hands[s] = True 
        cont = 1

        
        for i in range(s+K, N, K):
            if T[i] != T[i-K]:
                hands[i] = True
                cont = 1
            else:
                cont += 1
                if cont % 2 == 0:
                    hands[i] = False
                else:
                    hands[i] = True

    answer = 0
    
    for i in range(N):
        if hands[i]:
            
            answer += score[T[i]]
    return answer

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

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

l = []
for i in t:
    if i == "r":
        l.append("p")
    elif i == "s":
        l.append("r")
    else:
        l.append("s")

for i in range(k, n):
    if l[i] == l[i-k]:
        l[i] = "a"

for i in range(len(l)):
    if l[i] == "r":
        l[i] = r
    elif l[i] == "s":
        l[i] = s
    elif l[i] == "a":
        l[i] = 0
    else:
        l[i] = p

print(sum(l))

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
add = [p, r, s]
rsp_str = ['r', 's', 'p']
t = input()
box = [None]*k
points = [None]*k

for i in range(k):
    box[i] = [rsp_str.index(str) for str in t[i::k]]

for i in range(k):
    prev = -1
    point = 0
    for hand in box[i]:
        if hand == prev:
            prev = -1
            continue
        point += add[hand]
        prev = hand
    points[i] = point

print(sum(points))

Solution (Python):


N,K=map(int,input().split())
R,S,P=map(int,input().split())
l=list(input())
play=[]

def point(hand):
    if hand=="r":
        return ["p",P]
    elif hand=="s":
        return ["r",R]
    else:
        return ["s",S]

ans=0
for i in range(N)[::-1]:
    if len(play)>=K:
        if point(l[i])[0]==play[-K]:
            if i-K>=0:
                temp=point(l[i-K])[0]
                for item in ["r","s","p"]:
                    if item!=play[-K] and item!=temp:
                        play.append(item)
                        break
            else:
                for item in ["r","s","p"]:
                    if item!=play[-K]:
                        play.append(item)
                        break
        else:
            play.append(point(l[i])[0])
            ans+=point(l[i])[1]
            
    else:
        play.append(point(l[i])[0])
        ans+=point(l[i])[1]

print(ans)

Solution (Python):
N, K  = map(int, input().split())
R, S, P  = map(int, input().split())
T = list(input())
Group = [[0]*0 for h in range(K)]
Ans = 0

for i in range(len(T)):
    L = i % K
    Group[L].append(T[i])

for i in range(K):
    A = Group[i][0]
    Bit = 0
    for j in range(len(Group[i])):
        if Group[i][j] == A and Bit == 0:
            Bit = 1
            A = Group[i][j]
            if Group[i][j] == "r":
                Ans += P
            elif Group[i][j] == "s":
                Ans += R
            else:
                Ans += S
        elif Group[i][j] == A and Bit == 1:
            Bit = 0
            A = Group[i][j]
        else :
            Bit = 1
            A = Group[i][j]
            if Group[i][j] == "r":
                Ans += P
            elif Group[i][j] == "s":
                Ans += R
            else:
                Ans += S
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])
tab = {'r':R, 's':S, 'p':P}
T = input()

from collections import defaultdict
counter = defaultdict(int)

if len(T) <= K:
  pass
else:
  ts = [T[i::K] for i in range(K)]
  for tt in ts:
    duplicate = True
    prev = tt[0]
    for t in tt[1:]:
      if prev == t:
        if duplicate:
          counter[t] += 1
          duplicate = False
        else:
          duplicate = True
      else:
        duplicate = True
      prev = t
score = (T.count('r')-counter['r'])*P\
+(T.count('s')-counter['s'])*R\
+(T.count('p')-counter['p'])*S
print(score)

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

pt = [0 for i in range(n)]

for i in range(n):
  if t[i] == "r":
    pt[i] = p
  elif t[i] == "s":
    pt[i] = r
  else:
    pt[i] = s
  if i >= k and t[i] == t[i - k] and pt[i - k] > 0:
    pt[i] = 0

print(sum(pt))

Solution (Python):
MOVES = 'rsp'

def points(me, rival):
    if me == 'r' and rival == 's':
        return r
    if me == 's' and rival == 'p':
        return s
    if me == 'p' and rival == 'r':
        return p
    return 0

def pos(hand):
    return MOVES.index(hand)

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().rstrip()
ret = 0
me = []
for i, rival in enumerate(t):
    if rival == 'r':
        if i - k >= 0 and me[i - k] == 'p':
            if i + k < n:
                if t[i + k] == 'p':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('s')
                elif t[i + k] == 'r':
                    me.append('s')
        else:
            me.append('p')
            ret += p
    elif rival == 's':
        if i - k >= 0 and me[i - k] == 'r':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('s')
                elif t[i + k] == 'p':
                    me.append('p')
                elif t[i + k] == 's':
                    me.append('s')
        else:
            me.append('r')
            ret += r
    elif rival == 'p':
        if i - k >= 0 and me[i - k] == 's':
            if i + k < n:
                if t[i + k] == 'r':
                    me.append('r')
                elif t[i + k] == 's':
                    me.append('p')
                elif t[i + k] == 'p':
                    me.append('r')
        else:
            me.append('s')
            ret += s
print(ret)

Solution (Python):
N, K = map(int,input().split())
z_list = list(map(int,input().split()))
T = str(input())
sum = 0
aiko_flag=[0] * K

for i in range(N):
    if i > K-1:
        if T[i] == 'r' and (T[i-K] != 'r' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[2]
            aiko_flag[i % K] = 0
        elif T[i] == 's'and (T[i-K] != 's' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[0]
            aiko_flag[i % K] = 0
        elif T[i] == 'p' and (T[i-K] != 'p' or aiko_flag[(i-K) % K]) == 1:
            sum = sum + z_list[1]
            aiko_flag[i % K] = 0
        else:
            aiko_flag[i % K] = 1
    elif i < K:
        if T[i] == 'r':
            sum = sum + z_list[2]
        elif T[i] == 's':
            sum = sum + z_list[0]
        elif T[i] == 'p':
            sum = sum + z_list[1]

print(sum)

Solution (Python):
import sys

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

n=int(n)
k=int(k)
r=int(r)
s=int(s)
p=int(p)

esp=list(t)

totalWin=0

for i in range(len(esp)):
    win=0
    if esp[i]=="r": 
        win=p
    elif esp[i]=="s": 
        win=r
    elif esp[i]=="p": 
        win=s
    
    bef=(i-k)

    if bef>=0:
        if esp[bef]==esp[i]:
            win=0
            esp[i]="v"

    totalWin+=win

print(totalWin)
sys.exit()

Solution (Python):
from collections import deque

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

history = deque()
score = 0

def bestChoiceTo(op):
    if op == 'r':
        return 'p'
    if op == 's':
        return 'r'
    if op == 'p':
        return 's'

for i, op in enumerate(t):
    bestChoice = bestChoiceTo(op)
    futureBestChoice = '' if i + k >= n else bestChoiceTo(t[i + k])
    prohibitedChoice = '' if i < k else history.popleft()
    if bestChoice != prohibitedChoice:
        choice = bestChoice
        if choice == 'r': score += r
        if choice == 's': score += s
        if choice == 'p': score += p
    else:
        if futureBestChoice == prohibitedChoice:
            choice = 'x'
        else:
            choices = ['r', 's', 'p']
            if prohibitedChoice != '':
                choices.remove(prohibitedChoice)
            if futureBestChoice != '':
                choices.remove(futureBestChoice)
            choice = choices[0]
    history.append(choice)

print(score)

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

score_dic = {}
score_dic['r']=P
score_dic['s']=R
score_dic['p']=S

past_list = []

score = 0
for i in range(N):
    t = T[i]
    if i >= K:


        if past_list[i-K] != t:
            score += score_dic[t]
            past_list.append(t)
        else:
            past_list.append("n")
    else:
        score += score_dic[t]
        past_list.append(t)

print(score)

Solution (Python):
n,k=map(int,input().split())
point=list(map(int,input().split()))
s=input()
t=[]
d={'r':0,'s':1,'p':2}
for x in s:
    t.append(d[x])
d=[]
for i in range(k):
    d.append(point[ (t[i]+2)%3 ])
    
for i in range(k,n):
    if t[i]==t[i-k] and d[i-k]!=0:
        d.append(0)
    else:
        d.append(point[ (t[i]+2)%3 ])
print(sum(d))

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

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

t = list(input())

point = []

for i in range(n):
  if i >= k:
    if t[i] == t[i - k]:
      if point[i - k] != 0:
        point.append(0)
        continue

  if t[i] == "r":
    point.append(p)
  elif t[i] == "s":
    point.append(r)
  elif t[i] == "p":
    point.append(s)

print(sum(point))

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

A = []

for i in range(0, N):
    if i <= K-1:
        A.append(optimal[T[i]])
        ans = ans + point[A[i]]
    elif A[i-K] != optimal[T[i]]:
        A.append(optimal[T[i]])
        ans = ans + point[A[i]]
    elif i + K <= N-1:
        if optimal[T[i]] != 'r' and optimal[T[i+K]] != 'r':
            A.append('r')
        elif optimal[T[i]] != 's' and optimal[T[i+K]] != 's':
            A.append('s')
        else:
            A.append('p')
    else:
        if optimal[T[i]] != 'r':
            A.append('s')
        elif optimal[T[i]] != 's':
            A.append('p')
        else:
            A.append('r')

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    pre = [None] * n

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

if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
T_my = ['?' for i in range(N)]
ans = 0
same_flag = 0




for i in range(N):
  if i < K: 
    if T[i] == 'r':
      ans += P
      T_my[i] = 'p'
    if T[i] == 's':
      ans += R
      T_my[i] = 'r'
    if T[i] == 'p':
      ans += S
      T_my[i] = 's'
  else:
    if T[i] == 'r':
      T_my[i] = 'p'
    if T[i] == 's':
      T_my[i] = 'r'
    if T[i] == 'p':
      T_my[i] = 's'
    if T[i] == T[i-K]:
      if T_my[i-K] == T_my[i]:
        T_my[i] = '?'
      else:
        if T[i] == 'r':
          ans += P
        if T[i] == 's':
          ans += R
        if T[i] == 'p':
          ans += S
    else:
      if T[i] == 'r':
        ans += P
      if T[i] == 's':
        ans += R
      if T[i] == 'p':
        ans += S
        
print(ans)

Solution (Python):
import numpy as np
from collections import Counter

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

i = 0
minus = 0
for i in np.arange(K):
    arr = T[np.arange(i, N, K)]
    c = 1
    for j in np.arange(arr.shape[0] - 1):
        if arr[j] == arr[j + 1]:
            c += 1
        else:
            if c >= 2:
                if arr[j] == 'r':
                    score = P
                elif arr[j] == 's':
                    score = R
                else:
                    score = S
                minus += c // 2 * score
            c = 1
    else:
        if c >= 2:
            if arr[j] == 'r':
                score = P
            elif arr[j] == 's':
                score = R
            else:
                score = S
            minus += c // 2 * score

counter = Counter(T)


max_score = 0
for i, j in counter.items():
    if i == 'r':
        max_score += P * j
    elif i == 's':
        max_score += R * j
    else:
        max_score += S * j
print(max_score - minus)

Solution (Python):
import math
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = 0
def calc_score(a):
    ret = 0
    if a == "s":
        ret = R
    elif a == "p":
        ret = S
    elif a == "r":
        ret = P
    return ret
    
for i in range(K):
    tmp = T[i::K]
    
    score += calc_score(tmp[0])
    flag = True
    for j in range(1,len(tmp)):
        if tmp[j] == tmp[j - 1]:
            flag = not flag
        else:
            flag = True
        if flag:
            
            score += calc_score(tmp[j])
print(score)

Solution (Python):
N, K = (int(i) for i in input().split())
R, S, P = (int(i) for i in input().split())
T = input()
hands = []
a = ['r', 's', 'p']

def add(s):
    if s == 'r': return P
    if s == 's': return R
    else: return S
   
def win(s):
    if s == 'r': return 'p'
    if s == 's': return 'r'
    else: return 's'
         
point = 0
for i in range(N):
    if i < K: 
        hands.append(win(T[i]))
        point += add(T[i])
    else:
        if win(T[i]) != hands[i-K]:
            hands.append(win(T[i]))
            point += add(T[i])
        else:
            if i+K < N:
                a.remove(win(T[i]))
                if win(T[i+K]) != win(T[i]):
                    a.remove(win(T[i+K]))
                
                hands.append(a[0])
                a = ['r', 's', 'p']
            else:
                hands.append(T[i])

print(point)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
point = {"r": r, "s": s, "p": p}
v = {"r": "p", "s": "r", "p": "s"}
t = input()
ans = 0
for i in range(k):
    for j in range(i, n, k):
        if j == i:
            prev = v[t[j]]
            ans += point[v[t[j]]]
        else:
            if v[t[j]] != prev:
                ans += point[v[t[j]]]
                prev = v[t[j]]
            else:
                
                if t[j] == "r":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "s"
                        elif t[j + k] == "s":
                            prev = "s"
                        else:
                            prev = "r"
                elif t[j] == "s":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "s"
                        elif t[j + k] == "s":
                            prev = "s"
                        else:
                            prev = "p"
                elif t[j] == "p":
                    if j + k < n:
                        if t[j + k] == "r":
                            prev = "r"
                        elif t[j + k] == "s":
                            prev = "p"
                        else:
                            prev = "r"
print(ans)

Solution (Python):
def GetPoint(a, b, p):
    if (a == 0 and b == 1) or (a == 1 and b == 2) or (a == 2 and b == 0):
        return p
    else:
        return 0


N, K = map(int, input().split())
G, C, P = map(int, input().split())
s = input()
A = [[] for i in range(K)]
for i in range(N):
    c = i % K
    if s[i] == 'r':
        A[c].append(0)
    elif s[i] == 's':
        A[c].append(1)
    else:
        A[c].append(2)

ans = 0

for S in A:
    l = len(S)
    dp = [[0 for i in range(3)] for i in range(l + 1)]
    for i in range(l):
        dp[i + 1][0] = max(dp[i][1], dp[i][2]) + GetPoint(0, S[i], G)
        dp[i + 1][1] = max(dp[i][0], dp[i][2]) + GetPoint(1, S[i], C)
        dp[i + 1][2] = max(dp[i][0], dp[i][1]) + GetPoint(2, S[i], P)

    ans += max(dp[l][0], dp[l][1], dp[l][2])

print(ans)