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:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())

d = {'r':p,'s':r,'p':s}
c = 0

for i in range(k):
    c += d[t[i]]
for i in range(k,n):
    if(t[i] == t[i-k]):
        t[i] = 'x'
    else:
        c += d[t[i]]
print(c)

Submission status: Accepted

Solution code:
from sys import stdin
#n = int(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
n, k = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに
r,s,p = map(int, stdin.readline().rstrip().split())
ts = str(stdin.readline().rstrip())

points  = 0

win = []

def padd(t, points,win):
    if t == "r":
        win.append("r")
        return points+p, win
    elif t=="s":
        win.append("s")
        return points+r, win
    else:
        win.append("p")
        return points+s, win

for i in range(n):
    if i < k:
        points,win = padd(ts[i], points,win)
    else:
        if ts[i] == win[i-k]:
            win.append("n")
        else:
            points, win = padd(ts[i],points,win)

print(points)

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc149/tasks/abc149_d"

def main():
    n,k = list(map(int, input().split()))
    rsp = list(map(int, input().split()))
    j_machine = list(input())
    j_human = []
    score = 0
    for i, v in enumerate(j_machine):
        hand = ''
        points = 0
        hand, points = ret_win_hand_point(v, rsp)

        if i - k >= 0 and hand == j_human[i - k]:
            f_hand = [hand]
            if i + k < len(j_machine):
                h = ret_win_hand_point(j_machine[i + k], rsp)
                f_hand.append(h[0])
            for v in ['r','s','p']:
                if v not in f_hand:
                    hand = v
                    break
            points = 0
        score += points
        j_human.append(hand)
    print(score)


def ret_win_hand_point(machine, rsp):
    if machine == 'r':
        hand = 'p'
        points = rsp[2]
    elif machine == 'p':
        points = rsp[1]
        hand = 's'
    else:
        points = rsp[0]
        hand = 'r'
    return hand, points

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
score_map = {'r':r, 's':s, 'p':p}
t = input()
history = [0] * n
score = 0
for i in range(n):
    opposite = t[i]
    if opposite == 'r':
        hand_win = 'p'
    elif opposite == 's':
        hand_win = 'r'
    else:
        hand_win = 's'

    if i + 1 <= k:
        score += score_map[hand_win]
        history[i] = hand_win
    else:
        if history[i-k] == hand_win:
            continue
        else:
            score += score_map[hand_win]
            history[i] = hand_win
print(score)

Submission status: Accepted

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

o = ''
for j in range(k):
    o += (''.join(t[j::k])).replace('rr', 'r').replace(
        'pp', 'p').replace('ss', 's')

