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:
def main():
  N, K = map(int, input().split())
  R, S, P = map(int, input().split())
  T = input()

  set_rsp = set(['r', 's', 'p'])

  def vict_output(c):
    if c == 'r':
        output = 'p'
        point = P
    elif c == 's':
        output = 'r'
        point = R
    else:
        output = 's'
        point = S
    return output, point

  history = []
  score = 0
  for i in range(len(T)):
      # print('i: {}, T[i]: {}, pre_K_output: {}'.format(i, T[i], history[i-K:i-K+1]))
      # print('history: {}'.format(history))
      output, point = vict_output(T[i])
      if i >= K:
          # print('if1')
          pre_K_output = history[i - K]
          # K手前と等しかった場合
          if output == pre_K_output:
              # print('if2')
              point = 0
              # K手先に有利になるように見る
              if (i + K) <= (len(T) - 1):
                  # print('if3')
                  output, _ = vict_output(T[i + K])
                  if output == pre_K_output:
                        # print('if4')
                        # pre_K_input, _ = vict_output(pre_K_output)
                        # pre_K_input, _ = vict_output(pre_K_input)
                        # rest = list(set_rsp - set(pre_K_input))
                        # o1, p1 = vict_output(rest[0])
                        # o2, p2 = vict_output(rest[1])
                        o1, p1 = vict_output(pre_K_output)
                        o2, p2 = vict_output(o1)
                        # print('rest: {}'.format(rest))
                        # print('o1:{}, o2:{}'.format(o1, o2))
                        if (p1 > p2):
                            output = o1
                            # point = p1
                        else:
                            output = o2
                            # point = p2
                  else:
                        output, _ = vict_output(output)
                        output, _ = vict_output(output)
              else:
                  # この場合K手前の手以外ならなんでも良い
                  output, _ = vict_output(list(set_rsp - set(pre_K_output))[0])
                  # K手前以外のの手で最も得点が大きい手を選ぶ

      # print('output: {}, point: {}'.format(output, point))
      score += point
      history.append(output)

  # print(history)
  print(score)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

row=[]
for i in range(K):
  row.append(T[i::K])
  
score=0
for r in row:
  pre="n"
  for i, hand in enumerate(r):
    if hand==pre:
      pre=0
    else:
      if hand=="r":
        score+=P
        pre="r"
      elif hand=="s":
        score+=R
        pre="s"
      else:
        score+=S
        pre="p"
        
print(score)

Submission status: Accepted

Solution code:
import sys
import math


N,K=map(int,input().split())
r,s,p=map(int,input().split()) 
t=input()
ans=0
for i in range(K):
    j=i
    tmp=[0,0,0]
    while j<N:
        if t[j]=='r':
            tmp=[max(tmp[1],tmp[2]),max(tmp[0],tmp[2]),max(tmp[0],tmp[1])+p]
        elif t[j]=='s':
            tmp=[max(tmp[1],tmp[2])+r,max(tmp[0],tmp[2]),max(tmp[0],tmp[1])]
        else:
            tmp=[max(tmp[1],tmp[2]),max(tmp[0],tmp[2])+s,max(tmp[0],tmp[1])]
        j+=K
        
    ans+=max(tmp)

print(ans)

Submission status: Accepted

Solution code:
def winList(t, r, s, p):
    if t == "r":
        return [0, 0, p]
    elif t == "s":
        return [r, 0, 0]
    else:
        return [0, s, 0]

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

