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: Time Limit Exceeded

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

def MaxScore(n,t):
    SC = 0
    Z=[0]*n
    if t[0]=='r':
        Z[0]='p'
        SC+=P
    elif t[0]=='s':
        Z[0]='r'
        SC+=R
    else:
        Z[0]='s'
        SC+=S
    for i in range(1,n):
        if t[i]=='r':
            if Z[i-1]!='p':
                Z[i]='p'
                SC+=P
            elif t[min([i+1,n-1])]=='p':
                Z[i]='r'
            else:
                Z[i]='s'
        elif t[i]=='s':
            if Z[i-1]!='r':
                Z[i]='r'
                SC+=R
            elif t[min([i+1,n-1])]=='r':
                Z[i]='s'
            else:
                Z[i]='p'
        else:
            if Z[i-1]!='s':
                Z[i]='s'
                SC+=S
            elif t[min([i+1,n-1])]=='s':
                Z[i]='p'
            else:
                Z[i]='r'        
    return Z,SC

for i in range(K):
    Ti=[T[j] for j in range(N) if j%K==i ]
    Ti=''.join(Ti)
    if (N-i)%K==0:
        Ni=(N-i)//K
    else:
        Ni=(N-i)//K+1
    Zi,SCi=MaxScore(Ni,Ti)
    SC+=SCi
    
print(SC)

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=list(input())
M={"r":P,"s":R,"p":S}

done=[]
point=0
for i in range(K):
    temp=T.pop(0)
    point+=M[temp]
    done.append(temp)

# print(T)
# print(done)

while T:
    temp=T.pop(0)
    back=done.pop(0)
    # print(temp,back)
    if temp!=back:
        point+=M[temp]
        done.append(temp)
    else:
        done.append('u')

print(point)

Submission status: Time Limit Exceeded

Solution code:
input_list = list(map(int, input().split()))
games = input_list[0]
temp = input_list[1]
input_list = list(map(int, input().split()))
r = input_list[0]
s = input_list[1]
p = input_list[2]
target = input()

def no_win_hand(next_hand):
  myhand = win_janken(next_hand)
  return myhand

def get_point(myhand):
  if myhand == 'r':
    point = r
  elif myhand == 's':
    point = s
  else:
    point = p
  return point

def win_janken(hand, next_hand = 'non', donate = 'non'):
  if hand == 'r':
    myhand = 'p'
  elif hand == 's':
    myhand = 'r'
  else:
    myhand = 's'
  point = get_point(myhand)
  if myhand == donate:
    nothand = no_win_hand(next_hand)[0]
    select_hands = ['r','s','p']
    for hand_1 in select_hands:
      if hand_1 is not donate and  hand_1 is not nothand:
        myhand = hand_1
    point = 0
  return myhand, point

allpoint = 0
for m in range(temp):
  group = ''
  for ind, n in enumerate(target):
    if ind % temp == m:
       group += n
  points = 0
  myhands =''
  for index, string in enumerate(group):
    if len(myhands) > 0:
      donate = myhands[index - 1]
    else:
      donate = 'non'
    if index < len(group) - 1:
      myhand, point = win_janken(string, group[index + 1], donate)
    else:
      myhand, point = win_janken(string, 'non', donate)
    points += point
    myhands += myhand
  allpoint += points
print(allpoint)

Submission status: Time Limit Exceeded

Solution code:
input_list = list(map(int, input().split()))
games = input_list[0]
temp = input_list[1]
input_list = list(map(int, input().split()))
r = input_list[0]
s = input_list[1]
p = input_list[2]
target = input()

def no_win_hand(next_hand):
  myhand = win_janken(next_hand)
  return myhand

def get_point(myhand):
  if myhand == 'r':
    point = r
  elif myhand == 's':
    point = s
  else:
    point = p
  return point

def win_janken(hand, next_hand = 'non', donate = 'non'):
  if hand == 'r':
    myhand = 'p'
  elif hand == 's':
    myhand = 'r'
  else:
    myhand = 's'
  point = get_point(myhand)
  if myhand == donate:
    nothand = no_win_hand(next_hand)[0]
    select_hands = ['r','s','p']
    for hand_1 in select_hands:
      if hand_1 is not donate and  hand_1 is not nothand:
        myhand = hand_1
    point = 0
  return myhand, point

