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



Sample input:
5 2
8 7 6
rsrpr


Sample output:
27

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
T=[a for a in T]
answer=0
takahashi=[]
if N%K==0:
    number=(N/K-1)*K
else:
    number=(N//K)*K
for a in range(K):
    if T[a]=='r':
        answer+=P
        takahashi.append('p')
    elif T[a]=='s':
        answer+=R
        takahashi.append('r')
    else:
        answer+=S
        takahashi.append('s')
for a in range(K,int(number)):
    if T[a]=='r':
        if takahashi[a-K]=='p':
            if T[a+K]=='s':
                takahashi.append('s')
            else:
                takahashi.append('r')
        else:
            answer+=P
            takahashi.append('p')
    elif T[a]=='s':
        if takahashi[a-K]=='r':
            if T[a+K]=='p':
                takahashi.append('p')
            else:
                takahashi.append('s')
        else:
            answer+=R
            takahashi.append('r')
    else:
        if takahashi[a-K]=='s':
            if T[a+K]=='r':
                takahashi.append('r')
            else:
                takahashi.append('p')
        else:
            answer+=S
            takahashi.append('s')
for a in range(int(number),N):
    if T[a]=='r':
        if takahashi[a-K]=='p':
            pass
        else:
            answer+=P
    elif T[a]=='s':
        if takahashi[a-K]=='r':
           pass
        else:
            answer+=R
    else:
        if takahashi[a-K]=='s':
            pass
        else:
            answer+=S
print(answer)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t_list = input()
pre_list = []
towin = {"r": "p", "s": "r", "p": "s"}
topoint = {"r": r, "s": s, "p": p}
pre = {}
if s >= p:
    pre["r"] = "s"
else:
    pre["r"] = "p"
if s >= r:
    pre["s"] = "r"
else:
    pre["s"] = "p"
if r >= s:
    pre["p"] = "r"
else:
    pre["p"] = "s"
ans = 0
for i in range(n):
    win_hand = towin[t_list[i]]
    if i < k:
       ans += topoint[win_hand]
       pre_list.append(win_hand)
    else:
        if win_hand == pre_list[i - k]:
            if i <= n - k:
                hand = pre[t_list[i + k]]
                pre_list.append(hand)
        else:
            ans += topoint[win_hand]
            pre_list.append(win_hand)
print(ans)

Submission status: Runtime Error

Solution code:
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 = []
    point = []
    for i in range(k):
        stri.append([])
        point.append([])
    for i in range(num):
        stri[i%k].append(t[i])
        point[i%k].append(conv(r,s,p,t[i]))
    return stri,point
def main(q,r,s,p):
    if len(q) == 1:
        return point[0]
    if len(q) == 0:
        return 0
    if q[0] != q[1]:
        return main(q[1:],point[1:]) + point[0]
    else:
        return main(q[2:],point[2:]) + point[0]
num,k = listint()
r,s,p = listint()
t = input()
afterperse,point = perse(t,k,num)
ans = 0
for i in range(k):
    ans = ans + main(afterperse[i],point)
print(ans)

Submission status: Runtime Error

Solution code:
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,r,s,p):
    if len(q) == 1:
        return conv(r,s,p,q[0])
    if len(q) == 0:
        return 0
    if q[0] != q[1]:
        return main(q[1:],r,s,p) + conv(r,s,p,q[0])
    else:
        return main(q[2:],r,s,p) + conv(r,s,p,q[0])
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],r,s,p)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split()) #RSP グーチョキパー
T=list(input())

c=0

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

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

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

print(c)

Submission status: Runtime Error

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

def MaxScore(rsp, t):
    Score = 0
    t = t + 't'
    for i in range(len(t)):
        if t[i] == t[i+1]:
            pass
        else:
            if t[i] == 'r':
                Score = Score + rsp[2]
            elif t[i] == 's':
                Score = Score + rsp[0]
            else:
                Score = Score + rsp[1]
    return Score

def strsplit(n, t, k):
    tList = [""] * k
    for i in range(n):
        tList[i % k] = tList[i % k] + t[i]
    return tList

RSP = [R, S, P]
Answer = 0
for i in range(K):
    Answer = MaxScore(RSP, strsplit(N, T, K)[i]) + Answer

print(Answer)

Submission status: Runtime Error

Solution code:
#149d

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 t[i]==t[i+k]:
			score-=win(t[i+k])
			t[i+k]='x'
print(score)

Submission status: Runtime Error

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

scores = {'r':p,'s':r,'p':s}