dp = []
for i in range(N):
    wList = winList(T[i], R, S, P)
    if i <= K-1:
        if i == 0:
            dp.append(wList)
        else:
            dp.append([wList[0] + max(dp[i - 1]),
                       wList[1] + max(dp[i - 1]),
                       wList[2] + max(dp[i - 1])])
    else:
        wKList = winList(T[i - K], R, S, P)
        tmpR = 0
        tmpS = 0
        tmpP = 0
        if dp[i - K][0] > dp[i - K][1] and dp[i - K][0] > dp[i - K][2]:
            tmpR = wList[0] + max(dp[i - 1]) - wKList[0]
        else:
            tmpR = wList[0] + max(dp[i - 1])

        if dp[i - K][1] > dp[i - K][0] and dp[i - K][1] > dp[i - K][2]:
            tmpS = wList[1] + max(dp[i - 1]) - wKList[1]
        else:
            tmpS = wList[1] + max(dp[i - 1])

        if dp[i - K][2] > dp[i - K][0] and dp[i - K][2] > dp[i - K][1]:
            tmpP = wList[2] + max(dp[i - 1]) - wKList[2]
        else:
            tmpP = wList[2] + max(dp[i - 1])
        dp.append([tmpR, tmpS, tmpP])

print(max(dp[N-1]))

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):
    if t[i]=="r":
        ans+=p
    elif t[i]=="s":
        ans+=r
    else:
        ans+=s
for i in range(n-k):
    if t[i+k]==t[i]:
        t=t[:i+k]+"n"+t[i+k+1:]
    else:
        if t[i+k]=="r":
            ans+=p
        elif t[i+k]=="s":
            ans+=r
        else:
            ans+=s

print(ans)

Submission status: Accepted

Solution code:
import sys
from pprint import pprint
import math

def solve(N, k, r, s, p, t):
    score = [r, s, p]
    ans = 0
    for i in range(k):
        new_t = []
        # print("i", i)
        # print(math.ceil(N / k))
        # print(N // k + 1)
        for j in range(math.ceil(N / k)):
            # print(j * k + i)
            if j * k + i >= len(t):
                break
            v = t[j * k + i]
            if v == "r":
                v_tmp = 0
            elif v == "s":
                v_tmp = 1
            else:
                v_tmp = 2
            new_t.append(v_tmp)
        # debug
        # pprint(new_t)
        len_nt = len(new_t)
        # dp_i = [0] * 3
        dp = []
        for _ in range(len_nt + 1):
            dp.append([0] * 3)
        for l in range(len_nt):
            for m in range(3):
                # 前回の手がmの場合
                for n in range(3):
                    # 今回の手がnの場合
                    if m == n:
                        continue

                    # print(m, n)
                    # 勝つ場合
                    # print(new_t[l], n)
                    if new_t[l] - n == 1 or n - new_t[l] == 2:
                        # print("win", score[n])
                        dp[l+1][n] = max(dp[l+1][n], dp[l][m] + score[n])
                    # それ以外の場合
                    else:
                        dp[l+1][n] = max(dp[l+1][n], dp[l][m])
        # print("score", i, max(dp[len_nt]))
        # pprint(dp)
        ans += max(dp[len_nt])
    # pprint(dp)
    print(ans)  

if __name__ == '__main__':
    n, k = map(int, sys.stdin.readline().strip().split(" "))
    r, s, p = map(int, sys.stdin.readline().strip().split(" "))
    t = list(sys.stdin.readline().strip())
    # debug
    # pprint(t)
    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()

ans = 0

def janken(r: int, s: int, p: int, i: str):
    if i == 'p':
        return s, 's'
    elif i == 'r':
        return p, 'p'
    elif i == 's':
        return r, 'r'

for i in range(K):
    sans = janken(R, S, P, T[i])
    tmp = sans[1]
    ans += sans[0]
    #print(ans)
    
    for j in range(i + K, N, K):
        # print(i, j)
        sans = janken(R, S, P, T[j])
        if tmp != sans[1]:
            ans += sans[0]
            tmp = sans[1]
        else:
            tmp = ''
        #print(ans)
        
print(ans)

Submission status: Accepted

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

TKlist=[]
for i in range(K):
  TKlist.append([])  

for i in range(N):
  TKlist[i%K].append(T[i])
#print(TKlist)