select_hands = ['r','s','p']
allpoint = 0
for m in range(temp):
  group = ''
  for ind, n in enumerate(target):
    if ind % temp == m:
       group += n
  points = 0
  myhands =''
  for index, string in enumerate(group):
    if len(myhands) > 0:
      donate = myhands[index - 1]
    else:
      donate = 'non'
    if index < len(group) - 1:
      myhand, point = win_janken(string, group[index + 1], donate)
    else:
      myhand, point = win_janken(string, 'non', donate)
    points += point
    myhands += myhand
  allpoint += points

print(allpoint)

Submission status: Time Limit Exceeded

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

def didWin(idx, c, tt):
    if c == 'r' and tt[idx] == 's':
        return True
    if c == 's' and tt[idx] == 'p':
        return True
    if c == 'p' and tt[idx] == 'r':
        return True
    return False

def solve(idx):
    ans = 0
    tt = []
    dp = [[0] * 4 for _ in range(n + 1)]

    for i in range(idx, n, k):
        tt.append(t[i])
    # print('tt:', tt)

    l = len(tt)

    prev = None
    for i in range(len(tt)):
        # Case rock
        if didWin(i, 'r', tt):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        # Case scissors
        if didWin(i, 's', tt):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        # Case paper
        if didWin(i, 'p', tt):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    # print('dp:', dp)
    # print('max:', max(dp[l][0], dp[l][1], dp[l][2]))
    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

# N回じゃんけん
# K回前の手は出せない
N, K = map(int, input().split())

# 得点
# グーで勝つ：R
# チョキで勝つ：S
# パーで勝つ: P
R,S,P = map(int, input().split())

# 筐体が出す手
# r=グー, s=チョキ, p=パー
T = input()
#print(T)
# 置換する

ans = copy.copy(T)
ans = ans.replace("r","P").replace("s", "R").replace("p", 'S')
#print(ans)

for i in range(K, len(ans)):
    # 2個前の手と同じだったら、0点にする
    if ans[i] == ans[i-K]:
        ans_list = list(ans)
        #ans_list[i] = T[i].upper()
        ans_list[i] = "D"
        ans = "".join(ans_list)

        
#print(ans)
r_cnt = ans.count("R") * R
s_cnt = ans.count("S") * S
p_cnt = ans.count("P") * P

print(r_cnt + s_cnt + p_cnt)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for i in range(n):
    if((i - k) < 0):
        if(t[i] == 'r'):ans += p
        elif(t[i] == 's'):ans += r
        else:ans += s
        continue
    
    else:
        if(t[i-k] == t[i]):t = t[:i] + 'n' + t[i+1:]
        else:
            if(t[i] == 'r'):ans += p
            elif(t[i] == 's'):ans += r 
            else:ans += s
print(ans)

Submission status: Time Limit Exceeded

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


def set_hand(l, k):
    if l + k < n:
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "s"}:
            return "p"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"r", "p"}:
            return "s"
        if {d.get(t[l - k]), d.get(t[l + k])} == {"p", "s"}:
            return "r"
        return t[l]
    else:
        return t[l]


for i in range(n):
    for l in range(i, n, k):
        if d2.get(l) is not None:
            continue
        if l - k >= 0:
            if d2[l - k] == d.get(t[l]):
                d2[l] = set_hand(l, k)
            else:
                d2[l] = d.get(t[l])
                r += score[d2[l]]
        if l - k < 0:
            d2[l] = d.get(t[l])
            r += score[d2[l]]
print(r)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
starlist = list(input())
po = 0
i = 0

exlist = list()

for how in starlist:
    if i <= k:
        if i >= k and starlist[i] == starlist[i - k] :
            exlist.append(i)
        elif how == "r":
            po += p
        elif how == "s":
            po += r
        elif how == "p":
            po += s

    else:
        if starlist[i] == starlist[i - k] and exlist.count(i - k) != 1:
            exlist.append(i)
        elif how == "r":
            po += p
        elif how == "s":
            po += r
        elif how == "p":
            po += s

    i += 1

print(po)

Submission status: Time Limit Exceeded

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

def replace(c):
    if c == "r":
        return "p"
    elif c == "s":
        return "r"
    else:
        return "s"


t_replace = [replace(c) for c in t]
score = 0


for i in range(0, k):
    list_t = [x for idx, x in enumerate(t_replace) if (idx - i) % k == 0 ]
    for i in range(1, len(list_t)):
        if list_t[i-1] == list_t[i]:
            list_t[i] = "z"

    score += list_t.count("r")*r + list_t.count("p")*p + list_t.count("s")*s

