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: Accepted

Solution code:
def solve(string):
    *nkrsp, t = string.split()
    n, k, *rsp = map(int, nkrsp)
    table = {k: v for k, v in zip("spr", rsp)}
    ans = 0
    for s in range(k):
        u = t[s::k]
        i = 0
        while i < len(u):
            c = 1
            while i < len(u) - 1 and u[i] == u[i + 1]:
                i += 1
                c += 1
            ans += (c + 1) // 2 * table[u[i]]
            i += 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    *nkrsp, t = string.split()
    *t, = map(int, t.replace("r", "2").replace("s", "0").replace("p", "1"))
    n, k, *rsp = map(int, nkrsp)
    ans = 0
    for s in range(k):
        u = t[s::k]
        i = 0
        while i < len(u):
            c = 1
            while i < len(u) - 1 and u[i] == u[i + 1]:
                i += 1
                c += 1
            ans += (c + 1) // 2 * rsp[u[i]]
            i += 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

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

def win(c):
  if c=="r":
    return "p"
  elif c=="s":
    return 'r'
  else:
    return 's'
def rew(c):
  if c=='r':
    return r
  elif c=='p':
    return p
  else:
    return s
bef=[""]*k
ans=0
for i in range(n):
  mod=i%k
  hand=t[i]
  wi=win(hand)
  if not bef[mod]==wi:
    ans+=rew(wi)
    bef[mod]=wi
  else:
    bef[mod]=''
  
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
tmodk = []
score = 0
for i in range(k):
  tmodk.append(t[i::k])
for i in range(k):
  for j in range(len(tmodk[i])):
    if j > 0 and beforeEnemyHand == tmodk[i][j]:
      beforeEnemyHand = ""
      continue
    if tmodk[i][j] == "r":
      score += p
    if tmodk[i][j] == "s":
      score += r
    if tmodk[i][j] == "p":
      score += s
    beforeEnemyHand = tmodk[i][j]
    
print(score)

Submission status: Accepted

Solution code:
N,K = map(int,raw_input().split())
R,S,P = map(int,raw_input().split())
T = raw_input()
ans = 0
win = []
for i in range(len(T)):
  if T[i] == "r":
    win.append("p")
  elif T[i] == "s":
    win.append("r")
  elif T[i] == "p":
    win.append("s")
    
for i in range(K,len(win)):
  if win[i] == win[i-K]:
    if win[i] == "r":
      win[i] = ""
    elif win[i] == "s":
      win[i] = ""
    elif win[i] == "p":
      win[i] = ""
      
for i in range(0,len(win)):
  if win[i] == "r":
    ans = ans + R
  elif win[i] == "s":
    ans = ans + S
  elif win[i] == "p":
    ans = ans + P
  #print ans
#print win
print ans

Submission status: Accepted

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

DP = [0 for i in range(N)]
pre = ["" for i in range(K)]
ans = 0

def Plus(s):
    if s =="s":
        return R
    elif s=="r":
        return P
    elif s=="p":
        return S
    else:
        return 0

for i in range(K):
    pre[i] = T[i]
    ans += Plus(pre[i])

for i in range(1,N//K+1):
    for j in range(K):
        if K*i+j >= N:
            break

        if pre[j] == T[K*i+j]:
            pre[j] = ""
        else:
            pre[j] = T[K*i+j]
            ans += Plus(pre[j])
print(ans)

Submission status: Accepted

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

ans = 0
hist = []
for _t in t:
    h = ""
    if len(hist) >= k:
        h = hist.pop(0)

    if _t == "r" and h != "p":
        ans += p
        hist.append("p")
    elif _t == "s" and h != "r":
        ans += r
        hist.append("r")
    elif _t == "p" and h != "s":
        ans += s
        hist.append("s")
    else:
        hist.append("")

print(ans)

Submission status: Accepted

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

n,k = (int(x) for x in input().split())
r,s,p = (int(x) for x in input().split())
#つまりs,p,rを出されたときに得る点数
mydict = {"s":r, "p":s, "r":p}
t = input()

ans = 0

for i in range(n):
	if i<k:
		ans += mydict[t[i]]
	elif t[i-k]!=t[i]:
		ans += mydict[t[i]]
	elif t[i-k]==t[i]:
		t = t[:i] + 'z' + t[i+1:]

print(ans)

Submission status: Accepted

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

ans = 0
dp = [[0 for _ in range(3)] for _ in range(N)]
for k in range(K):
  for i in range(k,N,K):
    if i < K:
      if T[i] == 'r':
        dp[i][2] = P
      elif T[i] == 's':
        dp[i][0] = R
      else:
        dp[i][1] = S
    else:
      if T[i] == 'r':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])+P
      elif T[i] == 's':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])+R
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])
      elif T[i] == 'p':
        dp[i][0] = max(dp[i-K][1],dp[i-K][2])
        dp[i][1] = max(dp[i-K][0],dp[i-K][2])+S
        dp[i][2] = max(dp[i-K][0],dp[i-K][1])
  ans += max(dp[i][0],dp[i][1],dp[i][2])
