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:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  r,s,p=LI()
  t=S()

  dp=[[0]*3 for _ in range(n+1)]
  for i in range(n):
    a=t[i]
    b=max(dp[i])
    for j in range(3):
      dp[i+1][j]=b
    if i+1<=k:
      if a=='r':
        dp[i+1][2]+=p
      elif a=='s':
        dp[i+1][0]+=r
      else:
        dp[i+1][1]+=s

    else:
      _a,_b,_c=dp[i+1-k]
      if a=='r':
        if not (_c>_a and _c>_b):
          dp[i+1][2]+=p
      elif a=='s':
        if not (_a>_b and _a>_c):
          dp[i+1][0]+=r
      else:
        if not (_b>_a and _b>_c):
          dp[i+1][1]+=s

  # print(dp)
  return max(dp[n])

# main()
print(main())

Submission status: Accepted

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

def getScore(t, R, S, P):
    if t == 'r':
        return P
    if t == 's':
        return R
    return S

latest = [{'string': '', 'count': 0} for _ in range(K)]

ret = 0
for idx, t in enumerate(T):
    latest_idx = idx % K
    if idx + 1 <= K or t != latest[latest_idx]['string']:
        ret += getScore(t, R, S, P)
        latest[latest_idx]['string'] = t
        latest[latest_idx]['count'] = 1
        continue

    if latest[latest_idx]['count'] % 2 == 0:
        ret += getScore(t, R, S, P)
    latest[latest_idx]['count'] += 1

print(ret)

Submission status: Accepted

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

tmap = {
    'r': 0,
    's': 1,
    'p': 2
    }

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

# 勝つ手
pair = {
    'r': 'p',
    's': 'r',
    'p': 's',
}

# i % K != j % k ならばじゃんけんの手は独立に決定できる
# なので、じゃんけんの回数 % Kでグループ分けする
# 同一グループ内では、連続して同じ手は出せない。貪欲にやる

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

total = 0
for k in range(K):
    ls = group[k]
    tmp = ''

    s = 0
    for i in range(len(ls)):
        t = ls[i]

        win = pair[t]        
        draw = t
        lose = pair[win]

        # 勝ちの手が出せるならそうする
        if tmp != win:
            s += score[win]
            tmp = win
            continue           
        
        # 勝ちの手が出せないなら、次で勝てるように手を出す
        if tmp == win:
            if i < len(ls)-1:
                next_win = pair[ls[i + 1]]
                if draw == next_win:                
                    tmp = lose
                else:
                    tmp = draw
            else:
                pass
    total += s

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
mod = ["" for _ in range(k)]
# mod k の値同士しか干渉しない
for i in range(1, n + 1):
    mod[i % k] += t[i - 1]

ans = 0

for modk in mod:
    my_hand = ""
    for your_hand in modk:
        if your_hand == 'r' and my_hand != 'p':
            my_hand = 'p'
            ans += p
        elif your_hand == 's' and my_hand != 'r':
            my_hand = 'r'
            ans += r
        elif your_hand == 'p' and my_hand != 's':
            my_hand = 's'
            ans += s
        else:
            my_hand = ""

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = input()
L = [[] for _ in range(K)]
for i in range(N):
    j = i % K
    L[j].append(T[i])
ans = 0
for i in range(K):
    for j in range(len(L[i])-1):
        if L[i][j] == L[i][j+1]:
            L[i][j+1] = 0
        if L[i][j] == 'r':
            L[i][j] = P
        if L[i][j] == 's':
            L[i][j] = R
        if L[i][j] == 'p':
            L[i][j] = S
    if L[i][-1] == 'r':
        L[i][-1] = P
    elif L[i][-1] == 's':
        L[i][-1] = R
    elif L[i][-1] == 'p':
        L[i][-1] = S
    ans += sum(L[i])
print(ans)

