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 sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
r, s, p = na()
t = input()
ans = 0
for i in range(k):
    #print(t[0+i::k])
    li = t[0+i::k]
    li  = 'x' + li
    flg = False
    for j in range(1, len(li)):
        if li[j] != li[j-1] or flg:
            flg = False
            if li[j] == 'r':
                ans += p
            if li[j] == 's':
                ans += r
            if li[j] == 'p':
                ans += s
        else:
            flg = True
        #print(ans)
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
from math import ceil
*A, T = open(0).read().split()
n, k, r, s, p = map(int, A)
T = T.strip()

d = {'r':p, 's':r, 'p':s}
X = [T[i::k] for i in range(k)]
s = 0
for x in X:
    for key, rep in groupby(x):
        s += d[key] * ceil(len(list(rep)) / 2)
print(s)

Submission status: Accepted

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
R,S,P = [int(x) for x in stdin.readline().rstrip().split()]
T = (stdin.readline().rstrip())
sub = [""]*K
for ind,i in enumerate(T):
    sub[(ind+1)%K] += i

ans = 0

for j in sub:
    former = ""
    for s in j:
        if s == "r":
            p = P
        elif s == "s":
            p = R
        else:
            p = S
        
        if s != former:
            ans += p
            former = s
        else:
            former = ""
            
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
arr=[0]*N
d={'r':P,'s':R,'p':S}
for i,t in enumerate(T):
    if i-K>=0:
        if T[i-K]!=t or arr[i-K]==0:
            arr[i]=d[t]
    else:
        arr[i]=d[t]
print(sum(arr))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
arr=[0]*N
d={'r':P,'s':R,'p':S}
for i,t in enumerate(T):
    if i-K>=0:
        if T[i-K]!=t:
            arr[i]=d[t]
        elif arr[i-K]==0:
            arr[i]=d[t]
    else:
        arr[i]=d[t]
print(sum(arr))

Submission status: Accepted

Solution code:
import math
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
# p = list(map(int, input().split()))
score = 0
rev = [True] * k
for i in range(min(k,n)):
    if t[i] == 'r':
        score += p

    elif t[i] == 'p':
        score += s

    elif t[i] == 's':
        score += r

if n>k:

    for i in range(k,n):

        if t[i] == t[i-k] and rev[i%k] :
            rev[i % k] = False

        elif t[i] == t[i-k] and not rev[i%k] :
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

        elif t[i] != t[i-k]:
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = [i for i in input()]
ans = 0
for i in range(n):
    if i >= k and t[i] == t[i-k]:
        t[i] = "o"
        continue
    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 = [i for i in input()]
ans = 0
rsp = {"r": 0, "s": 0, "p": 0}
for i in range(n):
    if t[i] == "r":
        ans += p
    elif t[i] == "s":
        ans += r
    else:
        ans += s
    if i >= k and t[i] == t[i-k]:
        rsp[t[i]] += 1
        t[i] = "o"
mi = int(rsp["r"] * p + rsp["s"] * r + rsp["p"] * s)
print(ans-mi)

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(K, N):
    if T[i] == T[i - K]:
        T[i] = 'x'
for i in range(N):
    if T[i] == 'x':
        continue
    elif T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    else:
        ans += S
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
R,S,P = inpl()
a = input()
res = 0
for rem in range(k):
    dp = [[0]*3 for _ in range(len(range(rem,n,k))+1)]
    for i,j in enumerate(range(rem,n,k)):
        for x in range(3):
            pls = 0
            if x == 0 and a[j] == 's':
                pls += R
            if x == 1 and a[j] == 'p':
                pls += S
            if x == 2 and a[j] == 'r':
                pls += P
            for y in range(3):
                if x == y: continue
                dp[i+1][x] = max(dp[i][y]+pls, dp[i+1][x])
    # print(dp)
    res += max(dp[-1])
print(res)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
r=0
s=0
p=0
a=list(input())
A=[]
for i in range(K):
    if a[i]=="r": 
        A.append(P)
    elif a[i]=="s":
        A.append(R)
    else:
        A.append(S)
for i in range(K,N):
    if a[i]==a[i-K] and A[i-K]!=0:
        A.append(0)
    elif a[i]=="r": 
        A.append(P)
    elif a[i]=="s":
        A.append(R)
    else:
        A.append(S)
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

tb = {'r': p, 's': r, 'p': s}
ans = 0
for i in range(len(t)):
    if i < k:
        ans += tb[t[i]]
    else:
        if t[i - k] == t[i]:
            t[i] = '-'
        else:
            ans += tb[t[i]]
