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: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jan  4 17:24:35 2020

@author: masat
"""

    

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

def janken(myhand, rivalhand):
    point = 0
    if myhand == "r":
        if rivalhand == "s":
            point = R
        else:
            pass
    if myhand == "s":
        if rivalhand == "p":
            point = S
        else:
            pass
    if myhand == "p":
        if rivalhand == "r":
            point = P
        else:
            pass
    return point

quotient = N // K
remainder = N % K

bucket_list = [[] for i in range(K)]

for i in range(N):
    bucket_list[i+1 % K].append(T[i])

best_total_point = 0

for bucket in bucket_list:
    my_hand_list = []
    for now_time in range(len(bucket)):
        if now_time == 0:
            now_rival_hand = bucket[now_time]
            next_rival_hand = bucket[now_time + 1]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                for next_my_hand in ["r", "s", "p"]:
                    if next_my_hand == now_my_hand:
                        pass
                    else:
                        this_point_now_and_next = \
                            janken(now_my_hand, now_rival_hand) \
                            +janken(next_my_hand, next_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)
        elif now_time == len(bucket):
            last_myhand = my_hand_list[now_time - 1]
            
            now_rival_hand = bucket[now_time]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                if now_my_hand == last_myhand:
                    pass
                else:
                    this_point_now_and_next = \
                        janken(now_my_hand, now_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)
        else:
            last_myhand = my_hand_list[now_time - 1]
            
            now_rival_hand = bucket[now_time]
            next_rival_hand = bucket[now_time + 1]
            
            best_point_now_and_next = 0
            best_point_now = 0
            best_now_my_hand = ""
            for now_my_hand in ["r", "s", "p"]:
                if now_my_hand == last_myhand:
                    pass
                else:
                    for next_my_hand in ["r", "s", "p"]:
                        if next_my_hand == now_my_hand:
                            pass
                        else:
                            this_point_now_and_next = \
                                janken(now_my_hand, now_rival_hand) \
                                +janken(next_my_hand, next_rival_hand)
                if this_point_now_and_next > best_point_now_and_next:
                    best_now_my_hand = now_my_hand
                    best_point_now = janken(now_my_hand, now_rival_hand)
                else:
                    pass
            best_total_point += best_point_now
            my_hand_list.append(best_now_my_hand)

print(best_total_point)

Submission status: Runtime Error

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()
        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: Runtime Error

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, resource

resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x100000)


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

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

int n,k,r,s,p;
string t;
int rps(char d)
{
    if(d=='r') return p;
    else if(d=='p') return s;
    else return r; 
}
int main()
{
    ios::sync_with_stdio(false);
	cin.tie(0);
    cin>>n>>k;
    cin>>r>>s>>p;
    cin>>t;
    int dp[100005]={0};
    int res=0;
    for(int i=0;i<k;i++)
    {
        res+=rps(t[i]);
        dp[i]=res;
    }
    for(int i=k;i<t.size();i++)
    {
        if(t[i-k]==t[i])
        {
            dp[i]=max(dp[i-k],dp[i-1]);
            t[i]='0';
        }
        else{
            dp[i]=dp[i-1]+rps(t[i]);
        }
    }
    cout<<dp[n-1];
    return 0;
}

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
syo = n//k 
total = 0
for i in range(k):
  for j in range(i, n, k):
    if j == i or score == 0 or t[j] ?= te:
      if t[j] = 'r'
      	score = p
      	total += p
        te = 'p'
      elif t[j] = 's'
      	score = r
      	total += r
        te = 'r'
      else:
        score = s
        total += s
        te = 's'
    else:
      score = 0
      
  
print(total)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
#dp[i][ｊ]:=i番目までで、最後にｊを出したときの点数の最大値
dp = [[-10**9]*3for i in range(n+10)]
for i in range(k):
    dp[i][0],dp[i][1],dp[i][2] = 0,0,0
    
    
 
for i in range(k):
    for j in range(n//k+3):
        if i > n-1:continue
        if t[i] == "r":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0]+p,dp[i][1]+p)
        
        elif t[i] == "s":
            dp[i+k][0] = max(dp[i][1]+r,dp[i][2]+r)
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        
        elif t[i] == "p":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0]+s,dp[i][2]+s)
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        i += k
ans = 0    
for i in range(n,n+k):
    ans += max(dp[i])
print(ans)

Submission status: Runtime Error

Solution code:
#降順ソートの方がうまくいきそう
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
cs=[]
sum=0
for i in range(n):
	sum+=a[i]
	cs.append(sum)

#和が引数以上の場合をカウントする
def overX(x):
	count=0
	#両手が違う人を握るときのみ考える
	if a[0]*2<x:
		return 0
	else:
		for i in range(n):
			if a[i]+a[0]<x:
				count+=0
			else:
				lb, ub = 0, n
				#パターンAのにぶたん。左がOなので[lb,ub)で持つ。
				while ub-lb>1:
					mid=(lb+ub)//2
					if a[i]+a[mid]>=x:
						lb=mid
					else:
						ub=mid
				count+=lb+1
		return count
#print(overX(130))

#overX>=mなる最大のxを探す->xは小さい方が実現しやすいのでパターンA->[lb,ub)でもつ
lb=1 #実現しやすい方
ub=2*10**5
while ub-lb>1:
	mid=(ub+lb)//2
	if overX(mid)>=m:
		lb=mid
	else:
		ub=mid
#ub=overX(x)>=mなる最小のx。これ以上大きくするとmに満たなくなる。
maxx=lb

hp=0
#maxx+1以上の要素をすべてたす
#和がmaxx+1以上になる組み合わせの数だけhpを加え、countをインクリメントする
if a[0]*2>=maxx+1:
	for i in range(n):
		if a[i]+a[0]<maxx+1:
			break
		else:
			lb, ub = 0, n
			#パターンAのにぶたん。左がOなので[lb,ub)で持つ。
			while ub-lb>1:
				mid=(lb+ub)//2
				if a[i]+a[mid]>=maxx+1:
					lb=mid
				else:
					ub=mid
			#a[0]からa[lb]は、a[i]と足した時にx以上になり条件を満たす。
			hp+=a[i]*(lb+1)+cs[lb]
hp+=(m-overX(maxx+1))*maxx
print(hp)

Submission status: Runtime Error

Solution code:
p = [int(i) for i in m]
n = input()    
m = n.split()
P = [int(i) for i in m]
n = input()
list = [""]*p[1]
for i, value in enumerate(n):
    list[i%p[1]] =list[i%p[1]] + value
A = 0
for i in list:
    able = [True,True,True]
    for j in i:
        if j == "r":
            if able[2]:
                A = A + P[2]
                able = [True,True,False]
            else:
                able = [True,True,True]
        if j == "s":
            if able[0]:
                A = A + P[0]
                able = [False,True,True]
            else:
                able = [True,True,True]
        if j == "p":
            if able[1]:
                A = A + P[1]
                able = [True,False,True]
            else:
                able = [True,True,True]

print(A)

Submission status: Runtime Error

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

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    SC+=point(T[i])
    if i+K<N and T[i]==T[i+K]:
        Flag[i+K]=False
return(SC)

Submission status: Runtime Error

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

# print('{} {} {} {} {} {}'.format(N, K, R, S, P, T))

history = []

HANDS = ['r', 's', 'p']
WIN_HANDS = {'r': 'p', 'p': 's', 's': 'r'}
WIN_SCORE = {'r': R, 's': S, 'p': P}

total_score = 0
for i, cpu_hand in enumerate(T):
    unavailable_hand = history[-K] if i >= K else ''
    hand = WIN_HANDS[cpu_hand]

    if hand != unavailable_hand:
        history.append(hand)
        total_score += WIN_SCORE[hand]
    else:
        if i + K < len(T) and WIN_HANDS[T[i + K]] != hand:
            history.append(WIN_HANDS[T[i + K]])
            total_score += WIN_SCORE[WIN_HANDS[T[i + K]]]
        else:
            tmp_hands = copy.deepcopy(HANDS)
            tmp_hands.remove(hand)
            history.append(tmp_hands[0])
# print(history)
print(total_score)

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;
int main(){
  int N, K;
  cin >> N >> K;
  int R, S, P;
  cin >> R >> S >> P;
  string T;
  cin >> T;
  int ans = 0;
  for(int i = 0; i < N; i++){
    if(T[i] == 'r'){
      ans += P;
      if(i+K < N && T[i+K] == 'r'){
        T[i+K] = 'n';
      }
    }
    else if(T[i] == 's'){
      ans += R;
      if(i+K < N && T[i+K] == 's'){
        T[i+K] = 'n';
      }
    }
    else if(T[i] == 'p'){
      ans += S;
      if(i+K < N && T[i+K] == 'p'){
        T[i+K] = 'n';
      }
    }
  }
  cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
hand=input()
ans=0
for i in range(k):
  free=True
  j=i
  while j<n:
    if free=True or hand[j-k]!=hand[j]:
      if hand[j]=='r':
        ans+=p
      elif hand[j]=='s':
        ans+=r
      else:
        ans+=s
      free=False
    else:
      free=True
    j+=k
print(ans)

Submission status: Runtime Error

Solution code:
n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
hand=input()
ans=0
for i in range(k):
  free=True
  j=i
  while j<n:
    if free=True or hand[j-k]!=hand[j]:
      if hand[j]='r':
        ans+=p
      elif hand[j]='s':
        ans+=r
      else:
        ans+=s
      free=False
    else:
      free=True
    j+=k
print(ans)

Submission status: Runtime Error

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

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

def DFS(cnt, point, string) : 
    if cnt == N : 
        return(point)
    else : 
        if cnt >= K : 
            r1 = 0
            r2 = 0
            r3 = 0
            r4 = 0
            r5 = 0
            r6 = 0
            s1 = 0
            s2 = 0
            s3 = 0
            s4 = 0
            s5 = 0
            s6 = 0
            p1 = 0
            p2 = 0
            p3 = 0
            p4 = 0
            p5 = 0
            p6 = 0

            if T[cnt] == 'r' : 
                if string[-K] == 'r' : 
                    r1 = DFS(cnt+1, point + P, string+'p') # p
                    r2 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 'p' : 
                    r3 = DFS(cnt+1, point, string+'r') # r
                    r4 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 's' : 
                    r5 = DFS(cnt+1, point, string+'r') # r
                    r6 = DFS(cnt+1, point + P, string+'p') # p
            elif T[cnt] == 's' : 
                if string[-K] == 'r' : 
                    s1 = DFS(cnt+1, point, string+'p') # p
                    s2 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 'p' : 
                    s3 = DFS(cnt+1, point + R, string+'r') # r
                    s4 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 's' : 
                    s5 = DFS(cnt+1, point + R, string+'r') # r
                    s6 = DFS(cnt+1, point, string+'p') # p
            elif T[cnt] == 'p' : 
                if string[-K] == 'r' : 
                    p1 = DFS(cnt+1, point, string+'p') # p
                    p2 = DFS(cnt+1, point + S, string+'s') # s
                elif string[-K] == 'p' : 
                    p3 = DFS(cnt+1, point, string+'r') # r
                    p4 = DFS(cnt+1, point + S, string+'s') # s
                elif string[-K] == 's' : 
                    p5 = DFS(cnt+1, point, string+'r') # r
                    p6 = DFS(cnt+1, point, string+'p') # p
                    
            return(max(r1, r2, r3, r4, r5, r6, s1, s2, s3, s4, s5, s6, p1, p2, p3, p4, p5, p6))

        else :         
            r1 = 0
            r2 = 0
            r3 = 0
            s1 = 0
            s2 = 0
            s3 = 0
            p1 = 0
            p2 = 0
            p3 = 0
            
            if T[cnt] == 'r' : 
                r1 = DFS(cnt+1, point, string+'r') # r
                r2 = DFS(cnt+1, point + P, string+'p') # p
                r3 = DFS(cnt+1, point, string+'s') # s
            elif T[cnt] == 's' : 
                s1 = DFS(cnt+1, point + R, string+'r') # r
                s2 = DFS(cnt+1, point, string+'p') # p
                s3 = DFS(cnt+1, point, string+'s') # s
            elif T[cnt] == 'p' : 
                p1 = DFS(cnt+1, point, string+'r') # r
                p2 = DFS(cnt+1, point, string+'p') # p
                p3 = DFS(cnt+1, point + S, string+'s') # s
               
            return(max(r1, r2, r3, s1, s2, s3, p1, p2, p3))
        
print(DFS(0, 0, ''))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

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

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


    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[i])
print(ans)

Submission status: Runtime Error

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

pre = [[] for i in range(((n-1)//k))]
for i in range(n):
    pre[i%k].append(t[i])
#print(pre)

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

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


    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[i])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
*t,s=open(0).read().split()
n,k,r,s,p=map(int,t)
d={'r':p,'s':r,'p':s}
b=['']*n*2
a=0
for i,c in enumerate(s[:n]):
  if c!=b[i-k]:
    b[i]=c
    a+=d[c]
print(a)

Submission status: Runtime Error

Solution code:
def f(x):
    if x == "r":
        return "p"
    elif x == "s":
        return "r"
    else:
        return "s"

def g(x):
    if x == "r":
        return r
    elif x == "s":
        return s
    else:
        return p 

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

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

t = str(input())
ssssppr
m = n
a = 0
ss = [' '] * m

if m <= k:
    for i in range(m):
        a += g(f(t[i]))
    print(a)
else:
    for i in range(k):
        a += g(f(t[i]))
        ss[i] = f(t[i])
    for i in range(k,m):
        if t[i-k] != t[i]:
            a += g(f(t[i]))
            ss[i] = f(t[i])
        elif (t[i-k] == t[i]) and (ss[i-k] != f(t[i])):
            a += g(f(t[i]))
            ss[i] = f(t[i])
    print(a)

Submission status: Runtime Error

Solution code:
def f(x):
    if x == "r":
        return "p"
    elif x == "s":
        return "r"
    else:
        return "s"

def g(x):
    if x == "r":
        return r
    elif x == "s":
        return s
    else:
        return p 

n,k = map(int,input().split())
 
r,s,p = map(int,input().split())
 
t = str(input())
ssssppr
m = n
a = 0
ss = [' '] ** m
 
if m <= k:
    for i in range(m):
        a += g(f(t[i]))
    print(a)
else:
    for i in range(k):
        a += g(f(t[i]))
        ss[i] = f(t[i])
    for i in range(k,m):
        if t[i-k] != t[i]:
            a += g(f(t[i]))
            ss[i] = f(t[i])
        elif (t[i-k] == t[i]) and (ss[i-k] != f(t[i])):
            a += g(f(t[i]))
            ss[i] = f(t[i])
    print(a)

Submission status: Runtime Error

Solution code:
def f(x):
    if x == "r":
        return "p"
    elif x == "s":
        return "r"
    else:
        return "s"

def g(x):
    if x == "r":
        return r
    elif x == "s":
        return s
    else:
        return p 
n,k = map(int,input().split())

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

t = str(input())
ssssppr
m = n
a = 0
ss = [' '] ** m

if m <= k:
    for i in range(m):
        a += g(f(t[i]))
    print(a)
else:
    for i in range(k):
        a += g(f(t[i]))
        ss[i] = f(t[i])
    for i in range(k,m):
        if t[i-k] != t[i]:
            a += g(f(t[i]))
            ss[i] = f(t[i])
        elif (t[i-k] == t[i]) and (ss[i-k] != f(t[i])):
            a += g(f(t[i]))
            ss[i] = f(t[i])
    print(a)

Submission status: Runtime Error

Solution code:

def solve(N,K,R,S,P,T):
    check = [0]*N
    ans = 0
    for i in range(N):
        if i < K or T[i] != T[i-K] or not check[i-K]:
            ans += P if T[i] == 'r' else R if T[i] == 's' else S
            check[i] = 1
    print(ans)

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

count = 0
result = []

def get(x):
  if x == "r":
    return ("p", P)
  elif x == "s":
    return ("r", R)
  elif x == "p":
    return ("s", S)


for i in range(N):
  x = T[i]
  hand, point = get(x)
  if i < K:
    # 前回の結果は気にしなくても良いとき
    result.append((hand, point))
  elif hand != result[i-K][0]:
    result.append((hand, point))
  else:
    if i + K >= N:
      # 後のことを考えなくて良いとき
      result.append((hand, 0))
    else:
      next = list(filter(lambda x: x not in (T[i+2], hand), ["r", "s", "p"]))[0]
      result.append((next, 0))

print(sum(list(map(lambda x: x[1], result))))

Submission status: Runtime Error

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

ans = 0
hands = []
for i in range(N):
    if i < K:
        # グー
        if T[i] == 'r':
            ans += P
            hands.append('p')
        # チョキ
        elif T[i] == 's':
            ans += R
            hands.append('r')
        # パー
        else:
            ans += S
            hands.append('s')
    else:
        if T[i] == 'r':
            if hands[i-K] != 'p':
                ans += P
                hands.append('p')
            else:
                if i + K <= N:
                    hands.append(list(set('rsp') - set([T[i+K], hands[i-K]]))[0])
        # チョキ
        elif T[i] == 's':
            if hands[i-K] != 'r':
                ans += R
                hands.append('r')
            else:
                if i + K <= N:
                    hands.append(list(set('rsp') - set([T[i+K], hands[i-K]]))[0])
        # パー
        else:
            if hands[i-K] != 's':
                ans += S
                hands.append('s')
            else:
                if i + K <= N:
                    hands.append(list(set('rsp') - set([T[i+K], hands[i-K]]))[0])

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
score_map = {
    "r": p,
    "p": s,
    "s": r,
    "n": 0,
}
t = list(t)
ans = 0
for i in range(k):
    skipped_list = t[i::k]
    print(f"{skipped_list}===")
    for j, c in enumerate(skipped_list[1:]):
        if skipped_list[j] == c:
            skipped_list[j+1] = "n"
    for c in skipped_list:
        ans += score_map[c]
print(ans)