Submission status: Accepted

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

    score = 0
    for i in range(K, N):
        if T[i] == T[i-K]:
            T[i] = 'x'
    
    for t in T:
        if t == 'r':
            score += P
        elif t == 's':
            score += R
        elif t == 'p':
            score += S
    
    print(score)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

rec=[]
ans=0

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

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


for i in range(N):
    win_hand=win(T[i])[0]
    win_point=win(T[i])[1]
    same_hand=T[i]
    if 0<=i<=K-1:
        rec.append(win_hand)
        ans+=win_point
    else:
        if win_hand!=rec[i-K]:
            rec.append(win_hand)
            ans+=win_point
        else:
            if N-K-1<i<=N-1:
                rec.append(same_hand)#不要
            else:
                next_win_hand=win(T[i+K])[0]
                rec.append(gap(next_win_hand))

    #print(ans)

#print(rec)
print(ans)

Submission status: Accepted

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


for i in range(N-K):
    if T[i]==T[i+K]:
        T[i+K]="0"

def win(c):
    if c=="r":
        return p
    elif c=="s":
        return r 
    elif c=="p":
        return s 
    else:
        return 0
ans=0
for i in T:
    ans+=win(i)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
sc = 0
te = []
for i in range(k):
    if t[i] == 'r':
        te.append('p')
        sc += p
    elif t[i] == 's':
        te.append('r')
        sc += r
    elif t[i] == 'p':
        te.append('s')
        sc += s
for i in range(k, n-k):
    if t[i] == 'r':
        if te[i-k] != 'p':
            te.append('p')
            sc += p
        else:
            if t[i+k] == 'r':
                te.append('r')
            elif t[i+k] == 's':
                te.append('s')
            elif t[i+k] == 'p':
                te.append('r')
    elif t[i] == 's':
        if te[i-k] != 'r':
            te.append('r')
            sc += r
        else:
            if t[i+k] == 'r':
                te.append('s')
            elif t[i+k] == 's':
                te.append('s')
            elif t[i+k] == 'p':
                te.append('p')
    elif t[i] == 'p':
        if te[i-k] != 's':
            te.append('s')
            sc += s
        else:
            if t[i+k] == 'r':
                te.append('r')
            elif t[i+k] == 's':
                te.append('p')
            elif t[i+k] == 'p':
                te.append('r')
for i in range(n-k, n):
    if t[i] == 'r':
        if te[i-k] != 'p':
            sc += p
    elif t[i] == 's':
        if te[i-k] != 'r':
            sc += r
    elif t[i] == 'p':
        if te[i-k] != 's':
            sc += s
print(sc)

Submission status: Accepted

Solution code:
import math
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] = "0"

rr = t.count("s")
ss = t.count("p")
pp = t.count("r")
  
print(rr*r + ss*s + pp*p)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = input()
ans = [0, 0, 0]
check = t[:k]
for i in range(k, n):
    if t[i] == check[i - k]:
        check += '?'
    else:
        check += t[i]
for i in range(n):
    if check[i] != check[i - k] or i < k:
        if check[i] == 'r':
            ans[0] += 1
        elif check[i] == 's':
            ans[1] += 1
        elif check[i] == 'p':
            ans[2] += 1
print(ans[0] * p + ans[1] * r + ans[2] * s)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
r, s, p = map(int, readline().split())
t = input()
ans = [0, 0, 0]
check = t[:k]
for i in range(k, n):
    if t[i] == check[i - k]:
        check += 'a'
    else:
        check += t[i]
for i in range(k):
    if t[i] == 'r':
        ans[0] += 1
    elif t[i] == 's':
        ans[1] += 1
    else:
        ans[2] += 1
for i in range(k, n):
    if check[i] != check[i - k]:
        if check[i] == 'r':
            ans[0] += 1
        elif check[i] == 's':
            ans[1] += 1
        elif check[i] == 'p':
            ans[2] += 1
print(ans[0] * p + ans[1] * r + ans[2] * s)

Submission status: Accepted

