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())
r,s,p = map(int,input().split())
tlist = list(input())
score = 0

for i in range(k):
    t = tlist[i]
    if t == "r":
        score += p
    elif t == "s":
        score += r
    elif t == "p":
        score += s

for i in range(k,n):
    if tlist[i] != tlist[i-k]:
        if tlist[i] == "r":
            score += p
        elif tlist[i] == "s":
            score += r
        elif tlist[i] == "p":
            score += s
    else:
        tlist[i] = "n"

print(score)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

N,K = map(int,input().split(' '))
r,s,p = map(int,input().split(' '))
S = list(input())
ANS = [None]*N

ans = 0
for i in range(K):
    k = S[i]
    if 'r' == k:
        ans += p
        ANS[i] = 'p'
    elif 's' == k:
        ans += r
        ANS[i] = 'r'
    else:
        ans += s
        ANS[i] = 's'

for j in range(K,N):
    k = S[j]
    pre = ANS[j-K]
    if 'r' == k:
        if pre=='p':
            ANS[j] = 0
        else:
            ans += p
            ANS[j] = 'p'
    elif 's' == k:
        if pre=='r':
            ANS[j] = 0
        else:
            ans += r
            ANS[j] = 'r'
    else:
        if pre=='s':
            ANS[j] = 0
        else:
            ans += s
            ANS[j] = 's'
print(ans)

Submission status: Accepted

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

r_count = t.count('r') *p
s_count = t.count('s') *r
p_count = t.count('p') *s
tot = r_count + s_count + p_count

count = 0
for i in range(k):
    List = [x for x in t[i::k]]
    for j in range(1, len(List)):
        if List[j-1] == List[j]:
            if List[j] == 'r':
                count += p
            elif List[j] == 's':
                count += r
            else:
                count += s
            List[j] = 'z'
          
