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())
R2, S, P = map(int, input().split())
T = input()

R = []

for i in range(K):
    if T[i]=='r':
        R.append('p')
    elif T[i]=='s':
        R.append('r')
    else:
        R.append('s')

for i in range(K, len(T)):
    if T[i]=='r':
        if R[i-K] == 'p':
            R.append('x')
        else:
            R.append('p')
    elif T[i]=='s':
        if R[i-K] == 'r':
            R.append('x')
        else:
            R.append('r')
    else:
        if R[i-K] == 's':
            R.append('x')
        else:
            R.append('s')

ans = 0
for r in R:
    if r=='p': ans += P
    elif r=='r': ans += R2
    elif r=='s': ans += S

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split( ))
r,s,p = map(int, input().split( ))
t = input()
x = []
ans = 0
hand = {"r":"p", "s":"r", "p":"s"}
point = {"r":r,"s":s,"p":p}
for i in range(n):
    h = hand[t[i]]
    if i<k:
        x.append(h)
        ans += point[h]
    else:
        if h !=x[i-k]:
            x.append(h)
            ans += point[h]
        else:
            x.append("x")
print(ans)

Submission status: Accepted

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

ans = 0
flag = [True]*k

for i in range(1,n+1):
    if i-k <= 0:
        if t[i-1] == 'r':
            ans += p
        elif t[i-1] == 's':
            ans += r
        else:
            ans += s
    else:
        if t[i-1] == t[i-k-1]:
            if flag[(i-1)%k]:
                flag[(i-1)%k] = False
            else:
                flag[(i-1)%k] = True
                if t[i-1] == 'r':
                    ans += p
                elif t[i-1] == 's':
                    ans += r
                else:
                    ans += s
        else:
            flag[(i-1)%k] = True
            if t[i-1] == 'r':
                ans += p
            elif t[i-1] == 's':
                ans += r
            else:
                ans += s

print(ans)

Submission status: Accepted

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

# 毎回勝つか勝たないかを選べる
# 勝つとK回後の手が制限される, 勝たないとK回後は好きな手が出せる(あいこ・負けはどちらでもよいので)
# 今勝ってもK回後勝っても勝利点は同じなので貪欲でよい

ans = 0
mem = ['x']*N  # 勝ったかどうかを覚えておく 'x'は勝ってない
for i,t in enumerate(T):
  if i < K:
    ans += score_dict[t]
    mem[i] = t
  elif t != mem[i-K]:
    ans += score_dict[t]
    mem[i] = t

print(ans)

Submission status: Accepted

Solution code:
def win_score(hand):
  global R,S,P,my
  if hand=="r":
    my+="P"
    return P
  elif hand=="s":
    my+="R"
    return R
  elif hand=="p":
    my+="S"
    return S
  return 0

nk=input().split(" ")
N,K=int(nk[0]),int(nk[1])
nk=input().split(" ")
R,S,P=int(nk[0]),int(nk[1]),int(nk[2])
T=input()
my=""

score=0
for i in range(N):
  if i<K:
    score+=win_score(T[i])
  elif T[i]!=T[i-K] or my[i-K]=="-":
    score+=win_score(T[i])
  else:
    my+="-"

#print(my)
print(score)

Submission status: Accepted

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

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

