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

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

ans = 0
for t in T:
    ans += points[t]

for k in range(K):
    left = k
    while left < N:
        right = left
        while right < N and T[right] == T[left]:
            right += K
        cnt = (right - left) // K
        ans -= points[T[left]] * (cnt // 2)
        left = right
print(ans)

Submission status: Accepted

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

def win_hand(machine):
  if machine == "s":
    return "r"
  elif machine == "p":
    return "s"
  elif machine == "r":
    return "p"

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

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

ans = 0
for i in range(K):
  player = []
  for j in range(len(t_sorted[i])):
    if j == 0:
      player.append(win_hand(t_sorted[i][j]))
      ans += judge(t_sorted[i][j])
    else:
      if win_hand(t_sorted[i][j]) == player[j-1]:
        if j != len(t_sorted[i])-1:
          if "r" != win_hand(t_sorted[i][j]) and "r" != win_hand(t_sorted[i][j+1]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]) and "s" != win_hand(t_sorted[i][j+1]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]) and "p" != win_hand(t_sorted[i][j+1]):
            player.append("p")
        else:
          if "r" != win_hand(t_sorted[i][j]):
            player.append("r")
          elif "s" != win_hand(t_sorted[i][j]):
            player.append("s")
          elif "p" != win_hand(t_sorted[i][j]):
            player.append("p")
      else:
          player.append(win_hand(t_sorted[i][j]))
          ans += judge(t_sorted[i][j])

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
p_list=[]
score=0
for i in range(len(t)):
    if i<k:
        if t[i]=='r':
            p_list.append('p')
            score+=p
        elif t[i]=='s':
            p_list.append('r')
            score+=r
        else:
            p_list.append('s')
            score+=s
    else:
        if t[i]=='r':
            if p_list[i-k]=='p':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('p')
                score+=p
        elif t[i]=='s':
            if p_list[i-k]=='r':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('r')
                score+=r
        else:
            if p_list[i-k]=='s':
                if i+k>=len(t):
                    p_list.append('a')
                else:
                    p_list.append(t[i+k])
            else:
                p_list.append('s')
                score+=s
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
S = input()
ans = 0
hand = ""
dic = {"r":"p","p":"s","s":"r"}
for i in range(n):
    if i < k:
        if S[i] == "r":
            ans += p
            hand += "p"
        elif S[i] == "p":
            ans += s
            hand += "s"
        else:
            ans += r
            hand += "r"
   
    else:
        if dic[S[i]] == hand[i-k]:
            hand += "q"
            continue
        else:
            if S[i] == "r":
                ans += p
                hand += "p"
            elif S[i] == "p":
                ans += s
                hand += "s"
            else:
                ans += r
                hand += "r"
        
print(ans)

Submission status: Accepted

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

ans = ""
score = 0

for i, t in enumerate(T):
    if t == 'r':
        if i >= K and "p" == ans[-K]:
            ans += " "
        else:
            ans += 'p'
            score += P    

    elif t == 's':
        if i >= K and "r" == ans[-K]:
            ans += " "
        else:
            ans += 'r'
            score += R 
    else:
        if i >= K and 's' == ans[-K]:
            ans += " "
        else:
            ans += 's'
            score += S
print(score)

Submission status: Accepted

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

R,S,P=map(int,input().split())
total=0
I=input()
count=0
T=list(I)
for i in range(N):
    if T[i]=='r':
        total+=P
    if T[i]=='s':
        total+=R
    if T[i]=='p':
        total+=S
for k in range(K):
    for i in range(count,N,K):
        if T[i-K]==T[i] and i>=K :
            if T[i] == 'r':
                total -= P
            if T[i] == 's':
                total -= R
            if T[i] == 'p':
                total -= S
            T[i]='Y'
    count+=1
#print(T)
print(total)

Submission status: Accepted

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

pre = [[] for i in range(k)]