print(score)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(_) for _ in input().split()]
r_s, s_s, p_s = [int(_) for _ in input().split()]
t = input()

s = ''
for i in range(k):
    s += t[i]

for start in range(k):
    c = 0
    for i in range(start + k, n, k):
        if t[i] == t[i - k]:
            c += 1
            if c % 2 == 0:
                s += t[i]
        else:
            s += t[i]
            c = 0

ans = 0
for i in range(len(s)):
    if s[i] == 'r':
        ans += p_s
    elif s[i] == 's':
        ans += r_s
    else:
        ans += s_s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**7)

input = sys.stdin.readline
N,K=map(int,input().rstrip().split())
R,S,P=map(int,input().rstrip().split())
T=list(input().rstrip())
TT=deque(T)

"""
2 1
10 10 10
ss
"""
#ans=0

def dfs(i,s):
 #   global ans
    ans=0
    
    
    if(len(s)>K):
        if(s[-K-1]==s[-1]):

            if(s[-1]=="r"):
                if(T[i-1]=="s"):
                    return -R
            if(s[-1]=="s"):
                if(T[i-1]=="p"):
                    return -S
            if(s[-1]=="p"):
                if(T[i-1]=="r"):
                    return -P


            return 0
    if(i==N):
        return 0
    t=T[i]


    if t=="r":
        ans=max(ans,dfs(i+1,s+"p")+P)
        ans=max(ans,dfs(i+1,s+"r"))
        ans=max(ans,dfs(i+1,s+"s"))
    
    if t=="s":
        ans=max(ans,dfs(i+1,s+"r")+R)
        ans=max(ans,dfs(i+1,s+"s"))
        ans=max(ans,dfs(i+1,s+"p"))
    if t=="p":
        ans=max(ans,dfs(i+1,s+"s")+S)
        ans=max(ans,dfs(i+1,s+"r"))
        ans=max(ans,dfs(i+1,s+"p"))
         
    return ans

def resolve():
    ans=dfs(0,"")
    print(ans)

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5 2
8 7 6
rsrpr"""
        output = """27"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """7 1
