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:
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(len(T)):
    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)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
d={"r":p,"s":r,"p":s}
li=[]
s=input()
ans=0
for i,j in enumerate(s):
    if i<k:
        ans+=d[j]
        li.append(j)
    else:
        if li[0]!=j:
            ans+=d[j]
            li=li[1:]+[j]
        else:
            li=li[1:]+["#"]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
A = input()
List = [[]]*K
for x in range(N):
  y = x % K
  List[y] = List[y] + [A[x]]
 
s = 0
for i in range(K):
  for j in range(len(List[i])-1):
    if List[i][j] == List[i][j+1]:
      List[i][j+1] = 't'
    k = List[i][j]
    if k == 'r':
      s += P
    elif k == 's':
      s += R
    elif k == 'p':
      s += S
    else:
      s = s
  l = List[i][len(List[i])-1]  
  if l == 'r':
      s += P
  elif l == 's':
      s += R
  elif l == 'p':
      s += S
  else:
      s = s
   
print(s)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
res = 0
k = ''
log = ''
for i in range(N):
  t = T[i]
  if t == 'r':
    if K <= i and log[i-K] == 'p':
      log += 'x'
    else:
      res += P
      log += 'p'
  elif t == 's':
    if K <= i and log[i-K] == 'r':
      log += 'x'
    else:
      res += R
      log += 'r'
  else:
    if K <= i and log[i-K] == 's':
      log += 'x'
    else:
      res += S
      log += 's'

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = input()
ans = [0, 0, 0]
for i in range(k):
    if t[i] == 'r':
        ans[0] += 1
    elif t[i] == 's':
        ans[1] += 1
    else:
        ans[2] += 1
memo = []
for i in range(k, n):
    if t[i] == t[i - k] and (i - k) not in memo:
        memo.append(i)
    elif t[i] == 'r':
        ans[0] += 1
    elif t[i] == 's':
        ans[1] += 1
    elif t[i] == 'p':
        ans[2] += 1
print(ans[0] * p + ans[1] * r + ans[2] * s)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
res = 0
chk1 = -1
chk2 = -1
chk3 = -1

a = []
b = []
c = []
for i,j in enumerate(list(t)):
	if j == "s":
		if i in a:
			a.remove(i)
			continue
		res += r
		chk1 = i+k
		a.append(chk1)
	if j == "p":
		if i in b:
			b.remove(i)
			continue
		res += s
		chk2 = i+k
		b.append(chk2)
	if j == "r":
		if i in c:
			c.remove(i)
			continue
		res += p
		chk3 = i+k
		c.append(chk3)
print (res)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
res = 0
chk1 = -1
chk2 = -1
chk3 = -1

a = []
b = []
c = []
for i,j in enumerate(list(t)):
	if j == "s":
		if i in a:
			continue
		res += r
		chk1 = i+k
		a.append(chk1)
	if j == "p":
		if i in b:
			continue
		res += s
		chk2 = i+k
		b.append(chk2)
	if j == "r":
		if i in c:
			continue
		res += p
		chk3 = i+k
		c.append(chk3)
print (res)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
res = 0
chk = -1
a = []
for i,j in enumerate(list(t)):
	if j == "s":
		if i in a:
			continue
		res += r
		chk = i+k
		a.append(chk)
chk = -1
a = []
for i,j in enumerate(list(t)):
	if j == "p":
		if i in a:
			continue
		res += s
		chk = i+k
		a.append(chk)
chk = -1
a = []
for i,j in enumerate(list(t)):
	if j == "r":
		if i in a:
			continue
		res += p
		chk = i+k
		a.append(chk)
print (res)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
score = list(map(int, input().split()))
t = input()
hand = 'rsp'

d = {'': 0}
for i in range(n):
    h = hand.index(t[i])
    dd = {}
    for s, x in d.items():
        for j in range(3):
            if i < k or s[0] != hand[j]:
                point = 0
                if (j - h) % 3 == 2:
                    point = score[j]
                if k == 1:
                    news = hand[j]
                else:
                    news = s[-k+1:] + hand[j]
                if news in dd:
                    dd[news] = max(dd[news], x + point)
                else:
                    dd[news] = x + point
    d = dd