x = 0
for i, h in enumerate(o):
    if h == 'r':
        x += p
    elif h == 's':
        x += r
    else:
        x += s

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc_max_score(choices, R, S, P):
    score = {
        'r': P,  # player: paper, machine: rock
        's': R,  # player: rock, machine: scissors
        'p': S   # player: scissors, machine: paper
    }
    before_choice = None
    seq_cnt = 1
    sum_score = 0
    for c in choices:
        if before_choice == c:
            seq_cnt += 1
            continue
        if before_choice is not None:
            sum_score += score[before_choice] * ((seq_cnt + 1) // 2)
        before_choice = c
        seq_cnt = 1
    if before_choice is not None:
        sum_score += score[before_choice] * ((seq_cnt + 1) // 2)
    return sum_score


def main():
    N, K = list(map(int, input().split(' ')))
    R, S, P = list(map(int, input().split(' ')))
    T = input()
    choice_list = [list() for _ in range(K)]
    for i, c in enumerate(T):
        i %= K
        choice_list[i].append(c)
    answer = 0
    for choices in choice_list:
        answer += calc_max_score(choices, R, S, P)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = [int(e) for e in input().split()]
r, s, p = [int(e) for e in input().split()]
t = input()
t1 = list()
for i in t:
    if i =='r':
        t1.append('p')
    elif i =='p':
        t1.append('s')
    else:
        t1.append('r')
t2=list()
for j,i in  enumerate(t1):
    if j < k:
        t2.append(i)
    else:
        if i == t2[j-k]:
            t2.append('x')
        else:
            t2.append(i)

num = 0

num += t2.count("r")*r
num += t2.count("s")*s
num += t2.count("p")*p

print(num)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r,s,p = map(int, input().split())
d = {"r":p, "s":r, "p":s}
t = input()
mode = [True]*n
score = 0
for i in range(n):
  score += d[t[i]]
  if i >= k and mode[i-k] and t[i] == t[i-k]:
    score -= d[t[i]]
    mode[i] = False
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):

    winToR = P
    winToS = R
    winToP = S
    # init
    dp = []
    for i in range(K):
        dp.append([])
        for _ in range(N // K + 1):
            m = [0] * 3
            dp[i].append(m)

    for k in range(K):
        if T[k] == "r":
            dp[k][0][0] = winToR
        elif T[k] == "s":
            dp[k][0][1] = winToS
        else:
            dp[k][0][2] = winToP

    for n in range(K, N):
        if T[n] == "r":

            dp[n % K][n // K][0] = winToR + max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])
        elif T[n] == "s":
            dp[n % K][n // K][0] = max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = winToS + max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])
        else:
            dp[n % K][n // K][0] = max(dp[n % K][n // K - 1][1], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][1] = max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][2])
            dp[n % K][n // K][2] = winToP + max(dp[n % K][n // K - 1][0], dp[n % K][n // K - 1][1])

    sum = 0
    for k in range(N - K, N):
        imax = 0
        for i in range(3):
            imax = max(imax, dp[k % K][k // K][i])
        sum += imax

    print(sum)




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

a = [0]*k
for i in range(k):
	a[i] = t[i::k]

ans = 0

for i in range(k):
	dp = [[0]*3 for l in range(len(a[i])+1)]
	for j in range(len(a[i])):
		if a[i][j] == 'r':
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2]))
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2]))
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1])+p)
		elif a[i][j] == 's':
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2])+r)
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2]))
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1]))
		else:
			dp[j+1][0] = max(dp[j+1][0], max(dp[j][1], dp[j][2]))
			dp[j+1][1] = max(dp[j+1][1], max(dp[j][0], dp[j][2])+s)
			dp[j+1][2] = max(dp[j+1][2], max(dp[j][0], dp[j][1]))
	ans += max(dp[-1][0], max(dp[-1][1], dp[-1][2]))

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    R, S, P = [int(x) for x in input().split()]
    T = input().strip()

    ans = 0
    for i in range(K):
        f = False
        for j in range(i, N, K):
            if j < K:
                if T[j] == 'r':
                    ans += P
                elif T[j] == 's':
                    ans += R
                else:
                    ans += S
            else:
                if T[j] == T[j - K] and not f:
                    f = True
                    continue
                else:
                    if T[j] == 'r':
                        ans += P
                    elif T[j] == 's':
                        ans += R
                    else:
                        ans += S
                    f = False
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def janken(val):
    if val == "r":
        return "p",p
    elif val == "p":
        return "s", s
    else:
        return "r",r
n, k=map(int, input().split())
r,s,p = map(int,input().split())
t = input()

jan = []
ans = 0
for i in range(n):
    hand, point = janken(t[i])
    if i < k :
        jan.append(hand)
        ans += point
    else:
        if jan[i-k] == hand:
            jan.append("all")
        else:
            jan.append(hand)
            ans+=point
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = [None]*N
for i in range(K):
    if T[i] == 'r':
        ans[i] = 'p'
    elif T[i] == 's':
        ans[i] = 'r'
    else:
        ans[i] = 's'
for i in range(K, N):
    if T[i] == 'r':
        if ans[i-K] == 'p':
            ans[i] = 'd'
        else:
            ans[i] = 'p'
    elif T[i] == 's':
        if ans[i-K] == 'r':
            ans[i] = 'd'
        else:
            ans[i] = 'r'
    else:
        if ans[i-K] == 's':
            ans[i] = 'd'
        else:
            ans[i] = 's'