print(ans)
#print(dp)

Submission status: Accepted

Solution code:
N, K = map(int, input().split(' '))
R, S, P = map(int, input().split(' '))
T = list(input())
score = 0
A = T
for i in range(N-K):
  if T[i] == T[i+K]:
    A[i+K] = 'a'

for i in range(N):
  if A[i] == 'r':
    score += P
  elif A[i] == 's':
    score += R
  elif A[i] == 'p':
    score += S
print(score)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
score = list(map(int,input().split()))
t = list(map(int,input().replace('r','2').replace('s','0').replace('p','1')))
lt = [t[i::k] for i in range(k)]
ans = 0
for it in range(k):
    string = lt[it]
    dp = [[0 for _ in range(3)]for j in range(len(string)+1)]
    for i in range(len(string)):
        for j in range(3):
            for l in range(3):
                if l==j:
                    continue
                elif l==string[i] and dp[i+1][l]<dp[i][j]+score[l]:
                    dp[i+1][l] = dp[i][j]+score[l]
                elif dp[i+1][l]<dp[i][j]:
                    dp[i+1][l] = dp[i][j]
    ans += max((dp[len(string)][0], dp[len(string)][1], dp[len(string)][2]))
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(K):
  c = 1
  t = T[i]
  for j in range(i + K, N, K):
    if T[j] == t:
      c += 1
    else:
      ans += J[t] * ((c + 1) // 2)
      c = 1
      t = T[j]
  ans += J[t] * ((c + 1) // 2)

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
x=0
t = list(input())
for i in range(k):
  temp=t[i::k]
  for j in range(1,len(temp)):
    if temp[j-1]==temp[j]:
      temp[j]='x'
      t[j*k+i]='x'
point=0
for i in t:
  if i == 'r':
    point+=p
  if i == 's':
    point+=r
  if i == 'p':
    point+=s
print(point)

Submission status: Accepted

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


for i in range(0,K):
    s = [T[k + i] for k in range(0,N,K) if k + i < len(T)]
    prev_e = ''
    for e in s:
      if prev_e != e:
        if e == 'r':
          score += P
        if e == 's':
          score += R
        if e == 'p':
          score += S
        prev_e = e
      else:
        prev_e = ''

print(score)

Submission status: Accepted

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

def point(str):
    if str=="r":
        return P
    elif str=="s":
        return R
    elif str=="p":
        return S


ans=0
o=[False]*K
for i in range(N):
    if i<K:
        ans+=point(T[i])
        o[i%K]=True
    else:
        if T[i]==T[i-K]:
            if o[i%K]==False:
                ans+=point(T[i])
                o[i%K]=True
            else:
                o[i%K]=False
        else:
            ans+=point(T[i])
            o[i%K]=True
print(ans)

Submission status: Accepted

Solution code:
import math


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



# print(N, K, R, S, P)


# Lets wrirte a  Greedy SOlution

mapped_dict = {
    0 : "r",
    1 : "s",
    2 : "p"
}
reverse_mapped_dict = {
    "r" : 0,
    "s" : 1,
    "p" : 2
}

def determine_R_P_S(val):
    if val == "r":
        # print("p")
        return "p"
    elif val == "p":
        # print("s")
        return "s"
    else:
        # print("r")
        return "r"
    
def add_profit(machine_val, you_played, succeed, R, P, S):
    if machine_val == "r":
        if succeed == 1:
            return P
        else:
            return 0

    if machine_val == "p":
        if succeed == 1:
            return S
        else:
            return 0

    if machine_val == "s":
        if succeed == 1:
            return R
        else:
            return 0
    


def total_profit(T, R, P, S, K, N):
    profit = 0
    S_played = ""
    for i in range(0, K):
        I_played = determine_R_P_S(T[i])
        S_played += I_played
        # print("SHOULD PLAY")
        # print(S_played)   
        # print(i)
        # print(I_played)
        # print()
        profit += add_profit(T[i], I_played, 1, R, P, S)

    for i in range(K, N):
        I_played = determine_R_P_S(T[i])
        I_played_previous = S_played[i - K]
        # # print("SHOULD PLAY")
        # print(i )
        # print(I_played, I_played_previous)
        # print()
        if I_played != I_played_previous:
            S_played += I_played
            profit += add_profit(T[i], I_played, 1, R,  P, S)
        else:   
            if i + K < N:
                To_play_future = determine_R_P_S(T[i + K])
                if To_play_future != I_played_previous:
                    S_played += mapped_dict[3 - reverse_mapped_dict[To_play_future] - reverse_mapped_dict[I_played_previous]]
                else:
                    S_played += T[i]
                    
            else:
                S_played += T[i]
                

    # print(S_played)
    return profit



print(total_profit(T, R, P, S, K, N))

Submission status: Accepted

Solution code:
a, b = input().split(" ")
c, d, e = input().split(" ")
str = input()

b = int(b)
c = int(c)
d = int(d)
e = int(e)
score = 0
last_data = []
count = 0
for char in str:
  if(count // b < 1):
    if char == "r":
      score += e
    if char == "s":
      score += c
    if char == "p":
      score += d
    last_data.append(char)
  else:
    mod = count % b
    if char == last_data[mod]:
      last_data[mod] = "a"
    else:
      if char == "r":
        score += e
      if char == "s":
        score += c
      if char == "p":
        score += d
      last_data[mod] = char
  count += 1
  
print(score)

Submission status: Accepted

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


def win(h):
    if h == 'r':
        return P
    elif h == 's':
        return R
    else:
        return S


score = []
for i in range(N):
    if i < K:
        score.append(win(T[i]))
    elif T[i] != T[i - K]:
        s = win(T[i])
        score.append(s)
    else:
        s = win(T[i])
        if s != score[i - K]:
            score.append(s)
        else:
            score.append(0)


print(sum(score))

Submission status: Accepted

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

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

isUsed = [False]*n
ans = 0
for i in range(n):
	if i < k:
		ans+=score(t[i])
		isUsed[i]=True
	else:
		if t[i] == t[i-k] and isUsed[i-k]:
			continue
		ans+=score(t[i])
		isUsed[i]=True

print(ans)

Submission status: Accepted

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

#output
dp = [True] * N
point = {"r": P, "s": R, "p": S}
answer = 0

for i in range(N):
    if i < K:
        answer += point[T[i]]
        dp[i] = False
    else:
        if T[i] == T[i-K]:
            if dp[i-K]:
                answer += point[T[i]]
                dp[i] = False
        else:
            answer += point[T[i]]
            dp[i] = False

print(answer)

Submission status: Accepted

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

def point(i):
	if T[i] == "r":
		return P
	elif T[i] == "s":
		return R
	elif T[i] == "p":
		return S

for i in range(N):
	if i < K:
		res.append(point(i))
	else:
		if T[i - K] != T[i]:
			res.append(point(i))
		elif res[-K] == 0:
			res.append(point(i))
		else:
			res.append(0)
			
print(sum(res))

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 0
for k in range(K):
    orig_str = T[k::K]
    rep_str = orig_str.replace('rr', 'r').replace('ss', 's').replace('pp', 'p')
    point = rep_str.count('r') * P + rep_str.count('s') * R + rep_str.count('p') * S
    ans += point

print(ans)

Submission status: Accepted

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

scores = {'r':r,'p':p,'s':s}
def calc_score(my_hand, other_hand):
    if my_hand == 'r' and other_hand == 's':
        return r
    elif my_hand == 's' and other_hand == 'p':
        return s
    elif my_hand == 'p' and other_hand == 'r':
        return p
    else:
        return 0

def winnable_hand(other_hand):
    if other_hand == "r":
        return "p"
    elif other_hand == "p":
        return "s"
    else:
        return "r"
def losable_hand(other_hand):
    if other_hand == "r":
        return "s"
    elif other_hand == "p":
        return "r"
    else:
        return "p"

result = 0
for i in range(k):
    target = t[i::k]
    rps = {'r':0,'p':0,'s':0}
    for other_hand in target:
        wh = winnable_hand(other_hand)
        lh = losable_hand(other_hand)
        r_max = max(rps['p'], rps['s'])
        p_max = max(rps['r'], rps['s'])
        s_max = max(rps['p'], rps['r'])
        current_max = {hand: m for hand, m in zip(["r","s","p"], [r_max, s_max, p_max])}
        rps[wh] = current_max[wh] + scores[wh]
        rps[other_hand] = current_max[other_hand]
        rps[lh] = current_max[lh]
    
    result += max(rps.values())

print(result)

Submission status: Accepted

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

rsp = [[r, 's'], [s, 'p'], [p, 'r']]
rsp.sort()
rsp.reverse()

sum = 0
t_list = [t[i] for i in range(len(t))]
temp_list = [t[i] for i in range(len(t))]
for hand in rsp:
  for i, _ in enumerate(t_list):
    if hand[1] == _:
      if i >= k and temp_list[i-k] != hand[1]:
        sum += hand[0]
      elif i < k:
        sum += hand[0]
      else:
        temp_list[i] = 'a'
print(sum)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
A = []
ans = 0
for j in range(k):
    A.append([])
for i in range(n):
    A[i%k].append(t[i])
for l in range(k):
    for t in range(1,len(A[l])):
        if A[l][t] == A[l][t-1]:
            A[l][t] = 0
    for t in range(len(A[l])):
        if A[l][t] == "r":
            ans += p
        elif A[l][t] == "s":
            ans += r
        elif A[l][t] == "p":
            ans += s
print(ans)

Submission status: Accepted

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

for i in range(k,n):
  if t[i]==t[i-k]:
    t[i]="a"
print((t.count("r")*p)+(t.count("s")*r)+(t.count("p")*s))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
point=0
l=[]
point_list=[r,s,p]
kind=['r','s','p','r']
for i in range(n):
  for j in range(len(kind)-1):
    if t[i]==kind[j]:
      if i-k>=0 and t[i]==kind[l[i-k]+1]:
        if i+k<n and t[i+k]==kind[j+1]:
          if j==2:j=-1
          l.append(j+1)
        else:
          l.append(j)
      else:
        point+=point_list[j-1]
        if j==0:j=3
        l.append(j-1)
      break
print(point)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = [i for i in input()]
result = 0

score = {"r": P, "s": R, "p": S}
for i in range(K):
      result += score[T[i]]
for i in range(K, N):
      if T[i] == T[i-K]:
            T[i] = ""
            continue
      else:
            result += score[T[i]]

print(result)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = [i for i in input()]
my_T = []
result = 0

for i in T:
  if i == "r":
    my_T.append("p")
  elif i == "s":
    my_T.append("r")
  elif i == "p":
    my_T.append("s")

for i in range(K, N):
      if my_T[i] == my_T[i-K]:
            if i+K < N:
                  if my_T[i+K] == "r":
                        my_T[i] = "s"
                  elif my_T[i+K] == "s":
                        my_T[i] = "p"
                  elif my_T[i+K] == "p":
                        my_T[i] = "r"
            else:
                  if my_T[i-K] == "r":
                        my_T[i] = "s"
                  elif my_T[i-K] == "s":
                        my_T[i] = "p"
                  elif my_T[i-K] == "p":
                        my_T[i] = "r"
      else:
        if my_T[i] == "r":
              result += R
        elif my_T[i] == "s":
              result += S
        elif my_T[i] == "p":
              result += P

for i in range(K):
      if my_T[i] == "r":
            result += R
      elif my_T[i] == "s":
            result += S
      elif my_T[i] == "p":
            result += P


print(result)

Submission status: Accepted

Solution code:
def getWinPoint(c):
    if(c == "r"):
        return P
    elif(c == "p"):
        return S
    elif(c == "s"):
        return R
    else:
        return 0
    return 0

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

ans = 0
hands = ""
for n in range(N):
    if(K <= n and T[n] == hands[n-K]):
        hands += "_"
        continue    
    ans += getWinPoint(T[n])
    hands += T[n]
print(ans)

Submission status: Accepted

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

hands = "rsp"

def f(t):
    n = len(t)
    if n == 0:
        return 0
    dp = [[-1 for hand in hands] for i in range(n)]
    for i in range(n):
        for j in range(len(hands)):
            point = RSP[j] if hands[(j + 1) % 3] == t[i] else 0
            if i == 0:
                dp[i][j] = point
            else:
                dp[i][j] = point + max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3])
    return max(dp[n - 1])

print(sum(map(lambda i: f(T[i::K]), range(K))))

Submission status: Accepted

Solution code:
import copy
n,k = map(int,input().split())
rsp = list(map(int,input().split()))
t = str(input())
count = 0
def win(h):
  if h == "r":
    return 2
  if h == "s":
    return 0
  if h == "p":
    return 1
for i in range(k):
  j = 0
  pre = [0,0,0]
  new = [0,0,0]
  while i + j*k < n:
    h = win(t[i+j*k])
    new[0] = max(pre[1],pre[2])
    new[1] = max(pre[0],pre[2])
    new[2] = max(pre[0],pre[1])
    new[h] += rsp[h]
    pre = copy.deepcopy(new)
    j += 1
  count += max(pre)
print(count)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
ans_list = [0 for i in range(n)]

for i in range(len(t)):
  if t[i] == 'r':
    if (t[i-k] != 'r') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += p
    else:
      ans_list[i] = 0
      ans += 0
  if t[i] == 's':
    if (t[i-k] != 's') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += r
    else:
      ans_list[i] = 0
      ans += 0
  if t[i] == 'p':
    if (t[i-k] != 'p') | (ans_list[i-k] == 0):
      ans_list[i] = 1
      ans += s
    else:
      ans_list[i] = 0
      ans += 0
      
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a,b,c=map(int,input().split())
t=input()
d={"r":c,"s":a,"p":b}
ans=0
for i in range(n):
    if i<k:
        ans+=d[t[i]]
    elif t[i]!=t[i-k]:
        ans+=d[t[i]]
    else:
        t=t[:i]+"z"+t[i+1:]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int,input().split())
