Problem:
 Score : 400 points    Problem Statement At an arcade, Takahashi is playing a game called RPS Battle , which is played as follows:   The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)  Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):  R points for winning with Rock;  S points for winning with Scissors;  P points for winning with Paper.    However, in the i -th round, the player cannot use the hand he/she used in the (i-K) -th round. (In the first K rounds, the player can use any hand.)   Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.  The information Takahashi obtained is given as a string T . If the i -th character of T  (1 \leq i \leq N) is r , the machine will play Rock in the i -th round. Similarly, p and s stand for Paper and Scissors, respectively.  What is the maximum total score earned in the game by adequately choosing the hand to play in each round?      Notes In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.   If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;  if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;  if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;  if both players play the same hand, it is a draw.       Constraints  2 \leq N \leq 10^5  1 \leq K \leq N-1  1 \leq R,S,P \leq 10^4  N,K,R,S, and P are all integers.  |T| = N  T consists of r , p , and s .         Input Input is given from Standard Input in the following format:  N  K  R  S  P  T       Output Print the maximum total score earned in the game.        Sample Input 1 5 2
8 7 6
rsrpr      Sample Output 1 27  The machine will play {Rock, Scissors, Rock, Paper, Rock}.  We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27 .       Sample Input 2 7 1
100 10 1
ssssppr      Sample Output 2 211       Sample Input 3 30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr      Sample Output 3 4996

Solution (Python):

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

    command = [''] * N
    ans = 0
    for i, t in enumerate(T):
        if t == 'r':
            point = P
            command_candidate = 'p'
        elif t == 's':
            point = R
            command_candidate = 'r'
        else:
            point = S
            command_candidate = 's'

        if i >= K and command[i - K] == command_candidate:
            point = 0
            command_candidate = ''
        ans += point
        command[i] = command_candidate
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def rle(a):
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append([l,a[i]])
            l=1
    ll.append([l,a[-1]])
    return ll
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
w=[]
for i in range(k):
    j=i
    b=""
    while j<n:
        b+=t[j]
        j+=k
    w.append(b)
for i in t:
    if i=="r":
        ans+=p
    elif i=="s":
        ans+=r
    else:
        ans+=s
