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:
if __name__ == '__main__':
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    elec = []
    point = 0
    for i in range(k):
        if t[i] == "r":
            point += p
            elec.append("r")
        if t[i] == "s":
            point += r
            elec.append("s")
        if t[i] == "p":
            point += s
            elec.append("p")

    for i in range(k, n):
        if elec[i - k] == t[i]:
            # if i < n - k:
            elec.append(" ")
        else:
            if t[i] == "r":
                point += p
                elec.append("r")
            if t[i] == "s":
                point += r
                elec.append("s")
            if t[i] == "p":
                point += s
                elec.append("p")
    print(point)

Submission status: Accepted

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.append('x')
    else:
      res += P
      log.append('p')
  elif t == 's':
    if K <= i and log[i-K] == 'r':
      log.append('x')
    else:
      res += R
      log.append('r')
  else:
    if K <= i and log[i-K] == 's':
      log.append('x')
    else:
      res += S
      log.append('s')

print(res)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    elec = []
    point = 0
    for i in range(k):
        if t[i] == "r":
            point += p
            elec.append("r")
        if t[i] == "s":
            point += r
            elec.append("s")
        if t[i] == "p":
            point += s
            elec.append("p")

    for i in range(k, n):
        if elec[i - k] == t[i]:
            if i < n - k:
                elec.append(" ")
        else:
            if t[i] == "r":
                point += p
                elec.append("r")
            if t[i] == "s":
                point += r
                elec.append("s")
            if t[i] == "p":
                point += s
                elec.append("p")
    print(point)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
R,S,P=map(int,input().split())
t=input()
ans=0
ra=[]
for i in range(k):
  if t[i]=='r':
    ra.append('p')
    ans+=P
  if t[i]=='p':
    ra.append('s')
    ans+=S
  if t[i]=='s':
    ra.append('r')
    ans+=R
for i in range(k,n):
  if t[i]=='r':
    if ra[i-k]=='p':
      ra.append('a')
    else:
      ra.append('p')
      ans+=P
  if t[i]=='p':
    if ra[i-k]=='s':
      ra.append('a')
    else:
      ra.append('s')
      ans+=S
  if t[i]=='s':
    if ra[i-k]=='r':
      ra.append('a')
    else:
      ra.append('r')
      ans+=R
print(ans)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,k = mi()
r,s,p = mi()
t = input()
lt = len(t)

ans = 0