100 10 1
ssssppr"""
        output = """211"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr"""
        output = """4996"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
leng = len(t)
ans = 0


for i in range(leng):
    if i >= k:
    
        if t[i] == t[i-k]:
            check = 2
            while True:
                if i < k*check:
                    break
                
                if t[i] == t[i-k*check]:
                    check += 1
                    continue
                else:
                    break
            
            if check%2 == 0:
                continue
            else:
                pass
        
    if t[i] == "r":
        ans += p
        
    elif t[i] == "s":
        ans += r

        
    elif t[i] == "p":
        ans += s
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
starlist = list(input())
po = 0
i = 0

exlist = list()

for how in starlist:

    if (i >= k and starlist[i] == starlist[i-k]) and (i <= k or exlist.count(i-k) != 1):
        exlist.append(i)
    elif how == "r":
        po += p
    elif how == "s":
        po += r
    elif how == "p":
        po += s

    i += 1

print(po)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
leng = len(t)
ans = 0
dic = {"r":0, "s":0, "p":0, "cr":0, "cs":0, "cp":0}


for i in range(leng):
    if i >= k:
    
        if t[i] == t[i-k]:
            check = 2
            while True:
                if i < k*check:
                    break
                
                if t[i] == t[i-k*check]:
                    check += 1
                    continue
                else:
                    break
            
            if check%2 == 0:
                continue
            else:
                pass
        
    if t[i] == "r":
        ans += p
        dic["r"] = 1
        dic["cr"] = i
        
    elif t[i] == "s":
        ans += r
        dic["s"] = 1
        dic["cs"] = i

        
    elif t[i] == "p":
        ans += s
        dic["p"] = 1
        dic["cp"] = i
        
print(ans)

Submission status: Time Limit Exceeded

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

count=0

for i in range(k):
  if t[i]=='r':
    count+=p
  elif t[i]=='s':
    count+=r
  else:
    count+=s

ban=[i for i in t[:k]]
    
for i in range(k,n):
  if t[i]!=ban[0]:
    if t[i]=='r':
      count+=p
    elif t[i]=='s':
      count+=r
    else:
      count+=s
    ban.append(t[i])
  else:
    count+=0
    ban.append('x')
      
  if len(ban)>k:
    ban=ban[1:]
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
score = list(map(int,input().split()))
T = list(map(str,input()))
H = [0,1,2]
for n in range(len(T)):
    if T[n] == "r":
        T[n] = 0
    elif T[n] == "s":
        T[n] = 1
    else:
        T[n] = 2
 
X = []
for a in range(K):
    X.append(T[a::K])
 
sum = 0
for x in X:
    M = 0
    for first in range(3):
        #初手の点数を計算
        if (first + 1) % 3 == x[0]:
            first_s = score[first]
        else:
            first_s = 0
        #bit 全探索
        n = len(x)
        for i in range(2**(n-1)):
            #print(i)
            temp = first_s
            last = first
            #print(last,end = "")
            for j in range(n-1):
                #print(last,end = "")
                h = list(H)
                h.remove(last)  #前回の手を封印
 
                if ((i >> j)&1):
                    last = h[1]
                else:
                    last = h[0]
                
                if (last + 1) % 3 == x[j + 1]:
                    temp += score[last]
            #print("")
            #print(temp)
            M = max(temp,M)
    sum += M
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n,k= map(int, input().split())
r,s,p= map(int, input().split())
T=input()
bsc=0    
sc=0
for i in range(k):
    TT=str()
    TT=''.join([T[j] for j in range(n) if j%k==i])
    #print(TT)
    if TT[0]=='r':
        sc=sc+p
    if TT[0]=='p':
        sc=sc+s
    if TT[0]=='s':
        sc=sc+r
    for j in range(1,len(TT)):
        if bsc==sc:
            if TT[j]=='r':
                sc=sc+p
            if TT[j]=='p':
                sc=sc+s
            if TT[j]=='s':
                sc=sc+r
        elif TT[j]==TT[j-1]:
            bsc=sc
        else:
            bsc=sc
            if TT[j]=='r':
                sc=sc+p
            if TT[j]=='p':
                sc=sc+s
            if TT[j]=='s':
                sc=sc+r
print(sc)

Submission status: Time Limit Exceeded

Solution code:
def winList(t, r, s, p):
    if t == "r":
        return [0, 0, p]
    elif t == "s":
        return [r, 0, 0]
    else:
        return [0, s, 0]

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

dp = []
for i in range(N):
    wList = winList(T[i], R, S, P)
    if i <= K-1:
        if i == 0:
            dp.append(wList)
        else:
            dp.append([wList[0] + max(dp[i - 1]),
                       wList[1] + max(dp[i - 1]),
                       wList[2] + max(dp[i - 1])])
    else:
        tmpRPointList = []
        tmpSPointList = []
        tmpPPointList = []
        wKList = winList(T[i - K], R, S, P)
        for j in range(K):
            if dp[i - K][0] > dp[i - K][1] and dp[i - K][0] > dp[i - K][2]:
                tmpRPointList.append(wList[0] + max(dp[i - K + j]) - wKList[0])
            else:
                tmpRPointList.append(wList[0] + max(dp[i - K + j]))
            if dp[i - K][1] > dp[i - K][0] and dp[i - K][1] > dp[i - K][2]:
                tmpSPointList.append(wList[1] + max(dp[i - K + j]) - wKList[1])
            else:
                tmpSPointList.append(wList[1] + max(dp[i - K + j]))
            if dp[i - K][2] > dp[i - K][0] and dp[i - K][2] > dp[i - K][1]:
                tmpPPointList.append(wList[2] + max(dp[i - K + j]) - wKList[2])
            else:
                tmpPPointList.append(wList[2] + max(dp[i - K + j]))
        dp.append([max(tmpRPointList), max(tmpSPointList), max(tmpPPointList)])

print(max(dp[N-1]))

Submission status: Time Limit Exceeded

Solution code:
# K回前と現在の問いが同じだったら0点

str1 = input().split(" ")
str2 = input().split(" ")
t = input()

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

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

result = 0
hoge = 0
douchilist = []
for i in range(0,n):
    if i < k:
        continue
    if t[i] == t[i-k] and i-k not in douchilist:
        hoge += skill[t[i]]
        douchilist.append(i)
        continue

pres = p * t.count("r")
rres = r * t.count("s")
sres = s * t.count("p")


print(pres + rres + sres - hoge)

Submission status: Time Limit Exceeded

Solution code:
def dpcalc(begin):
    global N,KR,S,P,T
    DP = [[0]*3 for _ in range(N+1)]
    pos = begin

    DP[0][0] = 0
    DP[0][1] = 0
    DP[0][2] = 0
    n = 0
    while pos < N:
        t = T[pos]
        if t == 'r':
            DP[n+1][0] = max(DP[n][1],DP[n][2]) 
            DP[n+1][1] = max(DP[n][0],DP[n][2])
            DP[n+1][2] = max(DP[n][0],DP[n][1])+P
        if t == 's':
            DP[n+1][0] = max(DP[n][1],DP[n][2])+R
            DP[n+1][1] = max(DP[n][0],DP[n][2])
            DP[n+1][2] = max(DP[n][0],DP[n][1]) 
        if t == 'p':
            DP[n+1][0] = max(DP[n][1],DP[n][2]) 
            DP[n+1][1] = max(DP[n][0],DP[n][2])+S
            DP[n+1][2] = max(DP[n][0],DP[n][1]) 
        #print(DP[n+1])
        n+=1
        pos += K 
    return max(DP[n])

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
sum1 = 0
for i in range(K):
    sum1 += dpcalc(i)
print(sum1)

Submission status: Time Limit Exceeded

Solution code:
def get_point(machine_hand, R, S, P):
    """ 筐体がmachine_handを出した場合，勝った時に得られるポイント
    """
    if machine_hand == 'r':
        return P
    elif machine_hand == 's':
        return R
    elif machine_hand == 'p':
        return S



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

    points = []
    used_trials = []
    for i in range(0, len(T)):
        if i < K:
            points.append(get_point(T[i], R, S, P))            
            used_trials.append(i)
        else:
            if T[i] == T[i - K] and i - K in used_trials:
                points.append(0)
            else:
                points.append(get_point(T[i], R, S, P))
                used_trials.append(i)
        
    print(sum(points))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# K回前と現在の問いが同じだったら0点

str1 = input().split(" ")
str2 = input().split(" ")
t = input()

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

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

result = 0
douchilist = []
for i in range(0,n):
    if i >= k:
        if t[i] == t[i-k] and i-k not in douchilist:
            result += 0
            douchilist.append(i)
            continue

    result += skill[t[i]]
    continue


print(result)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
r,s,p=map(int,input().split())
t=[i for i in input()]
tmp=0
for i in range(K):
    if t[i]=='s':
        tmp+=r
    elif t[i]=='p':
        tmp+=s
    else:
        tmp+=p   
    #print(tmp)
    #print('制約なしで勝ち')
flg=0
for i in range(K,N):
    if t[i]!=t[i-K]:
        if t[i]=='s':
            tmp+=r
            #print('グーで勝ち')
        elif t[i]=='p':
            tmp+=s
            #print('チョキで勝ち↓')
        else:
            tmp+=p
            #print('パーで勝ち')
    else:
        t[i]='w'*i
        #print('かぶった（1回目）')
        
            
    #print(tmp)
#print('ans='+str(tmp))
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
def dpcalc(begin):
    global N,KR,S,P,T
    DP = [[0]*3 for _ in range(N+1)]
    pos = begin

    DP[0][0] = 0
    DP[0][1] = 0
    DP[0][2] = 0
    n = 0
    while pos < N:
        t = T[pos]
        if t == 'r':
            DP[n+1][0] = max(DP[n][1],DP[n][2]) 
            DP[n+1][1] = max(DP[n][0],DP[n][2])
            DP[n+1][2] = max(DP[n][0],DP[n][1])+P
        if t == 's':
            DP[n+1][0] = max(DP[n][1],DP[n][2])+R
            DP[n+1][1] = max(DP[n][0],DP[n][2])
            DP[n+1][2] = max(DP[n][0],DP[n][1]) 
        if t == 'p':
            DP[n+1][0] = max(DP[n][1],DP[n][2]) 
            DP[n+1][1] = max(DP[n][0],DP[n][2])+S
            DP[n+1][2] = max(DP[n][0],DP[n][1]) 
        #print(DP[n+1])
        n+=1
        pos += K 
    return max(DP[n])

N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
sum1 = 0
for i in range(K):
    sum1 += dpcalc(i)
print(sum1)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
starlist = list(input())
po = 0
i = 0

exlist = list()

for how in starlist:

    if (i >= k and starlist[i] == starlist[i-k]) and (i <= k or exlist.count(i-k) != 1):
        po += 0
        exlist.append(i)
    elif how == "r":
        po += p
    elif how == "s":
        po += r
    elif how == "p":
        po += s

    i += 1

print(po)

Submission status: Time Limit Exceeded

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
 
ten2 = ten[:]
T2 = T
for i in range(N-K):
    if(T[i] == T[i+K]):
        ten[i+K] = 0
        str_list = list(T)
        str_list[i+K] = 'o'
        T = "".join(str_list)
for i in range(N-1,K-1,-1):
    if(T2[i] == T2[i-K]):
        ten2[i-K] = 0
        str_list = list(T2)
        str_list[i-K] = 'o'
        T2 = "".join(str_list)


print(max(sum(ten),sum(ten2)))

Submission status: Time Limit Exceeded

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

bsc=0    
sc=0
for i in range(k):
    TT=str()
    for j in range(n):
        if j%k==i:
            TT=TT+T[j]
    #print(TT,sc)
    if TT[0]=='r':
        sc=sc+p
    if TT[0]=='p':
        sc=sc+s
    if TT[0]=='s':
        sc=sc+r
    for j in range(1,len(TT)):
        if bsc==sc:
            if TT[j]=='r':
                sc=sc+p
            if TT[j]=='p':
                sc=sc+s
            if TT[j]=='s':
                sc=sc+r
        elif TT[j]==TT[j-1]:
            bsc=sc
        else:
            bsc=sc
            if TT[j]=='r':
                sc=sc+p
            if TT[j]=='p':
                sc=sc+s
            if TT[j]=='s':
                sc=sc+r
print(sc)

Submission status: Time Limit Exceeded

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

# N,K=7 ,1
# R,S,P=100 ,10, 1
# T="ssssppr"

# N,K=5, 2
# R,S,P=8, 7, 6
# T="rsrpr"

# N,K=30, 5
# R,S,P=325, 234, 123
# T="rspsspspsrpspsppprpsprpssprpsr"


max_ans=T.count("s")*R+T.count("p")*S+T.count("r")*P
for i in range(K):
    tmp_ans=T[i::K]
    for i in range(len((tmp_ans))-1):
        if tmp_ans[i]==tmp_ans[i+1]:
            if tmp_ans[i]=="s":
                max_ans-=R
            if tmp_ans[i]=="p":
                max_ans-=S
            if tmp_ans[i]=="r":
                max_ans-=P
            tmp_ans = tmp_ans[:i+1] + 'X' +tmp_ans[i+2:]

print(max_ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
score = list(map(int,input().split()))
T = list(map(str,input()))
H = [0,1,2]
for n in range(len(T)):
    if T[n] == "r":
        T[n] = 0
    elif T[n] == "s":
        T[n] = 1
    else:
        T[n] = 2

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

sum = 0
for x in X:
    M = 0
    for first in range(3):
        #初手の点数を計算
        if (first + 1) % 3 == x[0]:
            first_s = score[first]
        else:
            first_s = 0
        #bit 全探索
        n = len(x)
        for i in range(2**(n-1)):
            temp = first_s
            last = first
            #print(last,end = "")
            for j in range(n-1):
                #print(last,end = "")
                h = H.copy()
                h.remove(last)  #前回の手を封印

                if ((i >> j)&1):
                    last = h[1]
                else:
                    last = h[0]
                
                if (last + 1) % 3 == x[j + 1]:
                    temp += score[last]
            #print("")
            #print(temp)
            M = max(temp,M)
    sum += M
print(sum)

Submission status: Time Limit Exceeded

Solution code:
# K回前と現在の問いが同じだったら0点

str1 = input().split(" ")
str2 = input().split(" ")
t = input()

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

result = 0
douchilist = []
for i in range(0,n):
    if i >= k:
        if t[i] == t[i-k] and i-k not in douchilist:
            result += 0
            douchilist.append(i)
            continue

    if t[i] == "r":
        result += p
        continue

    if t[i] == "s":
        result += r
        continue

    if t[i] == "p":
        result += s
        continue


print(result)

Submission status: Time Limit Exceeded

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

ans = 0
check = []
for i in range(N):
    if i <= K-1:
        if T[i] == 'r':
            ans += P
        elif T[i] == 's':
            ans += R
        else:
            ans += S
    else:
        if T[i] != T[i-K]:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R
            else:
                ans += S
#        elif len(check) != 0:
#           a = bisect.bisect_left(check,i-K)
        elif T[i] == T[i-K] and i-K in check:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R   
            else:
                ans += S
        else:
            check.append(i)

print(ans)