for x in w:
    st=rle(x)
    for i,j in st:
        if j=="r":
            ans-=(i//2)*p
        elif j=="s":
            ans-=(i//2)*r
        else:
            ans-=(i//2)*s
print(ans)

Solution (Python):
n,k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(str(input()))
ans = 0
S = [-1]*n
for i in range(n):
    if i <= k-1:
        if T[i] == 'r':
            S[i] = 'p'
            ans += p
        elif T[i] == 's':
            S[i] = 'r'
            ans += r
        else:
            S[i] = 's'
            ans += s
    else:
        if T[i] == 'r':
            if S[i-k] != 'p':
                S[i] = 'p'
                ans += p
            else:
                if i+k <= n-1:
                    if T[i+k] == 's':
                        S[i] = 's'
                    elif T[i+k] == 'p':
                        S[i] = 'r'
                    else:
                        S[i] = 'r'
                else:
                    S[i] = 'r'
        elif T[i] == 's':
            if S[i-k] != 'r':
                S[i] = 'r'
                ans += r
            else:
                if i+k <= n-1:
                    if T[i+k] == 'p':
                        S[i] = 'p'
                    elif T[i+k] == 'r':
                        S[i] = 's'
                    else:
                        S[i] = 's'
                else:
                    S[i] = 's'
        else:
            if S[i-k] != 's':
                S[i] = 's'
                ans += s
            else:
                if i+k <= n-1:
                    if T[i+k] == 's':
                        S[i] = 'p'
                    elif T[i+k] == 'r':
                        S[i] = 'r'
                    else:
                        S[i] = 'p'
                else:
                    S[i] = 'p'
print(ans)

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

    ans = 0
    U = []
    for i, t in enumerate(T):
        if t == 'r':
            if i-K >= 0:
                if U[i-K] == 'p':
                    U.append('rs')
                    continue
            U.append('p')
            ans += P
        elif t == 's':
            if i-K >= 0:
                if U[i-K] == 'r':
                    U.append('sp')
                    continue
            U.append('r')
            ans += R
        else:
            if i-K >= 0:
                if U[i-K] == 's':
                    U.append('rp')
                    continue
            U.append('s')
            ans += S
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
rsp={"r":p,"s":r,"p":s}
sumt=0
chk=[1]*n
for i in range(len(t)):
  sumt+=rsp[t[i]]
  if i>=k and t[i]==t[i-k]:
    chk[i]=chk[i-k]+1
    chk[i-k]=1
ans=sumt
for i in range(len(chk)):
  if chk[i]!=1:
    chg=chk[i]//2
    ans-=(rsp[t[i]])*chg
print(ans)

Solution (Python):
def inp(r, s, p, jan):
  if jan == 's':
    return r
  elif jan == 'p':
    return s
  else:
    return p

n, k = map(int, input().split(" "))
r,s,p = map(int, input().split(" "))
t = list(input())
total = 0
for i in range(n):
  if i < k:
    
    total += inp(r,s,p,t[i])
  else:
    if t[i] != t[i-k]:
      
      total += inp(r,s,p,t[i])
    else:
      
      t[i] = 'x'
print(total)

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

t_list = [ c for c in t ]
for i in range( k, n ):
    if t_list[ i ] == t_list[ i - k ]:
        t_list[ i ] = "x"

score = 0
for hand in t_list:
    if hand == "r":
        score += p
    elif hand == "s":
        score += r
    elif hand == "p":
        score += s

print( score )

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    hand = [0] * N
    ans = 0
    for i in range(N):
        win = (T[i] - 1) % 3
        if i < K or hand[i - K] != win:
            hand[i] = win
            ans += point[win]
        else:
            hand[i] = -1

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    d = {'r': R, 's': S, 'p': P}
    T = input()
    scores = []
    for t in T:
        if t == 'r':
            scores.append('p')
        elif t == 's':
            scores.append('r')
        else:
            scores.append('s')
    for i in range(N-K):
        if scores[i] == scores[i+K]:
            scores[i+K] = 0
    print(sum([d[v] for v in scores if v in ['p', 'r', 's']]))


if __name__ == '__main__':
    main()

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

moi=[['x'] for i in range(K)]

for i in range(N):
    moi[i%K].append(T[i])

for i in range(K):
    moi[i].pop(0)

moji=''

for i in range(K):
    cnt=0
    for j in range(len(moi[i])):
        if j==0:
            moji+=moi[i][j]
        else:
            if moi[i][j-1]!=moi[i][j]:
                moji+=moi[i][j]
                cnt=0
            if moi[i][j-1]==moi[i][j] and cnt==1:
                moji+=moi[i][j]
                cnt=0
            elif moi[i][j-1]==moi[i][j] and cnt==0:
                cnt+=1

ans=0
for i in range(len(moji)):
    if moji[i]=='r':
        ans+=P
    elif moji[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=input()
ans=0
for i in range(k):
  R=0
  S=0
  P=0
  cnt=0
  while i+cnt*k<n:
    if i+cnt*k-k<0:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    elif i+cnt*k-k>=0 and t[i+cnt*k]==t[i+cnt*k-k]:
      if t[i+cnt*k]=='r' and P==0:
        ans+=p
        P=1
      elif t[i+cnt*k]=='r' and P==1:
        P=0
      elif t[i+cnt*k]=='s' and R==0:
        ans+=r
        R=1
      elif t[i+cnt*k]=='s' and R==1:
        R=0
      elif t[i+cnt*k]=='p' and S==0:
        ans+=s
        S=1
      elif t[i+cnt*k]=='p' and S==1:
        S=0
    elif i+cnt*k-k>=0 and t[i+cnt*k]!=t[i+cnt*k-k]:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    cnt+=1
print(ans)

Solution (Python):

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

S=input()
li=[1]*(K)
ans=[0,0,0]

for i in range(N-K):
    if S[i]==S[i+K]:
        li[i%K]+=1
    else:
        if S[i]=="r":
            ans[0]+=(li[i%K]+1)//2
        if S[i]=="s":
            ans[1]+=(li[i%K]+1)//2
        if S[i]=="p":
            ans[2]+=(li[i%K]+1)//2
        li[i%K]=1


for j in range(N-K,N):
    if S[j]=="r":
            ans[0]+=(li[j%K]+1)//2
    if S[j]=="s":
            ans[1]+=(li[j%K]+1)//2
    if S[j]=="p":
            ans[2]+=(li[j%K]+1)//2
    
    
print(p*ans[0]+r*ans[1]+s*ans[2])

Solution (Python):
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
cnt=[0 for _ in range(N)]
score={'r':P,'s':R,'p':S}
for i in range(N):
    if i<K:
        cnt[i]=score[T[i]]
    else:
        if T[i-K]!=T[i]:
            cnt[i]=score[T[i]]
        else:
            if cnt[i-K]==0:
                cnt[i]=score[T[i]]
print(sum(cnt))

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

for n in range(N-K):
  if T[n+K]==T[n]:
    T[n+K]=""
    
print(P*T.count("r")+R*T.count("s")+S*T.count("p"))

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

Solution (Python):
n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = list(input())
ans = 0
dic = {'p': s, 's': r, 'r': p}
for i in range(k):
  ans += dic[t[i]]
dic_ls = []
for i in range(k, n):
  if t[i] != t[i - k]:
    ans += dic[t[i]]
  else:
    t[i] = 'j'
print(ans)

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

tt = list(t)
d = {'r':p, 's': r, 'p':s}
dp = [d[t[i]] for i in range(n)]

for i in range(n-k):
    if tt[i]==tt[i+k]:
        dp[i+k] = 0
        tt[i+k] = 'x'

print(sum(dp))

Solution (Python):
N,K = map(int,input().split())
S,P,R = map(int,input().split())
point = {'s':S,'p':P,'r':R}
T = str(input())
max_point = 0
for i in range(K):
  while i < N:
    if i+K < N and T[i] == T[i+K]:
      max_point += point[T[i]]
      i = i+2*K
    else:
      max_point += point[T[i]]
      i = i+K
print(max_point)

Solution (Python):
import math

def main():
	N, K = [int(n) for n in input().split(" ")]
	R, S, P = [int(p) for p in input().split(" ")]
	T = list(input())
	T.reverse()
	cnt = 0
	for i in range(K):
		tmp = []
		for j in range(math.ceil((N - i) / K)):
			if len(tmp) == 0 or T[i + j * K] != tmp[-1][0]:
				if T[i + j * K] == "r":
					tmp.append(["r", P, 1])
				elif T[i + j * K] == "s":
					tmp.append(["s", R, 1])
				elif T[i + j * K] == "p":
					tmp.append(["p", S, 1])
			else:
				tmp[-1][2] += 1
		for l in range(len(tmp)):
			cnt += tmp[l][1] * math.ceil(tmp[l][2] / 2)

	print(cnt)


main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    hand = [0] * N
    ans = 0
    for i, h in enumerate(T):
        win = (h - 1) % 3
        if i >= K and win == hand[i - K]:
            hand[i] = -1
        else:
            hand[i] = win
            ans += point[win]

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    ans = 0
    for i in range(K):
        vec = T[i::K]
        M = len(vec)

        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == vec[j]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    ans = 0
    for i in range(K):
        M = (N - i + K - 1) // K
        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == T[i + j * K]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    ans = 0
    for i in range(K):
        vec = T[i::K]
        M = len(vec)

        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == vec[j]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


if __name__ == '__main__':
    main()

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

scr = 0

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




for i in range(k, n):
    if t[i] == t[i - k]:
        scr += 0
        t[i] = "q"
    else:
        if t[i] == "r":
            scr += p
        elif t[i] == "s":
            scr += r
        elif t[i] == "p":
            scr += s

print(scr)

Solution (Python):
f=lambda:map(int,input().split())
N,K=f()
r,s,p=f()
T=list(input())
F=[0]*N
res=0
for i in range(N):
    if F[i]==1:
        continue
    if i+K<N:
        if T[i]==T[i+K]:
            F[i+K]=1
    if T[i]=='s':
        res+=r
    elif T[i]=='p':
        res+=s
    else:
        res+=p
print(res)

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

w1=''
w2=''
ans=0
for i in range(N):
    if T[i]=='r':
        w1+='p'
    elif T[i]=='p':
        w1+='s'
    else:
        w1+='r'

for i in range(0,K):
    if T[i]=='r':
        w2+='p'
        ans+=P
    elif T[i]=='p':
        w2+='s'
        ans+=S
    else:
        w2+='r'
        ans+=R

if N>=2:
    for i in range(K,N):
        if w1[i]==w2[i-K]:
            w2+='_'
        else:
            if w1[i]=='p':
                w2+='p'
                ans+=P
            elif w1[i]=='s':
                w2+='s'
                ans+=S
            else:
                w2+='r'
                ans+=R
print(ans)

Solution (Python):
import copy

N, K = map(int, input().split())
my_R, my_S, my_P = map(int, input().split())
hand_score = {'r': my_P, 's': my_R, 'p': my_S}
T = input()
ans = 0

for i in range(K):
    cnt = (N - i - 1) // K + 1
    dp = {'R': 0, 'S': 0, 'P': 0}
    new_dp = {'R': 0, 'S': 0, 'P': 0}
    for j in range(cnt):
        check = {'r': 0, 's': 0, 'p': 0}
        check[T[(j * K) + i]] = hand_score[T[(j * K) + i]]

        new_dp['R'] = max(dp['S'] + check['s'], dp['P'] + check['p'])
        new_dp['S'] = max(dp['P'] + check['p'], dp['R'] + check['r'])
        new_dp['P'] = max(dp['R'] + check['r'], dp['S'] + check['s'])

        dp = copy.copy(new_dp)
    ans += max(dp.values())
print(ans)

Solution (Python):
from collections import deque

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

d = {"s":r, "p":s, "r":p}
before = deque([])
ans = 0
for i in range(n):
    if i<k:
        before.append(t[i])
        ans += d[t[i]]
    else:
        b = before.popleft()
        if t[i]!=b:
            before.append(t[i])
            ans += d[t[i]]
        else:
            before.append("n")
print(ans)

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

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



T = [0 if i=="r" else 1 if i=="s" else 2 for i in input()]

dp = [[0]*3 for i in range(N)]


for i in range(N):
    if i<K:
        for j in range(3):
            
            
            
            if (j+1)%3==T[i]:
                dp[i][j]=RST[j]
    else:
        
        for j in range(3):
            
            for l in range(3):
                if j!=l:
                    if (j+1)%3 == T[i]:
                        dp[i][j] = max(dp[i][j],RST[j]+dp[i-K][l])
                    else:
                        dp[i][j] = max(dp[i][j],dp[i-K][l])

ans = 0
for i in range(N-K,N):
    ans += max(dp[i])

print(ans)

Solution (Python):

N,K = map(int,input().split())
score = list(map(int,input().split()))
T = input()



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

def judge(a,b):
    
    if a == 'r' and b == 2:
        return score[2]
    
    elif a == 's' and b == 0:
        return score[0]
    
    elif a == 'p' and b == 1:
        return score[1]
    return 0
    

for i in range(1,N+1):
    
    for j in range(3):
        
        for k in range(3):
            
            if i > K:
                
                if j == k:
                    continue
            
            else:
                
                dp[i][j] = judge(T[i-1],j)
                continue
            
            dp[i][j] = max(dp[i][j],dp[i-K][k] + judge(T[i-1],j))

ans = 0
for a in dp[-K:]:
    ans += max(a)
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
d = []
for i in range(N):
    if T[i] == "r":
        if i < K or d[i-K] != "p":
            d.append("p")
        else:
            d.append("a")
    elif T[i] == "s":
        if i < K or d[i-K] != "r":
            d.append("r")
        else:
            d.append("a")
    else:
        if i < K or d[i-K] != "s":
            d.append("s")
        else:
            d.append("a")
ans = d.count("r") * R + d.count("s") * S + d.count("p") * P
print(ans)

Solution (Python):
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
d = []
for i in range(N):
    if T[i] == "r":
        if i < K or d[i-K] != "p":
            d.append("p")
        else:
            d.append("a")
    elif T[i] == "s":
        if i < K or d[i-K] != "r":
            d.append("r")
        else:
            d.append("a")
    else:
        if i < K or d[i-K] != "s":
            d.append("s")
        else:
            d.append("a")
ans = d.count("r") * R + d.count("s") * S + d.count("p") * P
print(ans)

Solution (Python):

n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
win=[False]*n
def janken(char):
    if char=='r':
        return p
    elif char=='s':
        return r
    else:
        return s
cnt=0
for i in range(n):
    if i-k<0:
        point=janken(t[i])
        cnt+=point
        win[i]=True
    else:
        if t[i]!=t[i-k]:
            point=janken(t[i])
            cnt+=point
            win[i]=True
        else:
            if not win[i-k]:
                point=janken(t[i])
                cnt+=point
                win[i]=True

print(cnt)

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

Solution (Python):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    R,S,P=MI()
    T=input()
    L=[]
    
    ans=0
    for i in range(N):
        pre="z"
        if i-K>=0:
            pre=L[i-K]
            
        if T[i]=="r":
            if pre!="p":
                ans+=P
                L.append("p")
            else:
                L.append("z")
                
        if T[i]=="s":
            if pre!="r":
                ans+=R
                L.append("r")
            else:
                L.append("z")

        if T[i]=="p":
            if pre!="s":
                ans+=S
                L.append("s")
            else:
                L.append("z")
                
    print(ans)
                
    

main()

Solution (Python):
from math import ceil
def point(x,y,r,s,p):
    if y == 'r':
        y = p
    if y == 's':
        y = r
    if y == 'p':
        y = s
    return ceil(x/2) * y
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
ans = 0
for _ in range(k):
    l = t[_::k]
    x = 1
    y = l[0]
    for i in l[1:]:
        if i != y:
            ans += point(x,y,r,s,p)
            x = 1
            y = i
        else:
            x += 1
    ans += point(x,y,r,s,p)
print(ans)

Solution (Python):
from collections import deque


def calc(t, r, s, p):
    if t == "r":
        return p
    elif t == "s":
        return r
    else:
        return s


n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = input()
d = deque()
ans = 0
for i, t in enumerate(T):
    if i < k:
        ans += calc(t, r, s, p)
        d.append(t)
    else:
        x = d.popleft()
        if x == t:
            d.append(0)
        else:
            ans += calc(t, r, s, p)
            d.append(t)
print(ans)

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

point = {"r":(p,"p"), "s":(r,"r"), "p":(s,"s")}
ans = 0

hand = ""
for i in range(n):
  if i < k:
    ans += point[t[i]][0]
    hand += point[t[i]][1]
  else:
    if (t[i-k] != t[i]):
      ans += point[t[i]][0]
      hand += point[t[i]][1]
    else:
      if hand[i-k] != point[t[i]][1]:
        ans += point[t[i]][0]
        hand += point[t[i]][1]
      else:
        if (i+k) < n:
          hand += "rsp".replace(point[t[i+k]][1],"").replace(hand[i-k],"")[0]
        else:
          hand += "rsp".replace(hand[i-k],"")[0]
  
      
print(ans)

Solution (Python):

"""
Created on Tue Sep  8 22:36:36 2020

@author: liang
"""

N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
hand = {"r":P, "s":R, "p":S}
ans = 0
for i in range(K):
    s = str()
    tmp = i
    while tmp < N:
        s += T[tmp]
        tmp += K
    
    count = 1
    prev = str()
    for j in range(len(s)):
        if s[j] == prev:
            count += 1
        else:
            count = 1
        if count%2 == 0:
            continue
        prev = s[j]
        """
        if s[j] == "r":
            ans += P
        elif s[j] == "s":
            ans += R
        else:
            ans += S
        """
        ans += hand[s[j]]
print(ans)

Solution (Python):

"""
Created on Tue Sep  8 22:36:36 2020

@author: liang
"""

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

ans = 0
for i in range(0,K):
    s = str()
    tmp = i
    while tmp < N:
        s += T[tmp]
        tmp += K
    
    count = 1
    prev = str()
    for j in range(len(s)):
        if s[j] == prev:
            count += 1
        else:
            count = 1
        if count%2 == 0:
            continue
        prev = s[j]
        if s[j] == "r":
            ans += P
        elif s[j] == "s":
            ans += R
        else:
            ans += S
print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N,K = mint()
R,S,P = mint()
T = input()
win = [False]*K
ans = 0
score = {'r':P, 's':R, 'p':S}
for i in range(N):
    if win[i] and T[i]==T[i-K]:
        win.append(False)
    else:
        win.append(True)
        ans += score[T[i]]
print(ans)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

import string

def main():
    n,k = i_map()
    r,s,p = i_map()
    dic = {"r":p,"s":r,"p":s}
    t = list(s_input())
    ans = 0

    for i,j in enumerate(t):
        if i < k:
            ans += dic[j]
        else:
            if j != t[i-k]:
                ans += dic[j]
            else:
                t[i] = "z"
    print(ans)



if __name__=="__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
scores = list(map(int, input().split()))
tables = {'s': 0, 'p': 1, 'r': 2}

patterns = input()
answer = 0
losers = [0 for _ in range(N)]
for i in range(N):
  if i - K < 0:
    answer += scores[tables[patterns[i]]]
  else:
    if patterns[i] == patterns[i-K]:
      if losers[i-K] == 0:
        losers[i] = 1
      else:
        answer += scores[tables[patterns[i]]]
    else:
      answer += scores[tables[patterns[i]]]
print(answer)

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

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


ans = 0
flag = 0

for i in range(len(T_split)):
    for j in range(len(T_split[i])):
        if flag == 1:
            flag = 0
            continue
        if T_split[i][j] == 'r':
            point = P
        elif T_split[i][j] == 's':
            point = R
        elif T_split[i][j] == 'p':
            point = S
        ans += point
        if j != len(T_split[i]) - 1:
            if T_split[i][j] == T_split[i][j+1]:
                flag = 1
print(ans)

Solution (Python):
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
dp = [0] * (len(t) + 1)
dp[0] = 0

def pon(d):
 if d  == 'r':
    return p
 elif d == 's':
    return r
 else:
    return s

m = []
for i,j in enumerate(t):
    m.append(j)
    if i >= k:
        if m[i-k] == j:
            dp[i+1] = dp[i]
            m[i] = 'skip'
        else:
            dp[i+1] = dp[i]+pon(j)
    else:
        dp[i+1] = dp[i] + pon(j)
print(dp[-1])

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

res = 0
log = []
for i in range(len(read_data)):
    
    if len(log) < k:
        if read_data[i] == 'r':
            res+=p
        if read_data[i] == 's':
            res+=r
        if read_data[i] == 'p':
            res+=s

        log.append(read_data[i])

    else:
        tmp = log.pop(0)
        if tmp != read_data[i]:
            if read_data[i] == 'r':
                res+=p
            if read_data[i] == 's':
                res+=r
            if read_data[i] == 'p':
                res+=s
            log.append(read_data[i])
        else:
            log.append('-')
    
    
print(res)