print(max(d.values()))

Submission status: Time Limit Exceeded

Solution code:
game_num, k = map(int, input().split())
r_score, s_score, p_score = map(int, input().split())
game = input()
score = 0
waste_r, waste_s, waste_p = [], [], []
count_waste_r = count_waste_s = count_waste_p = 0

for i in range(game_num):
    if i < k:
        pass
    elif game[i] == 'r' and game[i - k] == 'r' and not (i - k) in waste_r:
        count_waste_r += 1
        #if len(waste_r) > 2 * k:
        #    waste_r.pop()
        waste_r.append(i)
    elif game[i] == 's' and game[i - k] == 's' and not (i - k) in waste_s:
        count_waste_s += 1
        #if len(waste_s) > 2 * k:
        #    waste_s.pop()
        waste_s.append(i)
    elif game[i] == 'p' and game[i - k] == 'p' and not (i - k) in waste_p:
        count_waste_p += 1
        #if len(waste_p) > 2 * k:
        #    waste_p.pop()
        waste_p.append(i)

r_count, s_count, p_count = map(lambda x: game.count(x), ['r', 's', 'p'])

score += r_count * p_score + s_count * r_score + p_count * s_score
score -= count_waste_r * p_score + count_waste_s * r_score + count_waste_p * s_score
print(score)

Submission status: Time Limit Exceeded

Solution code:
game_num, k = map(int, input().split())
r_score, s_score, p_score = map(int, input().split())
game = input()
score = 0
waste_r, waste_s, waste_p = [], [], []

for i in range(game_num):
    if i < k:
        pass
    elif game[i] == 'r' and game[i - k] == 'r' and not (i - k) in waste_r:
        waste_r.append(i)
    elif game[i] == 's' and game[i - k] == 's' and not (i - k) in waste_s:
        waste_s.append(i)
    elif game[i] == 'p' and game[i - k] == 'p' and not (i - k) in waste_p:
        waste_p.append(i)

r_count, s_count, p_count = map(lambda x: game.count(x), ['r', 's', 'p'])

score += r_count * p_score + s_count * r_score + p_count * s_score
score -= len(waste_r) * p_score + len(waste_s) * r_score + len(waste_p) * s_score
print(score)