for i in range(n):
    #print(i, pre[i%k], t[i])
    pre[i%k].append(t[i])
#print(pre)

def score(h):
    c_r = 0
    c_s = 0
    c_p = 0
    num = "".join(h)
    count = 1
    result = ''

    # 配列全体のループ
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
                count = count + 1
            else:
                result = result + str(num[i]) + str(count)
                ko = num[i]
                wa = count 
                if wa%2 == 0:
                    if ko == "r":
                        c_r += wa//2
                    elif ko == "s":
                        c_s += wa//2
                    else:
                        c_p += wa//2
                else:
                    if ko == "r":
                        c_r += 1 + (wa-1)//2
                    elif ko == "s":
                        c_s += 1 + (wa-1)//2
                    else:
                        c_p += 1 + (wa-1)//2
                count = 1
        except:
            # 最後の文字セットがOutOfRangeになるので結果に出力
            result = result + str(num[i]) + str(count)
            ko = num[i]
            wa = count 
            if wa%2 == 0:
                if ko == "r":
                    c_r += wa//2
                elif ko == "s":
                    c_s += wa//2
                else:
                    c_p += wa//2
            else:
                if ko == "r":
                    c_r += 1 + (wa-1)//2
                elif ko == "s":
                    c_s += 1 + (wa-1)//2
                else:
                    c_p += 1 + (wa-1)//2

    #print(result,c_r * p + c_s * r + c_p * s )
    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[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
for i in range(N):
    if i<K:
        if T[i]=="r":
            ans+=P
        elif T[i]=="s":
            ans+=R
        else:
            ans+=S
            
    else:
        if T[i]=="r":
            if T[i-K]!="r":
                ans+=P
            else:
                T[i]="-"
        elif T[i]=="s":
            if T[i-K]!="s":
                ans+=R
            else:
                T[i]="-"
        else:
            if T[i]=="p":
                if T[i-K]!="p":
                    ans+=S
                else:
                    T[i]="-"
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = ""
score = 0
for i,t in enumerate(T):
    if t == "r":
        if i >= K and "p" == ans[-K]:
            ans += " "
        else:
            ans += "p"
            score += P
    elif t == "s":
        if i >= K and "r" == ans[-K]:
            ans += " "
        else:
            ans += "r"
            score += R
    else:
        if i >= K and "s" == ans[-K]:
            ans += " "
        else:
            ans += "s"
            score += S
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
best_score = {"r": P, "s": R, "p": S}
ans = 0
# Initially, plus best scores till K.
# Because anything is OK till K count.
for i in range(K):
  ans += best_score[T[i]]
# After K count
for i in range(K, N):
  # Check i - K count.
  if T[i - K] == T[i]:
    # draw
    T[i] = ""
    continue
  ans += best_score[T[i]]
print(ans)

Submission status: Accepted

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

count = [0] * 3
flag = [False] * N
for i in range(N):
    if i >= K and T[i - K] == T[i]:
        if flag[i - K]:
            pass
        else:
            flag[i] = True
            continue
    if T[i] == 'r':
        count[0] += 1
    elif T[i] == 's':
        count[1] += 1
    else:
        count[2] += 1
#     print(i, count)
            

print(count[0] * P + count[1] * R + count[2] * S)

Submission status: Accepted

Solution code:
def d_prediction_and_restriction():
    import re
    N, K = [int(i) for i in input().split()]
    R, S, P = [int(i) for i in input().split()]
    T = input()

    can_win = {'r': 'p', 's': 'r', 'p': 's'}
    win_hand = [can_win[ch] for ch in T]
    points = {'r': R, 's': S, 'p': P}

    for i in range(K, N):
        if win_hand[i - K] == win_hand[i]:
            win_hand[i] = 'x'  # 制限に反するので手を適当に変える

    return sum(win_hand.count(ch) * points[ch] for ch in ('r', 's', 'p'))

print(d_prediction_and_restriction())

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

num,k = map(int,input().split(" "))
r,s,p = map(int,input().split(" "))
machine = list(input())

score = 0
score_list = []
for i in range(num):
    if i <= k - 1:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    elif machine[i] != machine[i - k]:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    elif score_list[i - k] == 0:
        if machine[i] == "r":
            score += p
            score_list.append(p)
        elif machine[i] == "s":
            score += r
            score_list.append(r)
        else:
            score += s
            score_list.append(s)
    else:
        score_list.append(0)
    
    

print(score)

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split())
    r,s,p = map(int,input().split())
    T = list(input()) #後で適宜書き換えるのでlist型
    score = {"r": p, "s":r, "p":s} #辞書型で, 手に対応する得点を予めinput
    ans = sum([score[T[i]] for i in range(k)]) #初めのk回は勝利
    for i in range(k,n):
        if T[i]==T[i-k]:
            T[i]="." #i回目の手を適切に選べば, i+k回目は勝てる.
            continue
        ans += score[T[i]]
    print(ans)