answer=0
for i in range(K):
  lastChar="#"
  for j in range(len(TKlist[i])):
    if TKlist[i][j]!=lastChar:
      lastChar=TKlist[i][j]
      if TKlist[i][j]=='r':        
        answer+=P
      elif TKlist[i][j]=='s':        
        answer+=R
      elif TKlist[i][j]=='p':        
        answer+=S
    else:
      lastChar="#"
      answer+=0

print(answer)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
W = [2]*N
list =(R,S,P)
list_2 = sorted(list)
count = 0
for i in range (0,3):
	if list_2[2-i] == P:
		for j in range (0,N):
			if T[j] == 'r':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 'r' :
							W[j+K] = 0
	if list_2[2-i]== R:
		for j in range (0,N):
			if T[j] == 's':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 's' :
							W[j+K] = 0
	if list_2[2-i]== S:
		for j in range (0,N):
			if T[j] == 'p':
				if W[j] == 2:
					W[j] = 1
					if j+K<N:
						if T[j+K] == 'p' :
							W[j+K] = 0

for i in range (N):
	if T[i] == 'r':
		count += P*W[i]
	elif  T[i] == 's':
		count += R*W[i]
	elif  T[i] == 'p':
		count += S*W[i]

print(count)

Submission status: Accepted

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

log = list()
ans = 0
for i in range(n):
  if i > k-1:
    if t[i] == log[i-k]:
      ans += 0
      log.append("null")
    else:
      log.append(t[i])
      if t[i] == "r":
        ans += p
      if t[i] == "s":
        ans += r
      if t[i] == "p":
        ans += s  
  else:
    log.append(t[i])
    if t[i] == "r":
      ans += p
    if t[i] == "s":
      ans += r
    if t[i] == "p":
      ans += s  
print(ans)

Submission status: Accepted

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

hand = {
    "s": R,
    "p": S,
    "r": P,
}

hands = sorted(
    hand.items(), key=lambda x: x[1],
    reverse=True
)
r = np.zeros(n)
ans = 0
for j in range(3):
    a = hands[j][0]
    b = hands[j][1]
    for i in range(n):
        if t[i] == a:
            if t[i-k] == a and i+1 > k and r[i-k] == 1:
                continue
            else:
                ans += b
                r[i] = 1
        else:
            continue
print(ans)

Submission status: Accepted

Solution code:
# K回前と現在の問いが同じだったら0点

str1 = input().split(" ")
str2 = input().split(" ")
t = list(input())

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

result = 0
hoge = 0
for i in range(0,n):
    if i < k:
        continue

    j = i-k
    if t[i] == t[j]:
        t[i] = "_"

pres = p * t.count("r")
rres = r * t.count("s")
sres = s * t.count("p")

print(pres + rres + sres)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**5)
win_hand = {k:v for k, v in zip("r s p".split(), "p r s".split())}


def solve(pt_dict, pred, cur, prev, memo):
  if cur == len(pred):
    return 0
  elif (cur, prev) in memo:
    return memo[(cur, prev)]
  else:
    tmp = 0
    for hand in "r s p".split():
      if prev == hand:
        continue
      else:
        pt = 0
        if hand == win_hand[pred[cur]]:
          pt = pt_dict[hand]
        tmp = max(tmp, pt+solve(pt_dict, pred, cur+1, hand, memo))
    memo[(cur, prev)] = tmp
    return tmp
    
N, K = map(int, input().split())
R, S, P = map(int, input().split())
pt_dict = {}
pt_dict["r"] = R
pt_dict["s"] = S
pt_dict["p"] = P
T = list(input())
k = 0
ans = 0
while k < K:
  ans += solve(pt_dict, T[k::K], 0, "", {})
  k += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    rsp = list(map(int, input().split()))
    ts = list(input())

    points = [0] * n
    for i, t in enumerate(ts):
        prev_idx = i - k
        if prev_idx >= 0:
            prev_t = ts[prev_idx]
            if prev_t == t:
                if points[prev_idx] == t:
                    pass
                else:
                    points[i] = t
                    continue
        if t == 'r':
            point = rsp[2]
        elif t == 's':
            point = rsp[0]
        else:
            point = rsp[1]
        points[i] = point

    ans = 0
    for p in points:
        if type(p) == int:
            ans += p
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P  =  map(int,input().split())
t = input()
s = []
for i in range(N):
    s.append(t[i])