r = R * ans.count('r')
s = S * ans.count('s')
p = P * ans.count('p')
print(r+s+p)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    point = {'r': p, 's': r, 'p': s}

    ans = 0

    for i in range(k):
        next_skip = False
        for j in range(i, n, k):
            if next_skip:
                next_skip = False
                continue
            if j + k < n and t[j] == t[j + k]:
                next_skip = True
            ans += point[t[j]]
    print(ans)

Submission status: Accepted

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


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

    n_win = {"r": 0, "s": 0, "p": 0}
    lock = [False] * N
    for i in range(K):
        n_win[T[i]] += 1
        lock[i] = True
    for i in range(K, N):
        if T[i-K] == T[i]:
            if not lock[i-K]:
                n_win[T[i]] += 1
                lock[i] = True
        else:
            n_win[T[i]] += 1
            lock[i] = True

    ans = R * n_win["s"] + S * n_win["p"] + P * n_win["r"]
    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 = input()

# R, S, P -> 0, 1, 2

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

Submission status: Accepted

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

array=[True]*len(T)

for t in range(K,len(T)):
  if T[t-K] == T[t] and array[t-K]==True:
    array[t]=False
  

for t in range(len(T)):
  if T[t]=="r" and array[t]==True:
    ans += P
  elif T[t]=="s" and array[t]==True:
    ans += R
   
  elif T[t]=="p"and array[t]==True:
    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())
aite=[]
tetuple=('r','s','p')
def kati(te,ans):
  if te=='r':
    ans+=P
  elif te=='s':
    ans+=R
  else:
    ans+=S
  return ans
for _ in range(k):
  aite.append([])
for i in range(n):
  aite[i%k].append(t[i])
ans=0
for i in range(k):
  ans=kati(aite[i][0],ans)
  for j in range(1,len(aite[i])):
    if aite[i][j]!=aite[i][j-1]:
      ans=kati(aite[i][j],ans)
    else:
      for m in range(3):
        if j!=len(aite[i])-1 and aite[i][j]==aite[i][j+1]==tetuple[m]:
          aite[i][j]=tetuple[(m+1)%3]
        elif j!=len(aite[i])-1 and aite[i][j]!=aite[i][j+1] and aite[i][j]!=tetuple[m] and aite[i][j+1]!=tetuple[m]:
          aite[i][j]=tetuple[m]
print(ans)

Submission status: Accepted

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

mods = [[] for i in range(K)]
for i in range(N):
    mods[i%K].append(T[i])

ans = 0
for mod in mods:
    L = len(mod)
    before = 'N'
    for i in range(L):
        if mod[i] != before:
            if mod[i] == 'r':
                ans += P
            elif mod[i] == 's':
                ans+= R
            else:
                ans+= S
            before = mod[i]
        else:
            before = 'N'
#         print(ans)
#     print('mod',ans)
print(ans)

Submission status: Accepted

Solution code:
def check(list_num,test):
  dp = [[0] * 3 for i in range(len(list_num) + 1)]

  for i in range(len(list_num)):
    for j in range(3):
      if(int(list_num[i]) == int(j)):
        dp[i + 1][j] = max(dp[i][(j + 1) % 3], dp[i][(j + 2) % 3]) + test[j]
      else:
        dp[i + 1][j] = max(dp[i][(j + 1) % 3], dp[i][(j + 2) % 3])

  return max(dp[-1])

def main():
  n,k = map(int, input().split())
  r,s,p = map(int, input().split())
  j = [r,s,p]
  janken = list(input())
  t = []
  for i in janken:
    if(i == 'r'):
      t.append(2)
    elif(i == 's'):
      t.append(0)
    elif(i == 'p'):
      t.append(1)
      
  count = 0
  list_num = [[] for i in range(k)]

  for i in range(len(t)):
    list_num[i % k].append(t[i])


  for i in range(k):
    count += check(list_num[i], j)

  print(count)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