print(ans)

Submission status: Accepted

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

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

for i in range(K):
    hand = T[i]
    ans += data[hand]

    for j in range(i + K, N, K):
        if T[j] == hand:
            hand = "other"
        else:
            hand = T[j]
            ans += data[hand]

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
flag = 0
for i in range(K) :
    flag = T[i]
    ans += point[T[i]]
    while True :
        i += K
        if i >= N :
            break
        if T[i] != flag :
            ans += point[T[i]]
            flag = T[i]
        else :
            flag = 0

print(ans)

Submission status: Accepted

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

for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Submission status: Accepted

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

ans = 0
select = [False] * N
for i in range(N):
    if i >= K:
        if T[i-K] == T[i]:
            if select[i-K]:
                continue
    if T[i] == "r":
        ans += P
    elif T[i] == "s":
        ans += R
    else:
        ans += S
    select[i] = True
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
t = input()
dp = [[0]*n for _ in range(3)]
for i in range(k):
    if t[i] == 'r':
        dp[2][i] = a[2]
    if t[i] == 's':
        dp[0][i] = a[0]
    if t[i] == 'p':
        dp[1][i] = a[1]
for i in range(k,n):
    id = -1
    if t[i] == 'r':
        id = 2
    if t[i] == 's':
        id = 0
    if t[i] == 'p':
        id = 1
    for x in range(3):
        for y in range(3):
            if x == y:
                continue
            if x == id:
                dp[x][i] = max(dp[x][i], dp[y][i-k] + a[x])
            else:
                dp[x][i] = max(dp[x][i], dp[y][i-k])

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000)
def conv(r,s,p,j):
    if j == 'r':
        return p
    if j == 's':
        return r
    if j == 'p':
        return s
def listint():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a
def perse(t,k,num):
    stri = []
    for i in range(k):
        stri.append([])
    for i in range(num):
        stri[i%k].append(t[i])
    return stri
def main(q):
    i = 0
    ans = 0
    while i < len(q)-1:
        if q[i] == q[i+1]:
            ans = ans + conv(r,s,p,q[i])
            i = i+2
        else:
            ans = ans + conv(r,s,p,q[i])
            i = i+1
    if i == len(q)-1:
        ans = ans + conv(r,s,p,q[i])
    return ans
num,k = listint()
r,s,p = listint()
t = input()
afterperse = perse(t,k,num)
ans = 0
for i in range(k):
    ans = ans + main(afterperse[i])
print(ans)

Submission status: Accepted

Solution code:
NK = input().split()
N = int(NK[0])
K = int(NK[1])

RSP = input().split()
R = int(RSP[0])
S = int(RSP[1])
P = int(RSP[2])
T = input()

hands = {'r':'p', 's':'r', 'p':'s', 'e':'e'}
points = {'r':R, 's': S, 'p': P}
hist = {}
point = 0
for i in range(len(T)):
  if (i-K >= 0) and (hist[i-K] == hands[T[i]]):
    hist[i] = hands['e']
  else:
    hist[i] = hands[T[i]]
    point += points[hist[i]]
print(point)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
R,S,P = map(int,input().split())
T = str(input())
t=[T[i] for i in range(n)]

ans = 0
for i in range(k):
     
     if t[i] == 'r':
          ans  += P
     elif t[i] == 's':
          ans += R
     else:
          ans += S

for i in range(k,n):

     if t[i] == t[i-k]:
          t[i] = 'o'
          continue
     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())
T = list(input())
score = 0
for i in range(N):
    if(T[i]=="r"):
        score +=P
    elif(T[i]=="s"):
        score +=R
    elif(T[i]=="p"):
        score +=S
    if(i <= N-K-1):
        if(T[i]==T[i+K]):
            T[i+K] = "0"
print(score)

Submission status: Accepted

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

score = 0

for i in range(k):
    aa = t[i::k]
    before = ''
    for ja in aa:
        if ja==before:
            before = ''
            continue
        else:
            if ja=='s':
                score += R
            elif ja=='p':
                score += S
            else:
                score += P
            before = ja

print(score)

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

def win(s):
    if s == 'r':
        return 'p'
    elif s == 's':
        return 'r'
    elif s == 'p':
        return 's'
    else:
        return 'x'
        
def point(s):
    if s == 'r':
        return R
    elif s == 's':
        return S
    elif s == 'p':
        return P
    else:
        return 0

ans = 0
tmp = ''
for i in range(len(T)):
    if i < K:
        tmp += T[i]
    elif T[i] != tmp[i-K]:
        tmp += T[i]
    else:
        tmp += 'x'