RSP = ["r", "s", "p"]
RSP_Point = list(map(int,input().split()))
T = list(input().rstrip())
li = [-1]*K
sum = 0
for i in range(N):
    player = (RSP.index(T[i])+2)%3
    if li[i%K] == player:
        if i+K < N:
            li[i%K] = (RSP.index(T[i+K])+1)%3
    else:
        sum += RSP_Point[player]
        li[i%K] = player
print(sum)

Submission status: Accepted

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

for i in range(k):
    u = t[i::k]
    m = len(u)
    dp = [[0] * 3 for j in range(m+1)]
    for j in range(m):
        dp[j+1][0] = max(dp[j][1], dp[j][2]) + (score[u[j]] if u[j] == "r" else 0)
        dp[j+1][1] = max(dp[j][0], dp[j][2]) + (score[u[j]] if u[j] == "s" else 0)
        dp[j+1][2] = max(dp[j][0], dp[j][1]) + (score[u[j]] if u[j] == "p" else 0)
    ans += max(dp[m])

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()

ans_list = [ 0 for i in range(n)]
ans_list2 = [0 for i in range(n)] 
ans_list3 = ['' for i in range(n)] 

for i in range(len(t)):
    if t[i] == 'r':
        if (t[i-k] != 'r') | (ans_list2[i-k] == 0):
            ans_list[i] = p
            ans_list2[i] = 1
            ans_list3[i] = 'p'
        else:
            ans_list2[i] = 0
            
    elif t[i] == 's':
        if (t[i-k] != 's') | (ans_list2[i-k] == 0):        
            ans_list[i] = r
            ans_list2[i] = 1
            ans_list3[i] = 'r'
        else:
            ans_list2[i] = 0

    else: #t[i] == 'p'
        if (t[i-k] != 'p') | (ans_list2[i-k] == 0):        
            ans_list[i] = s
            ans_list2[i] = 1
            ans_list3[i] = 's'
        else:
            ans_list2[i] = 0

