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:
#!/usr/bin/env python3

# a 回目、b 回目を考えたとき、a mod K と b mod K の値が異なれば、a, b それぞれの回で独立に手を選べる。
# mod K の値に応じて、N 回のジャンケンを K 個のグループに分けたとき、各グループに対して、最大何点取れるかを考えればよい。
# あるグループで最大何点取れるかは、前回の手を覚えておきながら DP or 貪欲法で求めればよい。

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

t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])
# print(groups)

def calc_score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    else:
        return 0

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

def get_best_hand(j, hands, my_hand_prev):
    hand = hands[j]
    win_hand = get_win_hand(hand)
    # decide which hand to take
    candidates = ['r', 's', 'p']
    # when we can win
    if j == 0 or j != 0 and win_hand != my_hand_prev:
        candidates = [win_hand]
    else:
        # can't take a hand twice in a row
        candidates.remove(my_hand_prev)
        # just pop when it's last hand in a group
        if j == len(hands) - 1:
            candidates.pop()
        else:
            hand_next = hands[j+1]
            win_hand_next = get_win_hand(hand_next)
            # just pop when win_hand_next has already removed
            if win_hand_next == my_hand_prev:
                candidates.pop()
            else:
                candidates.remove(win_hand_next)
    return candidates[0]

result = 0
# for each group
for i in range(k):
    hands = groups[i]
    my_hand_prev = ''
    # for each hand in a group
    for j in range(len(hands)):
        my_hand = get_best_hand(j, hands, my_hand_prev)
        my_hand_prev = my_hand
        # calculate score according to the hand decided
        result += calc_score(my_hand, hands[j])

print(result)

Submission status: Accepted

Solution code:
#dの正解
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())

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

ans=0
for i in range(K):
  ans += score[T[i]]

for i in range(K,N):
  if T[i-K] == T[i]:
    T[i] = '*' #god hands
  else:
    ans += score[T[i]]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# a 回目、b 回目を考えたとき、a mod K と b mod K の値が異なれば、a, b それぞれの回で独立に手を選べる。
# mod K の値に応じて、N 回のジャンケンを K 個のグループに分けたとき、各グループに対して、最大何点取れるかを考えればよい。
# あるグループで最大何点取れるかは、前回の手を覚えておきながら DP or 貪欲法で求めればよい。

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

t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])
# print(groups)

def score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    return 0

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

result = 0
# for each group
for i in range(k):
    hands = groups[i]
    # for each hand in a group
    for j in range(len(hands)):
        hand = hands[j]
        win_hand = get_win_hand(hand)
        if j == 0:
            my_hand = win_hand
        else:
            candidates = ['r', 's', 'p']
            candidates.remove(my_hand_prev)
            if win_hand == my_hand_prev:
                if j == len(hands) - 1:
                    candidates.pop()
                    my_hand = candidates[0]
                else:
                    hand_next = hands[j+1]
                    win_hand_next = get_win_hand(hand_next)
                    if win_hand_next == my_hand_prev:
                        candidates.pop()
                    else:
                        candidates.remove(win_hand_next)
                my_hand = candidates[0]
            else:
                my_hand = win_hand
        result += score(my_hand, hand)
        my_hand_prev = my_hand

print(result)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
Pt=[0,p,r,s]
Ans=[0]*k
Prev=[0]*k
for i in range(n):
  if t[i]=="r":
    cur=1
  elif t[i]=="s":
    cur=2
  else:
    cur=3
  ii=i%k
  if Prev[ii]==cur:
    Prev[ii]=0
  else:
    Ans[ii]+=Pt[cur]
    Prev[ii]=cur