for i in range(k):
    idx = i
    dp = [[0]*3 for j in range((n//k)+1)]
    if t[idx] == 'r': dp[0][2] = p
    elif t[idx] == 's': dp[0][0] = r
    elif t[idx] == 'p': dp[0][1] = s

    for j in range(1,n+1):
        idx += k
        if idx > lt-1:
            ans += max(dp[j-1])
            break

        ot = t[idx]
        if ot == 'r': 
            dp[j][2] = p + max(dp[j-1][0], dp[j-1][1])
            dp[j][0] = max(dp[j-1][1], dp[j-1][2])
            dp[j][1] = max(dp[j-1][0], dp[j-1][2])
        elif t[idx] == 's':
            dp[j][0] = r + max(dp[j-1][1], dp[j-1][2])
            dp[j][1] = max(dp[j-1][0], dp[j-1][2])
            dp[j][2] = max(dp[j-1][0], dp[j-1][1])
        elif t[idx] == 'p':
            dp[j][1] = s + max(dp[j-1][0], dp[j-1][2])
            dp[j][0] = max(dp[j-1][1], dp[j-1][2])
            dp[j][2] = max(dp[j-1][0], dp[j-1][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
hand = [-1]*n
mk = -1
for i in range(n):
    if i>=k:
        mk = hand[i-k]
    if t[i] =="r" and mk!=2:
        ans+=p
        hand[i] =2
    if t[i] =="s" and mk!=0:
        ans+=r
        hand[i] =0
    if t[i] =="p" and mk!=1:
        ans+=s
        hand[i] =1
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
dp = [3] * k
point = 0
choice = (r, s, p)
t = t.replace('r', '2').replace('s', '0').replace('p', '1')
for j, i in enumerate(t):
    if dp[j % k] != int(i):
        point += choice[int(i)]
        dp[j % k] = int(i)
    else:
        dp[j % k] = 3
print(point)

Submission status: Accepted

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

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

r_cnt = 0
s_cnt = 0
p_cnt = 0
dic = {}
for i in range(N-K):
    if i in dic:
        continue
    if T[i] == T[i+K]:
        if T[i] == "r":
            r_cnt += 1
        elif T[i] == "s":
            s_cnt += 1
        elif T[i] == "p":
            p_cnt += 1
        dic[i+K] = 1


print(calc_point(T) - ((r_cnt)*P + (s_cnt)*R + (p_cnt)*S))

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, K = rl()
R, S, P = rl()
T = rs()

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

score = 0
ans = 0
state = [None]*len(T)
for i in range(N):
	if i < K:
		score += scores[wins[T[i]]]
		state[i] = wins[T[i]]
	else:
		if state[i-K] != wins[T[i]]:
			score += scores[wins[T[i]]]
			state[i] = wins[T[i]]
		else:
			if scores[state[i-K]] < scores[wins[T[i]]]:
				score -= scores[state[i-K]]
				score += scores[wins[T[i]]]
				state[i-K] = '*'
				state[i] = wins[T[i]]
			else:
				state[i] = '*'
print(score)

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

    T = [int(t_) for t_ in T.replace("r","0").replace("s","1").replace("p","2")]
    # win = (h-1) % 3
    r = 0
    s = 1
    p = 2 
    PS = (R,S,P)
    pt = 0
    for k in range(K):
        pre_ = 3
        for c_ind in range(k, len(T), K):
            c_ = T[c_ind]
            if c_ != pre_:
                pt +=  PS[(c_-1)%3]
                pre_ = c_
            else:
                pre_ = 3
    print(pt)



    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())
score = list(map(int, input().split()))
t = input()
self = [0] * n
for i in range(n):
    if t[i] == 'r':
        self[i] = 2
    elif t[i] == 's':
        self[i] = 0
    elif t[i] == 'p':
        self[i] = 1
    if i >= k and self[i] == self[i - k]:
        self[i] = 3
ans = 0
for x in self:
    if x != 3:
        ans += score[x]
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
R,S,P=map(int,input().split())
t=list(input())
ans=0
cmds=[""]*n
for i,j in enumerate(t):

    if j=="r":
        cmd="p"
        point=P

    elif j=="s":
        cmd="r"
        point=R

    elif j=="p":
        cmd="s"
        point=S

    if i-k>=0 and cmds[i-k]==cmd:
        cmd=""
        point=0
    ans+=point
    cmds[i]=cmd

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K =map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
takahashi = [0]*N
for i in range(N):     
    if i < K:
        if T[i] == 'r':
            takahashi[i] = 'p'
        elif T[i] == 's':
            takahashi[i] = 'r'
        elif T[i] == 'p':
            takahashi[i] = 's'
            
    if i >= K:
        if T[i] == 'r' and takahashi[i-K] != 'p':
            takahashi[i] = 'p'
        elif T[i] == 's' and takahashi[i-K] != 'r':
            takahashi[i] = 'r'
        elif T[i] == 'p' and takahashi[i-K] != 's':
            takahashi[i] = 's'

c = Counter(takahashi)
print(R*c['r'] + S*c['s'] + P*c['p'])

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
point = {"r":P, "s":R, "p":S}
dp = [point[T[0]]] + [0] * (N - 1)

for i in range(1, len(T)):
    if i < K:
        dp[i] = dp[i - 1] + point[T[i]]
    else:
        if T[i] == T[i - K]:
            dp[i] = dp[i - 1]
            T[i] = None
        else:
            dp[i] = dp[i - 1] + point[T[i]]

print(dp[N - 1])

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    r,s,p=map(int,input().split())
    T=input()
    T_lis=[False]*n
    ans=0
    for i,t in enumerate(T):
        if i-k>=0:
            if T[i-k]==t and T_lis[i-k]:
                continue
        if t=='r':
            ans+=p
        elif t=='s':
            ans+=r
        else:
            ans+=s
        T_lis[i]=True
    print(ans)


if __name__=='__main__':
    main()

Submission status: Accepted

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

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

# Kのあまりごとに独立して計算する
ans = 0
num = N // K + 1


def vic_sol(s):
    if s == 'r':
        return 2
    if s == 's':
        return 0
    if s == 'p':
        return 1


def ret_score(mine, i, mod, past_rsp):
    sol = vic_sol(T[i * K + mod])
    if (mine == sol) and (sol != past_rsp):
        return score[sol]
    else:
        return 0


for mod in range(K):
    dp = [[0] * 3 for _ in range(num+2)]
    ret = 0
    # 0: R, 1: S, 2: P
    for i in range(1, num+1):
        if (i-1) * K + mod >= N:
            break
        # print(i, num, (i-1) * K + mod)
        dp[i][0] = max(dp[i-1][1] + ret_score(0, i-1, mod, 1), dp[i-1][2] + ret_score(0, i-1, mod, 2))
        dp[i][1] = max(dp[i-1][0] + ret_score(1, i-1, mod, 0), dp[i-1][2] + ret_score(1, i-1, mod, 2))
        dp[i][2] = max(dp[i-1][0] + ret_score(2, i-1, mod, 0), dp[i-1][1] + ret_score(2, i-1, mod, 1))

        if mod == 0:
            dp[i][0] = max(dp[i][0], dp[i - 1][0] + ret_score(0, i - 1, mod, 0))
            dp[i][1] = max(dp[i][1], dp[i - 1][1] + ret_score(1, i - 1, mod, 1))
            dp[i][2] = max(dp[i][2], dp[i - 1][2] + ret_score(2, i - 1, mod, 2))

        ret = max(ret, dp[i][0], dp[i][1], dp[i][2])
    # print(dp)
    ans += ret

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
total=0
t=[[T[i] for i in range(k,N,K)] for k in range(K)]
def janken(x):
  if x=='p':
  	return S
  elif x=='r':
    return  P
  else:
  	return R

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


for k in range(K):
  value=janken(t[k][0])
  memory = [0]*len(t[k])
  memory[0] = janken_hand(t[k][0])
  
  for i in range(1,len(t[k])):
    if t[k][i]!=t[k][i-1]:
      value +=janken(t[k][i])
      memory[i] = janken_hand(t[k][i])
    elif memory[i-1] != janken_hand(t[k][i]):
      value +=janken(t[k][i])
      memory[i] = janken_hand(t[k][i])
    else:
      if janken_hand(t[k][i]) == 's':
        memory[i] = 'p'
      if janken_hand(t[k][i]) == 'r':
        memory[i] = 's'
      else:
        memory[i] = 'r'
  #print(t[k])
  #print(memory)
  #print(value)
  total += value
print(total)

Submission status: Accepted

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

x = [True for _ in range(n)]

ans = 0
for i in range(n):
    if k <= i and x[i - k] and t[i] == t[i - k]:
        x[i] = False
    else:
        if t[i] == 'r':
            ans += p
        elif t[i] == 's':
            ans += r
        else:
            ans += s
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
point={'s':r,'p':s,'r':p}
string=input()
ans=[0]*(n+1)
for i in range(k):
  ans[i]=point[string[i]]
  double=string[i]
  for j in range(1,n//k+1):
    if j*k+i>n-1:
      break
    else:
      now=string[j*k+i]
      if now==double:
        double=''
      else:
        ans[j*k+i]=point[now]
        double=now
print(sum(ans))

Submission status: Accepted

Solution code:
from collections import Counter, deque

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
con = Counter(t)
ans = con['r'] * p + con['s'] * r + con['p'] * s
dic = {'r': p, 's': r, 'p': s}
for i in range(k):
    target = list(t[i::k])
    hp = 0
    count = 1
    for j in range(len(target)):
        hn = target[j]
        if hn == hp:
            count += 1
        elif count > 1:
            ans -= count // 2 * dic[hp]
            count = 1
        hp = hn
    else:
        ans -= count // 2 * dic[hp]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque

n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
con = Counter(t)
ans = con['r'] * p + con['s'] * r + con['p'] * s
dic = {'r': p, 's': r, 'p': s}
for i in range(k):
    target = deque(list(t[i::k]))
    hp = 0
    count = 1
    while target:
        hn = target.popleft()
        if hp == hn:
            count += 1
        elif count > 1:
            ans -= count // 2 * dic[hp]
            count = 1
        hp = hn
    else:
        ans -= count // 2 * dic[hp]
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t = input()
le = len(t)//k
his = []
ans=0
for i in range(k):
    if t[i]=="r":
        ans+=p
        his.append("r")
    if t[i]=="s":
        ans+=r
        his.append("s")
    if t[i]=="p":
        ans+=s
        his.append("p")

for i in range(k, n):
    if his[i-k]==t[i]:
        if i<n-k:
            his.append(" ")
    else:
        if t[i]=="r":
            ans+=p
            his.append("r")
        if t[i]=="s":
            ans+=r
            his.append("s")
        if t[i]=="p":
            ans+=s
            his.append("p")
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r = [int(i) for i in input().split()]
t = list(input())
def func(d,ans):
    if d==0:
        ans += r[2]
        dasite.append(2)
    elif d==1:
        ans += r[0]
        dasite.append(0)
    else:
        ans += r[1]
        dasite.append(1)
    return ans

for i in range(n):
    if t[i]=='r':
        t[i]=0
    elif t[i]=='s':
        t[i]=1
    else:
        t[i]=2
#グーを０にチョキを１にパーを2に書き換える
ans = 0
dasite = []
for i in range(n):
    if i<k:
        ans = func(t[i],ans)
    elif i>=n-k:
        if dasite[i-k]==(t[i]+2)%3:
            continue
        else:
            ans = func(t[i],ans)
    else:
        if dasite[i-k]==(t[i]+2)%3:
            if t[i]==t[i+k]:
                dasite.append(t[i])
            elif t[i]==(t[i+k]+1)%3:
                dasite.append(t[i+k])
            else:
                dasite.append((t[i]+1)%3)
        else:
            ans = func(t[i],ans)
print(ans)

Submission status: Accepted

Solution code:
import collections

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] = "a"

c = collections.Counter(T)

print(c["r"]*P + c["s"]*R + c["p"]*S)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
t2 = []
for i in range(k):
  t2.append(t[i:n:k])
ans = 0
for i in t2:
  c = [0,0,0]
  nc = [0,0,0]
  for j in i:
    if j == "s":
      nc[0] = max(c[1],c[2]) + r
    else:
      nc[0] = max(c[1],c[2])
    if j == "p":
      nc[1] = max(c[0],c[2]) + s
    else:
      nc[1] = max(c[0],c[2])
    if j == "r":
      nc[2] = max(c[0],c[1]) + p
    else:
      nc[2] = max(c[0],c[1])
    c = nc.copy()
  ans += max(c)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
getpoint = {'r':p,'s':r,'p':s}
ans = 0
for i in range(k):
    point = getpoint[t[i]]
    ans += point
    j = i+k
    while j <= n-1:
        if t[j] == t[j-k] and point != 0:
            point = 0
            j += k
        else:
            point = getpoint[t[j]]
            ans += point
            j += k
print(ans)

Submission status: Accepted

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

dp = [[0]*(n+1) for _ in range(3)]
wn = [[0]*(n+1) for _ in range(3)]

for i, t in enumerate(T):
  dp[0][i+1] = dp[1][i+1] = dp[2][i+1] = max(dp[0][i], dp[1][i], dp[2][i])

  if i <= k-1:
    if t == 'r':
      dp[2][i+1] += p
      wn[2][i+1] = True
    elif t == 's':
      dp[0][i+1] += r
      wn[0][i+1] = True
    elif t == 'p':
      dp[1][i+1] += s
      wn[1][i+1] = True

  else:
    if t == 'r' and (not wn[2][i+1-k]):
      dp[2][i+1] += p
      wn[2][i+1] = True
    elif t == 's' and (not wn[0][i+1-k]):
      dp[0][i+1] += r
      wn[0][i+1] = True
    elif t == 'p' and (not wn[1][i+1-k]):
      dp[1][i+1] += s
      wn[1][i+1] = True

ans = max(dp[0][n], dp[1][n], dp[2][n])
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
ts = input()
hands = []

ans = 0
for i, t in enumerate(ts):
  if t == "r":
    if not i - k < 0:
      if hands[i - k] == "p":
        ans += 0
        hands.append("no")
        continue
    ans += p
    hands.append("p")
  elif t == "s":
    if not i - k < 0:
      if hands[i - k] == "r":
        ans += 0
        hands.append("no")
        continue
    ans += r
    hands.append("r")
  elif t == "p":
    if not i - k < 0:
      if hands[i - k] == "s":
        ans += 0
        hands.append("no")
        continue
    ans += s
    hands.append("s")

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T=input()
count=0
RSP={"r" : P, "s" : R, "p" : S}
check=[0 for j in range(N)]
for i in range(N):
    if i<K:
        count+=RSP[T[i]]
        check[i]=1
    else:
        if T[i]!=T[i-K]:
            count+=RSP[T[i]]
            check[i]=1
        else:
            if check[i-K]==0:
                count+=RSP[T[i]]
                check[i]=1
print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans=0
for i in range(n):
    if i < k or (i >= k and t[i]!=t[i-k]):
        if t[i]=='r':
            ans += p
        elif t[i]=='s':
            ans += r
        else:
            ans += s
    else:
        t[i]='e'
print(ans)

Submission status: Accepted

Solution code:

N,shift = map(int,input().split())
R,S,P=map(int,input().split())
Prediction= input()

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

flag = [False]*shift
answer = 0

#processing unit
for i in range(N):
    if i<shift:
        answer+=janken_caliculater(Prediction[i])
    else:
        if Prediction[i] == Prediction[i-shift]:
            if flag[i%shift]:
                answer+=janken_caliculater(Prediction[i])
                flag[i%shift] = False
            else:
                flag[i%shift] = True
        else:
            flag[i%shift] = False
            answer+=janken_caliculater(Prediction[i])


print(answer)

Submission status: Accepted

Solution code:
x,y=map(int,input().split())
a,b,c=map(int,input().split())
n=list(input())
s=0
for i in range(y):
    if n[i]=="r":
        s+=c
    elif n[i]=="s":
        s+=a
    else:
        s+=b
for i in range(y,x):
    if n[i-y]==n[i]:
        n[i]="!"
    else:
        if n[i]=="r":
            s+=c
        elif n[i]=="s":
            s+=a
        elif n[i]=="p":
            s+=b       
print(s)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
takahashiHand =[]
ans = 0

for i in range(k):
    if t[i] == "r":
        takahashiHand.append("p")
    elif t[i] == "s":
        takahashiHand.append("r")
    elif t[i] == "p":
        takahashiHand.append("s")

for i in range(k, n):
    if t[i] == "r" and takahashiHand[i-k] == "p":
        takahashiHand.append(0)
    elif t[i] == "r" and takahashiHand[i-k] != "p":
        takahashiHand.append("p")
    elif t[i] == "s" and takahashiHand[i-k] == "r":
        takahashiHand.append(0)
    elif t[i] == "s" and takahashiHand[i-k] != "r":
        takahashiHand.append("r")
    elif t[i] == "p" and takahashiHand[i-k] == "s":
        takahashiHand.append(0)
    elif t[i] == "p" and takahashiHand[i-k] != "s":
        takahashiHand.append("s")

ans = r*takahashiHand.count("r") + s*takahashiHand.count("s") + p*takahashiHand.count("p")
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
p = [int(i) for i in input().split()]
t = list(input())
a = 0
for i in range(n):
    if i < k or t[i] != t[i - k]:
        a += p["spr".index(t[i])]
    else:
        t[i] = "q"
print(a)

Submission status: Accepted

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

cmd = []
total = 0

for i in range(N):
    if i < K or cmd[i-K] != T[i]:
        if T[i] == "r":
            total += P
        elif T[i] == "s":
            total += R
        elif T[i] == "p":
            total += S
        cmd.append(T[i])
    else:
        cmd.append("")

print(total)

Submission status: Accepted

Solution code:
N, K = map(int, input().split(" "))
R, S, P = map(int, input().split(" "))
T = input().strip()
hands = {i:[] for i in range(K)}
win_hand = {0:'s', 1:'p', 2:'r'}
point = {0:R, 1:S, 2:P}

for i, s in enumerate(T):
    hands[i % K].append(s)

ans = 0
for k in range(K):
    if len(hands[k]) != 0:
        dp = [[0 for _ in range(3)] for i in range(len(hands[k]) + 1)]
        for i in range(1, len(hands[k]) + 1):
            for j in range(3):
                # i番目にjの手を出す -> R, S, P
                dp[i][j] += max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3])
                if hands[k][i - 1] == win_hand[j]:
                    dp[i][j] += point[j]
        ans += max(dp[len(hands[k])])
print(ans)

Submission status: Accepted

Solution code:
nim, mike = map(int, input().split())
A, B, C = map(int, input().split())
array = list(input())
sets = {'r':C, 's':A, 'p':B}

counter = 0

for i in range(nim):
    if i < mike:
        counter += sets[array[i]]
    else:
        if array[i] != array[i - mike]:
            counter += sets[array[i]]
        else:
            array[i] = 'z'

print(counter)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = [0] * n
for i, c in enumerate(t):
    if i >= k and c == t[i - k] and ans[i - k] > 0:
        continue
    if c == 'r':
        ans[i] = p
    elif c == 's':
        ans[i] = r
    else:
        ans[i] = s
print(sum(ans))
'''
じゃんけんマシンのK回前が違う手の場合、勝てる手の得点を加算
じゃんけんマシンのK回前が同じ手で、その時に得点を得ている場合は、
                                   禁止ルールで0点
じゃんけんマシンのK回前が同じ手で、その時に0点の場合は、
                                   勝てる手の得点を加算
'''

Submission status: Accepted

Solution code:
from itertools import permutations as perm
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v, yes="Yes", no="No", upper=False):
    if upper:
        print([yes.upper(), no.upper()][not v])
    else:
        print([yes, no][not v])


def main():
    """エントリーポイント"""
    N, K, R, S, P, T = input([int, 5]) + input([str, 1])
    hand = []
    ans = 0
    for c, t in enumerate(T):
        temp = {"r": "p", "s": "r", "p": "s"}[t]
        if 0 <= c - K:
            if temp == hand[c - K]:
                hand.append("-")
                continue
        hand.append(temp)
        ans += {"r": P, "s": R, "p": S}[t]
    print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

Solution code:
n,k= list(map(int, input().split()))
r,s,p= list(map(int, input().split()))
t=list(input())
ans=0
rf=sf=pf=0
for i in range(k):
    tmp=t[i::k]
    before="a"
    for j in tmp:
        if before==j:
            if before=="r":
                rf+=1
            elif before=="s":
                sf+=1
            else:
                pf+=1
            before="a"
        else:
            before=j
from collections import Counter
d=Counter(t)
for k,v in d.items():
    if k=="r":
        ans+=(v-rf)*p
    elif k=="s":
        ans+=(v-sf)*r
    else:
        ans+=(v-pf)*s
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
t = list(t)
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
for i in range(k, n):
    if t[i] == t[i - k]:
        if t[i] == 'r':
            t[i] = 'x'
            ans -= p
        elif t[i] == 's':
            t[i] = 'x'
            ans -= r
        elif t[i] == 'p':
            t[i] = 'x'
            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())

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