ans = 0
for i in range(K):
    if s[i] == 'r':
        ans += P
        s[i] = 'a'
    elif s[i] == 's':
        ans += R
        s[i] = 'b'
    else:
        ans += S
        s[i] = 'c'
T = []
for i in range(K,N):
    if s[i] == 'r':
        if s[i-K] != 'a':
            ans += P
            s[i] = 'a'
    elif s[i] == 's':
        if s[i-K] != 'b':
            ans += R
            s[i] = 'b'
    else:
        if s[i-K] != 'c':
            ans += S
            s[i] = 'c'
print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int,input().split()))
r, s, p = list(map(int,input().split()))
t = list(input())
i = 0
sum = 0
while i < n:
  if i < k:
    if t[i] == 'r':
      sum = sum + p
    elif t[i] == 's':
      sum = sum + r
    else:
      sum = sum + s
  else:
    if t[i] != t[i-k]:
      if t[i] == 'r':
        sum = sum + p
      elif t[i] == 's':
        sum = sum + r
      else:
        sum = sum + s 
    else:
      t[i] = 'U'
  i = i + 1

print(sum)

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
N, K = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7
R,S,P = map(int, input().split())		# 5 7 2
T = input()

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

X=[""]*K
for i in range(K):
	X[i]=T[i::K]

p(X)

def max_score(x):
	a=0
	c=''
	A=x
	for s in A:
		if c==s:
			c=''
		else:
			a+=score(s)
			c=s

	#B=x[1:]+x[0]
	#b=0
	#c=''
	#for s in B:
	#	if c==s:
	#		c=''
	#	else:
	#		b+=score(s)
	#		c=s
	
	#p("A,a=", A,a)
	#p("B,b=", B,b)
	#return max(a,b)
	return a
	
	

ANS=[0]*K
for i,x in enumerate(X):
	ANS[i] = max_score(x)

ans=sum(ANS)
print(ans)

Submission status: Accepted

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

count=0
m=n//k

for i in range(k):
    flag=False
    for j in range(m+1):
        if j==0:
            if t[i] == 'r':
                count += P
            elif t[i] == 's':
                count += R
            else:
                count += S
        else:
            if i+k*j>=n:
                break

            if t[i+k*j]==t[i+k*(j-1)]:
                if flag==False:
                    flag=True
                    continue
                else:
                    flag=False
                    if t[i+k*j] == 'r':
                        count += P
                    elif t[i+k*j] == 's':
                        count += R
                    else:
                        count += S

            else:
                flag=False
                if t[i+k*j] == 'r':
                    count += P
                elif t[i+k*j] == 's':
                    count += R
                else:
                    count += S




print(count)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())
data=[[] for i in range(K)]
for i in range(N):
    zzz=i%K
    u=T[i]
    data[zzz].append(u)
ans=0
for h in data:
    l=len(h)
    dp=[[0,0,0] for i in range(l)]
    u=h[0]
    if u=="r":
        dp[0][2]=P
    elif u=="s":
        dp[0][0]=R
    else:
        dp[0][1]=S
    for i in range(1,l):
        u=h[i]
        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][1],dp[i-1][0])
        if u=="r":
            dp[i][2]+=P
        elif u=="s":
            dp[i][0]+=R
        else:
            dp[i][1]+=S
    ans+=max(dp[-1])
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
kr = 1
ks= 1
kp = 1
Is = 0
Ir=0
Ip=0
A =[]