for i in range(len(T)):
    ans += point(win(tmp[i]))


print(ans)

Submission status: Accepted

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

    for i in range(k):
        t2 = t[i::k]
        l = len(t2)
        dp = [0, 0, 0]
        for j in range(l):
            if t2[j] == "r":
                dp = [max(dp[1], dp[2]), max(dp[0], dp[2]),
                      max(dp[0]+p, dp[1]+p)]
            elif t2[j] == "s":
                dp = [max(dp[1]+r, dp[2]+r), max(dp[0], dp[2]),
                      max(dp[0], dp[1])]
            else:
                dp = [max(dp[1], dp[2]), max(dp[0]+s, dp[2]+s),
                      max(dp[0], dp[1])]
        ans += max(dp)
    print(ans)


main()

Submission status: Accepted

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

ans = 0
log = ["*"]*N
win = {"r":"p","s":"r","p":"s"}
score = {"r":P,"s":R,"p":S}
for i,t in enumerate(T):
    if log[i-K]==win[t]: continue
    log[i] = win[t]
    ans += score[t]
print(ans)

Submission status: Accepted

Solution code:
N, K =(int(i) for i in input().split())
R, S, P = (int(i) for i in input().split())
T = input()
ans = 0
d = {}
d["r"] = P
d["s"] = R
d["p"] = S
rens = [0] * N
for i in range(N):
    if i>=K and T[i-K] == T[i]:
        rens[i] = rens[i-K] + 1
    if rens[i] %2 == 0:
        ans += d[T[i]]
print(ans)

Submission status: Accepted

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

for i in range(k, n):
    if T[i] == T[i-k]:
        T[i] = 'x'

points = 0
for i in range(n):
    if T[i] == 'r':
        points += p
    elif T[i] == 's':
        points += r
    elif T[i] == 'p':
        points += s

print(points)

Submission status: Accepted

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

def rps(hero, villain):
    hero = hand.index(hero)
    villain = hand.index(villain)
    result = hero - villain
    if result % 3 == 0 or result % 3 == 1:
        return 0
    else:
        return 1

if_rock, if_scissors, if_paper = ([0]*k for _ in range(3))
for i in range(n):
        tmp_r = rps('r', T[i]) * r + max(if_scissors[i%k], if_paper[i%k])
        tmp_s = rps('s', T[i]) * s + max(if_rock[i%k], if_paper[i%k])
        tmp_p = rps('p', T[i]) * p + max(if_rock[i%k], if_scissors[i%k])
        if_rock[i%k] = tmp_r
        if_scissors[i%k] = tmp_s
        if_paper[i%k] = tmp_p

maxpoint = 0
for i in range(k):
    maxpoint += max(if_rock[i], if_scissors[i], if_paper[i])
print(maxpoint)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
dic = {'r':p, 's':r, 'p':s}
b = [1]*n
ans = 0
for i in range(n):
    if i > (k-1) and t[i]==t[i-k]:
        b[i] -= b[i-k]
    ans += dic[t[i]] if b[i]==1 else 0

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = str(input())
score = 0
lose = []
def janken(hand):
    if hand == 'r':
        return(p)
    if hand == 's':
        return(r)
    if hand == 'p':
        return(s)

for i in range(n):
    if i <= k - 1:
        score += janken(T[i])
    else:
        if T[i] == T[i - k]:
            if i - k not in lose:
                lose.append(i)
            else:
                score += janken(T[i])
        else:
            score += janken(T[i])
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r,s,p = map(int, input().split())
T = input()
P = []
# プレイヤーのだす手の制約を無しとしたときの最善手のスコアのリストを作成(K回前でも同じ手を出せたとするということ)
for i in range(n):
    if T[i] == 'r':
        P.append('p')
    elif T[i] == 's':
        P.append('r')
    elif T[i] == 'p':
        P.append('s')

# K回前と出す手がかぶるときはその出す手の後者のスコアを0とする
for i in range(n-k):
    if P[i] == P[i+k]:
        P[i+k] = 0

count = 0
for i in range(n):
    if P[i] == 'r':
        count += r
    elif P[i] == 's':
        count += s
    elif P[i] == 'p':
        count += p

