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: Wrong Answer

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

ans = 0
# count for 0 ~ K
for i in range(N-K):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point

    if(T[i+K] == T[i]):
        print(i, "taken")
        T[i+K] = 'n'

# count for K ~ N
for i in range(N-K, N):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys

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

#print(N,K)
#print(R,S,P)
#print(T)

def maxp(s):
    vr = 0
    vs = 0
    vp = 0
    for i in range(0,len(s)):
        if s[i] == 'r':
            nvr = 0 + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = P + max(vr,vs)
        elif s[i] == 's':
            nvr = R + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = 0 + max(vr,vs)
        else:
            nvr = 0 + max(vs,vp)
            nvs = S + max(vp,vr)
            nvp = 0 + max(vr,vs)
        vr = nvr
        vs = nvs
        vp = nvp
        #print(s[i],vr,vs,vp)
    return(max(vr,vs,vp))
ans=0
for i in range(0,K):
    p = ""
    for k in range(i,len(T),K):
        p = p + T[k]
    #print(p)
    ans  = ans + maxp(p)

Submission status: Wrong Answer

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

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

dp = [''] * n
a = 0
for i in range(k):
    a += m[t[i]]
    
for i in range(k, n):
    if t[i] != dp[i-k]:
        a += m[t[i]]
        dp[i] = t[i]

print(a)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
hand = {"r" : p, "s" : r, "p" : s}
ans = 0
for i in range(n-k):
  temp= i-k
  if temp >=0 and t[i]==t[temp]:
    t[i]=""
  else:
    ans+=hand[t[i]]
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
	# a,b,k = map(int,input().split())
	n,k = map(int,input().split())
	# s = str(input())
	a = list(map(int,input().split()))
	rsp = list(str(input()))
	# l = [list(map(int, input().split())) for _ in range(n-1)]
	# s = list(s)

	# print(n,k)
	# print(a)
	# print(rsp)
	ans = 0
	jan = []
	r_count = 0
	s_count = 0
	p_count = 0
	for i in range(n):
		# print("---i = {}---".format(i))
		if i < k:
			if rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		elif rsp[i] == "s" and jan[i-k] == "r":
			jan.append("s")
		elif rsp[i] == "r" and jan[i-k] == "p":
			jan.append("r")
		elif rsp[i] == "p" and jan[i-k] == "s":
			jan.append("p")
		else:
			if rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		# print(jan)
		# print(len(jan))
	
	print(ans)

	
 
 
if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
def main():
	# a,b,k = map(int,input().split())
	n,k = map(int,input().split())
	# s = str(input())
	a = list(map(int,input().split()))
	rsp = list(str(input()))
	# l = [list(map(int, input().split())) for _ in range(n-1)]
	# s = list(s)

	print(n,k)
	print(a)
	print(rsp)
	ans = 0
	jan = []
	r_count = 0
	s_count = 0
	p_count = 0
	for i in range(n):
		# print("---i = {}---".format(i))
		if i < k:
			if rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		elif rsp[i] == "s" and jan[i-k] == "r":
			jan.append("s")
		elif rsp[i] == "r" and jan[i-k] == "p":
			jan.append("r")
		elif rsp[i] == "p" and jan[i-k] == "s":
			jan.append("p")
		else:
			if rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		# print(jan)
		# print(len(jan))
	
	print(ans)

	
 
 
if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
R, S, P = [int(_) for _ in input().split()]
T = input()

sum = 0
flagP, flagR, flagS = 0,0,0
countP, countR, countS = 0, 0, 0

for i in range(K-1):
    if T[i]=='r':
        countP += 1

    elif T[i]=='s':
        countR += 1

    elif T[i]=='p':
        countS += 1
for i in range(K-1,N):
    if T[i]=='r':
        if T[i-K]!='r':
            countP += 1
        else:
            if flagP == 1:
                countP += 1
                flagP = 0
            else:
                flagP = 1

    if T[i]=='s':
        if T[i-K]!='s':
            countR += 1
        else:
            if flagR == 1:
                countR += 1
                flagR = 0
            else:
                flagR = 1

    if T[i]=='p':
        if T[i-K]!='p':
            countS += 1
        else:
            if flagS == 1:
                countS += 1
                flagS = 0
            else:
                flagS = 1

sum = countR * R+ countP * P + countS * S
print(sum)

Submission status: Wrong Answer

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

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

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