main()

Submission status: Accepted

Solution code:
import copy
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] = "x"
cr = T.count("r")
cp = T.count("p")
cs = T.count("s")
print(cp*s+cr*p+cs*r)

Submission status: Accepted

Solution code:
from collections import deque

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

que = deque([], maxlen=k)

score = 0
for i, t in enumerate(ttt):
  if i >= k and ttt[i-k] == t:
    t_ = que.popleft()
    if t_ is not None:
      que.append(None)
      continue
      
  if t == 'r':
    score += p
  elif t == 's':
    score += r
  else:
    score += s
  
  que.append(True)
else:
  print(score)

Submission status: Accepted

Solution code:
# N 回じゃんけんを行う,  K 回前のじゃんけんで出した手と同じ手を出すことはできない
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

# mod_N_list = [[] for i in range(K)]
# T_list = [[] for i in range(K)]
# T_list = []
# N_list = list(range(N))

ans = 0

for i in range(N):
    if T[i] == 'r':
        # 前回がrでは無かった時
        if i < K or T[i - K] != 'r':
            ans += P
        else:
            T = T[:i] + 'x' + T[i + 1:]

    elif T[i] == 's':
        # 前回がsでは無かった時
        if i < K or T[i - K] != 's':
            ans += R
        else:
            T = T[:i] + 'x' + T[i + 1:]

    elif T[i] == 'p':
        # 前回がpでは無かった時
        if i < K or T[i - K] != 'p':
            ans += S
        else:
            T = T[:i] + 'x' + T[i + 1:]

print(ans)

Submission status: Accepted

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

dict_sample = {
    "r":P,
    "s":R,
    "p":S
}
ans = 0

A = [[] for k in range(K)]
for i in range(N):
    A[i%K].append(T[i])
    ans += dict_sample[T[i]]