T = input()
ans = 0
for i in range(K):
    j = i
    count = 0
    DP = [[0]*3 for i in range((N-i-1)//K + 2)]
    
    while j<N:
        if T[j]=="r":
            DP[count+1][0] = max(DP[count][1],DP[count][2])
            DP[count+1][1] = max(DP[count][0],DP[count][2])
            DP[count+1][2] = max(DP[count][0],DP[count][1])+P
        elif T[j]=="s":
            DP[count+1][0] = max(DP[count][1],DP[count][2])+R
            DP[count+1][1] = max(DP[count][0],DP[count][2])
            DP[count+1][2] = max(DP[count][0],DP[count][1])
        elif T[j]=="p":
            DP[count+1][0] = max(DP[count][1],DP[count][2])
            DP[count+1][1] = max(DP[count][0],DP[count][2])+S
            DP[count+1][2] = max(DP[count][0],DP[count][1])
        j+=K
        count+=1
    ans += max(DP[count][0],DP[count][1],DP[count][2])

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
dic = {'r': P, 's': R, 'p': S}
tmp = [''] * (N + 1)
for i in range(K):
    ans += dic[T[i]]
    tmp[i] = T[i]

for i in range(K, N):
    if T[i] != tmp[i - K]:
        ans += dic[T[i]]
        tmp[i] = T[i]
    else:
        tmp[i] = 't'
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
c=0
for i in range(k):
    q,d=t[i],1
    if i+k<n:
        for j in range(i+k,n,k):
            if q==t[j]:
                d+=1
            else:
                x=(d-1)//2+1
                if q=="r":
                    c+=x*p
                elif q=="s":
                    c+=x*r
                else:
                    c+=x*s
                q=t[j]
                d=1
    x=(d-1)//2+1
    if q=="r":
        c+=x*p
    elif q=="s":
        c+=x*r
    else:
        c+=x*s
print(c)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
score=0
free=[0]*n
for i in range(n):
    if i>=k:
        if (t[i]==t[i-k])&(free[i-k]==0):
            free[i]=1
        else:
            if t[i]=="r":
                score+=p
            elif t[i]=="s":
                score+=r
            elif t[i] == "p":
                score += s
    else:
        if t[i] == "r":
            score += p
        elif t[i] == "s":
            score += r
        elif t[i] == "p":
            score += s
print(score)

Submission status: Accepted

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

A=[0]*N

for i in range(N):
    if T[i]==T[i-K] and A[i-K]!=0:
        A[i]=0
    else:
        if T[i]=="r":
            A[i]=P
        elif T[i]=="s":
            A[i]=R
        else:
            A[i]=S

print(sum(A))

Submission status: Accepted

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

def score(c) :
  if c == 'r' : return P
  if c == 's' : return R
  if c == 'p' : return S

ret = 0
for fr in range(K) :
  U = list(T[fr::K])
  s = 0
  s += score(U[0])
  for i in range(1, len(U)) :
    if U[i] == U[i-1] :
      U[i] = '#'
    else :
      s += score(U[i])
  ret += s
  
print(ret)

Submission status: Accepted

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

ans = 0
hand = []
for i in range(K):
    if T[i] == "r":
        ans += P
        hand.append("p")
    elif T[i] == "s":
        ans += R
        hand.append("r")
    else:
        ans += S
        hand.append("s")

for i in range(K, N):
    if T[i] == "r" and hand[i-K] != "p":
        ans += P
        hand.append("p")
    elif T[i] == "s" and hand[i-K] != "r":
        ans += R
        hand.append("r")
    elif T[i] == "p" and hand[i-K] != "s":
        ans += S
        hand.append("s")
    else:
        hand.append("joker")
        
        
print(ans)

Submission status: Accepted

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

flag = [[0] * 3 for _ in range(N)]
score = 0

for i in range(K):
    t = T[i]
    if t == 'r':
        flag[i][0] = 1
        score += P
    elif t == 's':
        flag[i][1] = 1
        score += R
    elif t == 'p':
        flag[i][2] = 1
        score += S


for i in range(K,N):
    t = T[i]
    if t == 'r':
        if flag[i-K][0] != 1:
            flag[i][0] = 1
            score += P
    if t == 's':
        if flag[i-K][1] != 1:
            flag[i][1] = 1
            score += R
    if t == 'p':
        if flag[i-K][2] != 1:
            flag[i][2] = 1
            score += S

print(score)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict as dd

N, K = map(int,input().split())
R, S, P = map(int,input().split())
rsp = [R,S,P]
T = input()
res = 0
tb = dd(list)
for i in range(N):
    tb[i%K].append(T[i])
for key in tb.keys():
    dic = tb[key]
    lg = len(dic)+1
    dp = [[0,0,0] for _ in range(lg)]
    for i in range(1,lg):
        for j in range(3):
            rsp2 = [0,0,0]
            if dic[i-1] == 'r':
                rsp2[2] = P
            elif dic[i-1] == 's':
                rsp2[0] = R
            else:
                rsp2[1] = S
            dp[i][j] = max([dp[i-1][(j+1)%3]+rsp2[(j+1)%3],dp[i-1][(j+2)%3]+rsp2[(j+2)%3]])
    res += max(dp[lg-1])
print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

C = [0 for i in range(N)]
D = {"r": 0, "s": 0, "p": 0}

for i in range(N):
    if i <= K-1:
        C[i] = 1
        D[T[i]] += 1
    else:
        if T[i] == T[i-K] and C[i-K] == 1:
            continue
        else:
            C[i] = 1
            D[T[i]] += 1

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
#print(T)
L=[[]for i in range(K)]
for i in range(N):
    L[i%K].append(T[i])
#print(L)
ans=0
for i in range(K):
    dp=[[0,0,0]for a in range(len(L[i])+1)]
    for x in range(1,len(dp)):
        dp[x][0]=max(dp[x-1][1],dp[x-1][2])
        dp[x][1]=max(dp[x-1][0],dp[x-1][2])
        dp[x][2]=max(dp[x-1][0],dp[x-1][1])
        if L[i][x-1]=="r":
            dp[x][2]+=P
        elif L[i][x-1]=="s":
            dp[x][0]+=R
        else:
            dp[x][1]+=S
    #print(dp)
    ans+=max(dp[-1])
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    N, K = MI()
    R, S, P = MI()
    T = input()
    # グー
    r = 'r'
    # ちょき
    s = 's'
    # パー
    p = 'p'
    m = {
        'r': P,
        's': R,
        'p': S
    }
    ms = {
        'r': 'p',
        's': 'r',
        'p': 's'
    }
    ans = 0
    records = []

    for i in range(len(T)):
        if len(records) < K:
            ans += m[T[i]]
            records.append(ms[T[i]])
        else:
            if records[i - K] == ms[T[i]]:
                records.append('empty')
            else:
                ans += m[T[i]]
                records.append(ms[T[i]])

    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()
ans = 0
ls = []
for i in range(n):
    if t[i] == "r":
        if len(ls) >= k:
            if ls[-k] != "r":
                ans += p
                ls.append("r")
            else:
                ls.append("x")
        else:
            ans += p
            ls.append("r")
    elif t[i] == "s":
        if len(ls) >= k:
            if ls[-k] != "s":
                ans += r
                ls.append("s")
            else:
                ls.append("x")
        else:
            ans += r
            ls.append("s")
    else:
        if len(ls) >= k:
            if ls[-k] != "p":
                ans += s
                ls.append("p")
            else:
                ls.append("x")
        else:
            ans += s
            ls.append("p")
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
R,S,P=map(int,input().split())
dic={"s":R,"p":S,"r":P}
t=input()
ans=0
l=[t[i::k] for i in range(k)]
for j in l:
  V=[]
  unit=0
  for i in j:
    if len(V)==0 or V[-1][0]!=i:
      V.append([i,1])
    else:
      V[-1][1] += 1
  for i in V:
    ans+=dic[i[0]]*((i[1]+1)//2)
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
S, P, R = MAP()
t = input()

lis = [[] for i in range(k)]
for i in range(n):
    lis[i%k].append(t[i])

res = 0
for i in range(k):
    for j in range(len(lis[i])):
        if j != 0 and lis[i][j] == lis[i][j-1]:
            lis[i][j] = 'n'
        if lis[i][j] == 'r':
            res += R
        if lis[i][j] == 's':
            res += S
        if lis[i][j] == 'p':
            res += P

print(res)

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]==T[i-K]:
            T[i]='0'
            continue
        elif T[i]=='r':
            ans+=P
        elif T[i]=='s':
            ans+=R
        else:
            ans+=S
print(ans)

Submission status: Accepted

Solution code:
def main():

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

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

    ans = 0
    prev = ["" for _ in range(K)]
    for i in range(N):
        c = T[i]
        if opp[c] == prev[i%K]:
            prev[i%K] = ""
        else:
            ans += d[opp[c]]
            prev[i % K] = opp[c]
        # print(prev)

    return ans

if __name__ == '__main__':
    print(main())

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}

prev = [None for _ in range(k)]
flag = [False for _ in range(k)]    # if consective

ind = 0
ans = 0
for char in t:
    if char == prev[ind]:
        if flag[ind]:
            flag[ind] = False
        else:
            ans += d[char]
            flag[ind] = True
    else:
        ans += d[char]
        flag[ind] = True

    prev[ind] = char
    ind = (ind + 1) % k


print(ans)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
N,M = m()
a,b,c = m()
s = input()
kkk = [0 for i in range(N)]
co = 0
for i in range(N):
    if i < M:
        if s[i] == "s":
            co += a
        elif s[i] == "p":
            co += b
        else:
            co += c
        kkk[i] = 1
    else:
        if s[i] != s[i-M] or (kkk[i-M] == 0 and s[i] == s[i-M]):
            if s[i] == "s":
                co += a
                kkk[i] = 1
            elif s[i] == "p":
                co += b
                kkk[i] = 1
            else:
                co += c
                kkk[i] = 1

print(co)

Submission status: Accepted

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

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

hero_rsp_list = [''] * K
ans = 0

for i in range(N):
    opponent_rsp = T[i]
    hero_last_rsp = hero_rsp_list[i % K]
    if opponent_rsp == 'r':
        if hero_last_rsp != 'p':
            hero_rsp_list[i % K] = 'p'
            ans += P
        else:
            hero_rsp_list[i % K] = ''
    elif opponent_rsp == 's':
        if hero_last_rsp != 'r':
            hero_rsp_list[i % K] = 'r'
            ans += R
        else:
            hero_rsp_list[i % K] = ''
    elif opponent_rsp == 'p':
        if hero_last_rsp != 's':
            hero_rsp_list[i % K] = 's'
            ans += S
        else:
            hero_rsp_list[i % K] = ''

print(ans)

Submission status: Accepted

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

C = [''] * N
ans = 0
for i, t in enumerate(T):
    if t == 'r':
        c = 'p'
        win = P

    if t == 's':
        c = 'r'
        win = R

    if t == 'p':
        c = 's'
        win = S

    if (i - K >= 0) and (C[i - K] == c):
        win = 0

    if win != 0:
        C[i] = c
    ans += win

print(ans)

Submission status: Accepted

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

def func(string):
    if string == "r":
        return P
    if string == "s":
        return R
    if string == "p":
        return S

ans = 0
for k in range(K):
    ans += func(T[k])

boolean_lis = [True] * N
#boolean_lis[0] = 

for n in range(K, N):
    if T[n - K] != T[n]:
        ans += func(T[n])
    elif T[n - K] == T[n]:
        if not boolean_lis[n - K]:
            ans += func(T[n])
        else:
            boolean_lis[n] = False
    #print(ans)
print(ans)
#print(*boolean_lis)

Submission status: Accepted

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

lstk = [0]*n
ans = 0
def f(index):
    if lstk[index%k] == lst1[index]:
        lstk[index%k] = "free"
        return False
    else:
        lstk[index%k] = lst1[index]
        return True

"""
def score(index):
    global lst1
    global ans
    if lst1(index) == "r":
        ans += p
    elif lst1(index) == "s":
        ans += r
    else:
        ans += s
"""
for i in range(n):
    if i < k:
        lstk[i] = lst1[i]
        if lst1[i] == "r":
            ans += p
        elif lst1[i] == "s":
            ans += r
        else:
            ans += s
    else:
        if f(i): #True=okとする
            if lst1[i] == "r":
                ans += p
            elif lst1[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()
res = 0
f = [True for _ in range(n)]
for i in range(n):
    m = t[i]
    if i >= k:
        if m == t[i - k]:
            if f[i - k]:
                f[i] = False
                continue
    if m == "r":
        res += p
    elif m == "s":
        res += r
    else:
        res += s
print(res)

Submission status: Accepted

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

score = 0
mine = ''
for i, t_ in enumerate(t):
    if t_ == 'r':
        if i >= k:
            if mine[i-k] != 'p':
                score += p
                mine += 'p'
            else:
                mine += '*'
        else:
            score += p
            mine += 'p'
    elif t_ == 's':
        if i >= k:
            if mine[i-k] != 'r':
                score += r
                mine += 'r'
            else:
                mine += '*'
        else:
            score += r
            mine += 'r'
    else:
        if i >= k:
            if mine[i-k] != 's':
                score += s
                mine += 's'
            else:
                mine += '*'
        else:
            score += s
            mine += 's'

print(score)

Submission status: Accepted

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

a = n // k
b = n % k

ans = 0

for i in range(k):
    dp = [[0, 0, 0] for i in range(a+2)]
    tj = 0
    for j in range(a+1):
        l = i + k * j
        if l >= n:
            break
        tj = j
        c = t[l]
        if c == 'r':
            dp[j+1][0] = max(dp[j][1], dp[j][2])
            dp[j+1][1] = max(dp[j][0], dp[j][2])
            dp[j+1][2] = max(dp[j][0]+p, dp[j][1]+p)
        if c == 'p':
            dp[j+1][0] = max(dp[j][1], dp[j][2])
            dp[j+1][1] = max(dp[j][0]+s, dp[j][2]+s)
            dp[j+1][2] = max(dp[j][0], dp[j][1])
        if c == 's':
            dp[j+1][0] = max(dp[j][1]+r, dp[j][2]+r)
            dp[j+1][1] = max(dp[j][0], dp[j][2])
            dp[j+1][2] = max(dp[j][0], dp[j][1])
    ans += max(dp[tj+1])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def solve1(n, r, s, p, t):
    cur = {'r': 0, 's': 0, 'p': 0}
    for c in t:
        prv = cur
        cur = {'r': 0, 's': 0, 'p': 0}
        for x in 'rsp':
            if x == 'r' and c == 's':
                score = r
            elif x == 's' and c == 'p':
                score = s
            elif x == 'p' and c == 'r':
                score = p
            else:
                score = 0
            for y in 'rsp':
                if y != x:
                    cur[x] = max(cur[x], prv[y] + score)
    return max(cur.values())

def solve(n, k, r, s, p, t):
    u = [[] for _ in range(k)]
    for i, c in enumerate(t):
        u[i % k].append(c)
    return sum([solve1(n, r, s, p, u[j]) for j in range(k)])

def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    print(solve(n, k, r, s, p, t))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
flag = []
for i in range(N):
    if i < K:
        if T[i] == 'r':
            ans += P
            flag.append('p')
        elif T[i] == 's':
            ans += R
            flag.append('r')
        elif T[i] == 'p':
            ans += S
            flag.append('s')
    else:
        if T[i] == 'r' and flag[i-K] != 'p':
            ans += P
            flag.append('p')
        elif T[i] == 's'  and flag[i-K] != 'r':
            ans += R
            flag.append('r')
        elif T[i] == 'p'  and flag[i-K] != 's':
            ans += S
            flag.append('s')
        else:
            flag.append(0)
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().split()
    Ts = ["" for _ in range(K)]
    for i, c in enumerate("".join(T)):
        Ts[i % K] += c

    ans = 0
    for t in Ts:
        c = t[0]
        if c == "r":
            ans += P
        elif c == "s":
            ans += R
        else:
            ans += S
        for i in range(1, len(t)):
            if t[i] == c:
                c = ""
            else:
                c = t[i]
                if c == "r":
                    ans += P
                elif c == "s":
                    ans += R
                else:
                    ans += S
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
"""
dp = [[0 for i in range(3)] for j in range(N)]
for i in range(N):
	r,s,p = 0,0,0
	if i > 0:
		r,s,p = dp[i-1][0],dp[i-1][1],dp[i-1][2]
	m = max(r,s,p)
	if T[i] == "r":
		if i < K:
			dp[i][0] = m
			dp[i][1] = m
			dp[i][2] = m + P
		else:
			dp[i][0] = max(s,p)
			dp[i][1] = max(r,p)
			dp[i][2] = max(r,s) + P
	elif T[i] == "s":
		if i < K:
			dp[i][0] = m + R
			dp[i][1] = m
			dp[i][2] = m
		else:
			dp[i][0] = max(s,p) + R
			dp[i][1] = max(r,p)
			dp[i][2] = max(r,s)
	else:
		if i < K:
			dp[i][0] = m
			dp[i][1] = m + S
			dp[i][2] = m
		else:
			dp[i][0] = max(s,p)
			dp[i][1] = max(r,p) + S
			dp[i][2] = max(r,s)
print(max(dp[-1]))
"""
ans = 0
point = {
	"r": P,
	"s": R,
	"p": S
}
win = [False for i in range(N)]
for i in range(N):
	if i < K:
		win[i] = True
		ans += point[T[i]]
	else:
		if T[i] == T[i-K] and win[i-K]:
			continue
		win[i] = True
		ans += point[T[i]]
print(ans)

Submission status: Accepted

Solution code:
import sys

def point(s, S, P, R):
    if s == "s": return S
    elif s == "p": return P
    else: return R

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    S, P, R = map(int, input().split())
    T = input()

    total = 0
    Takahashi = [None] * N
    for i in range(K):
        total += point(T[i], S, P, R)
        Takahashi[i] = T[i]
    for i in range(K, N):
        if T[i] == Takahashi[i-K]:
            Takahashi[i] = "NG"
        else:
            Takahashi[i] = T[i]
            total += point(T[i], S, P, R)
    print(total)


    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


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


def main():
    # A, B, K = get_inputs(int)
    # if A >= K:
    #     print(A-K, B)
    # elif A + B >= K:
    #     print(0, B - (K-A))
    # else:
    #     print(0, 0)

    # N = get_input(int)
    # n = X
    # while True:
    #     if is_prime(n):
    #         print(n)
    #         return
    #     else:
    #         n += 1

    N, K = get_inputs(int)
    R, S, P = get_inputs(int)
    T = get_input()

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

    ret = ''
    score = 0
    for i in range(N):
        if i < K:
            choice = towin[T[i]]
            ret += choice
            score += points[choice]
        else:
            if ret[-K] != towin[T[i]]:
                choice = towin[T[i]]
                ret += choice
                score += points[choice]
            else:
                choices = set(['r', 's', 'p'])
                choices -= set([ret[-K],])
                if i + K < len(T):
                    choices -= set([towin[T[i+K]],])
                choice = list(choices)[0]
                ret += choice


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

win = []

for i in range(n):
   if i >= k:
      if t[i-k] == t[i]:
         if win[i-k] == 1:
            win.append(0)
         else:
            win.append(1)
      else:
         win.append(1)
   else:
      win.append(1)
   
ans = 0
for w,tt in zip(win, t):
   if w == 1:
      if tt == "r":
         ans += p
      if tt == "s":
         ans += r
      if tt == "p":
         ans += s
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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 start in range(k):
    prev = "nil"
    for e in t[start::k]:
        if e != prev:
            ans += point[e]
            prev = e
        else:
            prev = "nil"

print(ans)