print(a)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ans=0
f=True;
for i in range(k):
  if t[i::k][0]=="r": ans+=p
  elif t[i::k][0]=="s": ans+=r
  else: ans+=s
  for j in range(1,len(t[i::k])):
    if t[i::k][j]==t[i::k][j-1] and f: f=False
    else:
      f=True
      if t[i::k][j]=="r": ans+=p
      elif t[i::k][j]=="s": ans+=r
      else: ans+=s
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ans=0
f=True;
for i in range(k):
  if t[i::k][0]=="r": ans+=p
  elif t[i::k][0]=="s": ans+=r
  elif t[i::k][0]=="p": ans+=s
  for j in range(1,len(t[i::k])):
    if t[i::k][j]==t[i::k][j-1] and f: f=False
    else:
      f=True
      if t[i::k][j]=="r": ans+=p
      elif t[i::k][j]=="s": ans+=r
      elif t[i::k][j]=="p": ans+=s
print(ans)

Submission status: Wrong Answer

Solution code:
def getdata_input():
  while True:
    yield input()
def calc(it):
    n,k=map(int, next(it).split())
    r,s,p=map(int, next(it).split())
    t=next(it)
    win={"r":p,"s":r,"p":s}
    draw={"r":r,"s":s,"p":p}
    arr=[-1]*n
    res=[-1]*n
    for i,x in enumerate(t):
        y=win[x]
        r=1
        if i > k-1:
            if arr[i-k]==y:
                y=draw[x]
                r=0
        arr[i]=y
        res[i]=r
        
    return sum([a*b for a,b in zip(arr,res)])
it=getdata_input()
print(calc(it))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)