ans = 0
memo = []

for i in range(n):
  if i < k:
    ans += scores[T[i]]
    memo.append(T[i])
  else:
    if memo[i-k] == T[i]:
      memo.append('N')
    else:
      ans += scores[T[i]]
      memo.append(T[i])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

def main(n,k,r,s,p,t):
    score = 0
    for i in range(len(t)):
        if i < k:
            continue
        if t[i] == t[i-k]:
            t[i] = "N"
    c = Counter(t)
    i = dict(c)
    score += i["r"]*p + i["s"]*r + i["p"]*s
    return score
    
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = list(input())
print(main(n,k,r,s,p,t))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
count_rsp = [0,0,0]
for i in range(k):
  hand = ['N']*(n//k+1)
  if t[i] == 'r':
      hand[0] = 'p'
      count_rsp[2] += 1
  elif t[i] == 's':
      hand[0] = 'r'
      count_rsp[0] += 1
  else:
      hand[0] = 's'
      count_rsp[1] += 1
  for j in range(i+k,n,k):
    if t[j] == 'r':
      if hand[j//k-1] != 'p':
        hand[j//k] = 'p'
        count_rsp[2] += 1
      else:
        if j+k <= n and t[j+k] == 'p':
          hand[j//k] = 'r'
        else:
          hand[j//k] = 's'
    elif t[j] == 's':
      if hand[j//k-1] != 'r':
        hand[j//k] = 'r'
        count_rsp[0] += 1
      else:
        if j+k <= n and t[j+k] == 's':
          hand[j//k] = 'p'
        else:
          hand[j//k] = 's'
    else:
      if hand[j//k-1] != 's':
        hand[j//k] = 's'
        count_rsp[1] += 1
      else:
        if j+k <= n and t[j+k] == 's':
          hand[j//k] = 'p'
        else:
          hand[j//k] = 'r'
print(count_rsp[0]*r+count_rsp[1]*s+count_rsp[2]*p)

Submission status: Runtime Error

Solution code:
N,K,R,S,P = map(int,input().split())
T = str(input())
i = 0
point = 0
for i in range(N):
  if T[i] == "r" and T[K] != "p":
    point += P
  elif T[i] == "s" and T[K] != "r":
    point += R
  elif T[i] == "p" and T[K] != "s":
    point += S
  else:
    break
  i += 1
print(point)

Submission status: Runtime Error

Solution code:
N,K,R,S,P = map(int,input().split())
T = map(str,input())
i = 0
point = 0
for i in range(N):
  if T[i] == "r" and T[K] != "p":
    point += P
  elif T[i] == "s" and T[K] != "r":
    point += R
  elif T[i] == "p" and T[K] != "s":
    point += S
  else:
    break
  i += 1
print(point)

Submission status: Runtime Error

Solution code:
N,K,R,S,P = map(int,input().split())
T = str(input())
point = 0
for i in range(N):
  if T[i] == "r" and T[K] != "p":
    point += P
  elif T[i] == "s" and T[K] != "r":
    point += R
  elif T[i] == "p" and T[K] != "s":
    point += S
  else:
    break
  i += 1
print(point)

Submission status: Runtime Error

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

commands = [''] * N
ans = 0
for i, t in enumerate(T):
        if t = 'r':
                command = 'p'
                point = P
        elif t = 's':
                command = 'r':
                point = R
        elif t = 'p':
                command = 's':
                point = S
        if i-K >= 0 and commands[i-K] == command:
                command = ''
                point = 0
        ans += point
        commands[i] = command

print(ans)

Submission status: Runtime Error

Solution code:
n=list(map(int,input().split()))
score=list(map(int,input().split()))
s=input()

n[1]-=1

def rscore(ss):
  if ss=='r':
    return score[2]
  elif ss=='s':
    return score[0]
  elif ss=='p':
    return score[1]

result=0
flg=[]
for i in range(n[1]):
  result+=rscore(s[i])
  flg.append(True)
for i in range(n[1],len(s)):
  if(s[i-n[1]-1]==s[i] and flg[i-n[1]-1]):
    flg.append(False)
    continue
  else:
    flg.append(True)
    result+=rscore(s[i])
print(result)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
r, p, s = map(int, input().split())
d = {"r":s, "p":r, "s":p}
t = input()
dp = [0]*n
for i in range(n):
  if i<k and dp[i-k]==d[t[i]]:
    dp[i] = 0
  else:
    dp[i] = d[t[i]]
print(sum(lst))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
r, p, s = map(int, input().split())
d = {"r":s, "p":r, "s":p}
t = input()
dp = [0]*n
for i in range(n):
  if i>k and dp[i-k]==d[t[i]]:
    dp[i] = 0
  else:
    dp[i] = d[t[i]]
print(sum(lst))

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
#r, s, p = map(int, input().split())
s, p, r = map(int, input().split())
t = input()
lst = []
arr = np.array(list(t.replace("r",f"{r} ".replace("p",f"{p} ").replace("s",f"{s} ").split())))
arr.reshape(k, n//k+bool(n%k)).T
print(arr)
for b in lst:
  tmp = ""
  for i in b:
    if i!=tmp:
      ans+=eval(i)
      tmp=i
print(ans)

Submission status: Runtime Error

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

buf=[]
score=0

for tt in t:
    if tt=='p':
        buf.append('s')
    elif tt=='r':
        buf.append('p')
    else:
        buf.append('r')

for b in buf:
    if b=='p':
        score+=p
    elif b=='s':
        score+=s
    else:
        score+=r

for i in range(n):
    if i >= k:
        if buf[i]==buf[i-k]:
            if buf[i]=='p':
                score-=p
                if n-i > k-1:
                    if buf[i+k]=='p':
                        buf[i]='r'      
                    elif buf[i+k]=='r':
                        buf[i]='s'
                    else:
                        buf[i]='r'
            elif buf[i]=='r':
                score-=r
                if n-i > k-1: 
                    if buf[i+k]=='p':
                        buf[i]='s'
                    elif buf[i+k]=='r':
                        buf[i]='p'
                    else:
                        buf[i]='p'
            elif buf[i]=='s':
                score-=s
                if n-i > k-1:
                    if buf[i+k]=='p':
                        buf[i]='r'
                    elif buf[i+k]=='r':
                        buf[i]='p'
                    else:
                        buf[i]='p'

print(score)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)

N, K = [int(v) for v in sys.stdin.readline().split()]
R, S, P = [int(v) for v in sys.stdin.readline().split()]
T = input()

l = [''] * K

for i in range(N):
    l[i % K] += T[i]


def okjudge(rest):
    if len(rest) == 0:
        return 0

    c = rest[0]

    if len(rest) == 1:
        if c == 'r':
            return P
        elif c == 's':
            return R
        else:
            return S
    if rest[1] == rest[0]:
        if c == 'r':
            return max(okjudge(rest[1:]), P + okjudge(rest[2:]))
        elif c == 's':
            return max(okjudge(rest[1:]), R + okjudge(rest[2:]))
        else:
            return max(okjudge(rest[1:]), S + okjudge(rest[2:]))
    elif c == 'r':
        return P + okjudge(rest[1:])
    elif c == 's':
        return R + okjudge(rest[1:])
    else:
        return S + okjudge(rest[1:])

print(sum([okjudge(v) for v in l]))

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=input()
ans=0
for i in range(k):
  u=s[i::k]
  u=u.replace("rr", "rx")
  u=u.replace("pp", "px")
  u=u.replace("ss", "sx")
  ans+=u.count("s")*r
  ans+=u.count("p")*s
  ans+=u.count("r")*p
print(ans)

Submission status: Runtime Error

Solution code:
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p'               
print(score)

Submission status: Runtime Error

Solution code:
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p               
print(score)

Submission status: Runtime Error

Solution code:
from functools import lru_cache
n, k = map(int, input().split(' '))
r, s, p = map(int, input().split(' '))
enemyMoves = input()

def solve(group):
    # becomes DP where we can't repeat the same move twice.
    @lru_cache(None)
    def helper(cur, prev):
        if cur >= len(group):
            return 0
        if group[cur] == 'r':
            if prev != 'p':
                return max(helper(cur + 1, 'r'), helper(cur + 1, 's'), p + helper(cur + 1, 'p'))
            return max(helper(cur + 1, 'r'), helper(cur + 1, 's'))
        elif group[cur] == 'p':
            if prev != 's':
                 return max(helper(cur + 1, 'r'), s + helper(cur + 1, 's'), helper(cur + 1, 'p'))
            return max(helper(cur + 1, 'r'), helper(cur + 1, 'p'))
        else:
            if prev != 'r':
                return max(r + helper(cur + 1, 'r'),  helper(cur + 1, 's'), helper(cur + 1, 'p'))
            return max(helper(cur + 1, 's'), helper(cur + 1, 'p'))
    return helper(0, 'a')

kgroups = []
for i in range(0, k):
    curGroup = []
    cur = i
    while cur < len(enemyMoves):
        curGroup.append(enemyMoves[cur])
        cur += k
    kgroups.append(curGroup)

total = 0
for group in kgroups:
    total += solve(group)
print(total)

Submission status: Runtime Error

Solution code:
n, k = tuple(map(int, input().split(' ')))
r, s, p = tuple(map(int, input().split(' ')))
t = (input())
point = {'r':p, 's':r, 'p':s, 'any':0}
st = {i:t[i] for i in range(k)}
ans1 = sum(map(lambda x: point[x], t[:k]))
ans2 = 0
for i in range(k, n):
    h = t[i]
    m = i%k
    if st[m] != h:
        ans2 += point[h]
        st[m] = h
    else:
        st[m] = 'any' 
print(ans1 + ans2)
return

Submission status: Runtime Error

Solution code:
// {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }

template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}

template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

#ifdef UNO
template<class T> void _dump(const char *s, T &&head) { cerr << s << "=" << head << endl; }
template<class T, class... Args> void _dump(const char *s, T &&head, Args &&... tail) {
    for (int c = 0; *s != ',' || c != 0; cerr << *s++) {
        if (*s == '(' || *s == '[' || *s == '{') c++;
        if (*s == ')' || *s == ']' || *s == '}') c--;
    }
    cerr << "=" << head << ", ";
    _dump(s + 1, tail...);
}
#define dump(...) do { fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); _dump(#__VA_ARGS__, __VA_ARGS__); } while (0)

template<class Iter> ostream &_out(ostream &s, Iter b, Iter e) {
    s << "[";
    for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it;
    return s << "]";
}

template<class A, class B> ostream &operator<<(ostream &s, const pair<A, B> &p) { return s << "(" << p.first << "," << p.second << ")"; }
template<class T> ostream &operator<<(ostream &s, const vector<T> &c) { return _out(s, ALL(c)); }
template<class T, size_t N> ostream &operator<<(ostream &s, const array<T, N> &c) { return _out(s, ALL(c)); }
template<class T> ostream &operator<<(ostream &s, const set<T> &c) { return _out(s, ALL(c)); }
template<class A, class B> ostream &operator<<(ostream &s, const map<A, B> &c) { return _out(s, ALL(c)); }
#else
#define dump(...)
#endif
// }}}

const int MAX_N=100010;
const int Z=1e9;
int n, K, r, s, p;
char T[MAX_N];

int score(char A[MAX_N]) {
    int ans = 0;
    REP(i, n) {
        if(T[i]==A[i]) continue;
        else {
            if(T[i]=='p') ans+=s;
            if(T[i]=='s') ans+=r;
            if(T[i]=='r') ans+=p;
        }
    }
    return ans;
}

void solve() {
    char left[MAX_N], right[MAX_N];
    REP(i, K) {
        if(T[i]=='p') left[i]='s';
        if(T[i]=='s') left[i]='r';
        if(T[i]=='r') left[i]='p';
    }

    for(int i=K; i<n; i++) {
        if(T[i]=='p') {
            if(left[i-K]=='s') left[i]='p';
            else left[i]='s';
        }
        if(T[i]=='s') {
            if(left[i-K]=='r') left[i]='s';
            else left[i]='r';
        }
        if(T[i]=='r') {
            if(left[i-K]=='p') left[i]='r';
            else left[i]='p';
        }
    }

    for(int i = n-1; i>=n-K; i--) {
        if(T[i]=='p') right[i]='s';
        if(T[i]=='s') right[i]='r';
        if(T[i]=='r') right[i]='p';
    }

    for(int i=n-K-1; i>=0; i--) {
        if(T[i]=='p') {
            if(right[i+K]=='s') right[i]='p';
            else right[i]='s';
        }
        if(T[i]=='s') {
            if(right[i+K]=='r') right[i]='s';
            else right[i]='r';
        }
        if(T[i]=='r') {
            if(right[i+K]=='p') right[i]='r';
            else right[i]='p';
        }
    }

    int ans = max(score(left), score(right));
    // cout << left << endl;
    // cout << right << endl;
    W(ans);
}

int main() {
    R(n,K,r,s,p);
    R(T);
    solve();
    return 0;
}

Submission status: Runtime Error

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

choice = ["r", "s", "p"]
score_dic = {}
score_dic["r"] = r
score_dic["s"] = s
score_dic["p"] = p

all_score = 0
# n mod k == i
for i in range(0, k):
    score_child = 0
    log_before = 0
    for one_n in range(1, n+1):
        # あるmodだけ取り出す
        if one_n % k == i:
            # もし勝てるなら勝つ
            if t[one_n-1] == "r" and log_before != "p":
                score_child += score_dic["p"]
                log_before = "p"
            elif t[one_n-1] == "s" and log_before != "r":
                score_child += score_dic["r"]
                log_before = "r"
            elif t[one_n-1] == "p" and log_before != "s":
                score_child += score_dic["s"]
                log_before = "s"

            # 勝てないなら次に邪魔にならないようにする
            elif t[one_n-1] == "r":
                # r or s 
                if one_n-1+k > len(t):
                    pass
                elif t[one_n-1+k] == "r":
                    log_before = "s"
                elif t[one_n-1+k] == "s":
                    log_before = "s"
                elif t[one_n-1+k] == "p":
                    log_before = "r"

            elif t[one_n-1] == "s":
                # s or p  
                if one_n-1+k > len(t):
                    pass
                elif t[one_n-1+k] == "r":
                    log_before = "s"
                elif t[one_n-1+k] == "s":
                    log_before = "s"
                elif t[one_n-1+k] == "p":
                    log_before = "p"

            elif t[one_n-1] == "p":
                # p or r 
                if one_n-1+k > len(t):
                    pass
                elif t[one_n-1+k] == "r":
                    log_before = "r"
                elif t[one_n-1+k] == "s":
                    log_before = "p"
                elif t[one_n-1+k] == "p":
                    log_before = "p"

    all_score += score_child

print(all_score)

Submission status: Runtime Error

Solution code:
n,k = (int(w) for w in input().split()[:2])
r,s,p = (int(w) for w in input().split()[:3])
t = input()

h = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': r, 's': s 'p': p}

x = sum(score[h[t[i]]] for i in range(k))

x += sum(score[h[c]] for c,cp in zip(t[k:], t) if c!=cp)

print(x)

Submission status: Runtime Error

Solution code:
import numpy as np

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

# マシンの出し手を整列させて、K分割してペア作成
# TODO: zero padding
num_split = (N + K - 1) // K
if N % K != 0:
    machine_arr = np.array(list(T) + ['0'] * (N % K)).reshape((-1, num_split), order='F')
else:    
    machine_arr = np.array(list(T)).reshape((-1, num_split), order='F')

ans = 0
for idx in range(machine_arr.shape[0]):
    orig_str = ''.join(machine_arr[idx].tolist())
    rep_str = orig_str.replace('rr', 'r').replace('ss', 's').replace('pp', 'p')
    point = rep_str.count('r') * P + rep_str.count('s') * R + rep_str.count('p') * S
    ans += point

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jan  4 17:24:35 2020

@author: masat
"""

    

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

def janken(myhand, rivalhand):
    point = 0
    if myhand == "r":
        if rivalhand == "s":
            point = R
        else:
            pass
    if myhand == "s":
        if rivalhand == "p":
            point = S
        else:
            pass
    if myhand == "p":
        if rivalhand == "r":
            point = P
        else:
            pass
    return point

quotient = N // K
remainder = N % K

bucket_list = [[] for i in range(K)]

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

best_total_point = 0

for bucket in bucket_list:
    my_hand_list = []
    for now_time in range(len(bucket)):
        if now_time == 0:
            now_rival_hand = bucket[now_time]
            next_rival_hand = bucket[now_time + 1]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                for next_my_hand in ["r", "s", "p"]:
                    if next_my_hand == now_my_hand:
                        pass
                    else:
                        this_point_now_and_next = \
                            janken(now_my_hand, now_rival_hand) \
                            +janken(next_my_hand, next_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)
        elif now_time == len(bucket)-1:
            last_myhand = my_hand_list[now_time - 1]
            
            now_rival_hand = bucket[now_time]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                if now_my_hand == last_myhand:
                    pass
                else:
                    this_point_now_and_next = \
                        janken(now_my_hand, now_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)
        else:
            last_myhand = my_hand_list[now_time - 1]
            
            now_rival_hand = bucket[now_time]
            next_rival_hand = bucket[now_time + 1]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                if now_my_hand == last_myhand:
                    pass
                else:
                    for next_my_hand in ["r", "s", "p"]:
                        if next_my_hand == now_my_hand:
                            pass
                        else:
                            this_point_now_and_next = \
                                janken(now_my_hand, now_rival_hand) \
                                +janken(next_my_hand, next_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)

print(best_total_point)