for i in range(n):
  if i-k < 0:
      A.append(1)
      if t[i] == "r":
        kr = 1
        ans += p
      elif t[i] == "s":
        ks=1
        ans += r
      else:
        kp=1
        ans += s
  else:
    if t[i] != t[i-k]:
      if t[i] == "r":
        kr=1
        ans += p
        Ir = i
        A[i%k]=1
      elif t[i] == "s":
        ans += r
        ks=1
        Is = i
        A[i%k]=1
      else:
        kp=1
        ans += s
        A[i%k]=1
    else:
      if t[i] == "r":
        if A[i%k] == 0:
          ans+=p
          A[i%k] = 1
        else:
            A[i%k]  = 0
      elif t[i] == "s":
        if A[i%k] ==0:
          ans += r
          A[i%k] = 1
        else:
            A[i%k] = 0
      else:
        if A[i%k]==0:
          ans+=s
          kp=1
          Ip = i
          A[i%k] = 1
        else: 
            A[i%k] = 0
      
print(ans)

Submission status: Accepted

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

batch = [T[i::K] for i in range(K)]

ans = 0
for b in batch:
    tmp = 0
    for p in 'rsp':
        t = 0
        before = p
        if p == win[b[0]]: t += points[win[b[0]]]
        for i in range(1,len(b)):
            if win[b[i]] != before:
                t += points[win[b[i]]]
                before = win[b[i]]
            else:
                if i+1 < len(b):
                    before = b[i+1]
        if t > tmp: tmp = t
    ans += tmp
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
ans = t.count("r") * p + t.count("s") * r + t.count("p") * s
R,S,P = 0,0,0
ok = [0] * (n+1)
for key,(i,j) in enumerate(zip(t[:-k],t[k:])):
  if i == j:
    if i == "r":
      if ok[key] == 0:
        ans -= p
        ok[key+k] = 1
    elif i == "s":
      if ok[key] == 0:
        ans -= r
        ok[key+k] = 1
    else:
      if ok[key] == 0:
        ans -= s
        ok[key+k] = 1
print(ans)

Submission status: Accepted

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