input = sys.stdin.readline


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

    def calc(c):
        if c == 'r':
            return p
        elif c == 's':
            return r
        else:
            return s
    t = input().strip()
    ans = 0
    win = [False]*n
    for i, c in enumerate(t):
        if (i < k):
            win[0] = True
            ans += calc(c)
            continue
        if t[i] == t[i-k] and win[i-k]:
            continue
        else:
            win[i] = True
            ans += calc(c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

tmp = ''
score = 0
char_list = list(t)
v_list = []

def judge(hand):
  global score
  if hand == 'r':
    if tmp == 'p':
      v_list.append('r')
    else:
      v_list.append('p')
      score += p
  elif hand == 's':
    if tmp == 'r':
      v_list.append('s')
    else:
      v_list.append('r')
      score += r
  else:
    if tmp == 's':
      v_list.append('p')
    else:
      v_list.append('s')
      score += s

for i in range(n):
  if i >= k:
    tmp = v_list[i-k]

  judge(char_list[i])

print(score)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
r, s, p = map(int, input().split(' '))
t = input()
rsp = {'r': ('p', p), 's': ('r', r), 'p': ('s', s)}
total = 0
co = 0
last = []
for s in t:
    pt, score = rsp[s]
    l = pt
    if co - k >= 0 and last[co - k] == pt:
        score = 0
        l = 'r' if pt != 'r' else 's' if  pt != 's' else 'p'
    last.append(l)
    total += score
    co += 1
print(total)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
num = N//K
result = 0
L = []
chagelist = {"r": "s", "s": "p", "p": "r"}


def change(s, index):
    result = ""
    for i in range(len(s)):
        if i in index:
            result += chagelist[s[i]]
        else:
            result += s[i]
    return result


for i in range(0, num):
    L.append(T[i*K:(i+1)*K])
point = {"r": P, "s": R, "p": S}

for i in L[0]:
    result += point[i]
for i in range(1, num):
    miss = []
    for j in range(len(L[i])):
        if L[i-1][j] == L[i][j]:
            if i + 1 < num and L[i+1][j] == L[i][j]:
                miss.append(j)
        else:
            result += point[L[i][j]]
    L[i] = change(L[i], miss)
print(result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
num = N//K
result = 0
L = []
chagelist = {"r": "s", "s": "p", "p": "r"}


def change(s, index):
    result = ""
    for i in range(len(s)):
        if i in index:
            result += chagelist[s[i]]
        else:
            result += s[i]
    return result


for i in range(0, num):
    L.append(T[i*K:(i+1)*K])
point = {"r": P, "s": R, "p": S}

for i in L[0]:
    result += point[i]
for i in range(1, num):
    miss = []
    for j in range(len(L[i])):
        if L[i-1][j] == L[i][j]:
            miss.append(j)
        else:
            result += point[L[i][j]]
    L[i] = change(L[i], miss)
print(result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
switch = 0
result = []

while switch <= 1:
    T_list = list(T)
    if switch == 1:
        T_list.reverse()
    length = len(T_list)
    point_list = []
    for i in range(length):
        if T_list[i] == "r":
            point_list.append(P)
        elif T_list[i] == "s":
            point_list.append(R)
        else:
            point_list.append(S)
        
    for i in range(K, length-K):
        val = []
        if T_list[i] == T_list[i-K]:
            point_list[i] = 0
            val.append(T_list[i-K])
            val.append(T_list[i+K])
            if val.count("r") == 0:
                T_list[i] = "r"
            elif val.count("s") == 0:
                T_list[i] = "s"
            else:
                T_list[i] = "p"
            
    for i in range(length-K, length):
        val = []
        if T_list[i] == T_list[i-K]:
            point_list[i] = 0
            val.append(T_list[i-K])
            if val.count("r") == 0:
                T_list[i] = "r"
            elif val.count("s") == 0:
                T_list[i] = "s"
            else:
                T_list[i] = "p"
    result.append(sum(point_list))
    switch += 1

print(max(result))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
T_list = list(T)
length = len(T_list)
point_list = []
for i in range(length):
    if T_list[i] == "r":
        point_list.append(P)
    elif T_list[i] == "s":
        point_list.append(R)
    else:
        point_list.append(S)
    
for i in range(K, length-K):
    val = []
    if T_list[i] == T_list[i-K]:
        point_list[i] = 0
        val.append(T_list[i-K])
        val.append(T_list[i+K])
        if val.count("r") == 0:
            T_list[i] = "r"
        elif val.count("s") == 0:
            T_list[i] = "s"
        else:
            T_list[i] = "p"
        
for i in range(length-K, length):
    val = []
    if T_list[i] == T_list[i-K]:
        point_list[i] = 0
        val.append(T_list[i-K])
        if val.count("r") == 0:
            T_list[i] = "r"
        elif val.count("s") == 0:
            T_list[i] = "s"
        else:
            T_list[i] = "p"

print(sum(point_list))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
ans = 0
count = 0
for i in range(0,len(T)):
    if T[i] == "r" :
        if i - K >= 0 and T[i] == T[i - K] and i - K != count:
            ans += 0
            count = i
        else:
            ans += P
    elif T[i] == "s":
        if i - K >= 0 and T[i] == T[i - K] and i - K != count:
            ans += 0
            count = i
        else:
            ans += R
    else:
        if i - K >= 0 and T[i] == T[i - K] and i - K != count:
            ans += 0
            count = i
        else:
            ans += S
print(ans)

Submission status: Wrong Answer

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

subGames = [[] for _ in range(k)]

pattern = {'r': p, 's': r, 'p': s, '': 0}
win = {'r': 'p', 's': 'r', 'p': 's'}

subPts = [0 for _ in range(k)]
for i, hand in enumerate(t):
    subGame = subGames[i % k]
    winPt = pattern[hand]
    winHand = win[hand]
    if subGame and win[hand] == subGame[-1]:
        winPt = 0
        winHand = hand

    subGame.append(winHand)
    subPts[i % k] += winPt

ans = sum(subPts)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=input()
dp=[[None]*k for _ in range(n//k+1)]
for i in range(k):
    if t[i]=='r':
        dp[0][i]=p
    elif t[i]=='s':
        dp[0][i]=r
    elif t[i]=='p':
        dp[0][i]=s
for i in range(1, n//k+1):
    for j in range(k):
        if i*k+j<n:
            if t[i*k+j]=='r':
                if dp[i-1][j]==p:
                    dp[i][j]=0
                else:dp[i][j]=p
            elif t[i*k+j]=='s':
                if dp[i-1][j]==r:
                    dp[i][j]=0
                else:dp[i][j]=r
            elif t[i*k+j]=='p':
                if dp[i-1][j]==s:
                    dp[i][j]=0
                else:dp[i][j]=s
        else:dp[i][j]=0
poi=0
for i in range(len(dp)):
    poi+=sum(dp[i])
print(poi)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=input()
dp=[[None]*k for _ in range(n//k+1)]
for i in range(k):
    if t[i]=='r':
        dp[0][i]=p
    elif t[i]=='s':
        dp[0][i]=r
    elif t[i]=='p':
        dp[0][i]=s
for i in range(1, n//k+1):
    for j in range(k):
        if i*k+j<n:
            if t[i*k+j]=='r':
                if dp[i-1][j]==p:
                    dp[i][j]=0
                else:dp[i][j]=p
            elif t[i*k+j]=='s':
                if dp[i-1][j]==r:
                    dp[i][j]=0
                else:dp[i][j]=r
            elif t[i*k+j]=='p':
                if dp[i-1][j]==s:
                    dp[i][j]=0
                else:dp[i][j]=s
for i in range(len(dp)):
    for j in range(len(dp[0])):
        if dp[i][j]==None:
            dp[i][j]=0
poi=0
for i in range(len(dp)):
    poi+=sum(dp[i])
print(poi)

Submission status: Wrong Answer

Solution code:

N, K = [int(x) for x in input().split()]
R, S, P = [int(x) for x in input().split()]
T = input()

T = list(T)
OUT = T.copy()
score = T.copy()
TF = [False for x in range(len(OUT))]
for i in range(len(OUT)):
    # K回目まで(1<=i<=K)
    if i < K:
        if T[i] == "r":
            OUT[i] = "p"
            score[i] = P
        elif T[i] == "s":
            OUT[i] = "r"
            score[i] = R
        else:
            OUT[i] = "s"
            score[i] = S
        TF[i] = True

    else:
        if T[i] == "r":
            if OUT[i - K] == "p":
                pass
            else:
                OUT[i] = "p"
                score[i] = P
                TF[i] = True
        elif T[i] == "s":
            if OUT[i - K] == "r":
                pass
            else:
                OUT[i] = "r"
                score[i] = R
                TF[i] = True
        else:
            if OUT[i - K] == "s":
                pass
            else:
                OUT[i] = "s"
                score[i] = S
                TF[i] = True

                
#OUT = [OUT[x] for x in range(len(TF)) if TF[x]]
score = [score[x] for x in range(len(TF)) if TF[x]]
print(sum(score))

Submission status: Wrong Answer

Solution code:
N = 30
K = 5
R = 325 
S = 234 
P = 123
T = "rspsspspsrpspsppprpsprpssprpsr"

if False:
    N = 5
    K = 2
    R = 8
    S = 7 
    P = 6
    T = "rsrpr"


N = 7
K = 1
R = 100
S = 10
P = 1
T = "ssssppr"


T = list(T)
OUT = T.copy()
score = T.copy()
TF = [False for x in range(len(OUT))]
for i in range(len(OUT)):
    # K回目まで(1<=i<=K)
    if i < K:
        if T[i] == "r":
            OUT[i] = "p"
            score[i] = P
        elif T[i] == "s":
            OUT[i] = "r"
            score[i] = R
        else:
            OUT[i] = "s"
            score[i] = S
        TF[i] = True

    else:
        if T[i] == "r":
            if OUT[i - K] == "p":
                pass
            else:
                OUT[i] = "p"
                score[i] = P
                TF[i] = True
        elif T[i] == "s":
            if OUT[i - K] == "r":
                pass
            else:
                OUT[i] = "r"
                score[i] = R
                TF[i] = True
        else:
            if OUT[i - K] == "s":
                pass
            else:
                OUT[i] = "s"
                score[i] = S
                TF[i] = True

                
#OUT = [OUT[x] for x in range(len(TF)) if TF[x]]
score = [score[x] for x in range(len(TF)) if TF[x]]
print(sum(score))

Submission status: Wrong Answer

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

D = DD(int)
D["s"] = R
D["p"] = S
D["r"] = P

ans = 0
C = DD(int)
for i in range(N):
  C["s"] = C["s"]-1
  C["p"] = C["p"]-1
  C["r"] = C["r"]-1
  if C[T[i]] != 0:
    ans +=  D[T[i]]
    if C[T[i]] < 0 : C[T[i]] = K
print (ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
S = input()
ans = 0
hand = ""
dic = {"r":"p","p":"s","s":"r"}
for i in range(n):
    if i < k:
        if S[i] == "r":
            ans += p
            hand += "p"
        elif S[i] == "p":
            ans += s
            hand += "s"
        else:
            ans += r
            hand += "r"
   
    else:
        if dic[S[i]] == hand[i-k]:
            hand += S[i]
            continue
        else:
            if S[i] == "r":
                ans += p
                hand += "p"
            elif S[i] == "p":
                ans += s
                hand += "s"
            else:
                ans += r
                hand += "r"
        
print(ans)

Submission status: Wrong Answer

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

dp = [None for _ in range(n)]

for play in range(n):
    c_play = t[play] 

    # rock
    if c_play == 's':
        dp[play] = 'r'
    # paper
    elif c_play == 'r':
        dp[play] = 'p'

    # scissor
    elif c_play == 'p':
        dp[play] = 's'

for play in range(k, n-1):
    if dp[play] == dp[play-k]:
        dp[play] = None


ans = 0
for play in dp:
    if play is not None:
        if play == 'r':
            ans += r
        elif play == 'p':
            ans += p
        elif play == 's':
            ans += s

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# N 回じゃんけんを行う,  K 回前のじゃんけんで出した手と同じ手を出すことはできない
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

# mod_N_list = [[] for i in range(K)]
# T_list = [[] for i in range(K)]
T_list = []
# N_list = list(range(N))

ans = 0

for i in range(N):
    if T[i] == 'r':
        # 前回がpでは無かった時
        if i < K or T_list[i - K] != 'p':
            ans += P
            T_list.append('p')

        # 前回がpだった時
        elif i != N-1:
            # 次がr, またはpの時は最善手はr
            if T[i + 1] == 'r' or T[i + 1] == 'p':
                T_list.append('r')
            # 次がsの時は最善手はs
            else:
                T_list.append('s')

    elif T[i] == 's':
        # 前回がrでは無かった時
        if i < K or T_list[i - K] != 'r':
            ans += R
            T_list.append('r')

        # 前回がrだった時
        elif i != N-1:
            # 次がp, またはrの時は最善手はs
            if T[i + 1] == 'p' or T[i + 1] == 'r':
                T_list.append('s')
            # 次がsの時は最善手はr
            else:
                T_list.append('p')

    elif T[i] == 'p':
        # 前回がsでは無かった時
        if i < K or T_list[i - K] != 's':
            ans += S
            T_list.append('s')

        # 前回がsだった時
        elif i != N-1:
            # 次がs, またはpの時は最善手はp
            if T[i + 1] == 's' or T[i + 1] == 'p':
                T_list.append('p')
            # 次がrの時は最善手はr
            else:
                T_list.append('r')

print(ans)

Submission status: Wrong Answer

Solution code:
# N 回じゃんけんを行う,  K 回前のじゃんけんで出した手と同じ手を出すことはできない
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

# mod_N_list = [[] for i in range(K)]
# T_list = [[] for i in range(K)]
T_list = []
# N_list = list(range(N))

ans = 0

for i in range(N):
    if T[i] == 'r':
        # 前回がpでは無かった時
        if i < K or T_list[i - K] != 'p':
            ans += P
            T_list.append('p')

        # 前回がpだった時
        elif i != N-1:
            # 次がr, またはpの時は最善手はr
            if T[i + 1] == 'r' or T[i + 1] == 'p':
                T_list.append('r')
            # 次がsの時は最善手はs
            else:
                T_list.append('s')

    elif T[i] == 's':
        # 前回がrでは無かった時
        if i < K or T_list[i - K] != 'r':
            ans += R
            T_list.append('r')

        # 前回がrだった時
        elif i != N-1:
            # 次がp, またはrの時は最善手はs
            if T[i + 1] == 'p' or T[i + 1] == 'r':
                T_list.append('s')
            # 次がsの時は最善手はr
            else:
                T_list.append('p')

    elif T[i] == 'p':
        # 前回がsでは無かった時
        if i < K or T_list[i - K] != 's':
            ans += S
            T_list.append('s')

        # 前回がsだった時
        elif i != N-1:
            # 次がs, またはpの時は最善手はp
            if T[i + 1] == 's' or T[i + 1] == 'p':
                T_list.append('p')
            # 次がrの時は最善手はr
            else:
                T_list.append('r')

Submission status: Wrong Answer

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
n=len(T)
for i in range(n):
  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=T[:i-K]+"o"+T[i-K+1:]
print(c)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
# N 回じゃんけんを行う,  K 回前のじゃんけんで出した手と同じ手を出すことはできない
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

mod_N_list = [[] for i in range(K)]
T_list = [[] for i in range(K)]


ans = 0
for j in range(K):
    mod_N_list[j] = [i for i in range(N) if (i % K == j)]

    for k in range(len(mod_N_list[j])):

        if T[mod_N_list[j][k]] == 'r':
            # 前回がpでは無かった時
            if k == 0 or T_list[j][k - 1] != 'p':
                ans += P
                T_list[j].append('p')
            # 前回がpだった時
            elif k != len(mod_N_list[j]) - 1:
                # 次がr, またはpの時は最善手はr
                if T[mod_N_list[j][k + 1]] == 'r' or T[mod_N_list[j][k + 1]] == 'p':
                    T_list[j].append('r')

                # 次がsの時は最善手はs
                else:
                    T_list[j].append('s')

        elif T[mod_N_list[j][k]] == 's':
            # 前回がrでは無かった時
            if k == 0 or T_list[j][k - 1] != 'r':
                ans += R
                T_list[j].append('r')
            # 前回がrだった時
            elif k != len(mod_N_list[j]) - 1:
                # 次がp, またはrの時は最善手はs
                if T[mod_N_list[j][k + 1]] == 'p' or T[mod_N_list[j][k + 1]] == 'r':
                    T_list[j].append('s')

                # 次がrの時は最善手はr
                else:
                    T_list[j].append('p')

        elif T[mod_N_list[j][k]] == 'p':
            # 前回がsでは無かった時
            if k == 0 or T_list[j][k - 1] != 's':
                ans += S
                T_list[j].append('s')
            # 前回がsだった時
            elif k != len(mod_N_list[j]) - 1:
                # 次がs, またはpの時は最善手はp
                if T[mod_N_list[j][k + 1]] == 's' or T[mod_N_list[j][k + 1]] == 'p':
                    T_list[j].append('p')

                # 次がrの時は最善手はr
                else:
                    T_list[j].append('r')

print(ans)














'''
import copy
 
# N 回じゃんけんを行う,  K 回前のじゃんけんで出した手と同じ手を出すことはできない
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()


takahashi_T_List = [''] * N
is_win_list = [False] * N
score_list = []

R_S_P = [['r', R], ['s', S], ['p', P]]

# じゃんけんに勝ったかどうか判定する関数
def is_win(pro, opp):
    if (pro == 'r' and opp == 's') or (pro == 's' and opp == 'p') or (pro == 'p' and opp == 'r'):
        return True
    else:
        return False


def dfs(i, takahashi_T_List, is_win_list, ans_takahashi_T_List):
    # N回目まで決め終わった場合
    if i >= N:
        score = 0
        
        if (sorted(takahashi_T_List) in ans_takahashi_T_List) == False:

            # j回目のじゃんけんについて考慮する
            for j in range(N):

                # j回目に出した手によって得点を加算していく
                if is_win_list[j] == False:
                    continue
                for k in range(3):
                    if takahashi_T_List[j] == R_S_P[k][0]:
                        score += R_S_P[k][1]

            score_list.append(score)
            ans_takahashi_T_List.append(sorted(copy.copy(takahashi_T_List)))
        return


    prohibited_T = ''
    if i >= K:
        prohibited_T = takahashi_T_List[i - K]

    # それぞれ出す手を決定する
    for l in range(3):
        if prohibited_T != R_S_P[l][0]:
            is_win_list[i] = is_win(R_S_P[l][0], T[i])
            takahashi_T_List[i] = R_S_P[l][0]
            dfs(i + 1, takahashi_T_List, is_win_list, ans_takahashi_T_List)

dfs(0, takahashi_T_List, is_win_list, [])
print(max(score_list))
'''

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split() ]
R, S, P = [int(_) for _ in input().split() ]
T = input()

#pri = max(R,S,P)
#last = min(R,S,P)
#sec = 

hands = {'rock':R, 'scis':S, 'paper':P}

myhand = []

for x in T:
  if x == 'r':
    myhand.append('p')
  elif x == 's':
    myhand.append('r')
  else:
    myhand.append('s')
    
r_minus_counter = 0
s_minus_counter = 0
p_minus_counter = 0
    
for i in range(0, N-K): #index
  if myhand[i] == myhand[i+K]:
    if myhand[i] == 'r':
      s_minus_counter += 1
      myhand[i+K] = 's'
    elif myhand[i] == 's':
      p_minus_counter += 1
      myhand[i+K] = 'p'
    elif myhand[i] == 'p':
      r_minus_counter += 1
      myhand[i+K] = 'r'
      
score = R*(myhand.count('r')-r_minus_counter) + S*(myhand.count('s')-s_minus_counter) + P*(myhand.count('p')-p_minus_counter)

print(score)