ans = tot - count
print(ans)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
R, S, P = [int(x) for x in input().split()]
T = input()
d = {'r': P, 's': R, 'p': S}
ans = 0
m = set()
for i in range(N):
    t = T[i]
    if i < K:
        ans += d[t]
    else:
        if t == T[i-K]:
            if i-K in m:
                ans += d[t]
            else:
                m.add(i)
        else:
            ans += d[t]
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = list(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(K, N):
        if T[i-K] != T[i]:
            if T[i] == 'r':
                ans += P
            elif T[i] == 's':
                ans += R
            else:
                ans += S
        else:
            T[i] = 'n'
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
import collections
import numpy as np
input = sys.stdin.readline
import sys
import collections
import numpy as np
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
R, S, P = [int(x) for x in input().split()]
score = {}
score["r"] = P
score["s"] = R
score["p"] = S
T = list(input().rstrip())
ans = 0
for i in range(K):
    temp = T[i::K]
    ans += score[temp[0]]
    if len(temp) != 1:
        flag = 1
        for j in range(1, len(temp)):
            if temp[j] == temp[j-1]:
                if flag:
                    flag = 0
                else:
                    ans += score[temp[j]]
                    flag = 1
            else:
                ans += score[temp[j]]
                flag = 1
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
l = [[] for i in range(k)]
for i in range(n):
    l[i%k].append(t[i])
ans = 0
for i in range(k):
    a = l[i]
    count = 0
    mae = "a"
    for j in a:
        if mae == j:
            count += 1
        else:
            if count == 0:
                count += 1
            elif count == 1:
                if mae == "r":
                    ans += p
                elif mae == "s":
                    ans += r
                else:
                    ans += s
            else:
                if count % 2 == 0:
                    b = count//2
                else:
                    b = count//2 + 1
                if mae == "r":
                    ans += p*b
                elif mae == "s":
                    ans += r*b
                else:
                    ans += s*b
            mae = j
            count = 1
    if count == 0:
        count += 1
    elif count == 1:
        if mae == "r":
            ans += p
        elif mae == "s":
            ans += r
        else:
            ans += s
    else:
        if count % 2 == 0:
            b = count//2
        else:
            b = count//2 + 1
        if mae == "r":
            ans += p*b
        elif mae == "s":
            ans += r*b
        else:
            ans += s*b
print(ans)

Submission status: Accepted

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

katsu = {}
katsu["r"] = "p"
katsu["s"] = "r"
katsu["p"] = "s"

score = {}
score["r"] = r
score["s"] = s
score["p"] = p

jibuns = ""
out_score = 0

jibuns = [katsu[aite] for aite in t]
out_score = 0
for i, jibun in enumerate(jibuns):
    if i >= k and jibuns[i-k] == jibun:
        nokoris = ["r", "s", "p"]
        nokoris.remove(jibun)
        if i+k <= n-1:
            if jibuns[i+k] != jibun:
                nokoris.remove(jibuns[i+k])
        jibun = nokoris[0]
        jibuns[i] = jibun
    else:
        out_score += score[jibun]

#print(jibuns)
print(out_score)

Submission status: Accepted

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

c = {'r': P, 's': R, 'p': S}
ans = 0
for j in range(K):
    for i in range(j, N, K):
        if i == j:
            con = 1
            ans += c[T[i]]
        elif T[i] == T[i - K]:
            con += 1
            if con % 2 == 1:
                ans += c[T[i]]
        else:
            con = 1
            ans += c[T[i]]

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
t=input()
y=[[] for i in range(k)]
for i in range(n):
    if t[i]=="r":
        y[i%k].append(2)
    elif t[i]=="s":
        y[i%k].append(0)
    else:
        y[i%k].append(1)

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
c=0
for i in range(k):
    z=groupby(y[i])
    l=len(z)
    for j in range(l):
        c+=(x[z[j][0]]*((z[j][1]+1)//2))
print(c)

Submission status: Accepted

Solution code:
def point(m):
    if m=="r":
        return p
    elif m=="s":
        return r
    else:
        return s

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = str(input())
q = [ [i] for i in range(k)]
for i in range(k,n):
    q[i%k].append(i)
ans = 0
for i in range(k):
    j = 0
    while j<len(q[i]):
        if j==len(q[i])-1:
            ans += point(t[q[i][j]])
            j+=1
        else:
            if t[q[i][j]] == t[q[i][j+1]]:
                ans += point(t[q[i][j]])
                j +=2
            else:
                ans += point(t[q[i][j]])
                j +=1
print(ans)

Submission status: Accepted

Solution code:
LOCAL = True and False


def dprint(*args):
  if LOCAL:
    print(*args)


n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
t = input()
t = '*' * k + t

dprint(t)

ans = 0
# for i, x in enumerate(t):
for i in range(k, n + k):
  x = t[i]
  dprint(i, x)
  j = i - k
  if x == 'r':
    dprint('t[j]', '=', t[j])
    dprint('x', '=', x)
    if t[j] != x:
      ans += p
    else:
      t = t[:i] + '*' + t[i + 1:]
  if x == 's':
    if t[j] != x:
      ans += r
    else:
      t = t[:i] + '*' + t[i + 1:]
  if x == 'p':
    if t[j] != x:
      ans += s
    else:
      t = t[:i] + '*' + t[i + 1:]
  dprint(t)
  dprint(ans)

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
t=list(input())
q=['q']*K
t=t+q

s=0
for i in range(K,N+K):
    if t[-i-1] == t[-i-1+K]:
        t[-i-1]='q'
        continue
    elif t[-i-1] =='r':
        s+=P
    elif t[-i-1] =='s':
        s+=R
    elif t[-i-1] =='p':
        s+=S


print(s)

Submission status: Accepted

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

import numpy as np

score = np.array([0, R, S, P], dtype=int)
M = (1 + (N-1)//K)
addi = M * K - N
t = np.array([{'n':0, 's':1, 'p':2, 'r':3}[i] for i in T+'n'*addi], dtype=int)

t = t.reshape(M,K).T

for i in range(K):
    for j in range(M):
        if j!=0 and t[i,j] == t[i,j-1]:
            t[i,j] = 0

print(score[t].sum())

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

def cal(t):
    if t=="r":
        return (t,P)
    elif t=="s":
        return (t,R)
    elif t=="p":
        return (t,S)
point = 0
for i in range(K):
    count = i
    data = []
    while count < N:
        data.append(cal(T[count]))
        count += K
    n = "q"
    for j in data:
        if n != j[0]:
            point += j[1]
            n = j[0]
        else:
            n = "q"

print(point)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
A=[[] for i in range(k)]
for i in range(k):
  A[i]=[]
for i in range(n):
  A[i%k].append(t[i])
ans=0
for i in range(k):
  a=0
  for j in range(len(A[i])):
    if j==0:
      if A[i][j]=="r":
        ans=ans+p
      elif A[i][j]=="s":
        ans=ans+r
      else:
        ans=ans+s
    else:
      if A[i][j]!=A[i][j-1]:
        a=0
        if A[i][j]=="r":
          ans=ans+p
        elif A[i][j]=="s":
          ans=ans+r
        else:
          ans=ans+s
      else:
        a=a+1
        if a%2==0:
          if A[i][j]=="r":
            ans=ans+p
          elif A[i][j]=="s":
            ans=ans+r
          else:
            ans=ans+s
print(ans)

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

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

dicts={}
dicts["r"] = R
dicts["s"] = S
dicts["p"] = P

checks=[False for i in range(N)]
hand=[]

index=0
output = []
for i in range(N):
    if T[i]=="r":
        output.append("p")
    elif T[i]=="s":
        output.append("r")
    else:
        output.append("s")

sums =0
for i in range(len(output)):
    if i <K:
        sums += dicts[output[i]]
        checks[i]=True
    else:
        if output[i]==output[i-K]:
            if checks[i-K]!=True:
                sums += dicts[output[i]]
                checks[i]=True

        else:
            sums += dicts[output[i]]
            checks[i]=True


print(sums)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
a = t.count("s")
b = t.count("p")
c = t.count("r")
t = list(t)
for i in range(n-k):
    if t[i] == t[i + k]:
        if t[i] == "s":
            a -= 1
            t[i+k] = "x"
        elif t[i] == "p":
            b -= 1
            t[i+k] = "x"
        else:
            c -= 1
            t[i+k] = "x"
print(a*r+b*s+c*p)

Submission status: Accepted

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

score= {"r": p,"s": r,"p": s}
ans=0
for i in range(k):
    tt = t[i:n:k]    
    old="x"
    c=1
    for ttt in tt:
        if ttt!=old:
            ans+=score[ttt]
            c=1
        else:
            if c==1:
                c=-1
            else:
                c=1
                ans+=score[ttt]
        old = ttt
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
dicta = {'r':P,'s':R,'p':S}
T = input()
count = 0
 
for i in range(N):
    if i >= K:
        if T[i-K] == T[i]:
            T = T[:i] + 'z' + T[i+1:]
            
        else:
            count += dicta[T[i]]
    else:
        count += dicta[T[i]]
print(count)

Submission status: Accepted

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


def janken(x):
    if x == "r":
        return P
    elif x == "s":
        return R
    else:
        return S


ans = 0
for i in range(K):
    ans += janken(T[i])
flg = [True] * N
for i in range(K, N):
    if T[i - K] == T[i] and flg[i - K] == True:
        flg[i] = False
        continue
    else:
        ans += janken(T[i])
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
for j in range(k):
    pre = 'no'
    for i in range(n - 1 - j, -1, -k):
        score = 0
        if t[i] == 'r' and pre != 'p':
            score = p
            pre = 'p'
        if t[i] == 's' and pre != 'r':
            score = r
            pre = 'r'
        if t[i] == 'p' and pre != 's':
            score = s
            pre = 's'
        if score == 0:
            pre = 'no'
        ans += score
print(ans)

Submission status: Accepted

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

ans = 0
te = ["" for _ in range(n)]
for i in range(n):
	if t[i] == "r" and te[i - k] != "p":
		ans += p
		te[i] = "p"
	elif t[i] == "s" and te[i - k] != "r":
		ans += r
		te[i] = "r"
	elif t[i] == "p" and te[i - k] != "s":
		ans += s
		te[i] = "s"

print(ans)

Submission status: Accepted

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

ans = ["" for _ in range(K)]
for i in range(N):
    ans[i%K] += T[i]

score = ""

for a in ans:
    for i in range(len(a)):
        if i == 0:
            score += a[i]
            continue
        else:
            if a[i] == score[-1]:
                score += "?"
            else:
                score += a[i]
    score += "?"
final = 0

for i in range(len(score)):
    if score[i] == "r":
        final += P
    if score[i] == "p":
        final += S
    if score[i] == "s":
        final += R

print(final)

Submission status: Accepted

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

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

ans = 0
for i in range(K):
    tmp = T[i::K]
    b_hand = T[i]
    s = 1
    cum = []

    for j in range(1,len(tmp)):
        if b_hand == tmp[j]:
            s += 1
        else:
            cum.append([s, b_hand])
            s = 1
            b_hand = tmp[j]
    cum.append([s, b_hand])

    for j in range(len(cum)):
        if cum[j][0] == 1:
            ans += win_score_each_op_hand[cum[j][1]]
        elif cum[j][0] % 2 == 1:
            ans += win_score_each_op_hand[cum[j][1]] * (-(-cum[j][0] // 2))
        else:
            ans += win_score_each_op_hand[cum[j][1]] * (cum[j][0] // 2)

print(ans)

Submission status: Accepted

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

def janken(s):
  if s == "r":
    return P
  if s == "s":
    return R
  if s == "p":
    return S

answer = 0
for i in range(K):
  j = 0
  flag = 0
  while i + K * j < N:
    if j == 0:
      answer += janken(T[i + K * j])
    else:
      if T[i + K * j] != T[i + K * (j - 1)] or flag == 1:
        answer += janken(T[i + K * j])
        flag = 0
      else:
        flag = 1
    #print(T[i + K * j], answer)
    j += 1
print(answer)

Submission status: Accepted

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

print(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=list(input())
score=[[0,0,0]*K]
for i in range(N-K):
    if T[i]==T[i+K]:
        T[i+K]="."

print(T.count("p")*S + T.count("s")*R + T.count("r")*P )

Submission status: Accepted

Solution code:
#coding:utf-8
n,k = map(int,input().split())
d = dict()
d['s'],d['p'],d['r'] = map(int,input().split())
T = list(input())
ans = 0
for i in range(len(T)):
    if (i-k >= 0 and T[i-k]==T[i]):
        T[i] = "*"
    else:
        ans += d[T[i]]

print (ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
li = list(t[:k])
cnt = 0
for itr, val in enumerate(t):
  if itr >= k and val == li[itr % k]:
    li[itr % k] = "x"
  else:
    if val == "r":
      cnt += p
    elif val == "s":
      cnt += r
    else:
      cnt += s
    li[itr % k] = val
print(cnt)

Submission status: Accepted

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

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

ans = 0
l = [False] * N

for i in range(K):
  ans += pt[T[i]]
  l[i] = True

for i in range(K, N):
  if T[i] == T[i-K] and l[i-K]: continue
  ans += pt[T[i]]
  l[i] = True
    
print(ans)

Submission status: Accepted

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

def point(A):
    if A == "r":
        return P
    elif A == "s":
        return R
    else:
        return S

ans = 0
for i in range(K):
    s = T[i::K]
    t = "a"
    for j in range(len(s)):
        if t == s[j]:
            t = "a"
        else:
            ans += point(s[j])
            t = s[j]

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def getscore(c):
  if c == "r":
    return P
  if c == "s":
    return R
  if c == "p":
    return S
  return 0
r = 0
for n in range(K):
  p = ""
  for i in range(n, N, K):
    c = T[i]
    if c == p:
      p = ""
    else:
      r += getscore(c)
      p = c
print(r)

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
}
use = [False for i in range(len(t))]
score = 0
for i in range(len(t)):
    if i < k:
        score += win[t[i]]
        use[i] = True
    elif t[i] != t[i-k] or (not use[i-k]):
        score += win[t[i]]
        use[i] = True
print(score)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = list(input())
d = {"r":P,"s":R,"p":S}
ans = 0
for i, v in enumerate(T):
    if i < K:
        ans += d[v]
    else:
        if v != T[i-K]:
            ans += d[v[0]]
        else:
            T[i] = v + "a"

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t=input()
score=0
cnt=0
dame=""
for i in range(n):
    if k>i or dame[i-k]!=t[i]:
        if t[i]=='r':
            score+=p
            dame+='r'
        elif t[i]=='s':
            score+=r
            dame+='s'
        elif t[i]=='p':
            score+=s
            dame+='p'
    else:
        dame+='n'
print(score)

Submission status: Accepted

Solution code:
#coding:utf-8

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

str_arr = []
for i in range(k):
    j = i
    chars = ''
    while j < n:
        chars += t[j]
        j += k
    str_arr.append(chars)

total = 0
for chars in str_arr:
    pre_char = ''
    for char in chars:
        if pre_char == char:
            pre_char = ''
            continue
        else:
            if char == 'r':
                total += p
            elif char == 'p':
                total += s
            else:
                total += r
        pre_char = char
print(total)

Submission status: Accepted

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
R, S, P = read_ints()
T = read()[:-1]

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

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

handsls = []

ans = 0
for i in range(N):
    now = T[i]
    if i < K:
        ans += points[now]
        handsls.append(now)
    else:
        if now == handsls[i - K]:
            handsls.append('n')
            continue
        ans += points[now]
        handsls.append(now)
        # print(T[i], points[now])
# print(handsls)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
#dp=[[0 for i in range(3)]for j in range(n+1)]
t=input()
ans=0
janken={"r":p,"s":r,"p":s}
aiko=[]
for i in range(n):
    if i<k:
        ans+=janken[t[i]]
        aiko.append(False)
    else:
        #print(t[i-k],t[i])
        if t[i-k]!=t[i] or (t[i-k]==t[i] and aiko[i-k]):
            ans+=janken[t[i]]
            aiko.append(False)
        else:
            aiko.append(True)
    #print(ans)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
point = 0
result = []
for i in range(n):
  if i+1 <= k or result[i-k] == "f" or t[i] != t[i-k]:
    if t[i] == "r":
      point += p
    elif t[i] == "s":
      point += r
    else:
      point += s
    result.append("t")
  else:
    result.append("f")
print(point)

Submission status: Accepted

Solution code:
d = {'r': 0, 's': 1, 'p': 2}
win = {0: 2, 1: 0, 2: 1}

N, K = map(int, input().split())
point = list(map(int, input().split()))
T = input()

dp = [[0]*3 for _ in range(K)]  # K周期で最後に出した手別の最大得点

for i in range(N):
    k = i%K
    w = win[d[T[i]]]
    mx, mxw = 0, 0
    for j in range(3):
        mx = max(mx, dp[k][j])
        if j != w:
            mxw = max(mxw, dp[k][j])
    for j in range(3):
        if j != w:
            dp[k][j] = mx   # 今回は負けるので前回のmax
        else:
            dp[k][j] = mxw + point[j]   # 今回勝つので、前回別の手のmax + point[j]

ans = 0
for i in range(K):
    mx = 0
    for j in range(3):
        mx = max(mx, dp[i][j])
    ans += mx

# print(dp)
print(ans)

Submission status: Accepted

Solution code:

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


for k in range(K):
    if T[k] == "r":
        ans += P
    elif T[k] == "p":
        ans += S
    else:
        ans += R

for k in range(K,N):
    if T[k] == "r":
        if T[k-K] != "r":
            ans += P
        else:
            T[k] = "_"
    elif T[k] == "p":
        if T[k-K] != "p":
            ans += S
        else:
            T[k] = "_"
    else:
        if T[k-K] != "s":
            ans += R
        else:
            T[k] = "_"


print(ans)

Submission status: Accepted

Solution code:
n,k = map(lambda x: int(x),input().split())
r,s,p = map(lambda x: int(x),input().split())
t = input()
ans = ''
dont = 'free'
count = 0
for i in range(n):
    if i - k >= 0 :
        dont = ans[i-k]
    if t[i] == 'r' and dont != 'p':
        ans += 'p'
        count += p
    elif t[i] == 's' and dont != 'r' :
        ans += 'r'
        count += r
    elif t[i] == 'p' and dont != 's':
        ans += 's'
        count += s
    else :
        ans += 'f'
print(count)

Submission status: Accepted

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

# 勝つ手を求める
def f(ch):
    if ch == 'r':
        return 'p'
    elif ch == 's':
        return 'r'
    else:
        return 's'

te = []
for i in range(N):
    r = f(T[i])
    if i < K:
        te.append(r)
    else:
        if te[i-K] == r:
            te.append('*')
        else:
            te.append(r)

ans = 0
for ti in te:
    if ti == 'r':
        ans += R
    elif ti == 's':
        ans += S
    elif ti == 'p':
        ans += P
print(ans)
# print(te)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
j = 0
U = []
for i in T:
    if j < K:
        U.append(i)
    else:
        if U[j-K] == "o":
            U.append(i)
        elif T[j-K] == i:
            U.append("o")
        else:
            U.append(i)
    j += 1
print(U.count("r")*P+U.count("s")*R+U.count("p")*S)

Submission status: Accepted

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


for i in range(K , N):
    now = T[i]
    before = T[i - K]
    if now == before and is_get_list[i - K]:
        is_get_list.extend([False])
    else:
        is_get_list.extend([True])
# print(is_get_list)
for i in range(N):
    if not is_get_list[i]:
        continue
    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 collections

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

T_count = collections.Counter(T)
# print(T_count)

point = T_count["r"]*P + T_count["s"]*R + T_count["p"]*S

for idx, t in enumerate(T):
    # print(idx + K - 1)
    if idx + K >= N:
        break
    if t == T[idx+K]:
        if t == "r":
            point -= P
        elif t == "s":
            point -= R
        elif t == "p":
            point -= S
        T[idx+K] = "x"
print(point)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, K = map(int, input().split())
        R, S, P = map(int, input().split())
        T = input().rstrip()

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

        win = []
        for t in T:
            win.append(jyanken[t])

        pointer = len(T) - 1
        while pointer - K >= 0:
            if win[pointer] == win[pointer - K]:
                win[pointer - K] = 'x'
            pointer -= 1

        points = {'r': R, 's': S, 'p': P, 'x': 0}
        ans = 0
        for w in win:
            ans += points[w]

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
from collections import deque

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

hands = deque()
score = 0
for i in range(N):
    hand = {'r': 'p', 's': 'r', 'p': 's'}[T[i]]

    if len(hands) == K:
        last = hands[0]
        if last == hand:
            hand = '*'
        hands.popleft()
    hands.append(hand)

    score += {'r': R, 's': S, 'p': P}.get(hand, 0)
print(score)

Submission status: Accepted

Solution code:
from collections import deque

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

p_table = {"r": P, "s": R, "p": S}
h_table = {"r": "p", "s": "r", "p": "s"}
hist = deque(maxlen=K)
point = 0
for i, t in enumerate(T, 1):
    if i <= K or h_table[t] != hist[0]:
        point += p_table[t]
        hist.append(h_table[t])
    else:
        hist.append("_")
    #print(hist, t, h_table[t], point, i)

print(point)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
	N, K = map(int, input().split())
	R, S, P = map(int, input().split())
	T = input()
	point = {"r": P, "s": R, "p": S}
	ans = 0
	for i in range(K):
		flag = False
		idx = i
		ans += point[T[idx]]
		while idx + K < N:
			prev_idx = idx
			idx = idx + K
			if T[prev_idx] != T[idx]:
				ans += point[T[idx]]
				flag = False
			else:
				if flag:
					ans += point[T[idx]]
					flag = False
				else:
					flag = True

	print(ans)