print(sum(ans_list))

Submission status: Accepted

Solution code:
n,k = [int(i) for i in input().split(" ")]
r,s,p = [int(i) for i in input().split(" ")]
points = {"r":p, "s":r, "p":s}
hand = input()
check = [False]*n
totalpoint = 0

for i in range(n):
	
    # 既に探索済みなら確認する必要なし
    if check[i]:
        continue
    
    now = i
    which = 1
    point = [points[hand[i]],0]
    
    while now+k<n and hand[now]==hand[now+k]:
        
        check[now+k] = True
        point[which]+=points[hand[now+k]]        
        now += k
        which = (which+1)%2
    
    totalpoint += max(point)   

print(totalpoint)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())
    sub_ts = [[] for i in range(K)]
    for index, t in enumerate(T):
        sub_ts[index % K].append(t)

    total = 0
    for sub_t in sub_ts:
        total += sub(sub_t, R, S, P)
    print(total)

def sub(T, R, S, P):
    point = 0
    last_te = None
    for t in T:
        if last_te is None or can_win(last_te, t):
            te, p = win(t, R, S, P)
            last_te = te
            point += p
        else:
            last_te = 'o'
    return point

def win(enemy_t, R, S, P):
    if enemy_t == 'r':
        return 'p', P
    elif enemy_t == 's':
        return 'r', R
    else:
        return 's', S

