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:
n,k=map(int, input().split())
point = [int(i) for i in input().split()]
janken = input()

for i in range(k,n):
  if janken[i] == janken[i-k]:
    janken= janken[0:i]+'0'+janken[i+1:n]

count=0

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

print(count)

Submission status: Accepted

Solution code:
def points(i):
    if i=="r":
        point=p
    elif i=="s":
        point=r
    else:
        point=s
    return point
    
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()

ans=0
L=[]
for j in range(n):
    if j<k:
        ans+=points(t[j])
        L.append(t[j])
    else:
        if t[j]==L[j-k]:
            L.append("b")
            continue
        else:
            ans+=points(t[j])
            L.append(t[j])
print(ans)

Submission status: Accepted

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

m=0
ll=[]
for i in range(len(l)):
    if k<=i:
        if l[i]=="r" and ll[i%k]!=l[i]:
            m+=p
            ll[i%k]="r"
        elif l[i]=="r" and ll[i%k]==l[i]:
            ll[i%k]="."

        elif l[i]=="s" and ll[i%k]!=l[i]:
            m+=r
            ll[i%k]=("s")
        elif l[i]=="s" and ll[i%k]==l[i]:
            ll[i%k]=(".")
            
        elif l[i]=="p" and ll[i%k]!=l[i]:
            m+=s
            ll[i%k]=("p")
        elif l[i]=="p" and ll[i%k]==l[i]:
            ll[i%k]=(".")

    else:
        if l[i]=="r":
            m+=p
            ll.append("r")
        elif l[i]=="s":
            m+=r
            ll.append("s")
        elif l[i]=="p":
            m+=s
            ll.append("p")

print(m)

Submission status: Accepted

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

dp = [[0] * 3 for i in range(N+1)]
for i in range(1, N+1):
    # print(i)

    if T[i-1] == 'r':
        if i < K:
            dp[i][0] = 0
            dp[i][1] = 0
            dp[i][2] = P
        else:
            dp[i][0] = max(dp[i-K][1], dp[i-K][2])
            dp[i][1] = max(dp[i-K][0], dp[i-K][2])
            dp[i][2] = max(dp[i-K][0]+P, dp[i-K][1]+P)

    elif T[i-1] == 's':
        if i < K:
            dp[i][0] = R
            dp[i][1] = 0
            dp[i][2] = 0
        else:
            dp[i][0] = max(dp[i-K][1]+R, dp[i-K][2]+R)
            dp[i][1] = max(dp[i-K][0], dp[i-K][2])
            dp[i][2] = max(dp[i-K][0], dp[i-K][1])

    else:
        if i < K:
            dp[i][0] = 0
            dp[i][1] = S
            dp[i][2] = 0
        else:
            dp[i][0] = max(dp[i-K][1], dp[i-K][2])
            dp[i][1] = max(dp[i-K][0]+S, dp[i-K][2]+S)
            dp[i][2] = max(dp[i-K][0], dp[i-K][1])
    # print(max(dp[i]))
ans = 0
for i in range(K):
    ans += max(dp[N-i])
print(ans)

Submission status: Accepted

Solution code:
def ints():
  return [int(x) for x in input().split()]
def ii():
  return int(input())

n, k = ints()
R, S, P = ints()
cpu = input()
jibun = [0] * n

w = ['r', 's', 'p']

for i in range(n):
  c = cpu[i]
  if c=='r':
    t = ['p']
  elif c=='s':
    t = ['r']
  else:
    t = ['s']

  j = i-k
  if j>=0:
    o = jibun[j]
    te = t[0]
    if te in o:
      if len(o)==1:
        ww = w.copy()
        ww.remove(te)
        t = ww
      else:
        o.remove(te)

  jibun[i] = t

s = 0
for i in range(n):
  w = jibun[i][0]+cpu[i]
  if w=='rs':
    s += R
  elif w=='sp':
    s += S
  elif w=='pr':
    s += P
print(s)

Submission status: Accepted

Solution code:
import math

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


