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:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    ans=0
    flag=[False]*N
    for i in range(N):
        if i<K or T[i]!=T[i-K] or (not flag[i-K]):
            ans+=P if T[i]=='r' else S if T[i]=='p' else R
            flag[i]=True
        
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
P=0
R=0
S=0
pp = 0
rr = 0
ss = 0 

for i in t:
    if i == 'r':
        P+=1
    elif i =='s':
        R+=1
    else:
        S+=1
# for i,j in zip(t,t[b:]):
#     print(i,j)
#     if i == j and i == 'r':
#         pp+=1
#     elif i == j and i == 's':
#         rr +=1
#     elif i == j and i =='p':
#         ss+=1
def cal(x):
    return -((-x)//2)
# ans  = p*(P-pp//2) + r*(R-rr//2) + s*(S-ss//2)
# print(ans)

PP =[]
RR = []
SS = []
# print(P,R,S)
for i in range(b):
    A = 0
    PPP = 1
    RRR=1
    SSS = 1

    for j in range(i+b,a,b):
        # print(t[j-b] , t[j])
        if t[j-b] == t[j]:
            if t[j] == 'r':
                PPP+=1
            elif t[j] =='s':
                RRR +=1
            elif t[j] == 'p':
                SSS+=1
        else:
            # print(PPP,RRR,SSS)
            P-=PPP//2
            R-=RRR//2
            S-=SSS//2
            PPP = 1
            RRR=1
            SSS = 1

    else:
        P-=PPP//2
        R-=RRR//2
        S-=SSS//2
        PPP = 1
        RRR=1
        SSS = 1


ans  = p*(P) + r*(R) + s*(S)
# print(P,R,S)
print(ans)

Submission status: Accepted

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

score = 0
#dp[i][j]をi回目のジャンケンで、j(0-> R, 1 -> S, 2 -> P)の手を出したときまでのスコアの和とする
for i in range(n):
    if i - k < 0:
        if t[i] == 'r':
            score += p
        elif t[i] == 's':
            score += r
        else:
            score += s
        continue
    
    else:
        if t[i-k] == t[i]:
            t = t[:i] + 'n' + t[i+1:]
            # print(t)
        else:
            if t[i] == 'r':
                score += p
            elif t[i] == 's':
                score += r 
            else:
                score += s
        
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input().replace('r', 'a').replace('s', 'b').replace('p', 's').replace(
    'b', 'r').replace('a', 'p')
ans = t.count('r') * r + t.count('s') * s + t.count('p') * p
for i in range(k):
    while i + k < n:
        if t[i] == t[i + k] == 'r':
            ans -= r
            i += k * 2
        elif t[i] == t[i + k] == 's':
            ans -= s
            i += k * 2
        elif t[i] == t[i + k] == 'p':
            ans -= p
            i += k * 2
        else:
            i += k
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    R, S, P = (int(i) for i in input().split())
    T = input()
    pre = []
    win = {"r": "p", "s": "r", "p": "s"}
    ans = 0
    for i, t in enumerate(T):
        if K <= i and win[t] == pre[i-K]:
            pre.append("")
        else:
            if t == "r":
                ans += P
                pre.append("p")
            elif t == "s":
                ans += R
                pre.append("r")
            elif t == "p":
                ans += S
                pre.append("s")

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

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

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

result = [''] * n
result_num = 0
for j in range(n):
  if j >= k and result[j - k] == win_map[t[j]]:
    if j + k < n and t[j] == win_map[t[j + k]]:
      result[j] = lose_map[t[j]]
    else:
      result[j] = t[j]
  else:
    result[j] = win_map[t[j]]
    result_num += score[win_map[t[j]]]


print(result_num)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r,s,p = map(int, input().split())
point = {}
can_win = {}
point["r"] = r
point["s"] = s
point["p"] = p
can_win["r"] = "p"
can_win["s"] = "r"
can_win["p"] = "s"
t = input()
acc = [0 for i in range(n + k + 10)]
ans = 0
pattern = ""
for i in range(n):
    pattern += (can_win[t[i]])
ans = 0
pattern = list(pattern)
for i in range(n):
    if i - k < 0:
        ans += point[pattern[i]]
    else:
        if pattern[i] == pattern[i-k]:
            pattern[i] = "q"
            continue
        else:
            ans += point[pattern[i]]
print(ans)

Submission status: Accepted

Solution code:
num = input().split(' ')
point = input().split(' ')
point_int = [int(point[i]) for i in range(3)]
data = input()
count = 1
result = 0
judge = [0 for i in range(int(num[0]))]
for i in range(int(num[0])):
    if(data[i] == 'r'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'r'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[2]
                    judge[i] = 1
                else:
                    pass

            else:
                result += point_int[2]
                judge[i] = 1
        else:
            result += point_int[2]
            judge[i] = 1
    elif(data[i] == 's'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 's'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[0]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[0]
                judge[i] = 1
        else:
            result += point_int[0]
            judge[i] = 1

    else:
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'p'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[1]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[1]
                judge[i] = 1
        else:
            result += point_int[1]
            judge[i] = 1

print(result)

Submission status: Accepted

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

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

T = input()

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

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

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

points = decision.count('r')*R + decision.count('s')*S + decision.count('p')*P
print(points)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
line1 = input().split(' ')
line2 = input().split(' ')
N = int(line1[0])
K = int(line1[1])
R = int(line2[0])
S = int(line2[1])
P = int(line2[2])
T = input()
score = 0
mem = list(range(K))

for i in range(N):
  index = i % K

  if (mem[index] != T[i:i+1]):
    if (T[i:i+1] == 'r'):
      score = score + P
    elif (T[i:i+1] == 's'):
      score = score + R
    elif (T[i:i+1] == 'p'):
      score = score + S
    mem[index] = T[i:i+1]
  else:
    mem[index] = 0

print(score)

Submission status: Accepted

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

i = 1
A = ['t']*K
mp1 = {'r':'p','s':'r','p':'s'}
mp2 = {'r':'r','s':'s','p':'p'}
mp3 = {'r':R,'s':S,'p':P}
ans = 0
for janken in T:
    tmp = i%K
    if A[tmp] == mp1[janken]:
        A[tmp] = 't'
    else:
        A[tmp] = mp1[janken]
        ans += mp3[mp1[janken]]
    i += 1

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = list(input())
c = [[0 for i in range(3)] for _ in range(N)]
if T[0] == 'r':
    c[0][0] = 0
    c[0][1] = 0
    c[0][2] = P
elif T[0] == 's':
    c[0][0] = R
    c[0][1] = 0
    c[0][2] = 0
elif T[0] == 'p':
    c[0][0] = 0
    c[0][1] = S
    c[0][2] = 0
# for i in range(n):
#     for j in range(3):
#         for k in range(3):
#             if j == k:
#                 continue
#             c[i][j]
def change(s):
    l = []
    for i in range(len(s)):
        if s[i] == 'r':
            v = 'p'
        elif s[i] == 's':
            v = 'r'
        elif s[i] == 'p':
            v = 's'
        l.append(v)
    return l
new = change(T)
point = 0
for i in range(len(new)-K):
    if new[i] == new[i+K]:
        new[i+K] = 'stop'
for i in new:
    if i == 'p':
        point += P
    elif i == 'r':
        point += R
    elif i == 's':
        point += S
print(point)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def win_rsp(hand):
    if hand == 'r':
        return 'p'
    elif hand == 's':
        return 'r'
    else:  # 'p'
        return 's'


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

get_score = {'r': R, 's': S, 'p': P}

player_hands = [''] * N
score = 0
# for i in range(N):
#     if player_hands[i] != '':
#         continue
#     if i > K - 1:  # K 回前と同じ手は出せない
#         for j in range(i, N, K):
#             if player_hands[j] != '':
#                 continue
#             if win_rsp(T[j]) == player_hands[j - K]:  # 勝てない
#                 player_hands[j] = 'X'  # 残りふたつのどちらか
#                 player_hands[j + K] = win_rsp[T[j + K]] # j + K 回目で勝つしか無い
#                 # add score
#                 score += get_score[player_hands[j + K]]

#     else:  # ただ勝てばいい
#         player_hands[i] = win_rsp(T[i])
#         # add score
#         score += get_score[player_hands[i]]

t = list(T)
# print(t)
for i in range(K):
    spl = t[i::K]
    win_l = list(map(win_rsp, spl))
    # print(win_l)
    # for i in range(len(win_l)):
    j = 0
    while j < len(win_l):
        if j == len(win_l) - 1:
            score += get_score[win_l[j]]
            # print(get_score[win_l[j]])
            # print('score:', score)
            break
        if win_l[j] != win_l[j + 1]:
            score += get_score[win_l[j]]
            # print(get_score[win_l[j]])
            # print('score:', score)
            j += 1
        else:
            score += get_score[win_l[j]]
            # print(get_score[win_l[j]])
            j += 2

    # print(spl)
    # odd_l = spl[::2]
    # eve_l = spl[1::2]
    # print(odd_l)
    # print(eve_l)

    # win_odd_l = list(map(win_rsp, odd_l))
    # win_eve_l = list(map(win_rsp, eve_l))
    # print('win_odd_l', win_odd_l)
    # print('win_eve_l', win_eve_l)

    # odd_score = 0
    # eve_score = 0
    # for o in win_odd_l:
    #     odd_score += get_score[o]
    # for e in win_eve_l:
    #     eve_score += get_score[e]

    # m = max(odd_score, eve_score)
    # print(m)
    # score += m

print(score)

Submission status: Accepted

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

pattern = []
score = 0

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

double = []
single = []
for i in range(K, N - K):
    if pattern[i] == pattern[i - K] and pattern[i] == pattern[i + K]:
        pattern[i] = ''

for i in range(K, N):
    if pattern[i] == pattern[i - K]:
        pattern[i] = ''

for p in pattern:
    if p == 'r':
        score += R
    elif p == 's':
        score += S
    elif p == 'p':
        score += P

print(score)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N, K = MAP()
	R, S, P = MAP()
	T = input().rstrip()

	Tokuten = [R, S, P]
	Te = ['r', 's', 'p']

	A = [-1] * N
	for i in range(N):
		now = 0 if T[i]=='s' else 1 if T[i]=='p' else 2
		if i-K < 0:
			A[i] = now
		else:
			past = A[i-K]
			if now == past:
				if i+K < N:
					future = 0 if T[i+K]=='s' else 1 if T[i+K]=='p' else 2
					for j in range(3):
						if not j in [now, future]:
							A[i] = j
			else:
				A[i] = now
	
	# print(A)

	ans = 0
	for i in range(N):
		te = A[i]
		if te==0 and T[i]=='s':
			ans += Tokuten[0]
		elif te==1 and T[i]=='p':
			ans += Tokuten[1]
		elif te==2 and T[i]=='r':
			ans += Tokuten[2]

	print(ans)

if __name__ == '__main__':
	main()

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(N - K):
    if T[i] == T[i + K]:
        T[i + K] = ""

ans = 0
for i in range(N):
    if T[i] == "r":
        ans += P
    elif T[i] == "s":
        ans += R
    elif T[i] == "p":
        ans += S

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t = input()
res = 0
te_log = []

def judg(te):
  if te == "r":
    return "p"
  if te == "s":
    return "r"
  if te == "p":
    return "s"

def score(te):
  if te == "r":
    return r
  if te == "s":
    return s
  if te == "p":
    return p
  
for i in range(n):
  if i+1 <= k:
    res += score(judg(t[i]))
    te_log.append(judg(t[i]))
    continue
  comp = t[i]
  no = te_log[i-k]
  me = judg(comp)
  if no == me:
    if i+1+k > n:
      te_log.append(comp)
    elif judg(t[i+k]) == comp:
      te_log.append(judg(judg(comp)))
    else:
      te_log.append(comp)
    continue
  res += score(me)
  te_log.append(me)

print(res)

Submission status: Accepted

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

stra = {"r":"p","s":"r","p":"s"}
point = {"r":R,"s":S,"p":P}
c = 0
result = []
for i in range(K):
    c += point[stra[T[i]]]
    result.append(stra[T[i]]) 
    

for i in range(K,N):
    before = result[i-K]
    if i <= N - K - 1:
        after = stra[T[i+K]]
    if stra[T[i]] == before:
        result.append("ok")
    else:
        c += point[stra[T[i]]]
        result.append(stra[T[i]])
        
print(c)

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
r, s, p = [int(x) for x in input().split()]
t = input()
z = {'r': 'p', 's': 'r', 'p': 's'}
v = {'r': r, 's': s, 'p': p, 'z': 0}
x = []
for a in t:
    x.append(z[a])
for i in range(k):
    for j in range(k + i, n, k):
        if x[j] == x[j - k]:
            x[j] = 'z'
ans = 0
for i in range(n):
    ans += v[x[i]]
print(ans)

Submission status: Accepted

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

st = ''
for i in t:
    if i == 'r':
        st += 'p'
    elif i == 'p':
        st += 's'
    else:
        st += 'r'

stt = st[:k]

for i in range(k, n):
    if st[i] != stt[i - k]:
        stt += st[i]
    else:
        stt += 'x'

sum = 0
for i in stt:
    if i == 'r':
        sum += r
    elif i == 's':
        sum += s
    elif i == 'p':
        sum += p
    else:
        pass
print(sum)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
#point = {'r':P, 's':R, 'p':S}
#print(point)
T = input()

r_num = 0
s_num = 0
p_num = 0

mycurrent = ['a' for _ in range(K)]

for n in range(N):
    if T[n] == 'r':
        me = 'p'
    if T[n] == 's':
        me = 'r'
    if T[n] == 'p':
        me = 's'
    
    if mycurrent[n%K] != me:
        if me == 'r':
            r_num += 1
            mycurrent[n%K] = me
        if me == 's':
            s_num += 1
            mycurrent[n%K] = me
        if me == 'p':
            p_num += 1
            mycurrent[n%K] = me
            
    else:
        mycurrent[n%K] = 'a'
        
    #print(r_num, s_num, p_num)
print(r_num* R + s_num* S + p_num*P)

Submission status: Accepted

Solution code:
[N, K] = list(map(int, input().split()))
[R, S, P] = list(map(int, input().split()))
T = str(input())
t = []
for i in range(K):
  t.append(T[i])
for i in range(K, N):
  if t[-K] != T[i]:
    t.append(T[i])
  else:
    t.append('Z')
R_s = R * len([r for r in t if r == 's'])
S_s = S * len([s for s in t if s == 'p'])
P_s = P * len([p for p in t if p == 'r'])
ans = int(R_s + S_s + P_s)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
for i in range(K,N):
    if T[i]==T[i-K] and T[i-K]!="x":
      T=(T[:i]+"x"+T[i+1:])
    else:
      continue
     
R_win=T.count("s")
S_win=T.count("p")
P_win=T.count("r")
point=R_win*R+S_win*S+P_win*P
print(point)

Submission status: Accepted

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

t_sep = []
for i in range(k):
    t_sep.append([])

for j in range(n):
    list_num = j%k
    t_sep[list_num].append(t[j])

ans = 0
for each_list in t_sep:
    if each_list[0] == 'r':
        ans += p
    elif each_list[0] == 's':
        ans += r
    else:
        ans += s


for each_list in t_sep:
    for i in range(len(each_list)-1):
        if each_list[i+1]==each_list[i]:
            if each_list[i + 1] != each_list[i-1] or i ==0:
                con = 1
            if  con ==1:
                ans += 0
            else:
                if each_list[i + 1] == 'r':
                    ans += p
                elif each_list[i + 1] == 's':
                    ans += r
                else:
                    ans += s
            con = con * (-1)


        else:
            if each_list[i+1] == 'r':
                ans += p
            elif each_list[i+1] == 's':
                ans += r
            else:
                ans += s

print(ans)

Submission status: Accepted

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

#r - P
#s - R
#p - S

ans = [0] *len(T)
for i,x in enumerate(T):
  if x == 'r':
    ans[i] = P
  elif x == 's':
    ans[i] = R
  elif x == 'p':
    ans[i] = S
  
  if i >= K:
    if (T[i-K] == x) & (ans[i-K] == ans[i]):
      ans[i] = 0

print(sum(ans))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = str(input())
janken = {"r":"p","s":"r","p":"s"}
point = {"r":P,"s":R,"p":S}
kekka = []
ans = 0

for i in range(K):
    kekka.append(janken[T[i]]) 
    ans += point[T[i]]

for i in range(K,N):
    if janken[T[i]] == kekka[i-K] and kekka[i-K] != T[i-K]: # kekka[i-K]を負けてもアイコでもOKなら次向けに融通聞かせる手がある！
        kekka.append(T[i])
    else:
        kekka.append(janken[T[i]])
        ans += point[T[i]]

print(ans)

Submission status: Accepted

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


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    ans=0
    flag=[False]*N
    for i in range(N):
        if i<K:
            ans+=P if T[i]=='r' else S if T[i]=='p' else R
            flag[i]=True
    for i in range(K, N):
        if T[i]!=T[i-K] or (not flag[i-K]):
            ans+=P if T[i]=='r' else S if T[i]=='p' else R
            flag[i]=True
        
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    solve(N, K, R, S, P, T)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


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


for i in range(k,n):    
    if hands[i] == hands[i-k]:
        c = 'rsp'
        c = c.replace(hands[i], '')
        if i+k < n:
            c = c.replace(hands[i+k], '')
        if len(c) == 1:
            hands = hands[:i] + c + hands[i+1:]
        else:
            hands = hands[:i] + t[i] + hands[i+1:]

cnt = 0
for i in range(n):
    if (hands[i]=='r')&(t[i]=='s'):
        cnt += r
    elif (hands[i]=='s')&(t[i]=='p'):
        cnt += s
    elif (hands[i]=='p')&(t[i]=='r'):
        cnt += p
print(cnt)

Submission status: Accepted

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

N,K=map(int, sys.stdin.readline().split())
R,S,P=map(int, sys.stdin.readline().split())
T=raw_input()

max_T=[]
for x in T:
    if x=="r": max_T.append("p")
    elif x=="s": max_T.append("r")
    elif x=="p": max_T.append("s")

for i,x in enumerate(max_T):
    if i+K<N:
        if max_T[i]==max_T[i+K]:
            max_T[i+K]=None

ans=0
for x in max_T:
    if x=="r": ans+=R
    elif x=="s": ans+=S
    elif x=="p": ans+=P
print ans

Submission status: Accepted

Solution code:
N , K = map(int,input().split())
R , S, P = map(int,input().split())
T = list(input())
Tstr = str(T)
r = Tstr.count('r')
s = Tstr.count('s')
p = Tstr.count('p')

for i in range(N-K):
    if T[K+i] == T[i] == 'r':
        T[K+i] = 'x'
    elif T[K+i] == T[i] == 's':
        T[K+i] = 'x'
    elif T[K+i] == T[i] == 'p':
        T[K+i] = 'x'
    else:
        r = r
Tstr = str(T)
r2 = Tstr.count('r')
s2 = Tstr.count('s')
p2 = Tstr.count('p')
    
print(r2*P+s2*R+p2*S)

Submission status: Accepted

Solution code:
import copy

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

total = 0
choice = {'r':0,'s':1,'p':2}
lst = [P,R,S]

for i in range(K):
    total += lst[choice[T[i]]]
    
for i in range(K,N):
    if T[i] != T[i-K]:
        total += lst[choice[T[i]]]
    else:
        T[i] = ''

print(total)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
line,last=divmod(n,k)
for i in range(last):
  lr,ls,lp=0,0,0
  for j in range(line+1):
    x=t[j*k+i]
    if x=="r":
      tr=max(ls,lp)
      ts=max(lp,lr)
      tp=max(lr,ls)+p
      lr,ls,lp=tr,ts,tp
    elif x=="s":
      tr=max(ls,lp)+r
      ts=max(lp,lr)
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
    else:
      tr=max(ls,lp)
      ts=max(lp,lr)+s
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
  ans+=max(lr,ls,lp)
for i in range(last,k):
  lr,ls,lp=0,0,0
  for j in range(line):
    x=t[j*k+i]
    if x=="r":
      tr=max(ls,lp)
      ts=max(lp,lr)
      tp=max(lr,ls)+p
      lr,ls,lp=tr,ts,tp
    elif x=="s":
      tr=max(ls,lp)+r
      ts=max(lp,lr)
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
    else:
      tr=max(ls,lp)
      ts=max(lp,lr)+s
      tp=max(lr,ls)
      lr,ls,lp=tr,ts,tp
  ans+=max(lr,ls,lp)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
best = ''
for ti in t:
    if ti == 's':
        best += 'r'
    elif ti == 'p':
        best += 's'
    else:
        best += 'p'
lis = [[] for _ in range(k)]
# print(lis)
final_st = ''
for bi,b in enumerate(best):
    lis[bi%k].append(b)
for each_lis in lis:
    each_st = ''
    for e_i,e_lis in enumerate(each_lis):
        if e_i == len(each_lis) - 1:
            if e_i == 0:
                each_st += each_lis[e_i]
            else:
                if each_lis[e_i] != each_lis[e_i - 1]:
                    each_st += each_lis[e_i]
            break
        else:
            if each_lis[e_i] != each_lis[e_i + 1]:
                each_st += each_lis[e_i]
            else:
                each_st += each_lis[e_i]
                each_lis[e_i + 1] = 'x'
    #print(each_st)
    final_st += each_st
#print(lis)
#print(final_st)
final_sum = 0
for f in final_st:
    if f == 'r':
        final_sum += r
    elif f == 's':
        final_sum += s
    elif f == 'p':
        final_sum += p
print(final_sum)

Submission status: Accepted

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

ans = 0
a = []
for i, t in enumerate(T):
  if i < K:
    ans += d[t]
    a.append(t)
  else:
    if t == a[i-K]:
      a.append('a')
    else:
      ans += d[t]
      a.append(t)

print(ans)