Solution code:
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
from functools import reduce
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm_base(a, b):
    return (a * b) // gcd(a, b)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def main():
  N, K = map(int, readline().split())
  R, S, P = map(int, readline().split())
  T = read().rstrip().decode('utf-8')
  k_to_word = [[] for _ in range(K)]
  for i, x in enumerate(T):
    k = i% K
    k_to_word[k].append(x)
  
  def solve(A):
    r, s, p = 0, 0, 0
    for x in A:
      if x == 'r':
        #筐体がグーを出したときに、グーチョキパーそれぞれを出すと、どう点数が増加するか
        r, s, p = max(s,p), max(r,p), max(r,s)+P
      elif x == 's':
        r, s, p = max(s,p)+R, max(r,p), max(r,s)
      elif x == 'p':
        r, s, p = max(s,p), max(r,p)+S, max(r,s)
    return max(r,s,p)
  
  ans = sum(solve(A) for A in k_to_word)
  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()

# print(n, k, r, s, p)
# print(t)

# 貪欲法
point = 0
score = {'r': p, 's': r, 'p': s}
taka = [True] * k # i 番目で勝った

for i in range(k):
    point += score.get(t[i])
for i in range(k, n):
    if t[i] == t[i - k]: # k回前と同じ手は出せない
        if taka[i - k]: # k回前に勝っている = 今回は勝てない
            taka.append(False)
        else: # k回前に負けている = 今回勝てばいい
            point += score.get(t[i])
            taka.append(True)
    else:
        # 勝てる
        point += score.get(t[i])
        taka.append(True)

print(point)

Submission status: Accepted

Solution code:
N,K=map(int,input().split(' '))
R,S,P=map(int,input().split(' '))
h = {'r':P,'p':S,'s':R}
T=input()
hands=[1]*N
ans = 0
for i in range(N-K):
    if T[i] != T[i+K] and hands[i]:
        ans += h[T[i]]
    elif hands[i]:
        hands[i+K]=0
        ans +=h[T[i]]
for i in range(N-K,N):
    if hands[i]:
        ans += h[T[i]]
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    R, S, P= [int(item) for item in input().split()]
    T = input()
    memo = [1 for _ in range(K)]
    def point_chk(s):
        if s == 'r':
            return P
        elif s == 's':
            return R
        elif s == 'p':
            return S
        else:
            raise

    
    max_point = 0
    for i, s in enumerate(T):
        if i < K:
            max_point += point_chk(s)
            memo[i%K] = s
        else:
            if s == memo[i%K]:
                memo[i%K] = 1
            else:
                memo[i%K] = s
                max_point += point_chk(s)
        # print(i, s, memo, max_point)
    
    print(max_point)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
# D - Prediction and Restriction

n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(str(input()))
A = []
ans = 0

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