dp = [0] * n
commands = [""] * n

for i in range(n):
    if t[i] == "r":
        commands[i] = "p"
        dp[i] = p
    elif t[i] == "s":
        commands[i] = "r"
        dp[i] = r
    elif t[i] == "p":
        commands[i] = "s"
        dp[i] = s

    if k <= i:
        if commands[i] == commands[i-k]:
            commands[i] = ""
            dp[i] = 0

print(sum(dp))

Submission status: Accepted

Solution code:

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

answer = 0
for i, t in enumerate(T):
    if t == 's':
        if i - K >= 0:
            if T[i-K] != t:
                answer += R
            else:
                T[i]=0
        else:
            answer += R
    elif t == 'p':
        if i - K >= 0:
            if T[i-K] != t:
                answer += S
            else:
                T[i]= 0
        else:
            answer += S
    elif t == 'r':
        if i - K >= 0:
            if T[i-K] != t:
                answer += P
            else:
                T[i]=0
        else:
            answer += P
    
print(answer)

Submission status: Accepted

Solution code:
def add_point(string):
    global ans
    if string=="r":
        ans+=p
    elif string=="s":
        ans+=r
    else:
        ans+=s

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

ans=0

for i in range(n):
    if i<k:
        add_point(S[i])
    else:
        if S[i]==S[i-k]:
            if i+k<n and S[i]==S[i+k]:
                S[i]="a"
            continue
        add_point(S[i])
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
a=(t.count("r"))*p+(t.count("s"))*r+(t.count("p"))*s
for i in range(n-k):
    if t[i]==t[i+k]:
        if t[i]=="r":
            a-=p
        elif t[i]=="s":
            a-=r
        elif t[i]=="p":
            a-=s
        t[i+k]=i
print(a)

Submission status: Accepted

Solution code:
#n回じゃんけん
#i回目とi+k回目は違うもの
n,k=map(int,input().split())
#R：グーで買ったら(相手がｓ)
#S：チョキで買ったら（相手がｐ）
#P：パーで買ったら（相手がｒ）
r,s,p=map(int,input().split())

point={}
point["r"]=p
point["p"]=s
point["s"]=r

#t：相手の手
t=input()
len_t=len(t)

tt=[[] for _ in range(k)]
for i in range(len_t):
    tt[i%k].append(t[i])