def can_win(last_te, enemy_t):
    win_t = None
    if enemy_t == 'r':
        win_t = 'p'
    elif enemy_t == 's':
        win_t = 'r'
    else:
        win_t = 's'
    return last_te != win_t

main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
dic={}
dic["r"]=p
dic["s"]=r
dic["p"]=s
li=[[] for i in range(k)]
for i in range(n):
    li[i%k].append(t[i])
ans=0
for i in range(k):
    tmp=""
    for j in range(len(li[i])):
        # print(li[i][j])
        if tmp==li[i][j]:
            tmp=""
            continue
        else:
            ans+=dic[li[i][j]]
            tmp=li[i][j]
        # print(ans)
print(ans)

Submission status: Accepted

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

def JSC(hand):
    if hand == 'r':
        return p
    elif hand == 's':
        return r
    else:
        return s

for i in range(k):
    pre = 'n'
    for j in range(n//k+1):
        l = i+j*k
        if l < n:
            if t[l] != pre:
                total += JSC(t[l])
                pre = t[l]
            else:
                pre = 'n'
print(total)

Submission status: Accepted

Solution code:
import sys

def main():

    # sys.stdin = open('input149d.txt')

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

    score = 0

    for i in range(n):
        score += make_score(t[i], r, s, p)

    flag = []

    for i in range(k):
        flag.append(False)

    for i in range(k, n):
        if t[i] == t[i - k] and not flag[i - k]:
            score -= make_score(t[i], r, s, p)
            flag.append(True)

        else:
            flag.append(False)

    print(score)


def make_score(value, r, s, p):
    if value == 'r':
        return p
    elif value == 's':
        return r
    elif value == 'p':
        return s


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def rsp(hand: str) -> int:
  if hand == 'r':
    return p
  elif hand == 's':
    return r
  else:
    return s

for i in range(k):
  score += rsp(t[i])
  twice = True
  for j in range(i+k, n, k):
    if t[j] == t[j-k]:
      if twice:
        twice = False
      else:
        twice = True
        score += rsp(t[j])
    else:
      twice = True
      score += rsp(t[j])
      
print(score)

Submission status: Accepted

Solution code:
# D - Prediction and Restriction
# https://atcoder.jp/contests/abc149/tasks/abc149_d


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

def opt(seg):
    # dp[STEP][R/P/S]
    dp = [ [-1, -1, -1] for _ in range(len(seg))]

    dp[0][0] = (R if seg[0] == 's' else 0)
    dp[0][1] = (P if seg[0] == 'r' else 0)
    dp[0][2] = (S if seg[0] == 'p' else 0)

    for i in range(1, len(seg)):
        dp[i][0] = (R if seg[i] == 's' else 0) + max(dp[i-1][1], dp[i-1][2])
        dp[i][1] = (P if seg[i] == 'r' else 0) + max(dp[i-1][0], dp[i-1][2])
        dp[i][2] = (S if seg[i] == 'p' else 0) + max(dp[i-1][0], dp[i-1][1])

    return max(dp[-1])

segs = list()

for K_idx in range(K):
    segs.append( T[K_idx::K] )

print('{}'.format(sum(map(opt, segs))))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    dp = [[0 for _ in range(n + 1)] for _ in range(3)]

    for index, ti in enumerate(t, 1):
        if index <= k:
            if ti == 'r':
                dp[2][index] = p
            elif ti == 's':
                dp[0][index] = r
            elif ti == 'p':
                dp[1][index] = s
        else:
            if ti == 'r' and dp[2][index - k] == 0:
                dp[2][index] = p
            elif ti == 's' and dp[0][index - k] == 0:
                dp[0][index] = r
            elif ti == 'p' and dp[1][index - k] == 0:
                dp[1][index] = s

    ans = 0

    for d in dp:
        ans += sum(d)

    print(ans)


if __name__ == '__main__':
    main()