ans=0
for i in range(n):
    if i<k:
        ans+=f(t[i])
    elif t[i]==t[i-k]:
        t[i]="o"
    else:
        ans+=f(t[i])
print(ans)

Submission status: Accepted

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

ans = 0
commands = ['']*n
for i in range(n):
    if T[i] == 'r':
        command = 'p'
        score = P
    elif T[i] == 's':
        command = 'r'
        score = R
    else:
        command = 's'
        score = S

    if i - k >= 0 and commands[i - k] == command:
        command = ''
        score = 0

    commands[i] = command
    ans += score

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

player = ''
ans = 0
d = []
d = deque(d)
for i in range(k):
	if t[i] == 'r':
		ans += p
		d.append('p')
	elif t[i] == 's':
		ans += r
		d.append('r')
	else:
		ans += s
		d.append('s')

#print(ans)
for i in range(k,n):
	player = d.popleft()
	if t[i] == 'r':
		if player != 'p':
			ans += p
			d.append('p')
		else:
			d.append('')
	elif t[i] == 's':
		if player != 'r':
			ans += r
			d.append('r')
		else:
			d.append('')
	else:
		if player != 's':
			ans += s
			d.append('s')
		else:
			d.append('')
	#print(ans)
print(ans)

Submission status: Accepted

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

l = [['none'] for _ in range(k)]
for i in range(n):
    l[i % k].append(t[i])

ans = []
for i in l:
    num = 0
    li = len(i)
    mark = [0 for _ in range(li)]
    for j in range(1, li):
        if i[j-1] != i[j]:
            if i[j] == 'r':
                num += r
            elif i[j] == 's':
                num += s
            else:
                num += p
        else:
            if mark[j-1] == 1:
                if i[j] == 'r':
                    num += r
                elif i[j] == 's':
                    num += s
                else:
                    num += p
            else:
                mark[j] = 1
    ans.append(num)

print(sum(ans))