ts=[t[i:len(t):k] for i in range(k)]
def score_str(l):
  res=0
  stt=0
  for i,c1,c2 in zip(range(len(l)),l,l[1:]+' '):
    if c1!=c2:
      res+=scores[c1]*((i-stt+2)//2)
      stt=i+1
  return res
print(sum(score_str(l) for l in ts))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
d = {'p': S, 's': R, 'r': P}
flag = [0] * K
for n in range(N):
    q = n % K
    ans += d.get(T[n])
    if n >= K:
        if T[n] == T[n - K] and n < N - K:
            flag[q] += 1
        elif (flag[q] != 0 and T[n] != T[n - K]) or n >= N - K:
            if n >= N - K and T[n] == T[n - K]:
                flag[q] += 1
            ans -= d.get(T[n - K]) * ((flag[q] + 1) // 2)
            flag[q] = 0
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
ans=0
for i in range(len(t)):
    #print(t[i])
    if t[i]=="r":
        ans+=p
        if i+k<len(t):
            if t[i+k]=="r":
                t[i+k]=""
    if t[i]=="s":
        ans+=r
        if i+k<len(t):
            if t[i+k]=="s":
                t[i+k]=""
    if t[i]=="p":
        ans+=s
        if i+k<len(t):
            if t[i+k]=="p":
                t[i+k]=""
print(ans)

Submission status: Accepted

Solution code:

n,k = (int(_) for _ in input().split())
R,S,P = (int(_) for _ in input().split())
t = list(input())
a ={"r":P,"s":R,"p":S,"f":0}
Q = n//k
q = n%k
ans = 0
for i in range(n):
	ans+=a[t[i]]

for i in range(k,n):
	if t[i]==t[i-k]:
		ans-=a[t[i-k]]
		if t[i-k] is not "f":
			t[i]="f"
print(ans)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
c,d,e=map(int,input().split())
g=input()
l = a-b
for i in range(l):
   if g[i] == g[i+b]:
      g = g[:i+b] + 'a' + g[i+1+b:]
      
else:
  R = g.count('r')
  S = g.count('s')
  P = g.count('p')
  answer = c*S+d*P+e*R
  print(answer)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
u = []
a = 0
for i in range(n):
    if i <= k-1:
        if t[i] == "r":
            u.append("p")
            a += p
        elif t[i] == "s":
            u.append("r")
            a += r
        else:
            u.append("s")
            a += s
    else:
        if t[i] == "r" and u[i-k] != "p":
            u.append("p")
            a += p
        elif t[i] == "s" and u[i-k] != "r":
            u.append("r")
            a += r
        elif t[i] == "p" and u[i-k] != "s":
            u.append("s")
            a += s
        elif t[i] == "r" and u[i-k] == "p":
                u.append("_p")
        elif t[i] == "s" and u[i-k] == "r":
            u.append("_r")
        elif t[i] == "p" and u[i-k] == "s":
            u.append("_s")
print(a)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
sco=[0]*n
ans=0

def sc(S):
  if S=='r':
    return p
  elif S=='s':
    return r
  else:
    return s

def bchk(i):
  score=0
  stnum=int(i//k)
  sta=int(i%k)
  if stnum==0:
    return sc(t[i])
  else:
    score=bnum=sc(t[sta])
    for j in range(1,stnum+1):
      if t[sta+j*k]==t[sta+(j-1)*k]:
        if bnum==0:
          bnum=sc(t[sta+j*k])
          score+=bnum
        else:
          bnum=0
          continue
      else:
        bnum=sc(t[sta+j*k])
        score+=bnum
    return score

def achk(i):
  score=0
  stnum=int(i//k)
  if stnum==0:
    return sc(t[i])
  else:
    score=bnum=sc(t[i])
    for j in range(1,stnum+1):
      if t[i-j*k]==t[i-(j-1)*k]:
        if bnum==0:
          bnum=sc(t[i-j*k])
          score+=bnum
        else:
          bnum=0
          continue
      else:
        bnum=sc(t[i-j*k])
        score+=bnum
    return score

for i in range(n):
  if sco[n-1-i]==0:
    ans+=max(achk(n-1-i),bchk(n-1-i))
    j=0
    while n-1-i-j*k>=0:
      sco[n-1-i-j*k]=1
      j+=1
print(ans)

Submission status: Accepted

Solution code:
n,k= map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
takahashi = []
Sum = 0
for i in range(k):
    if t[i] == "r":
        takahashi.append("p")
    if t[i] == "p":
        takahashi.append("s")
    if t[i] == "s":
        takahashi.append("r")
for i in range(k,len(t)):
    if t[i] == "r":
        if takahashi[i-k] != "p":
            takahashi.append("p")
        else:
            takahashi.append("0")
    if t[i] == "p":
        if takahashi[i-k] != "s":
            takahashi.append("s")
        else:
            takahashi.append("0")
    if t[i] == "s":
        if takahashi[i-k] != "r":
            takahashi.append("r")
        else:
            takahashi.append("0")
for i in range(len(takahashi)):
    if takahashi[i] == "0":
        Sum += 0
    if takahashi[i] == "r":
        Sum += r
    if takahashi[i] == "p":
        Sum += p
    if takahashi[i] == "s":
        Sum += s
  
print(Sum)

Submission status: Accepted

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

for i in range(N):
    if i >= K and T[i] == T[i-K]:
        T[i] = 'zero'

for l in range(N):
    if T[l] == 'r':
        ans += P
    elif T[l] == 's':
        ans += R
    elif T[l] == 'p':
        ans += S

print(ans)

Submission status: Accepted

Solution code:
def get_point(machine_hand, R, S, P):
    """ 筐体がmachine_handを出した場合，勝った時に得られるポイント
    """
    if machine_hand == 'r':
        return P
    elif machine_hand == 's':
        return R
    elif machine_hand == 'p':
        return S



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

    points = 0
    used_trials = set()
    for i in range(0, len(T)):
        if i < K:
            
            points += get_point(T[i], R, S, P)
            used_trials.add(i)
        else:
            if T[i] == T[i - K] and i - K in used_trials:
                continue
            else:
                # points.append(get_point(T[i], R, S, P))
                points += get_point(T[i], R, S, P)
                used_trials.add(i)
        
    print(points)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = list()
for i in range(k):
    for j in range(int((n - i - 1) / k) + 1):
        if t[j * k + i] == "r":
            b = "p"
        elif t[j * k + i] == "s":
            b = "R"
        else:
            b = "S"
        a.append(b)
    a.append("/")
    
c = 2
ans = 0
e = 0
for i in range(len(a) - 1):
    if a[i] == a[i + 1]:
        c += 1
        if a[i] == "R":
            d = r
        elif a[i] == "S":
            d = s
        else:
            d = p
        e = 1
    elif a[i] != a[i + 1] and e == 1:
        ans += int(c / 2) * d
        c = 2
        d = 0
        e = 0
    elif a[i] != a[i + 1] and a[i] != "/":
        if a[i] == "R":
            f = r
        elif a[i] == "S":
            f = s
        else:
            f = p
        ans += f
        
print(ans)

Submission status: Accepted

Solution code:
def main():
    n , k = map(int,input().split())
    r, s, p = map(int,input().split())
    t = list(input())
    score = 0
    te = []

    for count, i in enumerate(t):
        if count < k :
            if i == 'r' :
                score += p
                te.append('p')
            elif i == 's' :
                score += r
                te.append('r')
            elif i == 'p' :
                score += s
                te.append('s')
        else:
            if i == 'r' and  te[-k] != 'p':
                score += p
                te.append('p')
            elif i == 's' and te[-k] != 'r':
                score += r
                te.append('r')
            elif i == 'p' and te[-k] != 's':
                score += s
                te.append('s')
            else:
                te.append('0')
    print(score)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = []
point = 0
for t in T[:K]:
    if t == 'r':
        ans.append(('p', True))
        point += P
    elif t == 's':
        ans.append(('r', True))
        point += R
    else:
        ans.append(('s', True))
        point += S

for i, t in enumerate(T[K:]):
    if t == 'r':
        if ans[i][0] == 'p' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('p', True))
            point += P
    elif t == 's':
        if ans[i][0] == 'r' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('r', True))
            point += R
    else:
        if ans[i][0] == 's' and ans[i][1]:
            ans.append(('-', False))
        else:
            ans.append(('s', True))
            point += S

print(point)

Submission status: Accepted

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

from itertools import groupby
from math import ceil

answer = 0
for i in range(K):
    for key,group in groupby(T[i::K]):
        if key == "r":
            answer += P * ceil(len(list(group))/2)
        elif key == "s":
            answer += R * ceil(len(list(group))/2)
        elif key == "p":
            answer += S * ceil(len(list(group))/2)

print(answer)

Submission status: Accepted

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

for i in range(n):
  if t[i]=="r":
    mine.append("p")
  elif t[i]=="s":
    mine.append("r")
  elif t[i]=="p":
    mine.append("s")
  
for i in range(len(mine)-k):
  if mine[i]==mine[i+k]:
    mine[i+k] = "x"
    
ans = mine.count("r") * r + mine.count("s") * s + mine.count("p") * p
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split(' '))
R,S,P = map(int, input().split(' '))
T = input()
win={'r':'p', 's':'r', 'p':'s'}
progress=''
for i,v in enumerate(T):
    if i<K:   progress+=win[v]
    else:
        if win[v]!=progress[i-K]:
            progress+=win[v]
        else:
            progress+='x'
print(progress.count('r')*R + progress.count('s')*S + progress.count('p')*P)