def point(i, hand):
    if i >= N: return 0
    t = T[i]
    if t == 'r': return P if hand == 2 else 0
    if t == 's': return R if hand == 0 else 0
    if t == 'p': return S if hand == 1 else 0
    return 0


def max_value(head_index):
    l = math.ceil(N / K)
    dp = [[0, 0, 0] for i in range(l)]
    dp[0] = [
      point(head_index, 0),
      point(head_index, 1),
      point(head_index, 2)
    ]
    for i in range(1, l):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + point(head_index + i * K, 0)
        dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + point(head_index + i * K, 1)
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + point(head_index + i * K, 2)
    return max(dp[l - 1])


def solve():
    ans = 0

    for i in range(K):
        ans += max_value(i)

    return ans


print(solve())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
li=[]
pt=0
for i in range(n):
  if i<k and t[i] == "r":
    li.append("p")
    pt+=p
  elif i<k and t[i] == "s":
    li.append("r")
    pt += r
  elif i<k and t[i] == "p":
    li.append("s")
    pt += s
  elif i>=k and t[i] == "r" and li[i-k] != "p":
    li.append("p")
    pt+=p
  elif i>=k and t[i]=="r" and li[i-k] =="p":
    li.append(0)
  elif i>=k and t[i] == "s" and li[i-k] != "r":
    li.append("r")
    pt += r
  elif i>= k and t[i]=="s" and li[i-k] == "r":
    li.append(0)
  elif i>=k and t[i] == "p" and li[i-k] != "s":
    li.append("s")
    pt += s
  elif i>=k and t[i] == "p" and li[i-k] == "s":
    li.append(0)
print(pt)

Submission status: Accepted

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

def opt(c):
    if c=='r':
        return 'p'
    if c=='s':
        return 'r'
    if c=='p':
        return 's'

def sec(c,x):
    if x!=opt(c):
        return opt(c)
    else:
        return 'x'
    
def cnt(ans):
    c = 0
    for i in range(len(ans)):
        if ans[i]=='r':
            c+=r
        if ans[i]=='s':
            c+=s
        if ans[i]=='p':
            c+=p
    return c


ans = ""
for i in range(len(t)):
    if i-k>=0:
        ans+=sec(t[i],ans[i-k])
    else:
        ans+=opt(t[i])
        
print(cnt(ans))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
t=list(reversed(t))
r_p, p_p, s_p = 0, 0, 0
r_c, p_c, s_c = None, None, None
for i in range(len(t)):
    if t[i] == 'r':
        if r_c is None:
            r_c = i
        if i+k < len(t) and t[i+k] == 'r':
            t[i+k] = 'x' 
        r_p += p
        r_c = i
    if t[i] == 'p':
        if p_c is None:
            p_c = i
        if i+k < len(t) and  t[i+k] == 'p':
            t[i+k] = 'x' 
        p_p += s
        p_c = i
    if t[i] == 's':
        if s_c is None:
            s_c = i
        if i+k < len(t) and t[i+k] == 's':
            t[i+k] = 'x' 
        s_p += r
        s_c = i
    #print(i, r_p, p_p, s_p,  t[i])
#print(r_p, p_p, s_p)
print(r_p + p_p + s_p)
#print(list(reversed(t)))

Submission status: Accepted

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

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input().strip()
D = deque(T)
D.appendleft('none')
T = list(D)

score = 0
count = 0
before = 'a'