print(sum(Ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# a 回目、b 回目を考えたとき、a mod K と b mod K の値が異なれば、a, b それぞれの回で独立に手を選べる。
# mod K の値に応じて、N 回のジャンケンを K 個のグループに分けたとき、各グループに対して、最大何点取れるかを考えればよい。
# あるグループで最大何点取れるかは、前回の手を覚えておきながら DP or 貪欲法で求めればよい。

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

t_list = list(t)
groups = [[] for i in range(k)]
for i in range(len(t_list)):
    mod = i % k
    groups[mod].append(t_list[i])
# print(groups)

def score(my_hand, hand):
    if my_hand == 'r' and hand == 's':
        return r
    elif my_hand == 's' and hand == 'p':
        return s
    elif my_hand == 'p' and hand == 'r':
        return p
    return 0

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

result = 0
# for each group
for i in range(k):
    hands = groups[i]
    # for each hand in a group
    for j in range(len(hands)):
        hand = hands[j]
        win_hand = get_win_hand(hand)
        if j == 0:
            my_hand = win_hand
            result += score(my_hand, hand)
            my_hand_prev = my_hand
        else:
            candidates = ['r', 's', 'p']
            candidates.remove(my_hand_prev)
            if win_hand == my_hand_prev:
                if j == len(hands) - 1:
                    candidates.pop()
                    my_hand = candidates[0]
                else:
                    hand_next = hands[j+1]
                    win_hand_next = get_win_hand(hand_next)
                    if win_hand_next == my_hand_prev:
                        candidates.pop()
                    else:
                        candidates.remove(win_hand_next)
                my_hand = candidates[0]
                result += score(my_hand, hand)
                my_hand_prev = my_hand
            else:
                my_hand = win_hand
                result += score(my_hand, hand)
                my_hand_prev = my_hand

print(result)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T =  input()
ans = 0
for k in range(K):
  for i in range(k,N,K):
    if i ==k:
      prv_t = T[i]
      prv_t_num = 1
    else:
      if prv_t != T[i]:
        if prv_t_num == 1:
          ans += R if prv_t=='s' else S if prv_t=='p' else P
        else:
          ans += R*(prv_t_num//2+prv_t_num%2) if prv_t=='s' else S*(prv_t_num//2+prv_t_num%2) if prv_t=='p' else P*(prv_t_num//2+prv_t_num%2)
        prv_t = T[i]
        prv_t_num = 1
      else:
        prv_t_num += 1
     
  if prv_t_num == 1:
    ans += R if prv_t=='s' else S if prv_t=='p' else P
  else:
    ans += R*(prv_t_num//2+prv_t_num%2) if prv_t=='s' else S*(prv_t_num//2+prv_t_num%2) if prv_t=='p' else P*(prv_t_num//2+prv_t_num%2)
print(ans)

Submission status: Accepted

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


n, k = map(int, input().split())

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

t = list(map(str, input().strip().split()[0]))
ans = 0
mp = {'r': p, 'p': s, 's': r}


for i, x in enumerate(t):
    if i >= k and t[i-k] == x:
        t[i] = 'o'
    else:
        ans += mp[x]

print(ans)

Submission status: Accepted

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

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

T = input()

result = 0
for i in range(K):
    before = ''

    for t in T[i:N:K]:
        if t != before:
            result += point[t]
            before = t
        else:
            before = ' '

print(result)

Submission status: Accepted

Solution code:
i=input()
n=int(i.split()[0])
k=int(i.split()[1])

rsp=input()
r=int(rsp.split()[0])
s=int(rsp.split()[1])
p=int(rsp.split()[2])

T=input()
rel=[]
for i in range(len(T)):
  temp=0
  if T[i]=="r":
    temp=p
  elif T[i]=="s":
    temp=r
  elif T[i]=="p":
    temp=s

  if i >= k and T[i]==T[i-k] and rel[i-k]!=0:
    temp=0
  rel.append(temp)

print(sum(rel))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(str(input()))
cntr = t.count("r")
cnts = t.count("s")
cntp = t.count("p")
cntr0 = 0
cnts0 = 0
cntp0 = 0
ans = 0
# print(t)
for i in range(n-k):
    if t[i] == "r"and t[i] == t[i+k]:
        cntr0 += 1
        t[i+k] = "q"
    elif t[i] == "s"and t[i] == t[i+k]:
        cnts0 += 1
        t[i+k] = "q"
    elif t[i] == "p"and t[i] == t[i+k]:
        cntp0 += 1
        t[i+k] = "q"

ans = p*(cntr - cntr0) + r*(cnts - cnts0) + s*(cntp - cntp0)
print(ans)

Submission status: Accepted

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

# restore
for i in range(n):
    if i >= k:
        if T[i] == T[i - k]:
            T[i] = '-'

# point sum
psum = 0
for i in range(n):
    if T[i] == 'r':
        psum = psum + p
    elif T[i] == 'p':
        psum = psum + s
    elif T[i] == 's':
        psum = psum + r
print(psum)

Submission status: Accepted

Solution code:
"""
References
----------
https://atcoder.jp/contests/abc149/submissions/9219652
"""
def solve(n, k, r, s, p, t):
    score = 0
    mapping = {"r": p, "s": r, "p": s}
    t = list(t)
    for i in range(k):
        score += mapping[t[i]]
    for i in range(k, n):
        if t[i] == t[i-k]:
            t[i] = "x"
        else:
            score += mapping[t[i]]
    return score

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

Submission status: Accepted

Solution code:
import numpy as np
def solve(n, k, r, s, p, t):
    dp = np.zeros((n, 3), dtype=int)
    win_hand_mapping = {
        "r": "p",
        "s": "r",
        "p": "s"
    }
    score_mapping = {
        "r": r,
        "s": s,
        "p": p
    }
    hand_index_mapping = {
        "r": 0,
        "s": 1,
        "p": 2
    }
    legal_hands_mapping = {
        i: [j for j in range(3) if i!=j] for i in range(3)
    }
    for i0 in range(k):
        for i in range(i0, n, k):
            win_hand = win_hand_mapping[t[i]]
            win_hand_index = hand_index_mapping[win_hand]
            win_hand_score = score_mapping[win_hand]
            for current in range(3):
                point = (current == win_hand_index) * win_hand_score
                if i < k:
                    dp[i][current] = max(dp[i][current], point)
                else:
                    for before in legal_hands_mapping[current]:
                        dp[i][current] = max(dp[i][current], dp[i-k][before] + point)
    return np.max(dp, axis=1)[-k:].sum()

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

Submission status: Accepted

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

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

score = 0
for i in range(0, K):
  a, b = scoreMap[T[i]], 0
  for j in range(i+K, N, K):
    na = b + scoreMap[T[j]]
    if T[j] != T[j-K]:
      na = max(na, a + scoreMap[T[j]])
    nb = max(a, b)
    a, b = na, nb
  score += max(a, b)
print(score)

Submission status: Accepted

Solution code:
'''input
3 5
1 2 1
2 3 0
'''
# connected components
from sys import stdin, setrecursionlimit
from bisect import bisect_left
import sys, threading
#


def get_cost(m, h):
    if m == 'r':
        if h == 'p':
            return paper
    elif m == 's':
        if h == 'r':
            return rock
    else:
        if h == 's':
            return scissor
    return 0


def make(first, second):
    return str(first) + ' ' + str(second)


def brute(string, index, last, dp):
    if index == len(string):
        return 0
    x = -float('inf'); y = -float('inf'); z = -float('inf')
    if make(index, last) in dp:
        return dp[make(index, last)]
    if index != 0:
        if last != 'p':
            x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        if last != 's':
            y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        if last != 'r':
            z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)

    else:
        x = get_cost(string[index], 'p') + brute(string, index + 1, 'p', dp)
        y = get_cost(string[index], 's') + brute(string, index + 1, 's', dp)
        z = get_cost(string[index], 'r') + brute(string, index + 1, 'r', dp)
    dp[make(index, last)] = max(x, y, z)
    return max(x, y, z)

#
# def solve(string):
#     dp = [[0 for x in range(3)] for y in range(len(string))]
#     if string[0] == 'r':
#         dp[0] = [0, 0, paper]
#     elif string[0] == 's':
#         dp[0] = [rock, 0, 0]
#     else:
#         dp[0] = [0, scissor, 0]
#
#     for i in range(1, len(string)):
#         if string[i] == 'r':
#             dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
#             dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
#             dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + paper
#         elif string[i] == 's':
#             dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + rock
#             dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
#             dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
#         else:
#             dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
#             dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + scissor
#             dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
#     # print(dp)
#     return max(dp[-1])

# main starts
paper = 0
scissor = 0
rock = 0
def main():
    global paper, rock, scissor
    n, k = list(map(int, stdin.readline().split()))
    rock, scissor, paper = list(map(int, stdin.readline().split()))
    string = stdin.readline().strip()
    cost = 0
    for i in range(k):
        temp = []
        j = i
        while j < n:
            temp.append(string[j])
            j += k
        dp = dict()
        cost += brute(temp, 0, '$', dp)
    print(cost)

if __name__ == "__main__":
    sys.setrecursionlimit(200005)
    threading.stack_size(1 << 27)
    thread = threading.Thread(target=main)
    thread.start()

Submission status: Accepted

Solution code:
'''input
3 5
1 2 1
2 3 0
'''
# connected components
from sys import stdin, setrecursionlimit
from bisect import bisect_left


def solve(string):
    dp = [[0 for x in range(3)] for y in range(len(string))]
    if string[0] == 'r':
        dp[0] = [0, 0, paper]
    elif string[0] == 's':
        dp[0] = [rock, 0, 0]
    else:
        dp[0] = [0, scissor, 0]

    for i in range(1, len(string)):
        if string[i] == 'r':
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + paper
        elif string[i] == 's':
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + rock
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
        else:
            dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + scissor
            dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
    # print(dp)
    return max(dp[-1])

# main starts
n, k = list(map(int, stdin.readline().split()))
rock, scissor, paper = list(map(int, stdin.readline().split()))
string = stdin.readline().strip()
cost = 0
for i in range(k):
    temp = []
    j = i
    while j < n:
        temp.append(string[j])
        j += k
    cost += solve(temp)
print(cost)

Submission status: Accepted

Solution code:
def get_score(enemy_hand):
    return {"r": P, "s": R, "p": S}[enemy_hand]


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

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

score = 0
for hands in enemy_hands:
    succession = {"r": [], "s": [], "p": []}
    current = hands[0]
    streak = 0
    for hand in hands:
        if hand == current:
            streak += 1
        else:
            succession[current].append(streak)
            streak = 1
            current = hand
    else:
        succession[current].append(streak)
    for k, v in succession.items():
        for streak in v:
            score += ((streak + 1) // 2) * get_score(k)

print(score)

Submission status: Accepted

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


def subtotal(sub_T):
    #dp[hand, sub_total_point]
    dp = [['non', 0]]
    sub_total_point = 0

    for i in range(0,len(sub_T)):
        hand = sub_T[i]
        if hand != dp[i][0]:
            if hand == 'r':
                sub_total_point += P
            elif hand == 's':
                sub_total_point += R
            elif hand == 'p':
                sub_total_point += S
            else:
                pass

        else:
            hand = 'n'

        dp.append([hand,sub_total_point])

    return sub_total_point

def main():
    total_point = 0

    if K==1:
        total_point += subtotal(T)

    else:
        for m in range(0, K):
            sub_T = []
            for n in range(0, int(N/K)+1):
                if n*K+m < N:
                    sub_T.append(T[n*K+m])
                else:
                    pass

            total_point += subtotal(sub_T)

    print(total_point)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
pre_jyanken = [0] * K  # K回前に相手が出した手
ans = 0
for i in range(N):
    if pre_jyanken[i % K] == T[i]:
        pre_jyanken[i % K] = i
    else:
        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        else:
            ans += S
        pre_jyanken[i % K] = T[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    score = {"r": P, "s": R, "p": S}
    T = input()
    skip = set()
    ans = 0
    for i in range(N):
        if i >= K and T[i] == T[i-K] and (i-K) not in skip:
            skip.add(i)
            continue
        ans += score[T[i]]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input().rstrip())
# if (i)th == (i-K)th: point=0
for i in range(K,N):
  if T[i] == T[i-K]:
    T[i] = 'o'
    
point = 0
for t in T:
  if t == 'r':
    point += P
  elif t == 's':
  	point += R
  elif t == 'p':
    point += S

print(point)

Submission status: Accepted

Solution code:
from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


def main():
    n,k = mi()
    r,s,p = mi()
    tmp = list(input())
    t = [0]
    t+=tmp

    ans = 0

    for i in range(1,n+1):
        if t[max(0,i-k)] == t[i]:
            t[i] = 0
            continue
        if t[i] == 'r':
            ans+=p
        elif t[i] == 'p':
            ans+=s
        else:
            ans+=r

    print(ans)



    


    
main()

Submission status: Accepted

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

line=[ t[i::k] for i in range(k)]

rr=0
ss=0
pp=0
for x in line:
    #print(x)

    combo=1
    al=x[0]
    for i in x[1:]:
        if al == i:
            combo+=1
            next
        else:
            if al == 'r':
                pp+=(combo+1)//2
            elif al == 's':
                rr+=(combo+1)//2
            else:
                ss+=(combo+1)//2
            combo=1
            al=i

    if al == 'r':
        pp+=(combo+1)//2
    elif al == 's':
        rr+=(combo+1)//2
    else:
        ss+=(combo+1)//2

#print(rr,ss,pp)

print(pp*p+rr*r+ss*s)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
for i in range(K):
    current_list = []
    k = i
    while k < len(T):
        current_list.append(T[k])
        k += K
    current_value = current_list[0]
    c = 1
    for j in range(1,len(current_list)):
        if current_value == current_list[j]:
            c += 1
        else:
            if current_value == 'r':
                ans += ((c+1)//2)*P
            elif current_value == 's':
                ans += ((c+1)//2)*R
            else:
                ans += ((c+1)//2)*S
            c = 1
            current_value = current_list[j]
    if current_value == 'r':
        ans += ((c+1)//2)*P
    elif current_value == 's':
        ans += ((c+1)//2)*R
    else:
        ans += ((c+1)//2)*S
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(input())
 
    score = {'r':  P, 's': R, 'p': S}
 
    ans = 0
    for i in range(K):
        ans += score[T[i]]
 
    for i in range(K, N):
        if T[i-K] == T[i]:
            T[i] = ' '
            continue
        ans += score[T[i]]
 
    print(ans)
 
if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

ans=0
for i in range(K):
  ans += score[T[i]]

for i in range(K,N):
  if T[i-K] == T[i]:
    T[i] = ''
    continue
  ans += score[T[i]]

print(ans)

Submission status: Accepted

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


def scr(inp):
	if inp == 'r':
		return P
	elif inp == 'p':
		return S
	else:
		return R


Score = 0
for i in range(N):
	if i >= K and T[i] == T[i-K]:
		if T[i-K] == 'z':
			Score += scr(T[i])
		else:
			T = T[:i] + 'z' + T[i + 1:]
	else:
		Score += scr(T[i])

print(Score)

Submission status: Accepted

Solution code:
def getPt(h):
    if h == "s":
        return R
    elif h == "p":
        return S
    elif h == "r":
        return P

N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = list(input())
pt = 0
for i in range(N):
    if i < K:
        pt += getPt(T[i])
    else:
        if T[i] == T[i-K]:
            T[i] = ""
        else:
            pt += getPt(T[i])
print(pt)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 1 << 100
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
r, s, p = LI()
D = {'r': p, 'p': s, 's': r}
t = S()
flag = [1] * n
ans = 0
for i in range(n):
    if i <= k - 1:
        ans += D[t[i]]
    else:
        if t[i - k] == t[i] and flag[i - k]:
            flag[i] = 0
        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())
T = input()
point = {"r":P, "s":R, "p":S}
score = 0
flg = [0]*N #勝ちを譲った時flgを立てる
for i in range(K):
    score += point[T[i]]
for i in range(K,N):
    # K回前と同じ手かつ、K回前に譲っていなければ譲る必要がある flgを立てる
    if T[i] == T[i-K] and flg[i-K]==0:
        flg[i] = 1
    else:
        score += point[T[i]]
print(score)

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

su  =  p*t.count('r') + r*t.count('s')  + s*t.count('p') 

ls = []
for i in range(len(t)-k):
    if t[i] == t[i+k]:
        ls.append(t[i])
        t = t[:i+k] + 'a' + t[i+k+1:]  #swap text

su2 = p*ls.count('r') + r*ls.count('s')  + s*ls.count('p') 
print(su-su2)

Submission status: Accepted

Solution code:
input_line1 = input().split(' ')
input_line2 = input().split(' ')
stT = input()
numN = int(input_line1[0])
numK = int(input_line1[1])
numR = int(input_line2[0])
numS = int(input_line2[1])
numP = int(input_line2[2])

vioR = []
vioS = []
vioP = []
result = [0] * numN 

for i, c in enumerate(stT):
  if i + numK < numN and stT[i + numK] == c and result[i] == 0:
    result[i + numK] = -1
  if result[i] != -1:
    result[i] = c

score = 0
for i in result:
    if i == 'r':
        score += numP
    elif i == 's':
        score += numR
    elif i == 'p':
        score += numS
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
p = dict()
p['r'] = R
p['s'] = S
p['p'] = P
T = input()


def solve(me, you, memo):
    point = 0
    for i, t in enumerate(T):
        if t == you and (i < K or (i - K >= 0 and memo[i - K] != me)):
            memo[i] = me
            point += p[me]
    return point


m = ['' for _ in range(N)]
pri = [(R, 'r', 's'), (S, 's', 'p'), (P, 'p', 'r')]
pri.sort(reverse=True)
ans = 0
for _, i, u in pri:
    ans += solve(i, u, m)
print(ans)

Submission status: Accepted

Solution code:

def solve():
  n,k = (int(i) for i in input().split())
  r,s,p = (int(i) for i in input().split())
  t = input()
  score = 0
  times = 0
  possible = [False]*n
  for i in range(n):
    if i >= k:
      if t[i-k] == t[i] and possible[i-k]== False:
        possible[i] = True
        continue

    if t[i] == "r":
        score += p
    elif t[i] == "s":
        score += r
    elif t[i] == "p":
        score += s
  print(score)
solve()

Submission status: Accepted

Solution code:
def main():
  [n, k] = [int(i) for i in input().split()]
  [r, s, p] = [int(i) for i in input().split()]
  T =  input()
  
  my = T.replace("r", "hoge").replace("s", "r").replace("p", "s").replace("hoge", "p") 
  for i in range(k, n):
    if my[i] == my[i-k]:
      my = my[:i] + "l" + my[i+1:]  

  print(my.count("r")* r + my.count("s")*s + my.count("p")* p)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

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

n, k = map(int, input().split())
s, p, r = map(int, input().split())
t = input().rstrip().decode("utf-8")

cnt = 0
for i in range(k):
    f = 0
    pre = ""
    for j in range(i, n, k):
        if pre != t[j] or f == 1:
            cnt += eval(t[j])
            f = 0
        else:
            f = 1
        pre = t[j]
print(cnt)

Submission status: Accepted

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

cnt = 0
for i in range(k):
    f = 0
    pre = ""
    for j in range(i, n, k):
        if pre != t[j] or f == 1:
            cnt += eval(t[j])
            f = 0
        else:
            f = 1
        pre = t[j]
print(cnt)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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)

answer = sum(solve(A) for A in k_to_word)
print(answer)