print(count)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    pre = [''] * N
    ans = 0
    for i in range(N):
        if i - K < 0:
            if T[i] == 'r':
                ans += P
                pre[i] = 'p'
            if T[i] == 's':
                ans += R
                pre[i] = 'r'
            if T[i] == 'p':
                ans += S
                pre[i] = 's'
        else:
            if T[i] == 'r':
                if pre[i-K] == 'p':
                    continue
                ans += P
                pre[i] = 'p'
            if T[i] == 's':
                if pre[i-K] == 'r':
                    continue
                ans += R
                pre[i] = 'r'
            if T[i] == 'p':
                if pre[i-K] == 's':
                    continue
                ans += S
                pre[i] = 's'
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
n,k = map(int,input().split())
r,s,p = map(int,input().split())
q = deque(input())
score = 0
memory = [0]*k
for i in range(n):
    a = q.popleft()
    if a == 'r': 
        if memory[i % k] != 'r':
            score += p 
            memory[i % k] = 'r'
        else:
            memory[i % k] = 0
    if a == 's': 
        if memory[i % k] != 's':
            score += r 
            memory[i % k] = 's'
        else:
            memory[i % k] = 0    
    if a == 'p': 
        if memory[i % k] != 'p':
            score += s 
            memory[i % k] = 'p'
        else:
            memory[i % k] = 0
print(score)

Submission status: Accepted

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

def solve(t, r, s, p):
    ret = 0
    pre = ""
    for c in t:
        if c == "r":
            if pre != "p":
                ret += p
                pre = "p"
            else:
                pre = ""
        elif c == "p":
            if pre != "s":
                ret += s
                pre = "s"
            else:
                pre = ""
        else:
            if pre != "r":
                ret += r
                pre = "r"
            else:
                pre = ""
    return ret


def main():
    N, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input().strip()

    ans = 0
    for i in range(k):
        j = i
        tmp = []
        while j < N:
            tmp.append(t[j])
            j += k
        ans += solve(tmp, r, s, p)
    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()
u = [[] for i in range(k)]
R=0
S=0
P=0

for i in range(n):
  u[i%k]+=t[i]

for i in range(k):
  l=len(u[i])
  for j in range(1,l):
    if u[i][j]==u[i][j-1]:
      u[i][j]='o'
  
  R+=u[i].count('s')
  S+=u[i].count('p')
  P+=u[i].count('r')

print(r*R+s*S+p*P)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
point=0
double=0
def put(what):
    if what=="s":
        return R
    if what=="r":
        return P
    if what=="p":
        return S
#print(T)
for i in range(K):
    check=[]
    for j in range(N//K+1):
          if i+K*j<N:
              check+=T[i+K*j]
    #print(check)####
    point+=put(T[i])
    for j in range(len(check)-1):
        if check[j]==check[j+1]:
            check[j+1]="n"#double
        else:
            point+=put(check[j+1])
    #print(point)#####
print(point)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
dp=[[0 for i in range(3)] for _ in range(N)] #dp[i][0]はグー、dp[i][1]はチョキ、dp[i][2]はパーを出す
b=[]
cnt=0
#print(dp)
def janken(machine,hand):
    p=0
    if ((machine=="r")and(hand==2)):
        p=p+P
    elif((machine=="s")and(hand==0)):
        p=p+R
    elif((machine=="p")and(hand==1)):
        p=p+S
    return p
c=0
for i in range(N):
    machine=T[i]
    if i>=K:
        dp[i][0]=janken(machine,0)+max(dp[i-K][1],dp[i-K][2])
        dp[i][1]=janken(machine,1)+max(dp[i-K][0],dp[i-K][2])
        dp[i][2]=janken(machine,2)+max(dp[i-K][0],dp[i-K][1])
    else:
        for j in range(3):
            dp[i][j]=(janken(T[i],j))
for i in range(K):
    cnt=cnt+max(dp[len(dp)-i-1])
print(cnt)

Submission status: Accepted

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

commands = ['']*n
score = 0

for i,c in enumerate(t):
    if c=='r':
        command = 'p'
        point = p
    elif c=='p':
        command = 's'
        point = s
    elif c=='s':
        command = 'r'
        point = r
        
    if (i-k>=0) and (commands[i-k]==command):
        command = ''
        point = 0
    
    score += point
    commands[i] = command

print(score)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
d={}
d["r"]=p
d["s"]=r
d["p"]=s
t=input()
ans=0

l=[0]*n

for i in range(n):
    if i>=k:
        if t[i]==t[i-k]:
            if l[i-k]:
                ans+=d[t[i]]
            else:
                l[i]=1
        else:
            ans+=d[t[i]]
    else:
        ans+=d[t[i]]
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split()) #RSP グーチョキパー
T=list(input())

c=0

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

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

for i in T:
    c+=jan(i)