#print(ans)    
for k in range(K):
    cnt = 0
    tmp = A[k]
    for i in range(len(tmp)-1):
        if tmp[i]!=tmp[i+1]:
            if cnt > 0:
                cnt+=1
                ans -= (cnt // 2 ) * dict_sample[tmp[i]]
            cnt = 0
        else:
            cnt += 1

    if cnt>0:
        cnt+=1
        ans -= (cnt // 2 ) * dict_sample[tmp[-1]]
        
print(ans)

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

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

hands = [None]*N
ans = 0

for i in range(N):
    if T[i] == 'r':
        win = 'p'
    elif T[i] == 's':
        win = 'r'
    else:
        win = 's'
    
    if i < K:
        hands[i] = win
        ans += SCORE[win]
    else:
        if hands[i-K] == win:
            hands[i] = None
        else:
            hands[i] = win
            ans += SCORE[win]
            
            
print(ans)

Submission status: Accepted

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

mo = [[] for i in range(k)]

for i in range(n):
    num = i % k
    mo[num].append(T[i])
    
mo1 = copy.deepcopy(mo)
for i in range(k): 
    for j in range(len(mo[i])):
        if mo[i][j] == "r":
            mo1[i][j] = "p"
        elif mo[i][j] == "p":
            mo1[i][j] = "s"
        else:
            mo1[i][j] = "r"
        if j > 0 and mo1[i][j] == mo1[i][j-1]:
            mo1[i][j] = "x"

ans = 0
for i in range(k):
    for j in range(len(mo1[i])):
        if mo1[i][j] == "r":
            ans += r
        elif mo1[i][j] == "p":
            ans += p
        elif mo1[i][j] == "s":
            ans += s
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=str(input())
c=int(0)
def get(x,c):
  if x=="r":
    c+=P
  elif x=="s":
    c+=R
  else:
    c+=S
  return c
n=len(T)
for i in range(n):
  if i<=K-1:
    c=get(T[i],c)
  else:
    if T[i-K]!=T[i]:
      c=get(T[i],c)
    if T[i-K]!="o" and T[i-K]==T[i]:
      T=T[:i]+"o"+T[i+1:]
print(c)

Submission status: Accepted

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
r, s, p = [int(i) for i in sys.stdin.readline().split()]
t = input()
score_dic = {"r":p, "s":r,"p":s}
f = [1 for i in range(n)]
score = 0
for i, (_t, flg) in enumerate(zip(t, f)):
    if i >= k and t[i-k] == _t and flg:
        if i < n - k:
            f[i+k] = 0
    else:
        score += score_dic[_t]
print(score)

Submission status: Accepted

Solution code:
# D - Prediction and Restriction
def get_win_hand(com_hand):
    if com_hand == 'r':
        return 'p'
    elif com_hand == 's':
        return 'r'
    elif com_hand == 'p':
        return 's'

def get_score(my_hand, R, S, P):
    if my_hand == 'r':
        return R
    elif my_hand == 's':
        return S
    elif my_hand == 'p':
        return P
        
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())

score_list = [0] * K
for i in range(K):
    tmp_score = 0
    before_my_hand = '*'
    partialT = T[i::K]
    for com_hand in partialT:
        my_hand = get_win_hand(com_hand)
        if my_hand == before_my_hand:
            my_hand = '*'
        else:
            tmp_score += get_score(my_hand, R, S, P)
        before_my_hand = my_hand
    score_list[i] = tmp_score
    
print(sum(score_list))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

rsp={"r":0, "s":1 , "p":2}
n,k=map(int,input().split())
rsp_points=list(map(int,input().split()))
T=input()
for i in range(k):
    dp=[[0]*3 for i in range(2)]
    odev=0
    for ti in range(i,n,k):
        now = (rsp[T[ti]]+2)%3
        rspp=rsp_points[now]
        dp[(odev+1)%2][now] = max(rspp+dp[odev][(now+1)%3],rspp+dp[odev][(now+2)%3])
        dp[(odev+1)%2][(now+1)%3] = max(dp[odev][now],dp[odev][(now+2)%3])
        dp[(odev+1)%2][(now+2)%3] = max(dp[odev][(now+1)%3],dp[odev][now])
        # print(dp[(odev+1)%2])
        odev=(odev+1)%2
    ans+=max(dp[odev])
    # print(*dp,sep="\n")
    # print()
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()
ans=0
for i in range(k):
	bef=None
	for c in t[i::k]:
		if c==bef:
			bef = None
			pass
		else:
			ans+=d[c]
			bef=c
print(ans)

Submission status: Accepted

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

def didWin(idx, c):
    if c == 'r' and t[idx] == 's':
        return True
    if c == 's' and t[idx] == 'p':
        return True
    if c == 'p' and t[idx] == 'r':
        return True
    return False

def solve(mod):
    l = math.ceil((n - mod) / k)
    dp = [[0] * 4 for _ in range(l + 1)]

    for i in range(l):
        # Case rock
        if didWin(i * k + mod, 'r'):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        # Case scissors
        if didWin(i * k + mod, 's'):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        # Case paper
        if didWin(i * k + mod, 'p'):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

Submission status: Accepted

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

def didWin(idx, c):
    if c == 'r' and t[idx] == 's':
        return True
    if c == 's' and t[idx] == 'p':
        return True
    if c == 'p' and t[idx] == 'r':
        return True
    return False

def solve(mod):
    # print('mod:',mod)
    # tt = []
    #
    # for i in range(mod, n, k):
    #     tt.append(t[i])
    # print('tt:', tt)
    # print('n/k:', n / k)

    # tt[i] = t[i * k + mod]
    l = math.ceil((n - mod) / k)
    # print('l:', l)
    dp = [[0] * 4 for _ in range(l + 1)]

    for i in range(l):
        # Case rock
        if didWin(i * k + mod, 'r'):
            dp[i + 1][0] = max(dp[i][1], dp[i][2]) + r
        else:
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
        # Case scissors
        if didWin(i * k + mod, 's'):
            dp[i + 1][1] = max(dp[i][0], dp[i][2]) + s
        else:
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
        # Case paper
        if didWin(i * k + mod, 'p'):
            dp[i + 1][2] = max(dp[i][0], dp[i][1]) + p
        else:
            dp[i + 1][2] = max(dp[i][0], dp[i][1])

    # print('dp:', dp)
    # print('max:', max(dp[l][0], dp[l][1], dp[l][2]))
    return max(dp[l][0], dp[l][1], dp[l][2])

ans = 0
for i in range(k):
    ans += solve(i)

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N,K = II()
R,S,P = II()
T = str(input())

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

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

ans = 0
for i in range(K):
    for j,c in enumerate(d[i]):
        if j==0:
            ans += calc(c)
            before = c
        else:
            if c!=before:
                ans += calc(c)
                before = c
            else:
                before = 'z'

print(ans)

Submission status: Accepted

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

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

ans = 0
for x in range(K):
    Tx = list(T[x::K])

    tmp = 0
    for i in range(len(Tx)):
        if i == 0:
            tmp += win[Tx[i]]
            continue
        if Tx[i] != Tx[i - 1]:
            tmp += win[Tx[i]]
        else:
            Tx[i] = "?"

    ans += tmp

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
d = dict()
for i in range(n):
    if t[i] == "r" and d.get(i) != "r":
        ans += p
        d[i+k] = "r"
    elif t[i] == "s" and d.get(i) != "s":
        ans += r
        d[i+k] = "s"
    elif t[i] == "p" and d.get(i) != "p":
        ans += s
        d[i+k] = "p"

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t = input()
te = {"r":"p", "s":"r", "p":"s"}
win_hand = [te[t[i]] for i in range(len(t))]
ans = "".join(win_hand)
for i in range(k, len(t)):
    if ans[i] == ans[i-k]:
        ans = ans[:i] + "x" + ans[i+1:]

cnt = ans.count('r')*r + ans.count('s')*s + ans.count('p')*p
print(cnt)

Submission status: Accepted

Solution code:
import math
import bisect
import collections
import itertools
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True

N,K = imn()
RSP = iln()
T = input()

Point = {}
Point['r'] = RSP[2]
Point['s'] = RSP[0]
Point['p'] = RSP[1]

ans = 0
for i in range(len(T)):
    prev = i - K
    if prev >= 0 and T[prev] == T[i]:
        T = T[:i] + 'x' + T[i+1:]
        continue
    ans += Point[T[i]]
print(ans)

Submission status: Accepted

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

# 勝てる手の組み合わせを持つ辞書型を生成する
win_hand = {"r": "p", "s": "r", "p": "s"}
ANS = []

# 勝てる手の配列を作成する
for i in range(N):
    ANS.append(win_hand[T[i]])

# 配列を１つの文字列にする
ans = "".join(ANS)

# 制約のある手以降を探索
for i in range(K, N):
    # K回前の手と同じなら出した手をxに置換する
    if ans[i] == ans[i-K]:
        ans = ans[:i] + "x" + ans[i+1:]

# 各手の数と得られる点数を求める
point = ans.count("r")*R + ans.count("s")*S + ans.count("p")*P

print(point)

Submission status: Accepted

Solution code:
def A():
    temp = input().split()
    print(''.join(temp[::-1]))

def B():
    temp = [int(n) for n in input().split()]
    A = temp[0]
    B = temp[1]
    K = temp[2]
    to_minus = min(A, K)
    A -= to_minus
    K -= to_minus
    if K > 0:
        B -= min(B, K)
    print (A, B)

import math
def C():
    X = int(input())
    if X <= 2:
        print(2)
        return

    def isPrime(n):
        if n <= 3:
            return True
        if(n % 2 == 0 or n % 3 == 0): 
            return False
      
        for i in range(5,int(math.sqrt(n) + 1), 6):  
            if(n%i == 0 or n%(i + 2) == 0): 
                return False
        
        return True
    
    while True:
        if isPrime(X):
            print(X)
            return
        X += 1

def D():
    temp = [int(n) for n in input().split()]
    N = temp[0]
    K = temp[1]

    temp = [int(n) for n in input().split()]
    point_dict = {'r': temp[0], 's': temp[1], 'p': temp[2]}

    T = input()

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

    ans = 0
    last_choice = ['' for i in range(K)]
    for i, c in enumerate(T):
        if i<K or last_choice[i%K] != winning_rule[c]:
            last_choice[i%K] = winning_rule[c]
            ans += point_dict[winning_rule[c]]
        else:
            last_choice[i%K] = None
    print(ans)

D()

Submission status: Accepted

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

dic = {"r":"p" , "s":"r" , "p":"s"}
ANS = []

for i in range(N):
    ANS.append(dic[T[i]])
    
ans = "".join(ANS)

for i in range(K , N):
    if ans[i] == ans[i-K]:
        ans = ans[:i] + "X" + ans[i+1:]
        
point = ans.count("r")*R + ans.count("s")*S + ans.count("p")*P

print(point)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
d = {"r":r,"s":s,"p":p}
d_ = {"r":"p","s":"r","p":"s"}
t = [d_[i] for i in t]
l = [[] for _ in range(k)]
for i,j in enumerate(t):
    l[i%k].append(j)

def solve(l):
    score = 0
    k_ = {}
    for i,j in enumerate(l):
        if i == 0:
            score += d[j]
            k_[j] = 1
        elif l[i-1] != j:
            score += d[j]
            k_ = {}
            k_[j] = 1
        else:
            if k_[j] == -1:
                score += d[j]
            k_[j] *= -1
    return score
score = 0
for i in l:
    score += solve(i)

print(score)

Submission status: Accepted

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

h = [''] * n
c = {'r': 'p', 's': 'r', 'p': 's'}
points = 0

for i, ti in enumerate(t):
    ci = c[ti]
    if i < k or h[i - k] != c[ti]:
        h[i] = ci
        points += eval(ci)
    else:
        # p1, p2 = eval(ci), eval(h[i - k])
        # if p1 > p2:
        #     h[i] = ci
        #     points -= p2
        #     points += p1
        # else:
        h[i] = '*'


print(points)

Submission status: Accepted

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

h = [''] * n
c = {'r': 'p', 's': 'r', 'p': 's'}
points = 0

for i, ti in enumerate(t):
    ci = c[ti]
    if i < k or h[i - k] != c[ti]:
        h[i] = ci
        points += eval(ci)
    else:
        p1, p2 = eval(ci), eval(h[i - k])
        if p1 > p2:
            h[i] = ci
            points -= p2
            points += p1
        else:
            h[i] = '*'


print(points)