Submission status: Time Limit Exceeded

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,point):
    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[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()
score = 0
for i in range(N):
    if(T[i]=="r"):
        score +=P
    elif(T[i]=="s"):
        score +=R
    elif(T[i]=="p"):
        score +=S
    if(i <= N-K-1):
        if(T[i]==T[i+K]):
            Tl = list(T)
            Tl[i+K] = "0"
            T = "".join(Tl)
print(score)

Submission status: Time Limit Exceeded

Solution code:
def prediction_and_restriction(k, r, s, p, t):
    t_mod = ['' for i in range(k)]
    for i in range(len(t)):
        t_mod[i%k] += t[i]
    ans = 0
    for t_sub in t_mod:
        dp = janken(r,s,p,t_sub[0])
        win = True
        for i, v in enumerate(t_sub):
            if i > 0:
                if win and t_sub[i-1] == t_sub[i]:
                    win = False
                else:
                    dp += janken(r,s,p,t_sub[i])
                    win = True
        ans += dp
    return ans

def janken(r,s,p,x):
    if x == 'r':
        return p
    elif x == 's':
        return r
    else:
        return s

n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = input()

print(prediction_and_restriction(k, r, s, p, t))

Submission status: Time Limit Exceeded

Solution code:
def prediction_and_restriction(k, r, s, p, t):
    t_mod = ['' for i in range(k)]
    for i in range(len(t)):
        t_mod[i%k] += t[i]
    ans = 0
    for t_sub in t_mod:
        dp = [0 for i in range(len(t_sub))]
        dp[0] = janken(r,s,p,t_sub[0])
        win = True
        for i, v in enumerate(dp):
            if i > 0:
                if win and t_sub[i-1] == t_sub[i]:
                    dp[i] = dp[i-1]
                    win = False
                else:
                    dp[i] = dp[i-1] + janken(r,s,p,t_sub[i])
                    win = True
        ans += dp[-1]
    return ans

def janken(r,s,p,x):
    if x == 'r':
        return p
    elif x == 's':
        return r
    else:
        return s

n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = input()

print(prediction_and_restriction(k, r, s, p, t))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = str(input())
score = 0
lose = []
def janken(hand):
    if hand == 'r':
        return(p)
    if hand == 's':
        return(r)
    if hand == 'p':
        return(s)

for i in range(n):
    if i <= k - 1:
        score += janken(T[i])
    else:
        if T[i] == T[i - k]:
            if i - k not in lose:
                lose.append(i)
            else:
                score += janken(T[i])
        else:
            score += janken(T[i])
print(score)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
 
def MaxScore(rsp, s):
    Score = 0
    t = list(s)
    t = t + ['t']
    for i in range(len(t)-1):
        if t[i] == t[i+1]:
            t[i+1] = 'x' + t[i+1]
    for i in range(len(t)):
        if t[i] == 'r':
            Score = Score + rsp[2]
        elif t[i] == 's':
            Score = Score + rsp[0]
        elif t[i] == 'p':
            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: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
score_list = list(input())
#rグー、sチョキ、pパー
for i in range(k):
    score_list.insert(0, 0)


ans = 0
for i in range(k, n+k):
    if score_list[i] == "r":
        if score_list[i] != score_list[i-k]:
            ans += p
        else:
            score_list[i] = 0
    elif score_list[i] == "s":
        if score_list[i] != score_list[i-k]:
            ans += r
        else:
            score_list[i] = 0
    elif score_list[i] == "p":
        if score_list[i] != score_list[i-k]:
            ans += s
        else:
            score_list[i] = 0

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
M = ''
for i in range(N):
	if i >= K and M[i-K] == T[i]:
		M += 'x'
		continue
	else:
		if T[i] == 'r':
			ans += P
			M += 'r'
		if T[i] == 's':
			ans += R
			M += 's'
		if T[i] == 'p':
			ans += S
			M += 'p'
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
M = ''
for i in range(N):
	if i < K:
		if T[i] == 'r':
			ans += P
			M += 'r'
		if T[i] == 's':
			ans += R
			M += 's'
		if T[i] == 'p':
			ans += S
			M += 'p'
	elif M[i-K] != T[i]:
		if T[i] == 'r':
			ans += P
			M += 'r'
		if T[i] == 's':
			ans += R
			M += 's'
		if T[i] == 'p':
			ans += S
			M += 'p'
	else:
		M += 'x'
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
scores=list(map(int,input().split()))
t=input()
winner={'r':2,'s':0,'p':1}
ans=0
for i in range(k):
  ts=''
  for j in range(n):
    if k*j+i>=n:
      continue
    ts+=t[k*j+i]
  ts+='?'
  l=len(ts)
  hand=ts[0]
  cnt=1
  for j in range(1,l):
    if hand==ts[j]:
      cnt+=1
    else:
      ans+=scores[winner[hand]]*((cnt+1)//2)
      hand=ts[j]
      cnt=1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    count = 0
    k = K
    while i - k >= 0 and T[i] == T[i - k]:
        count += 1
        k += K

    if count % 2 == 0:
        ans += score[T[i]]

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    flag = True
    for j in range(i // K - 1, -1, -1):
        if T[i - K * j] == T[i - K * (j + 1)]:
            flag = not flag
        else:
            flag = True

    if flag:
        ans += score[T[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()

def point(aite):
    if aite == "r":
        return p
    elif aite == "s":
        return r
    else:
        return s

ten = t.count('r')*p+t.count('s')*r+t.count('p')*s
lose = []

for i in range(n-k):
    if t[i] == t[i+k] and not i in lose:
        ten -= point(t[i])
        lose.append(i+k)

print(ten)

Submission status: Time Limit Exceeded

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

def point(aite):
    if aite == "r":
        return p
    elif aite == "s":
        return r
    else:
        return s

ten = 0
lose = []
for i in range(n):
    if i < k:
        ten += point(t[i])
    else:
        if t[i-k] == t[i] and not i-k in lose:
            lose.append(i)
        else:
            ten += point(t[i])

print(ten)

Submission status: Time Limit Exceeded

Solution code:
nk = input().split()
n=int(nk[0])
k=int(nk[1])
rsp=input().split()
r=int(rsp[0])
s=int(rsp[1])
p=int(rsp[2])
t=input()
index=[[],[],[]]
index[0]=[n for n, x in enumerate(t) if x=='r']
index[1]=[n for n, x in enumerate(t) if x=='s']
index[2]=[n for n, x in enumerate(t) if x=='p']
i=0
a=len(index[0])
b=len(index[1])
c=len(index[2])
while(i<a-1):
    if index[0][i]+k==index[0][i+1]:
        del index[0][i+1]
        i=i+1
        a=a-1
i=0
while(i<b-1):
    if index[1][i]+k==index[1][i+1]:
        del index[1][i+1]
        i=i+1
        b=b-1
i=0
while(i<c-1):
    if index[2][i]+k==index[2][i+1]:
        del index[2][i+1]
        i=i+1
        c=c-1
rc=len(index[0])
sc=len(index[1])
pc=len(index[2])
a=r*sc+s*pc+p*rc
print(a)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
win = {'r':P,'s':R,'p':S}
T = input()
WAKE = ['']*K

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

ans = 0
for i in range(K):
    if len(WAKE[i])>=0:
        pre = WAKE[i][0]
        ans += win[pre]
        for j in range(1,len(WAKE[i])):
            now = WAKE[i][j]
            if pre != now:
                ans += win[now]
                pre = now
            else:
                pre = ''
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
st = input()
for i in range(len(st)-k):
    if st[i] == st[i + k]:
        st = st[:i + k]+ 'n' + st[i + k + 1:]
win = r*st.count('s') + s*st.count('p') + p*st.count('r')
print(win)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
h = {"r":p, "s":r, "p":s}
t = input()
lst = []
for i in range(k):
  lst.append([t[j] for j in range(len(t)) if j%k==i])
ans = 0
#print(lst)
for b in lst:
  tmp = ""
  for i in b:
    if i==tmp:
      tmp=""
    else:
      ans+=h[i]
      tmp=i
print(ans)

Submission status: Time Limit Exceeded

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

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(ss, p):
    lss = len(ss)
    if p == lss:
        return 0

    c = ss[p]

    if lss - p == 1:
        if c == 'r':
            return P
        elif c == 's':
            return R
        else:
            return S
    if ss[p + 1] == ss[p]:
        if c == 'r':
            return max(okjudge(ss, p + 1), P + okjudge(ss, p + 2))
        elif c == 's':
            return max(okjudge(ss, p + 1), R + okjudge(ss, p + 2))
        else:
            return max(okjudge(ss, p + 1), S + okjudge(ss, p + 2))
    elif c == 'r':
        return P + okjudge(ss, p + 1)
    elif c == 's':
        return R + okjudge(ss, p + 1)
    else:
        return S + okjudge(ss, p + 1)

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

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = [c for c in input()]
subs = [[] for _ in range(K)]

def max_sub(sub):
  total_point = 0
  previous = None
  for enemy_hand in sub:
    if previous == None:
      my_hand, point = battle(enemy_hand)
      total_point += point
      previous = my_hand
    else:
      my_hand, point = battle(enemy_hand)
      if my_hand == previous:
        previous = None
      else:
        total_point += point
        previous = my_hand
  return total_point

def battle(enemy_hand):
  if enemy_hand == "r":
    return "p", P
  elif enemy_hand == "s":
    return "r", R
  elif enemy_hand == "p":
    return "s", S
  else:
    raise ValueError("HAND IS WRONG")
    

for n in range(N):
  for k in range(K):
    if n % K == k:
      subs[k].append(T[n])

total_point = 0
for sub in subs:
  total_point += max_sub(sub)
  
      
print(total_point)

Submission status: Time Limit Exceeded

Solution code:
#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, K = getlist()
	R, S, P = getlist()
	T = input()
	ans = 0
	for k in range(K):
		DPr = [0] * N
		DPs = [0] * N
		DPp = [0] * N
		#初期化
		if T[k] == "r":
			DPp[k] = P
		elif T[k] == "s":
			DPr[k] = R
		else:
			DPs[k] = S

		for i in range(1, N + 1):
			now = i * K + k
			pre = (i - 1) * K + k
			if now >= N:
				ans += max(DPr[pre], DPs[pre], DPp[pre])
				break
			if T[now] == "r":
				DPr[now] = max(DPs[pre], DPp[pre])
				DPs[now] = max(DPr[pre], DPp[pre])
				DPp[now] = max(DPr[pre] + P, DPs[pre] + P)
			elif T[now] == "s":
				DPr[now] = max(DPs[pre] + R, DPp[pre] + R)
				DPs[now] = max(DPr[pre], DPp[pre])
				DPp[now] = max(DPr[pre], DPs[pre])
			else:
				DPr[now] = max(DPs[pre], DPp[pre])
				DPs[now] = max(DPr[pre] + S, DPp[pre] + S)
				DPp[now] = max(DPr[pre], DPs[pre])
		# print(DPr)
		# print(DPs)
		# print(DPp)

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve(n, k, r, s, p, t):
    dp = np.zeros((n, 3), dtype=int)
    win_hand_mapping = {
        "r": "p",
        "s": "r",
        "p": "s"
    }
    score_mapping = {
        "r": r,
        "s": s,
        "p": p
    }
    hand_index_mapping = {
        "r": 0,
        "s": 1,
        "p": 2
    }
    for i0 in range(k):
        for i in range(i0, n, k):
            win_hand = win_hand_mapping[t[i]]
            win_hand_index = hand_index_mapping[win_hand]
            win_hand_score = score_mapping[win_hand]
            for before in range(3):
                for current in range(3):
                    point = (current == win_hand_index) * win_hand_score
                    if i < k:
                        dp[i][current] = max(dp[i][current], point)
                    elif before != current:
                        dp[i][current] = max(dp[i][current], dp[i-k][before] + point)
    return np.max(dp, axis=1)[-k:].sum()

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

Submission status: Time Limit Exceeded

Solution code:
NK=input().split()
N=int(NK[0])
K=int(NK[1])
RSP=input().split()
R=int(RSP[0])
S=int(RSP[1])
P=int(RSP[2])
T=input()
point=0

def battle(moji):
    if moji=='r':
        return P
    elif moji == 'p':
        return S
    else:
        return R

for i in range(N):
    if i-K >= 0:
        j=1
        while i-j*K >=0 and T[i] == T[i-j*K]:
            j+=1
        if j%2==1:        
            point+=battle(T[i])
    else:
        point+=battle(T[i])


print(point)

Submission status: Time Limit Exceeded

Solution code:
'''input
3 5
1 2 1
2 3 0
'''
# connected components
from sys import stdin, setrecursionlimit
from bisect import bisect_left
import sys, resource

resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x100000)


def get_cost(m, h):
    if m == 'r':
        if h == 'p':
            return paper
    elif m == 's':
        if h == 'r':
            return rock
    else:
        if h == 's':
            return scissor
    return 0


def make(first, second):
    return str(first) + ' ' + str(second)


def brute(string, index, last, dp):
    if index == len(string):
        return 0
    x = -float('inf'); y = -float('inf'); z = -float('inf')
    if make(index, last) in dp:
        return dp[make(index, last)]
    if index != 0:
        if last != 'p':
            x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        if last != 's':
            y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        if last != 'r':
            z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)

    else:
        x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)
    dp[make(index, last)] = max(x, y, z)
    return max(x, y, z)

#
# def solve(string):
#     dp = [[0 for x in range(3)] for y in range(len(string))]
#     if string[0] == 'r':
#         dp[0] = [0, 0, paper]
#     elif string[0] == 's':
#         dp[0] = [rock, 0, 0]
#     else:
#         dp[0] = [0, scissor, 0]
#
#     for i in range(1, len(string)):
#         if string[i] == 'r':
#             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][0], dp[i - 1][1]) + paper
#         elif string[i] == 's':
#             dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + rock
#             dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
#             dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
#         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]) + scissor
#             dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
#     # print(dp)
#     return max(dp[-1])

# main starts
n, k = list(map(int, stdin.readline().split()))
rock, scissor, paper = list(map(int, stdin.readline().split()))
string = stdin.readline().strip()
cost = 0
for i in range(k):
    temp = []
    j = i
    while j < n:
        temp.append(string[j])
        j += k
    dp = dict()
    cost += brute(temp, 0, '$', dp)
print(cost)

Submission status: Time Limit Exceeded

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

def jan(a):
  if a == "r":
    return p
  elif a == "s":
    return r
  return s

for i in range(len(l)):
  if i<k:
    ans += jan(l[i]) 
  elif l[i] != l[i-k]:
    ans += jan(l[i])
  j = i-k
  c = 0
  while(j>=0):
    if l[i] == l[j]:
      c+=1
    else:
      break
    j -= k
  if c%2 == 0 and c!=0:
    ans += jan(l[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()
SC=0

def MaxScore(n,t):
    SC = 0
    if t[0]=='r':
        SC+=P
    elif t[0]=='s':
        SC+=R
    else:
        SC+=S
    NC=1
    for i in range(1,n):
        if t[i]=='r':
            if t[i]!=t[i-1]:
                SC+=P
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=P
                NC+=1
            else:
                NC+=1
        elif t[i]=='s':
            if t[i]!=t[i-1]:
                SC+=R
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=R
                NC+=1
            else:
                NC+=1
        else:
            if t[i]!=t[i-1]:
                SC+=S
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=S
                NC+=1
            else:
                NC+=1
    return SC

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

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
    if t[0]=='r':
        SC+=P
    elif t[0]=='s':
        SC+=R
    else:
        SC+=S
    NC=1
    for i in range(1,n):
        if t[i]=='r':
            if t[i]!=t[i-1]:
                SC+=P
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=P
                NC+=1
            else:
                NC+=1
        elif t[i]=='s':
            if t[i]!=t[i-1]:
                SC+=R
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=R
                NC+=1
            else:
                NC+=1
        else:
            if t[i]!=t[i-1]:
                SC+=S
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=S
                NC+=1
            else:
                NC+=1
    return 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
    SCi=MaxScore(Ni,Ti)
    SC+=SCi
    
print(SC)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
import copy
N,K=map(int,input().split())
R,S,P=map(int,input().split())
point=score={'r':R,'s':S,'p':P}
T=input()
l=[[]for i in range(K)]
for i in range(N):
  l[i%K].append(T[i])
def get_point(s1,s2):
    num={'r':0,'s':1,'p':2}
    if num[s1]==(num[s2]+1)%3:
        return point[s2]
    else:return 0
def rest(hand):
    if hand=='r':return['s','p']
    elif hand=='s':return['r','p']
    elif hand=='p':return['r','s']
def cal(mini_l):
    size=len(mini_l)
    kind=['r','s','p']
    order={'r':[],'s':[],'p':[]}
    score={'r':[],'s':[],'p':[]}
    for hand in kind:
        order[hand].append(hand)
        score[hand].append(get_point(mini_l[0],hand))
    for i in range(1,size):
        new_order={'r':0,'s':0,'p':0}
        for hand in kind:
            choice=''
            tmp_score=-1
            for pre_hand in rest(hand):
                if score[pre_hand][i-1]+get_point(mini_l[i],hand)>tmp_score:
                    choice=pre_hand
                    tmp_score=score[pre_hand][i-1]+get_point(mini_l[i],hand)
            new_order[hand]=copy.copy(order[choice])
            new_order[hand].append(hand)
            score[hand].append(tmp_score)
        order=new_order
    tmp_score=-1
    for hand in kind:
        if score[hand][-1]>tmp_score:
            ans=hand
            tmp_score=score[hand][-1]
    return tmp_score
ans=0
for ll in l:
    ans+=cal(ll)
print(ans)