result=0
for item in tt:
    back_number=item[0]
    cnt=1
    for item2 in item[1:]:
        if item2 == back_number:
            cnt+=1
        else:
            result+=point[back_number]*((cnt*(-1))//2)*(-1)
            cnt=1
            back_number=item2
    result+=point[back_number]*((cnt*(-1))//2)*(-1)  

print(result)

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split(" "))
    r,s,p = map(int,input().split(" "))
    Ts = list(input())
    flags = [True]*(n)
    ks = {"r":0, "s":0, "p":0}
    for i in range(k,n):
        if Ts[i]==Ts[i-k] and flags[i-k]:
            ks[Ts[i]]+=1
            flags[i] = False

    sum = (Ts.count("r")-ks["r"])*p+(Ts.count("s")-ks["s"])*r+(Ts.count("p")-ks["p"])*s

    print(sum)
main()

Submission status: Accepted

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

Submission status: Accepted

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

Pred = []
dp = []

for i in range(K,N):
    if(T[i] == T[i-K]):
        T[i] = 'x'
    else:
        continue

x = T.count('r')
y = T.count('s')
z = T.count('p')

print(P * x + R * y + S * z)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
t = input()

win_hand = {'r': 'p', 's': 'r', 'p': 's'}
win_point = {'r': p, 's': r, 'p': s}
history = []
points = 0
# dp = [0] * (n+1)  # i回目までプレイしたときの最高の点数
# if t[0] == 'r':
#     dp[1] = p
#     history[1] = 'p'
# elif t[0] == 'p':
#     dp[1] = s
#     history[1] = 's'
# else:
#     dp[1] = r
#     history[1] = 'r'
# k回目までは勝ち続ける
for i, s in enumerate(t):
    if(i < k):
        history.append(win_hand[s])
        points += win_point[s]
    else:
        if (history[i - k] == win_hand[s]):
            history.append('other')
            continue
        else:
            points += win_point[s]
            history.append(win_hand[s])
print(points)
# for s in t[k:]:
#     if(history[])
#     points += win_point[s]


# for in range(2, n+1):
#     if(i-k > 0):
#         # i回目に普通に勝てる時
#         if (win_hand[t[i]] != history[i - k]):
#             dp[i] = dp[i-1] + win_hand[t[i]]
#         # k回前の勝負を諦める?
#         else:

#     else:
#         dp[i] = dp[i-1] + win_hand[t[i]]

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=input()
before=["a"]*k
dic={"r":a[2],"s":a[0],"p":a[1]}
ans=0
for i,s in enumerate(l):
    i=i%k
    if before[i]!=s:
        ans+=dic[s]
        before[i]=s
    else:
        before[i]="a"
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S, P, R = map(int, input().split())
T = input()
dic = {'s':S,'p':P,'r':R}
def solve(T):
    ans = 0
    for i in range(K):
        ans += dic[T[i]]
        serial = 1
        for j in range(i+K,N,K):
            if T[j]==T[j-K]:
                serial += 1
            else:
                serial = 1
            if serial%2==1:
                ans += dic[T[j]]
    return ans
print(solve(T))

Submission status: Accepted

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

C = ["s", "r", "p"]
Win = {"s": "r", "r": "p", "p": "s"}
Point = {"r": R, "p": P, "s": S}

Group = [[] for _ in range(K)]
for i in range(len(T)):
    Group[i % K].append(T[i])

res = 0
for g in Group:
    n = len(g)
    DP = [[0 for _ in range(3)] for _ in range(n + 1)]
    for index, tmp in enumerate(C):
        if Win[g[0]] == tmp:
            DP[0][index] += Point[tmp]
    for i in range(n - 1):
        for j, c in enumerate(C):
            p = 0
            if c == Win[g[i + 1]]:
                p = Point[c]
            if j == 0:
                DP[i + 1][j] = max(DP[i][1], DP[i][2]) + p
            elif j == 1:
                DP[i + 1][j] = max(DP[i][0], DP[i][2]) + p
            else:
                DP[i + 1][j] = max(DP[i][0], DP[i][1]) + p
    res += max(DP[n - 1][0], DP[n - 1][1], DP[n - 1][2])
    # for i in range(n):
    #     print(DP[i])
    # print("------------")

print(res)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
# MARCH = {"M": 0, "A": 0, "R": 0, "C": 0, "H": 0}
t = input()
me = []
ans = 0
for i in range(len(t)):
    if i < k:
        if t[i] == "r":
            me.append("p")
            ans += p
        elif t[i] == "s":
            me.append("r")
            ans += r
        else:
            me.append("s")
            ans += s
    else:
        if t[i] == "r" and me[i-k] != "p":
            me.append("p")
            ans += p
        elif t[i] == "s" and me[i-k] != "r":
            me.append("r")
            ans += r
        elif t[i] == "p" and me[i-k] != "s":
            me.append("s")
            ans += s
        else:
            me.append("?")
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
r, s, p = map(int,input().split())
t = input()
te = ["" for i in range(k)]
ans = 0
for i in range(n):
    if not t[i] == te[i%k]:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
        te[i%k] = t[i]
    else:
        te[i%k] = ""

print(ans)

Submission status: Accepted

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

# DP = [["s", "r", "p"] for _ in range(N + 1)]

res = 0
p = 0
commands = []
for i, t in enumerate(T):
    if t == 'r':
        commands.append('p')
        p = P
    elif t == 's':
        commands.append('r')
        p = R
    else:
        commands.append('s')
        p = S

    if (i - K) >= 0 and commands[i - K] == commands[i]:
        p = 0
        commands[i] = 'x'

    res += p

print(res)

Submission status: Accepted

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

point = [0] * n
for i in range(n):
    if t[i] == 'r':
        point[i] = p
    elif t[i] == 's':
        point[i] = r
    elif t[i] == 'p':
        point[i] = s

for i in range(k, n):
    if (t[i-k] == t[i]) and point[i-k]>0:
        point[i] = 0
print(sum(point))

Submission status: Accepted

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

RSP = [[] for _ in range(K)]

for i,t in enumerate(T):
    RSP[i%K].append(t)

ans = 0
road = []
for row in RSP:
    memo = 'baka'
    for a in row:
        if memo != a:
            if a == 'r':
                ans += P
                road.append('p')
            elif a == 's':
                ans+= R
                road.append('r')
            else:
                ans+= S
                road.append('s')
            memo = a
        else:
            memo = 'baka'

print(ans)

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 in range(N):
  if T[i] == 'r':
    if i > K-1 and T[i-K] == T[i] and ans[i-K]>0:
      pass
    else:
      ans[i] += P
  elif T[i] == 's':
    if i > K-1 and T[i-K] == T[i] and ans[i-K]>0:
      pass
    else:
      ans[i] += R
  else:
    if i > K-1 and T[i-K] == T[i] and ans[i-K]>0:
      pass
    else:
      ans[i] += S
print(sum(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())
point=[R,S,P]
T=input()

myhand=[]
for item in T:
    if item == "r":
        myhand.append("p")
    elif item == "s":
        myhand.append("r")
    else:
        myhand.append("s")

for i in range(N-K):
    if myhand[i]==myhand[i+K]:
        myhand[i+K]="-"
    
print(myhand.count("r")*R+myhand.count("s")*S+myhand.count("p")*P)

Submission status: Accepted

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

ans = {"r": 0, "s": 0, "p": 0}


def win(sr):
    if sr == "r":
        return "p"
    elif sr == "s":
        return "r"
    elif sr == "p":
        return "s"


for i in range(N):
    for j in range(K):
        if i * K + j >= N:
            break
        if T[i*K+j] != before[j]:
            ans[win(T[i*K+j])] += 1
            before[j] = T[i*K+j]
        else:
            before[j] = 0


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

Submission status: Accepted

Solution code:
# 2020/04/29
# AtCoder Beginner Contest 149 - D

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

# Init
d = dict()
d['r'] = r
d['s'] = s
d['p'] = p

d['wintr'] = 'p'
d['wints'] = 'r'
d['wintp'] = 's'

ngt = [''] * n

# Calc
ans = 0
for i in range(n-1, -1, -1):
    wint = d['wint' + t[i]]
    if ngt[i] != wint:
        ans = ans + d[wint]
        if i - k >= 0:
            ngt[i-k] = wint

# Output
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    dic = {"r": P, "s": R, "p": S} #相手の表示に対して出す手の評価値
    T = list(input())
    before = [-1] * (K)
    ans = 0
    for i, j in enumerate(T):
        if not before[i % K] == j:
            before[i % K] = j
            ans += dic[j]
        else:
            before[i%K] = -1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math
import sys

n, k = map(int, sys.stdin.readline().rstrip().split())
r, s, p = map(int, sys.stdin.readline().rstrip().split())
t = sys.stdin.readline().rstrip()
ans = 0
sim = []
for i, ti in enumerate(t):
    if ti == 'r':
        if i >= k and sim[i - k] == 'p':
            sim.append('x')
        else:
            ans += p
            sim.append('p')
    elif ti == 's':
        if i >= k and sim[i - k] == 'r':
            sim.append('x')
        else:
            ans += r
            sim.append('r')
    else:
        if i >= k and sim[i - k] == 's':
            sim.append('x')
        else:
            ans += s
            sim.append('s')
# print(sim)
print(ans)