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: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    R, S, P = (int(i) for i in input().split())
    T = input()
    dp = [[0 for _ in range(N+1)] for _ in range(3)]
    pre = []
    for i, t in enumerate(T):
        if i < K:
            if t == "r":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + P
            elif t == "s":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + R
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            elif t == "p":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + S
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
        else:
            if t == "r":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + P
            elif t == "s":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + R
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            elif t == "p":
                dp[0][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
                dp[1][i+1] = max(dp[0][i], dp[1][i], dp[2][i]) + S
                dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])
            dp[pre[-K]][i+1] = 0

        if dp[0][i+1] == max(dp[0][i+1], dp[1][i+1], dp[2][i+1]):
            pre.append(0)
        elif dp[1][i+1] == max(dp[0][i+1], dp[1][i+1], dp[2][i+1]):
            pre.append(1)
        else:
            pre.append(2)
    print(max(dp[0][N], dp[1][N], dp[2][N]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
# hand = [""]*N
# for i, x in enumerate(T):
#     if i < K:
#         if x == "r":
#             ans += P
#             hand[i] = "p"
#         elif x == "s":
#             ans += R
#             hand[i] = "r"
#         else:
#             ans += S
#             nad[i] = "s"

#     else:
#         if x == hand[i - K]:
#             continue
ans = 0
for i in range(K):
    table = collections.defaultdict(int)
    tmp = 1
    for j in range(i+K, N, K):
        if T[j] == T[j-K]:
            tmp += 1
        else:
            tmp = -(-tmp // 2)
            if T[j-K] == "r":
                tmp *= P
            elif T[j-K] == "s":
                tmp *= R
            else:
                tmp *= S
            ans += tmp
            tmp = 1

        if j + K >= N:
            tmp = -(-tmp // 2)
            if T[j] == "r":
                tmp *= P
            elif T[j] == "s":
                tmp *= R
            else:
                tmp *= S
            ans += tmp
print(ans)

Submission status: Wrong Answer

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

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

for idx, t in enumerate(T):
    if idx <= K-1:
        continue
    if t == 'r':
        if ans[idx-K] != P:
            ans.append(P)
        else:
            ans.append(0)
    elif t == 's':
        if ans[idx-K] != R:
            ans.append(R)
        else:
            ans.append(0)
    elif t == 'p':
        if ans[idx-K] != S:
            ans.append(S)
        else:
            ans.append(0)
    
print(sum(ans))

Submission status: Wrong Answer

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

def reverse_rocks(s):
    if 'r' == s:
        return 'p'
    elif 's' == s:
        return 'r'
    elif 'p' == s:
        return 's'

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

sum_score = 0
chechs = []
for i in range(N):
    must_not_char = ''
    if i >= K: 
        must_not_char = chechs[i-K]
        if reverse_rocks(T[i]) == must_not_char:
            chechs.append(T[i])
            continue
            #sum_score += score(T[i])
    sum_score += score(reverse_rocks(T[i]))
    chechs.append(reverse_rocks(T[i]))
            
print(sum_score)

Submission status: Wrong Answer

Solution code:
import sys
from random import choice,randint
inp=sys.stdin.readline
out=sys.stdout.write
flsh=sys.stdout.flush
 
sys.setrecursionlimit(10**9)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def MI(): return map(int, inp().strip().split())
def LI(): return list(map(int, inp().strip().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]
def LI_(): return [int(x)-1 for x in inp().strip().split()]
def LF(): return [float(x) for x in inp().strip().split()]
def LS(): return inp().strip().split()
def I(): return int(inp().strip())
def F(): return float(inp().strip())
def S(): return inp().strip()
def pf(s): return out(s+'\n')
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n,k=MI()
    r,s,p=MI()
    t=S()
    ans=0
    ns=""
    for i in range(k):
        if t[i]=="r":
            ans+=p
            ns+="p"
        if t[i]=="p":
            ans+=s
            ns+="s"
        if t[i]=="s":
            ans+=r
            ns+="r"
    for i in range(k,n):
        if t[i]=="r" and ns[i-k]=="p":
            ns+="r"
            ans+=0
        if t[i]=="r" and ns[i-k]!="p":
            ns+="p"
            ans+=p
        if t[i]=="p" and ns[i-k]=="s":
            ns+="p"
            ans+=0
        if t[i]=="p" and ns[i-k]!="s":
            ns+="s"
            ans+=s
        if t[i]=="s" and ns[i-k]=="r":
            ns+="s"
            ans+=0
        if t[i]=="s" and ns[i-k]!="r":
            ns+="r"
            ans+=r
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N_K = input("N:K").split()
N = int(N_K[0])
K = int(N_K[1])

R_S_P = input("R:S:P").split()
R = int(R_S_P[0])
S = int(R_S_P[1])
P = int(R_S_P[2])

T = input("T")

win_strategy = {
    "r": "p",
    "s": "r",
    "p": "s"
}

decision = ""
for index, value in enumerate(T):
    if (index <= K-1):
        decision += win_strategy[value]
    else:
        if (win_strategy[value] == decision[index-K]):
            decision += value
        else:
            decision += win_strategy[value]

point_acquire = {
    "r": R,
    "s": S,
    "p": P
}

points = 0
for i in range(N):
    if (decision[i] != T[i]):
        points += point_acquire[decision[i]]

print(points)

Submission status: Wrong Answer

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

score = {'p': P, 'r': R, 's': S}
hand = {'r': 'p', 's': 'r', 'p': 's'}
H = [' '] * N

ans = 0
for i, c in enumerate(T):
    if i - K >= 0 and hand[c] == H[i - K]:
        H[i] = c
        s = 0
    else:
        H[i] = hand[c]
        s = score[hand[c]]

    ans += s

print(ans)

Submission status: Wrong Answer

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

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

for idx, t in enumerate(T):
    if idx <= K-1:
        continue
    if t == 'r':
        if ans[idx-K] != P:
            ans.append(P)
        else:
            ans.append(0)
    elif t == 's':
        if ans[idx-K] != R:
            ans.append(R)
        else:
            ans.append(0)
    elif t == 'p':
        if ans[idx-K] != S:
            ans.append(S)
        else:
            ans.append(0)
    
print(ans)
print(sum(ans))

Submission status: Wrong Answer

Solution code:

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

ten = [0] * N
for i in range(N):
    if (T[i] == 'r'):
        ten[i] = P
    elif(T[i] == 's'):
        ten[i] = R
    else:
        ten[i] = S


for i in range(N-K):
    if(ten[i] == ten[i+K]):
        ten[i+K] = 0
print(sum(ten))

Submission status: Wrong Answer

Solution code:
import math
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for j in range(k):
    cnt = 1
    for i in range(k + j, n, k):
        if t[i] == t[i - k]:
            cnt += 1
        else:
            score = 0
            if t[i - k] == 'r':
                score = p
            if t[i - k] == 's':
                score = r
            if t[i - k] == 'p':
                score = s
            ans += math.ceil(cnt / 2) * score
            cnt = 1
        if i + k >= n:
            score = 0
            if t[i] == 'r':
                score = p
            if t[i] == 's':
                score = r
            if t[i] == 'p':
                score = s
            ans += math.ceil(cnt / 2) * score
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
s = str(input())
dp = [[0] * 3 for i in range(n+k+1)]
dp[0] = [0,0,0]
s = "*"+s
for i in range(1,k+1):
    if s[i] == "s":
        dp[i][0] = max(dp[i-1])+lis[0]
        dp[i][1] = max(dp[i-1])
        dp[i][2] = max(dp[i-1])
    elif s[i] == "p":
        dp[i][0] = max(dp[i-1])
        dp[i][1] = max(dp[i-1])+lis[1]
        dp[i][2] = max(dp[i-1])
    else:
        dp[i][0] = max(dp[i-1])
        dp[i][1] = max(dp[i-1])
        dp[i][2] = max(dp[i-1])+lis[2]
for i in range(k+1,n+1):
    if s[i] == "s":
        dp[i][0] = max(dp[i-1][1],dp[i-1][2])+lis[0]
        dp[i][1] = max(dp[i-1][0],dp[i-1][2])
        dp[i][2] = max(dp[i-1][1],dp[i-1][0])
    elif s[i] == "p":
        dp[i][0] = max(dp[i-1][1],dp[i-1][2])
        dp[i][1] = max(dp[i-1][0],dp[i-1][2])+lis[1]
        dp[i][2] = max(dp[i-1][1],dp[i-1][0])
    else:
        dp[i][0] = max(dp[i-1][1],dp[i-1][2])
        dp[i][1] = max(dp[i-1][0],dp[i-1][2])
        dp[i][2] = max(dp[i-1][1],dp[i-1][0])+lis[2]
print(max(dp[n]))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
L = []
for i in range(N):
    if i <= K - 1:
        if T[i] == "r":
            ans += P
            L.append("p")
        elif T[i] == "s":
            ans += R
            L.append("r")
        else:
            ans += S
            L.append("s")
    else:
        if T[i] == "r":
            if L[i-K] != "p":
              ans += P
              L.append("p")
            else:
                L.append("r")
        elif T[i] == "s":
            if L[i-K] != "r":
              ans += R
              L.append("r")
            else:
                L.append("s")

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

Submission status: Wrong Answer

Solution code:
import math
import queue
import bisect
import heapq
import time
import itertools

mod = int(1e9+7)

def swap(a,b):
    return (b,a)

def gcd(a,b): #最大公約数
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def lcm(a,b): #最小公倍数
    return a/gcd(a,b)*b

def divisors(a): # 約数列挙
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def is_prime(a): #素数判定
    if a<2:
        return False
    elif a==2:
        return True
    elif a%2==0:
        return False
    sqrt_num = int(a**0.5)
    for i in range(3,sqrt_num+1,2):
        if a%i==0:
            return False
    return True

def prime_num(a): #素数列挙
    pn = [2]
    for i in range(3,int(a**0.5),2):
        prime = True
        for j in pn:
            if i%j==0:
                prime = False
                break
        if prime:
            pn.append(i)
    return pn

def prime_fact(a): #素因数分解
    sqrt = math.sqrt(a)
    res = []
    i = 2
    if is_prime(a):
        res.append(a)
    else:
        while a!=1:
            while a%i==0:
                res.append(i)
                a //= i
            i += 1
    return res

def main():
    n,k = map(int,input().split())
    r,s,p = map(int,input().split())
    t = input()
    h = []
    ans = 0
    for i in range(n):
        if i>=k:
            if t[i]=='r' and h[i-k]!='p':
                ans += p
                h.append('p')
            elif t[i]=='s' and h[i-k]!='r':
                ans += r
                h.append('r')
            elif t[i]=='p' and h[i-k]!='s':
                ans += s
                h.append('s')
            else:
                h.append(t[i])
        else:
            if t[i]=='r':
                ans += p
                h.append('p')
            elif t[i]=='s':
                ans += r
                h.append('r')
            elif t[i]=='p':
                ans += s
                h.append('s')
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
# N回じゃんけん
# 勝ったら、
# グー勝: +R
# ちょき: +S
# パー勝: +P
# ただしK回前の手と同じ手は出せない

import itertools
maximum = 0
for path in itertools.product('rsp', repeat=N):
  score = 0
  path = list(path)
  #if path == list('prrsp'):
  #  print(path)
  for n,p in enumerate(path[K:]):
    if path[n] == p:
      break
  else:
    # OK
    for my,enemy in zip(path, T):
      if my == 'r' and enemy == 's': score += R
      elif my == 's' and enemy == 'p': score += S
      elif my == 'p' and enemy == 'r': score += P
    maximum = max(maximum, score)

print(maximum)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = 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] or (i - k) in dic_ls:
    ans += dic[t[i]]
  else:
    dic_ls.append(i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
    ti=[t[ii] for ii in range(n) if ii%k==i]
    ni=len(ti)
#    dp=[[0]*3 for i in range(ni+1)]
    dpir=0
    dpis=0
    dpip=0
    for i2 in range(ni):
        if ti[i2]=="r":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif ti[i2]=="s":
#            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif ti[i2]=="p":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2
#    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
    vsum+=max(dpir,dpis,dpip)
print(vsum)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
    ti=[t[ii] for ii in range(n) if ii%k==i]
    ni=len(ti)
    dp=[[0]*3 for i in range(ni+1)]
    for i2 in range(ni):
        if ti[i2]=="r":
            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
        elif ti[i2]=="s":
            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
        elif ti[i2]=="p":
            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
print(vsum)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

# from numba import njit
import sys
sys.setrecursionlimit(10**8)
# input = stdin.readline

# @njit
def solve(n,k,r,s,p,t):
  winHand = {"r":"p", "s":"b", "p":"s"}
  bestHandMemo = [""]*(n+1) # i手目の勝ち手
  
  def calcPoint(char):
    if char == "r":
      return p
    elif char == "s":
      return r
    elif char == "p":
      return s
    else:
      raise ValueError
  def dp(i):
    if i == 0:
      return 0
    elif i <= k:
      bestHandMemo[i-1] = winHand[t[i-1]] # 自由に出せる
      return dp(i-1) + calcPoint(t[i-1])
    elif winHand[t[i-1]] == bestHandMemo[i-k-1]:
      bestHandMemo[i-1] = "" # あいこでも負けても同じこと
      return dp(i-1)
    else: # 勝てる
      bestHandMemo[i-1] = winHand[t[i-1]]
      return dp(i-1) + calcPoint(t[i-1])
  
  memo = [0]*(n+1)
  for i in range(n+1):
    memo[i] = dp(i)

  return memo[n]
    



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

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]

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

next_hands = {
    'r': ['s', 'p'],
    's': ['r', 'p'],
    'p': ['r', 's'],
}


def battle(enemy_hand, my_hand):
    point = 0

    if enemy_hand == 'r' and my_hand == 'p':
        point = win_point[my_hand]
    elif enemy_hand == 's' and my_hand == 'r':
        point = win_point[my_hand]
    elif enemy_hand == 'p' and my_hand == 's':
        point = win_point[my_hand]

    return point


enemy_hands = input()
search = deque()
max_point = 0
result = 0
debug = []

for index in range(K):
    hands = ['r', 's', 'p']
    max_point = 0

    # 初期化
    search.appendleft((index, 'r', 0))
    search.appendleft((index, 's', 0))
    search.appendleft((index, 'p', 0))

    while search:
        target = search.popleft()

        enemy_hand = enemy_hands[target[0]]

        # 累積ポイントを加算
        point = target[2] + battle(enemy_hand, target[1])
        next_hand = next_hands[target[1]]

        # 累積ポイントのうち、最も大きいものをmax_pointとして保持
        max_point = max(max_point, point)

        for hand in next_hand:
            if target[0] + K < N:
                search.appendleft((target[0] + K, hand, point))
    result += max_point

print(result)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(k):
    tmp = str()
    for j, v in enumerate(t):
        if j % k == i:
            tmp += v
    flg = True
    for l in range(len(tmp)-1):
        if flg:
            ans += c[tmp[l]]
        if tmp[l] == tmp[l+1]:
            flg = not flg
            continue
        if tmp[l] != tmp[l+1]:
            flg = True
    ans += c[tmp[-1]] if flg else 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
p=0
N,K=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
L=list()
L_append = L.append
for i in range(N):
  if i<K:
    if s[i]=="r":
      p+=c
      L_append(i)
    elif s[i]=="s":
      p+=a
      L_append(i)
    else:
      p+=b
      L_append(i)
  else:
    if s[i]=="r" and s[i-K]!="r":
      p+=c
      L_append(i)
    elif s[i]=="s" and s[i-K]!="s":
      p+=a
      L_append(i)
    elif s[i]=="p" and s[i-K]!="p":
      p+=b
      L_append(i)
    if s[i]=="r" and s[i-K]=="r" and i-K not in L:
      p+=c
      L_append(i)
    if s[i]=="s" and s[i-K]=="s" and i-K not in L:
      p+=a
      L_append(i)
    if s[i]=="p" and s[i-K]=="p" and i-K not in L:
      p+=b
      L_append(i)
print(p)

Submission status: Time Limit Exceeded

Solution code:
p=0
N,K=map(int,input().split())
a,b,c=map(int,input().split())
s=input()
L=list()
for i in range(N):
  if i<K:
    if s[i]=="r":
      p+=c
      L.append(i)
    elif s[i]=="s":
      p+=a
      L.append(i)
    else:
      p+=b
      L.append(i)
  else:
    if s[i]=="r" and s[i-K]!="r":
      p+=c
      L.append(i)
    elif s[i]=="s" and s[i-K]!="s":
      p+=a
      L.append(i)
    elif s[i]=="p" and s[i-K]!="p":
      p+=b
      L.append(i)
    if s[i]=="r" and s[i-K]=="r" and i-K not in L:
      p+=c
      L.append(i)
    if s[i]=="s" and s[i-K]=="s" and i-K not in L:
      p+=a
      L.append(i)
    if s[i]=="p" and s[i-K]=="p" and i-K not in L:
      p+=b
      L.append(i)
print(p)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    ts = list(input())

    hands = {"r": p, "s": r, "p": s}
    ans = 0
    skip = []
    for i in range(n):
        if i in skip:
            continue

        if i+k > n-1:
            ans += hands[ts[i]]

        else:
            if ts[i] != ts[i+k]:
                ans += hands[ts[i]]
            else:
                ans += hands[ts[i]]
                skip.append(i+k)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

mp_score = {"r": P, "s": R, "p": S}

lines = [""] * K
for ix in range(K):
    for t in range(len(T)):
        if t%K == ix:
            lines[ix] += T[t]

# print(lines)
ans = 0
for l in lines:
    before = ""
    for c in l:
        if before == c:
            before = ""

        else:
            ans += mp_score[c]
            before = c

print(ans)

Submission status: Time Limit Exceeded

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

for j in range(k, len(t)):
    if t[j] == t[j-k]:
        t = list(t)
        t[j] = 'q'
        t = "".join(t)
ts = collections.Counter(t)
print(ts['p']*s + ts['s']*r + ts['r']*p)

Submission status: Time Limit Exceeded

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

for j in range(k, len(t)):
    if t[j] == t[j-k]:
        t = list(t)
        t[j] = 'q'
        t = "".join(t)
count = 0
for i in range(len(t)):
    if t[i] == 'r':
        count += p
    elif t[i] == 's':
        count += r
    elif t[i] == 'p':
        count += s
    else:
        continue
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
A=[[]*(N//K+1) for i in range(K)]
for i in range(N):
    for j in range(K):
        if i%K==j:
            A[j].append(T[i])
        else:
            pass
ans=0
flag=None
for i in range(K):
    for j in range(len(A[i])):
        if j==0:
            if A[i][j]=='r':
                ans+=P
                flag='r'
            elif A[i][j]=='s':
                ans+=R
                flag='s'
            else:
                ans+=S
                flag='p'
        elif A[i][j]==flag:
            flag=None
    
        else:
            if A[i][j]=='r':
                ans+=P
                flag='r'
            elif A[i][j]=='s':
                ans+=R
                flag='s'
            else:
                ans+=S
                flag='p'
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
s = list(map(int, input().split()))
T = list(input())
ans = 0
stoi = {'r' : 2, 's' : 0, 'p' : 1}
for i in range(k):
    T_sub = [stoi[t] for j, t in enumerate(T) if j % k == i]
    score = 0
    pre = 3
    for j in range(len(T_sub)):
        if T_sub[j] == pre:
            pre = 3
        else:
            score += s[T_sub[j]]
            pre = T_sub[j]
    ans += score
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**8)

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

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)

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

cir = defaultdict(list)
for i in range(k):
    cir[i] = t[i::k]


global memo
memo = []


def solve(li, r, s, p):
    v = []

    def f(li, v):
        if len(li) == 0:
            return 0

        if [li, v] in memo:
            return memo[[li, v]]

        score = 0

        # グーを出す時
        if "r" not in v:
            x = "r"
            if li[0] == "s":
                score = max(score, f(li[1:], ["r"]) + r)
            else:
                score = max(score, f(li[1:], ["r"]))

        # チョキを出す時
        if "s" not in v:
            x = "s"
            if li[0] == "p":
                score = max(score, f(li[1:], ["s"]) + s)
            else:
                score = max(score, f(li[1:], ["s"]))

        # パーを出す時
        if "p" not in v:
            x = "p"
            if li[0] == "r":
                score = max(score, f(li[1:], ["p"]) + p)
            else:
                score = max(score, f(li[1:], ["p"]))

        memo.append([li, score])

        return score
    return f(li, v)


ans = []
for i in range(k):
    ans.append(solve(cir[i], r, s, p))

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
R, S, P = map(int,input().split())
t = input()
score = 0
r_idx = []
s_idx = []
p_idx = []

for idx, v in enumerate(t):
    if v == "r":
        if not idx - k in p_idx:
            p_idx.append(idx)
            score += P
    elif v == "s":
        if not idx - k in r_idx:
            r_idx.append(idx)
            score += R
    else:
        if not idx - k in s_idx:
            s_idx.append(idx)
            score += S
print(score)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
R, S, P = map(int,input().split())
t = input()
score = 0
r_idx = [100000]
s_idx = [100000]
p_idx = [100000]

for idx, v in enumerate(t):
    if v == "r":
        for i in p_idx:
            if idx - i == k:
                break
            if i == p_idx[-1]:
                score += P
                p_idx.append(idx)
                break
    elif v == "s":
        for i in r_idx:
            if idx - i == k:
                break
            if i == r_idx[-1]:
                score += R
                r_idx.append(idx)
                break
    else:
        for i in s_idx:
            if idx - i == k:
                break
            if i == s_idx[-1]:
                score += S
                s_idx.append(idx)
                break
print(score)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque,Counter

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

T = T.translate(str.maketrans({'r': 'p', 's': 'r','p': 's'}))
cnt = Counter(T)
dic = {"r":R, "s":S, "p":P}

ans = cnt["r"]*R + cnt["s"]*S +cnt["p"]*P
checked = []
for i in range(N-K+1):
    if i%K in checked:
        continue
    else:
        j = i
        tmp = 0
        while j <len(T):
            if tmp == T[j]:
                ans -= dic[T[j]]
                tmp = 0
            else:
                tmp = T[j]
            j += K
        checked.append(i%K)

print(ans)