for i in range(1, K+1):
    for j in range(i, N+1, K):
        now = T[j]
        if now == before:
            count += 1
        else:
            if now == 'r':
                score += P
            elif now == 's':
                score += R
            elif now == 'p':
                score += S
            if count >= 2:
                if before == 'r':
                    score += P*((count-1)//2)
                elif before == 's':
                    score += R*((count-1)//2)
                elif before == 'p':
                    score += S*((count-1)//2)
            count = 1
        before = now
    if count >= 2:
        if now == 'r':
            score += P*((count-1)//2)
        elif now == 's':
            score += R*((count-1)//2)
        elif now == 'p':
            score += S*((count-1)//2)
    before = 'a'
        
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
te = str(input())
te = list(te)
point = 0

for i in range(1, N+1):
  flag = True
  if K < i:
    if te[i-1] == te[i-1-K]:
      te[i-1] = "a"
      flag = False
  if flag:
    if te[i-1] == "r":
      point += P
    elif te[i-1] == "s":
      point += R
    else:
      point += S
print(point)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
R, S, P = map(int,input().split())
t = str(input())
dic_rsp = {'r':P, 's':R, 'p':S}
ans = 0
for ik in range(k):
    tmp = 'o'
    for ii in range(ik,n,k):
        if ii==0:
            ans+=dic_rsp[t[ii]]
            tmp = t[ii]
        else:
            if t[ii]!=tmp:
                ans+=dic_rsp[t[ii]]
                tmp = t[ii]    
            else:
                tmp = 'o'                    
print(ans)

Submission status: Accepted

Solution code:
tmp = input().split()
N = int(tmp[0])
K = int(tmp[1])
tmp = input().split()
R = int(tmp[0])
S = int(tmp[1])
P = int(tmp[2])
T = input()

history = []
ans = 0
for i in range(N):
    if not (i > K-1):
        if T[i] == "r":
            ans += P
            history.append([T[i], True])
        elif T[i] == "s":
            ans += R
            history.append([T[i], True])
        else:
            ans += S
            history.append([T[i], True])
        # print(i)
    else:
        if T[i] != T[i-K] or (T[i] == T[i-K] and history[i-K][1] == False):
            if T[i] == "r":
                ans += P
                history.append([T[i], True])
            elif T[i] == "s":
                ans += R
                history.append([T[i], True])
            else:
                ans += S
                history.append([T[i], True])
            # print(i)
        else:
            history.append([T[i], False])
    # print(history)
print(ans)

Submission status: Accepted

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

te = ['r', 's', 'p']

def win(s):
	if s == 'r':
		return 2
	elif s == 's':
		return 0
	else:
		return 1

taka = []
cnt = 0

for i in range(n):
	winner = win(t[i])
	if i < k:
		taka.append(te[winner])
		cnt += rsp[winner]
	else:
		if taka[i-k] == te[winner]:
			if i < n-k:
				next = win(t[i+k])
				can = ['r', 's', 'p']
				del can[winner]
				if te[next] in can:
					for j in range(len(can)):
						if can[j] != te[next]:
							taka.append(can[j])
				else:
					taka.append(can[0])
			else:
				can = ['r', 's', 'p']
				del can[winner]
				taka.append(can[0])
		else:
			taka.append(te[winner])
			cnt += rsp[winner]
print(cnt)

Submission status: Accepted

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

ans = [0] * n
def janken(te):
  if te == 'r':
    return p
  elif te == 's':
    return r
  else:
    return s
    
for i in range(n):
  konkai = janken(t[i])
  if i < k:
    ans[i] = konkai
  elif ans[i-k] != konkai:
    ans[i] = konkai
  else:
    if t[i-k] == t[i]:
      ans[i] = 0
    else:
      ans[i] = konkai
print(sum(ans))

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
r, s, p = [int(i) for i in input().split()]
t = input()

x = [0 for i in range(n)]
y = [None for i in range(n)]

t = list(t)

def hoge(my_hand, target_char, value, x):
  for i in range(n):
    if t[i] == target_char:
      if i - k < 0:
        x[i] = value
        y[i] = my_hand
      else:
        if y[i - k] != my_hand:
          x[i] = value
          y[i] = my_hand

rr = [r, 'r', 's']
pp = [p, 'p', 'r']
ss = [s, 's', 'p']

fuga = [rr, pp, ss]

fuga.sort(key=lambda x: x[0], reverse=True)

for f in fuga:
  hoge(f[1], f[2], f[0], x)

print(sum(x))

Submission status: Accepted

Solution code:
# 2019/12/29
# ランレングス

n,k=map(int,input().split())
point=list(map(int,input().split()))
t=input()

rsp={'r':0,'s':1,'p':2}

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

for i in range(n):
    ls[i%k].append(t[i])    
ans=0
for e in ls:
    cnt=1
    for i in range(1,len(e)):
        if e[i-1]==e[i]:
            cnt+=1
        else:
            pp=(rsp[e[i-1]]-1+3)%3
            ans+=-(-cnt//2)*point[pp]
            cnt=1
    pp=(rsp[e[-1]]-1+3)%3
    ans+=-(-cnt//2)*point[pp]


print(ans)

Submission status: Accepted

Solution code:
#n,じゃんけん回数  k，ちょうど出せないk回前までの手
n,k = map(int,input().split())
#r,s,p：グー，ちょき，パーで勝ったときの点数
r,s,p = map(int,input().split())
#相手の手
arr = list(input())
point = 0
for x,y in enumerate(arr):
    if x+k < n:
        if arr[x+k] == arr[x]:
            arr[x+k]=None

for i in arr:
    if i == "r":
        point+=p
    elif i =="s":
        point+=r
    elif i =="p":
        point+=s

print(point)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
List = [[] for k in range(K)]
for n in range(N):
    List[n % K].append(T[n])

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

score = 0
for k in range(K):
    L = List[k]
    count = 1
    length = len(L)
    for i in range(length):
        try:
            if L[i] == L[i+1]:
                count += 1
            else:
                score += point[L[i]]*((count+1)//2)
                count = 1
        except:
            score += point[L[i]]*((count+1)//2)
print(score)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline
n, k = [int(x) for x in input().strip().split()]
p = {a:b for a, b in zip(['s', 'p', 'r'], [int(x) for x in input().strip().split()])}
p['b'] = 0
t = input().strip()
pre_ = ['b'] * k
cnt = [0] * k
ans = 0
for i, tt in enumerate(t, start=1):
    if tt != pre_[i%k-1]:
        ans += p[pre_[i%k-1]] * ((cnt[i%k-1] + 1) // 2)
        cnt[i%k-1] = 1
        pre_[i%k-1] = tt
    else:
        cnt[i%k-1] += 1
else:
    for i in range(k):
        ans += p[pre_[i%k-1]] * ((cnt[i%k-1] + 1) // 2)
print(ans)

Submission status: Accepted

Solution code:
import re

n, k = map(int,input().split()) # K 回前のじゃんけんで出した手と同じ手を出すことはできない
r, s, p = map(int,input().split()) # r:goo , s:choki, p:paa
t = input()

# aaaaa
ans = 0

for i in range(k):
    tmp = t[i::k]
    tmp = re.sub('rr','rz',tmp)
    tmp = re.sub('ss','sz',tmp)
    tmp = re.sub('pp','pz',tmp)
    ans += tmp.count('r') * p + tmp.count('s') * r + tmp.count('p') * s
    # print(tmp)
print(ans)

Submission status: Accepted

Solution code:
#D
n, k = map(int, input().split())
r, s, p = map(int, input().split())
hand = sorted([(r, 'r'), (s, 's'), (p, 'p')])
c = input()
ans = list('*'*n)
op = {'r':'p', 's':'r', 'p':'s'}
summ = 0
for i in range(n):
    if i-k >= 0:
        if ans[i-k] != op[c[i]]:
            ans[i] = op[c[i]]
            
            if op[c[i]] == 'r':
                summ += r
            elif op[c[i]] == 's':
                summ += s
            else:
                summ += p
    else:
        ans[i] = op[c[i]]

        if op[c[i]] == 'r':
            summ += r
        elif op[c[i]] == 's':
            summ += s
        else:
            summ += p
print(summ)

Submission status: Accepted

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

result = 0
ans = []

for t in range(N) :
  if t < K :
    result += dic[T[t]]
    ans.append(ans_dic[T[t]])
  else :
    if ans[t-K] == ans_dic[T[t]] :
      if t+K < N :
        ans.append(T[t+K])
      else :
        continue
    else :
      result += dic[T[t]]
      ans.append(ans_dic[T[t]])

print(result)

Submission status: Accepted

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

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

total = 0

my_result = [] # 勝ち:1, それ以外:0
# K回目までは自由に出せるので勝てる手を出す
for i in range(K):
  # print(T[i], win_point[T[i]])
  total += win_point[T[i]]
  my_result.append(1)

# K回目からは勝てるときだけ勝てる手を出す
for i in range(K, N):
  #print(T[i-K], T[i], my_result[i-K])
  win_te = win_result[T[i]]
  # K個前の手と異なるのであれば勝てる手を出す
  if T[i-K] != T[i]:
    total += point[win_te]
    my_result.append(1)
  # K個前と同じ手でも、勝てる手を出してないのであれば勝てる手を出す
  elif my_result[i-K] == 0:
    total += point[win_te]
    my_result.append(1)
  else:
    my_result.append(0)

print(total)

Submission status: Accepted

Solution code:
[N, K] = list(map(int,input().split()))
[R, S, P] = list(map(int,input().split()))
T = input()
def calc(s):
    tmp = [0,0,0]
    if s[0] == 'r':
        tmp[2] += 1
    elif s[0] == 's':
        tmp[0] += 1
    else:
        tmp[1] += 1
    old = s[0]
    for i in range(1,len(s)):
        new = s[i]
        if old != new:
            if  new == 'r':
                tmp[2] += 1
            elif new  == 's':
                tmp[0] += 1
            else:
                tmp[1] += 1
        if old == new:
            old = 'a'
        else:
            old = new
    re = R * tmp[0] + S * tmp[1] + P * tmp[2]
    return re
ans = 0
for i in range(K):
    s = ''
    idx = i
    while idx <= N - 1:
        s += T[idx]
        idx += K
    ans += calc(s)
print(ans)

Submission status: Accepted

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

ans = 0
for a in range(k):
    mae = ''
    for i in range(a, n, k):
        if (mae != t[i]):
            mae = t[i]
            # win
            if(t[i] == 'r'):
                ans += p
            elif(t[i] == 's'):
                ans += r
            else:
                ans += s
        else:
            # can't win
            mae = ''
print(ans)

Submission status: Accepted

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

hands = [None] * N
d1 = {'r': 'p', 's': 'r', 'p': 's'}
d2 = {'r': P, 's': R, 'p': S}
result = 0
for i in range(len(T)):
    if i >= K and d1[T[i]] == hands[i - K]:
        if i + K < N:
            hands[i] = set('rps') - set(T[i + K]) - set(T[i])
        else:
            hands[i] = T[i]
    else:
        hands[i] = d1[T[i]]
        result += d2[T[i]]
print(result)

Submission status: Accepted

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

for i in range(N - K):
    if T[K + i] == T[i]:
        T[K + i] = 'x'

#print(T)

Rc = T.count('r')
Sc = T.count('s')
Pc = T.count('p')
#print(Rc, Sc, Pc)
count = P * Rc + S * Pc + R * Sc
print(count)

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
W = {'r': 'p', 's': 'r', 'p': 's'}
H = ['r', 's', 'p']
T = [i for i in list(input())]
dp = [[0, 0, 0] for _ in range(N+10)]
hl = []

for i in range(N):
    flag = True
    f2 = False
    for j in range(3):
        if f2:
            break
        for k in range(3):
            if W[T[i]] == H[k]:
                if i >= K:
                    if hl[i-K] == k:
                        hl.append(4)
                        dp[i+1] = [max(dp[i])]*3
                        f2 = True
                        break
                # print(i, j, k, hl)
                if flag:
                    hl.append(k)
                    flag = False
                dp[i+1][k] = max(dp[i+1][k], dp[i][j] + P[k])

print(max(dp[N]))

Submission status: Accepted

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

A = N*[-1]

for i in range(N):
    if T[i] == 'r':
        if i >= K:
            if A[i-K] == 5:
                pass
            else:
                score += P
                A[i] = 5
        else:
            score += P
            A[i] = 5

    elif T[i] == "s":
        if i >= K:
            if A[i-K] == 0:
                pass
            else:
                score += R
                A[i] = 0
        else:
            score += R
            A[i] = 0
    else:
        if i >= K:
            if A[i-K] == 2:
                pass
            else:
                score += S
                A[i] = 2
        else:
            score += S
            A[i] = 2

print(score)

Submission status: Accepted

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

T = [c for c in input()]
score = 0

for i in range(N):
    if i >= K and T[i] == T[i-K]:
        T[i] = "a"
        continue
    if T[i] == "r":
        score += P
    elif T[i] == "s":
        score += R
    else:
        score += S

print(score)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    def win_point(a):
        if a == 'r':
            pre.append('r')
            return p
        elif a == 's':
            pre.append('s')
            return r
        else:
            pre.append('p')
            return s

    ans = 0
    for i, j in enumerate(t):
        if i < k:
            ans += win_point(j)
        elif j != pre[i-k]:
            ans += win_point(j)
        else:
            pre.append('xx')
    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()
l = {'r':'p','s':'r','p':'s'}
L = ['' for i in range(n)]
tmp = ['r','s','p']
for i in range(n):
    x = t[i]
    if (i - k) >= 0 and L[i-k] == l[x]:
        tmp.remove(L[i-k])
        if (i + k) <= (n-1) and l[t[i+k]] != l[x]:
            tmp.remove(l[t[i+k]])
        L[i] = tmp[0]
        tmp = ['r','s','p']
    else:
        L[i] = l[x]

score = 0
sc = {'r':r,'s':s,'p':p}
for i in range(n):
    if l[t[i]] == L[i]:
        score += sc[L[i]]

print(score)

Submission status: Accepted

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

hand_table = [[0, 0, P],[R, 0, 0],[0, S, 0]]

T_int = [0 if c == 'r' else 1 if c == 's' else 2 for c in T]

DP = []
for i in range(K):
    innerDP = [[hand_table[T_int[i]][0],hand_table[T_int[i]][1],hand_table[T_int[i]][2]]]
    DP.append(innerDP)

for i in range(K, N):
    offset = i % K
    loopnum = i // K
    DP[offset].append([max(DP[offset][loopnum - 1][1] + hand_table[T_int[i]][0], \
                           DP[offset][loopnum - 1][2] + hand_table[T_int[i]][0]), \
                       max(DP[offset][loopnum - 1][0] + hand_table[T_int[i]][1],DP[offset][loopnum - 1][2] + hand_table[T_int[i]][1]), \
                       max(DP[offset][loopnum - 1][0] + hand_table[T_int[i]][2],DP[offset][loopnum - 1][1] + hand_table[T_int[i]][2])])

ans = 0

for i in range(N - K, N):
    offset = i % K
    loopnum = i // K
    ans += max(DP[offset][loopnum])

print(ans)

Submission status: Accepted

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

score = 0
for k in range(K):
    if T[k]=='r':
        score += P
    elif T[k]=='s':
        score += R
    else:
        score += S
    odd = False
    for n in range(K+k, N, K):
        if T[n-K] == T[n] and not odd:
            odd = True
        else:
            if T[n]=='r':
                score += P
            elif T[n]=='s':
                score += R
            else:
                score += S
            odd = False

print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
sp_T = []
for i in range(K):
    sp_T.append(T[i::K])
ans = 0
win_d = {"s": "r", "r": "p", "p": "s"}
point = {"r": R, "s": S, "p": P}
for t in sp_T:
    sakki = ""
    for i in range(len(t)):
        if win_d[t[i]] != sakki:
            my_hand = win_d[t[i]]
            ans += point[my_hand]
            sakki = my_hand
        else:
            if i == len(t) - 1:
                pass
            else:
                next_want_hand = win_d[t[i + 1]]
                if sakki == next_want_hand:
                    sakki = "X"
                else:
                    sakki = "X"
print(ans)

Submission status: Accepted

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

dp = [0]
kl = [-1 for i in range(N)]
for i in range(N):
    tmp = dp[i]
    k = -1
    if kl[N-K+i] != 2 and T[i] == "r":
        tmp += P
        k = 2
    elif kl[N-K+i] != 0 and T[i] == "s":
        tmp += R
        k = 0
    elif kl[N-K+i] != 1 and T[i] == "p":
        tmp += S
        k = 1
    dp.append(tmp)
    kl.append(k)

print(dp[N])

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = list(str(input()))
Te=[]
for t in T:
    if t=='r':
        Te.append(P)
    elif t=='s':
        Te.append(R)
    else:
        Te.append(S)
te=['r','s','p']
ans=0
for i in range(N):
    if K>i:
        ans+=Te[i]
    else:
        if T[i-K]==T[i]:
            #未来がない時はなんでも良い
            if i+K>=N:
                T[i]='null'
            else:
            #現在でも未来でもない手を代入
                te.remove(T[i])
                try:
                    te.remove(T[i+K])
                except:
                    pass
                T[i]=te[0]
                te=['r','s','p']
        else:
            ans+=Te[i]
print(ans)

Submission status: Accepted

Solution code:
import math


def main():
    n, k = [int(x) for x in input().split()]
    r, s, p = [int(x) for x in input().split()]
    t = input()
    win = {"r": "p", "s": "r", "p": "s"}
    lose = {"r": "s", "s": "p", "p": "r"}
    score = {"r": r, "s": s, "p": p}
    machine = list(t)
    man = [win[x] for x in machine]

    changed = set()
    for i in range(n - k):
        ki = i + k
        if man[i] == man[ki]:
            if i in changed:
                man[i] = lose[machine[i]]
            elif score[man[i]] < score[man[ki]]:
                man[i] = machine[i]
            else:
                man[ki] = machine[ki]
                changed.add(ki)

    total = 0
    for i in range(n):
        if man[i] == win[machine[i]]:
            total += score[man[i]]

    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    for i in range(K):
        prev = ''
        for j in range(N//K+1):
            idx = j*K+i
            if idx >= N:
                break
            current = T[idx]
            if current != prev:
                score += scores[current]
                prev = current
            else:
                prev = ''

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

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

TT = ""
for c in T:
    if c == "r":
        TT+="p"
    elif c == "s":
        TT+="r"
    else:
        TT+="s"

T = T.replace('r', 's')
flag = [False] * N
ans = 0
for idx, c in enumerate(TT):
    if idx - K < 0:
        ans += score[c]
        flag[idx] = True
    else:
        if c == TT[idx-K]:
            if flag[idx-K] == False:
                ans += score[c]
                flag[idx] = True
            else:
                pass
        else:
            ans += score[c]
            flag[idx] = True
print(ans)

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 collections import Counter
import math
ans = 0
c={'r':0,'s':0,'p':0}
for i in range(K):
  x = [[a,len(list(g))] for a,g in groupby(T[i::K])]
  for a in x:
    c[a[0]]+=math.ceil(a[1]/2)
r = c['r']*P + c['s']*R + c['p']*S
print(r)

Submission status: Accepted

Solution code:
(N, K), *D, = [s.split() for s in open(0)]
points = 0
dd = {r: p for p, r in zip(list(map(int, D[0])), ["s", "p", "r"])}
records = []
for i in range(len(D[1][0])):
    if i < int(K):
        points += dd[D[1][0][i]]
        records.append(D[1][0][i])
    else:
        if D[1][0][i] == D[1][0][i-int(K)] and records[i-int(K)] == "pass":
            points += dd[D[1][0][i]]
            records.append(D[1][0][i])
        elif D[1][0][i] == D[1][0][i-int(K)] and records[i-int(K)] != "pass":
             records.append("pass")
        else:
            points += dd[D[1][0][i]]
            records.append(D[1][0][i])
            
print(points)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
for i in range(n-k):
    if t[i] == t[i+k]:
        t[i+k] = "d"

t = "".join(t)
t = t.replace("r", "{}+".format(p)).replace("p", "{}+".format(s)).replace("s", "{}+".format(r)).replace("d", "0+") + "0"
print(eval(t))