print(c)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split()) #RSP グーチョキパー
T=list(input())

c=0

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

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

for i in range(1,K+1): #modK
    for j in range(N//K+1):
        if i+K*j>N:
            continue
        c+=jan(T[i+K*j-1])

print(c)

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(k, n):
    if T[i] == T[i - k]:
        T[i] = "x"

answer = 0
for t in T:
    if t == "r":
        answer += p
    elif t == "s":
        answer += r
    elif t == "p":
        answer += s


print(answer)

Submission status: Accepted

Solution code:
N, K = [int(_) for _ in input().split()]
R, S, P = [int(_) for _ in input().split()]
T = input()
ans = 0
dp = {}
dp['r'] = []
dp['s'] = []
dp['p'] = []
for i in range(K):
    s = T[i::K]
    now = s[0]
    l = 1
    for d in s[1:]:
        if now == d:
            l += 1
        else:
            dp[now] += [l]
            now = d
            l = 1
    dp[now] += [l]
for r in dp['r']:
    ans += P * ((r + 1) // 2)
for s in dp['s']:
    ans += R * ((s + 1) // 2)
for p in dp['p']:
    ans += S * ((p + 1) // 2)
print(ans)

Submission status: Accepted

Solution code:
def hantei(a):
  if a == 'r':
    return p
  elif a == 's':
    return r
  else:
    return s

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

ans = 0

for i in range(k):
  flag = True
  last1 = 'a'
  last2 = 'b'
  
  for j in range(i, n, k):
    if last1 != t[j]:
      ans += hantei(t[j])
      
    elif last1 == last2 and last1 == t[j]:
      ans += hantei(t[j])
      last1 = 'y'
    
    last2 = last1
    last1 = t[j]
    
    
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

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

#r, s, pで勝つ時のスコア
dp = [[0] * 3 for _ in range(K)]

for i in range(K):
  if T[i] == "s":
    dp[i][0] = R
  elif T[i] == "p":
    dp[i][1] = S
  else:
    dp[i][2] = P
    
for i in range(K, N):
  k = i%K
  if T[i] == "s":
    dp[k][0], dp[k][1], dp[k][2] = max(dp[k][1], dp[k][2])+R, max(dp[k][0], dp[k][2]), max(dp[k][0], dp[k][1]) 
  elif T[i] == "p":
    dp[k][0], dp[k][1], dp[k][2] = max(dp[k][1], dp[k][2]), max(dp[k][0], dp[k][2])+S, max(dp[k][0], dp[k][1]) 
  else:
    dp[k][0], dp[k][1], dp[k][2] = max(dp[k][1], dp[k][2]), max(dp[k][0], dp[k][2]), max(dp[k][0], dp[k][1])+P
    
ans = 0
for tmp in dp:
  ans += max(tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

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

d = {"r":P, "s":R, "p":S} # じゃんけんで勝点数
ans = 0
wins = [False]*K
for i in range(len(T)):
  if not wins[i%K] or T[i-K] != T[i]:
    ans += d[T[i]]
    wins[i%K] = True
  else:
    wins[i%K] = False
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
dic = {}
dic['r'] = p
dic['s'] = r
dic['p'] = s
ans = sum([dic[s] for s in t])
for i in range(k,len(t)):
    if t[i-k] == t[i]:
        ans -= dic[t[i]]
        t = t[:i] + 'a' + t[(i+1):]
print(ans)

Submission status: Accepted

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

print(result)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
 
def EarnScore(rsp, t):
    if t == 'r':
        return rsp[2]
    elif t == 's':
        return rsp[0]
    elif t == 'p':
        return rsp[1]
    else:
        return 0
 
RSP = [R, S, P]
Answer = 0
ListT = list(T)
for i in range(N):
    Answer = EarnScore(RSP, ListT[i]) + Answer
    if i + K < N:
        if ListT[i] == ListT[i+K]:
            ListT[i+K] = 'x'
 
print(Answer)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
score_list = list(input())
#rグー、sチョキ、pパー
#for i in range(k):
#    score_list.insert(0, 0)


ans = 0
for i in range(n):
    if score_list[i] == "r":
        if score_list[i] != score_list[i-k] or i < k:
            ans += p
        else:
            score_list[i] = 0
    elif score_list[i] == "s":
        if score_list[i] != score_list[i-k] or i < k:
            ans += r
        else:
            score_list[i] = 0
    elif score_list[i] == "p":
        if score_list[i] != score_list[i-k] or i < k:
            ans += s
        else:
            score_list[i] = 0

print(ans)