for i in range(len(A)):
    if i<k:
        ans += A[i]
        continue

    if T[i]==T[i-k]:
        T[i] = 'o'
    else:
        ans += A[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
cnt = 0
flag = True
flag_win = 0
def jannkenn(string):
    if string ==  'r':
        return p
    elif string == 'p':
        return s
    elif string == 's':
        return r
for i in range(k):
    last = i
    for j in range(n):
        if i+k*j < n:
            if  j == 0:
                cnt += jannkenn(t[i+k*j])
                #print(i,j)
                flag_win = 1
            elif t[last] != t[i+k*j]:
        #        print(i,j)
                cnt += jannkenn(t[i+k*j])
                flag_win = 1
                
            elif t[last] == t[i+k*j]:
                if flag_win == 0:
                    cnt += jannkenn(t[i+k*j])
                    flag_win = 1
                    
                else:
                    flag_win = 0
                
        else:
            break
        last = i+k*j
print(cnt)

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

for i in range(n-k):
    if t[k+i] == t[i]:
        t[k+i] = 0

print(t.count("r")*p + t.count("s")*r + t.count("p")*s)

Submission status: Accepted

Solution code:
n,k = list(map(int, input().split()))
r,s,p = list(map(int, input().split()))
t = input()
point = {'r':r,'s':s,'p':p}
ans = 0
d = [] #出した手の履歴
win = {'r':'p', 's':'r', 'p':'s'} #キーの手に勝つ手を返す
lose = {'r':'s', 's':'p', 'p':'r'} #キーの手に負ける手を返す
for i in range(k):
    d.append(win[t[i]])
    ans += point[win[t[i]]]
for i in range(k, n):
    if d[-k]!=win[t[i]]:
        d.append(win[t[i]])
        ans += point[win[t[i]]]
    else:
        if win[t[min(i+k, n-1)]]==t[i]:
            d.append('')
        else:
            d.append(lose[t[min(i+k, n-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(" "))
ten_janken_dic ={"R":R,"S":S,"P":P} #じゃんけんの役ごとの点を辞書
ten_janken_sort =sorted(ten_janken_dic.items(),key=lambda x: x[1])
T =input()

#合計点と高橋君が出す手を同時進行
sum_janken = 0 # 合計点
takahasi = [] # 高橋君が出す手,K回前の手も出せる＝すべて勝つとした場合
for i in T:
    if i =="r":
        sum_janken += P #6
        takahasi.append("P")
    elif i == "s":
        sum_janken += R #8
        takahasi.append("R")

    elif i == "p":
        sum_janken += S #7
        takahasi.append("S")
takahasi_mae =[]
from copy import deepcopy
takahasi_mae = deepcopy(takahasi)

# K回前の手は出せない        
for i,v in enumerate(takahasi[K:]):
    if v ==takahasi[(i+K)-K]:
        sum_janken -= ten_janken_dic[v]
        x = ["R","S","P"]
        if takahasi[(i+K)-K] in x:
            x.remove( takahasi[(i+K)-K] ) #K回前の手を消す
        if i+K+K < len(takahasi):    
            if takahasi[i+K+K] in x:
                x.remove( takahasi[i+K+K] ) #K回後の手を消す
        if len(x) ==1:
            takahasi[i+K] =x[0]
        elif x[0]==ten_janken_sort[2][0] or x[1] ==ten_janken_sort[2][0] : #高い点を選ぶ
            takahasi[i+K] =ten_janken_sort[2][0]
        else:
            takahasi[i+K] =ten_janken_sort[1][0] #2番目に高い点のキー（点順でソートしてある）
print(sum_janken)

Submission status: Accepted

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

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

score = 0
flag = [('0', 1)] * k
for i in range(n):
    if flag[i % k][0] != t[i]:
        score += point[t[i]]
        flag[i % k] = (t[i], -1)
    elif flag[i % k][0] == t[i] and flag[i % k][1] == 1:
        score += point[t[i]]
        flag[i % k] = (t[i], -1)
    else:
        flag[i % k] = (t[i], 1)

print(score)

Submission status: Accepted

Solution code:
import sys

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

win_choice = {'s':'r', 'p':'s', 'r':'p'}
janken_point = {'r':r, 's':s, 'p':p}

dp = []
# a: 相手の手, x:自分が出せない手
def point(a, x=None):
    if x == win_choice[a]: return 0
    else: return janken_point[win_choice[a]]

score = 0
flag = [('0', 1)] * k
for i in range(n):
    if flag[i % k][0] != t[i]:
        score += point(t[i])
        flag[i % k] = (t[i], -1)
    elif flag[i % k][0] == t[i] and flag[i % k][1] == 1:
        score += point(t[i])
        flag[i % k] = (t[i], -1)
    else:
        flag[i % k] = (t[i], 1)

print(score)

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
INF = 1 << 60
sys.setrecursionlimit(10 ** 6)

#ここから書き始める
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = ""
for i in t:
    if i == "r":
        a += "p"
    elif i == "s":
        a += "r"
    else:
        a += "s"
ans = 0
for i in a:
    if i == "r":
        ans += r
    elif i == "s":
        ans += s
    else:
        ans += p
# print(ans)
for i in range(k, n):
    if a[i] == a[i - k]:
        if a[i] == "r":
            ans -= r
        elif a[i] == "s":
            ans -= s
        else:
            ans -= p
        a = a[:i] + "a" + a[i + 1:]
print(ans)

Submission status: Accepted

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

from heapq import heapify, heappush, heappop, heappushpop, heapreplace

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

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    ans = 0
    for start in range(K):
        dp = [[0] * 3 for _ in range(2)]
        switch = 0
        for i in range(start, len(T), K):
            hand = T[i]
            prev = switch ^ 1
            dp[switch][0] = max(dp[prev][1], dp[prev][2])
            dp[switch][1] = max(dp[prev][0], dp[prev][2])
            dp[switch][2] = max(dp[prev][0], dp[prev][1])
            if hand == 's':
                dp[switch][0] += R
            elif hand == 'p':
                dp[switch][1] += S
            else:
                dp[switch][2] += P
            switch ^= 1
        ans += max(dp[switch^1])
    print(ans)
resolve()

Submission status: Accepted

Solution code:

def resolve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    ans = 0
    for start in range(K):
        dp = [[0] * 3 for _ in range(2)]
        switch = 0
        for i in range(start, len(T), K):
            hand = T[i]
            dp[1][0] = max(dp[0][1], dp[0][2])
            dp[1][1] = max(dp[0][0], dp[0][2])
            dp[1][2] = max(dp[0][0], dp[0][1])
            if hand == 's':
                dp[1][0] += R
            elif hand == 'p':
                dp[1][1] += S
            else:
                dp[1][2] += P
            dp[0][:] = dp[1]
        ans += max(dp[1])
    print(ans)
resolve()

Submission status: Accepted

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

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


def score(i, j):
    if j == 0:  # 'r'
        if t[i] == 's':
            return r
    elif j == 1:  # 's'
        if t[i] == 'p':
            return s
    else:  # 'p'
        if t[i] == 'r':
            return p
    return 0


for i in range(n):
    for j in range(3):
        if i >= k:
            dp[i][j] = max(dp[i-k][(j+1)%3], dp[i-k][(j+2)%3]) + score(i, j)
        else:
            dp[i][j] = score(i, j)

total = 0
for i in range(n-k, n):
    total += max(dp[i][0], dp[i][1], dp[i][2])

print(total)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
tl = list(input())
ansl = []

for i in range(n):
    if i - k < 0:
        ansl.append(1)
    else:
        if ansl[i-k] == 1 and tl[i] == tl[i-k]:
            ansl.append(0)
        else:
            ansl.append(1)

ans = 0
for t,a in zip(tl, ansl):
    if a == 1:
        if t == "r":
            ans += p
        if t == "s":
            ans += r
        if t == "p":
            ans += s

print(ans)

Submission status: Accepted

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

dp=[0]*n
a = {"r":p, "s":r, "p":s}

for i in range(n):
    if i>=k and t[i]==t[i-k]: # どうせ得点できないので、次に影響しないように'x'
        dp[i]=0
        t[i]='x'
    else:
        dp[i]=a[t[i]]   
print(sum(dp))

Submission status: Accepted

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

score=0
scorei=0
myans=[]
myansi=""
for i in range(n):
    if(t[i]=="r"):
        myansi="p"
        scorei=p
    elif(t[i]=="s"):
        myansi="r"
        scorei=r
    else:
        myansi="s"
        scorei=s

    if(i>=k and myansi==myans[i-k]):
        myansi="*"
        scorei=0

    myans.append(myansi)
    score+=scorei
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
su = 0
kinshi = ''
dashita = [''] * N
for i in range(len(T)):
  s = T[i]
  if i - K >= 0:
    if s == dashita[i - K]:    
      continue
  if s == 'r':
    su += P
  elif s == 's':
    su += R
  elif s == 'p':
    su += S
  dashita[i] = s
print(su)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
wl = [1 for i in range(k)] + [0 for j in range(n-k)]
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] != t[i-k]:
            wl[i] = 1
            if t[i] == 'r':
                ans += p
            elif t[i] == 's':
                ans += r
            else:
                ans += s
        elif wl[i-k]:
            continue
        else:
            wl[i] = 1
            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())
T = input()

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

list = ['' for i in range(K)]

for i in range(N):
    list[i%K] = list[i%K] + T[i]


score = 0
for i in range(K):
    hand = list[i]
#    print('hand = {}'.format(hand))
    score += d[hand[0]]
#    print(d[hand[0]])
    for j in range(1, len(hand)):
#        print(hand)
        if hand[j] != hand[j-1]:
            score += d[hand[j]]
#            print(d[hand[j]])
        else:
            hand = hand[:(j)] + '#' + hand[(j+1):]

print(score)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,k = map(int, input().split())
    s,p,r = map(int, input().split())
    t = input().rstrip()
    hasi = []

    res = 0
    for i in range(n):
        if t[i]=='r':
            a = r
            b = 'p'
        if t[i]=='s':
            a = s
            b = 'r'
        if t[i]=='p':
            a = p
            b = 's'
        if i<k:
            res += a
            hasi.append(b)
        else:
            if t[i]==t[i-k] and b in hasi[i-k]:
                hasi.append('x')
                continue
            else:
                res += a
                hasi.append(b)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

start_idx = [i for i in range(k)]
hands = [0]*k
score = 0

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

for i in start_idx:
    for j in range(i+k,n,k):
        if t[j] == 'r':
            if hands[i] == 'p':
                hands[i] = 'a'
            
            else:
                hands[i] = 'p'
                score += p
        
        if t[j] == 's':
            if hands[i] == 'r':
                hands[i] = 'a'
            
            else:
                hands[i] = 'r'
                score += r
        
        if t[j] == 'p':
            if hands[i] == 's':
                hands[i] = 'a'
            
            else:
                hands[i] = '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())
ans = 0

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

for i in range(n):
    c = t[i]
    if i < k:
        ans += get_score(c)
    else:
        if c == t[i-k]:
            t[i] = '0'
        else:
            ans += get_score(c)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
string=list(input())
res=[0]*n
a=['t']*len(string)
for i in range(len(string)):
    if i-k>=0:
        if string[i]=='r' and a[i-k]!='p':
            res[i]=p
            a[i]='p'
        elif string[i]=='s' and a[i-k]!='r':
            res[i]=r
            a[i]='r'
        elif string[i]=='p' and a[i-k]!='s':
            res[i]=s
            a[i]='s'
    else:
        if string[i]=='r':
            res[i]=p
            a[i]='p'
        elif string[i]=='s':
            res[i]=r
            a[i]='r'
        elif string[i]=='p':
            res[i]=s
            a[i]='s'
print(sum(res))

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = input()
wol = []
ans = 0
def win(p):
    wol.append(1)
    if T[p]=='r':
        return P
    elif T[p]=='s':
        return R
    else:
        return S

for i in range(N):
    if i<K:
        ans += win(i)
    else:
        if T[i-K]==T[i] and wol[i-K]==1:
            wol.append(0)
        else:
            ans += win(i)
print(ans)

Submission status: Accepted

Solution code:
def main():
	n,k = map(int, input().split())
	r,s,p = map(int,input().split())
	t = list(input())
	t_winer = [ '' for _ in range(n)]
	t_point = [ 0 for _ in range(n)]
	
	for i in range(n):
		if t[i] == 'r':
			t_winer[i] = 'p'
			t_point[i] = p
		elif t[i] == 's':
			t_winer[i] = 'r'
			t_point[i] = r
		else:
			t_winer[i] = 's'
			t_point[i] = s
	for i in range(k,n):
		if t_winer[i-k] == t_winer[i]:
			t_winer[i] = '-'
			t_point[i] = 0
	print(sum(t_point))

if __name__ == '__main__':
	main()

Submission status: Accepted

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

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