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: Time Limit Exceeded

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

t = str(input())

t_ = ''
ans = 0
for i in range(n):
    if i < k:
        if t[i] == 'r':
            t_ += 'p'
            ans += p
        elif t[i] == 's':
            t_ += 'r'
            ans += r
        else:
            t_ += 's'
            ans += s
    elif i+k <= n-1:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i+k] == 'p':
                    t_ += 'r'
                else:
                    t_ += 's'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i+k] == 'p':
                    t_ += 'p'
                else:
                    t_ += 's'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i+k] == 's':
                    t_ += 'p'
                else:
                    t_ += 'r'
    else:
        if t[i] == 'r':
            if t_[i-k] != 'p':
                t_ += 'p'
                ans += p
            else:
                if t[i-k] == 'r':
                    t_ += 's'
                else:
                    t_ += 'r'
        elif t[i] == 's':
            if t_[i-k] != 'r':
                t_ += 'r'
                ans += r
            else:
                if t[i-k] == 'p':
                    t_ += 's'
                else:
                    t_ += 'p'
        else:
            if t_[i-k] != 's':
                t_ += 's'
                ans += s
            else:
                if t[i-k] == 'r':
                    t_ += 'p'
                else:
                    t_ += 'r'
print(ans)
#print(t_)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split())
R,S,P=map(int, input().split())
T=input()
# N,K=7 ,1
# R,S,P=100 ,10, 1
# T="ssssppr"

# N,K=5, 2
# R,S,P=8, 7, 6
# T="rsrpr"


max_ans=T.count("s")*R+T.count("p")*S+T.count("r")*P
for i in range(K):
    tmp_ans=T[i::K]
    for i in range(len((tmp_ans))-1):
        if tmp_ans[i]==tmp_ans[i+1]:
            if tmp_ans[i]=="s":
                max_ans-=R
            if tmp_ans[i]=="p":
                max_ans-=S
            if tmp_ans[i]=="r":
                max_ans-=P
            s=tmp_ans[:]
            tmp_ans = s[:i+1] + 'X' + s[i+2:]

print(max_ans)

Submission status: Time Limit Exceeded

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

i = 0
skipl = []
res = 0
while i < N:
    if i in skipl:
        i += 1
        continue
    if i+K < N and T[i+K] == T[i]:
        skipl.append(i+K)
    res += score[T[i]]
    i += 1

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
for k in range(K):
    dp = [[0] * 3 for i in range(N+1)]
    for i in range(N+1):
        # print(i)
        if k+i*K >= N:
            break

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

        elif T[k+i*K] == 's':
            dp[i+1][0] = max(dp[i][1]+R, dp[i][2]+R)
            dp[i+1][1] = max(dp[i][0], dp[i][2])
            dp[i+1][2] = max(dp[i][0], dp[i][1])

        else:
            dp[i+1][0] = max(dp[i][1], dp[i][2])
            dp[i+1][1] = max(dp[i][0]+S, dp[i][2]+S)
            dp[i+1][2] = max(dp[i][0], dp[i][1])
        # print(max(dp[i]))
    ans += max(dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T =input()
ans = 0
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]:
            T = T[:i] + "R" + T[i+1:]
        if T[i] == "r":
            ans += P
        elif T[i] == "s":
            ans += R
        elif T[i] == "p":
            ans += S
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
#T r→P点
#T s→R点
#T p→S点
r=0;s=1;p=2;m=3
#dp[r,s,p][range(N)]:
#dp[i][j]:j番目まで見たとき、最後がiのときのmax
def calc(U):
    M=len(U)
    dp=[[0 for j in range(M+1)] for i in range(4)]
    j=0
    if U[j]=="r":
        dp[p][j+1]=dp[m][j]+P
    elif U[j]=="s":
        dp[r][j+1]=dp[m][j]+R
    else:
        dp[s][j+1]=dp[m][j]+S
    dp[m][j+1]=max(dp[r][j+1],dp[s][j+1],dp[p][j+1])
    for j in range(1,M):
        for i in range(3):
            dp[i][j+1]=max(dp[(i+1)%3][j],dp[(i+2)%3][j])
        #print(dp[0][j+1],dp[1][j+1],dp[2][j+1])
        if U[j]=="r":
            dp[p][j+1]+=P
        elif U[j]=="s":
            dp[r][j+1]+=R
        else:
            dp[s][j+1]+=S
        dp[m][j+1]=max(dp[r][j+1],dp[s][j+1],dp[p][j+1])
    #for line in dp:
    #    print(line)
    
    return dp[3][-1]
Y=["" for i in range(K)]
for i in range(N):
    Y[i%K]=Y[i%K]+T[i]
#print(Y)
ans=0
for t in Y:
    k=calc(t)
    #print(t,k)
    ans+=k
print(ans)

Submission status: Time Limit Exceeded

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

score_dict = {'r':r, 's':s, 'p':p, 'z':0}
def to_win(x):
    if x=='r':
        return 'p'
    elif x=='s':
        return 'r'
    elif x=='p':
        return 's'
    else:
        return 'z'

def cnt_score(act):
    score = 0
    for a in act:
        score += score_dict[a]
    return score

act = ''
for i in range(len(t)):
    act += to_win(t[i])
act = list(act)
for a in range(k, len(act)):
    if act[a] == act[a-k]:
        act[a] = 'z' 
score = cnt_score(act)
print(score)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N,K = LMIIS()
    R,S,P = LMIIS()
    T = input()
    A = ''
    ans = 0
    for i in range(K):
        if T[i] == 'r':
            A += 'p'
            ans += P
        elif T[i] == 's':
            A += 'r'
            ans += R
        else:
            A += 's'
            ans += S
    for i in range(K,N):
        if T[i] == 'r':
            if A[i-K] != 'p':
                A += 'p'
                ans += P
            else:
                A += '-'
        elif T[i] == 's':
            if A[i-K] != 'r':
                A += 'r'
                ans += R
            else:
                A += '-'
        else:
            if A[i-K] != 's':
                A += 's'
                ans += S
            else:
                A += '-'
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

dic = {'r': p, 's': r, 'p': s}
ans = 0
lst = []
for i in range(0, n):
    if i < k:
        ans += dic[t[i]]
        continue

    if t[i] == t[i - k] and i - k not in lst:
        lst.append(i)
    else:
        ans += dic[t[i]]

print(ans)

Submission status: Time Limit Exceeded

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

score = {'r':P,'s':R,'p':S}
T2 = ''
for i in range(N):
    if i+K<=N-1:
        if a[i]==0 and T[i]==T[i+K]:
            a[i+K]=1
    if a[i]==0:
        T2+=T[i]
ans = 0
for one in T2:
    ans+=score[one]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()

    command = [''] * N
    ans = 0
    for i, t in enumerate(T):
        if t == 'r':
            point = P
            command_candidate = 'p'
        elif t == 's':
            point = R
            command_candidate = 'r'
        else:
            point = S
            command_candidate = 's'

        if i >= K and command[i - K] == command_candidate:
            point = 0
            command_candidate = ''
        ans += point
        command[i] = command_candidate
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def rle(a):
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append([l,a[i]])
            l=1
    ll.append([l,a[-1]])
    return ll
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
ans=0
w=[]
for i in range(k):
    j=i
    b=""
    while j<n:
        b+=t[j]
        j+=k
    w.append(b)
for i in t:
    if i=="r":
        ans+=p
    elif i=="s":
        ans+=r
    else:
        ans+=s
for x in w:
    st=rle(x)
    for i,j in st:
        if j=="r":
            ans-=(i//2)*p
        elif j=="s":
            ans-=(i//2)*r
        else:
            ans-=(i//2)*s
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(str(input()))
ans = 0
S = [-1]*n
for i in range(n):
    if i <= k-1:
        if T[i] == 'r':
            S[i] = 'p'
            ans += p
        elif T[i] == 's':
            S[i] = 'r'
            ans += r
        else:
            S[i] = 's'
            ans += s
    else:
        if T[i] == 'r':
            if S[i-k] != 'p':
                S[i] = 'p'
                ans += p
            else:
                if i+k <= n-1:
                    if T[i+k] == 's':
                        S[i] = 's'
                    elif T[i+k] == 'p':
                        S[i] = 'r'
                    else:
                        S[i] = 'r'
                else:
                    S[i] = 'r'
        elif T[i] == 's':
            if S[i-k] != 'r':
                S[i] = 'r'
                ans += r
            else:
                if i+k <= n-1:
                    if T[i+k] == 'p':
                        S[i] = 'p'
                    elif T[i+k] == 'r':
                        S[i] = 's'
                    else:
                        S[i] = 's'
                else:
                    S[i] = 's'
        else:
            if S[i-k] != 's':
                S[i] = 's'
                ans += s
            else:
                if i+k <= n-1:
                    if T[i+k] == 's':
                        S[i] = 'p'
                    elif T[i+k] == 'r':
                        S[i] = 'r'
                    else:
                        S[i] = 'p'
                else:
                    S[i] = 'p'
print(ans)

Submission status: Accepted

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

    ans = 0
    U = []
    for i, t in enumerate(T):
        if t == 'r':
            if i-K >= 0:
                if U[i-K] == 'p':
                    U.append('rs')
                    continue
            U.append('p')
            ans += P
        elif t == 's':
            if i-K >= 0:
                if U[i-K] == 'r':
                    U.append('sp')
                    continue
            U.append('r')
            ans += R
        else:
            if i-K >= 0:
                if U[i-K] == 's':
                    U.append('rp')
                    continue
            U.append('s')
            ans += S
    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=list(input())
rsp={"r":p,"s":r,"p":s}
sumt=0
chk=[1]*n
for i in range(len(t)):
  sumt+=rsp[t[i]]
  if i>=k and t[i]==t[i-k]:
    chk[i]=chk[i-k]+1
    chk[i-k]=1
ans=sumt
for i in range(len(chk)):
  if chk[i]!=1:
    chg=chk[i]//2
    ans-=(rsp[t[i]])*chg
print(ans)

Submission status: Accepted

Solution code:
def inp(r, s, p, jan):
  if jan == 's':
    return r
  elif jan == 'p':
    return s
  else:
    return p

n, k = map(int, input().split(" "))
r,s,p = map(int, input().split(" "))
t = list(input())
total = 0
for i in range(n):
  if i < k:
    #print(2)
    total += inp(r,s,p,t[i])
  else:
    if t[i] != t[i-k]:
      #print("OK")
      total += inp(r,s,p,t[i])
    else:
      #print("NG")
      t[i] = 'x'
print(total)

Submission status: Accepted

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

t_list = [ c for c in t ]
for i in range( k, n ):
    if t_list[ i ] == t_list[ i - k ]:
        t_list[ i ] = "x"

score = 0
for hand in t_list:
    if hand == "r":
        score += p
    elif hand == "s":
        score += r
    elif hand == "p":
        score += s

print( score )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    hand = [0] * N
    ans = 0
    for i in range(N):
        win = (T[i] - 1) % 3
        if i < K or hand[i - K] != win:
            hand[i] = win
            ans += point[win]
        else:
            hand[i] = -1

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    d = {'r': R, 's': S, 'p': P}
    T = input()
    scores = []
    for t in T:
        if t == 'r':
            scores.append('p')
        elif t == 's':
            scores.append('r')
        else:
            scores.append('s')
    for i in range(N-K):
        if scores[i] == scores[i+K]:
            scores[i+K] = 0
    print(sum([d[v] for v in scores if v in ['p', 'r', 's']]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

moi=[['x'] for i in range(K)]

for i in range(N):
    moi[i%K].append(T[i])

for i in range(K):
    moi[i].pop(0)

moji=''

for i in range(K):
    cnt=0
    for j in range(len(moi[i])):
        if j==0:
            moji+=moi[i][j]
        else:
            if moi[i][j-1]!=moi[i][j]:
                moji+=moi[i][j]
                cnt=0
            if moi[i][j-1]==moi[i][j] and cnt==1:
                moji+=moi[i][j]
                cnt=0
            elif moi[i][j-1]==moi[i][j] and cnt==0:
                cnt+=1

ans=0
for i in range(len(moji)):
    if moji[i]=='r':
        ans+=P
    elif moji[i]=='s':
        ans+=R
    else:
        ans+=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 i in range(k):
  R=0
  S=0
  P=0
  cnt=0
  while i+cnt*k<n:
    if i+cnt*k-k<0:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    elif i+cnt*k-k>=0 and t[i+cnt*k]==t[i+cnt*k-k]:
      if t[i+cnt*k]=='r' and P==0:
        ans+=p
        P=1
      elif t[i+cnt*k]=='r' and P==1:
        P=0
      elif t[i+cnt*k]=='s' and R==0:
        ans+=r
        R=1
      elif t[i+cnt*k]=='s' and R==1:
        R=0
      elif t[i+cnt*k]=='p' and S==0:
        ans+=s
        S=1
      elif t[i+cnt*k]=='p' and S==1:
        S=0
    elif i+cnt*k-k>=0 and t[i+cnt*k]!=t[i+cnt*k-k]:
      if t[i+cnt*k]=='r':
        ans+=p
        P=1
      elif t[i+cnt*k]=='s':
        ans+=r
        R=1
      else:
        ans+=s
        S=1
    cnt+=1
print(ans)

Submission status: Accepted

Solution code:

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

S=input()
li=[1]*(K)
ans=[0,0,0]

for i in range(N-K):
    if S[i]==S[i+K]:
        li[i%K]+=1
    else:
        if S[i]=="r":
            ans[0]+=(li[i%K]+1)//2
        if S[i]=="s":
            ans[1]+=(li[i%K]+1)//2
        if S[i]=="p":
            ans[2]+=(li[i%K]+1)//2
        li[i%K]=1


for j in range(N-K,N):
    if S[j]=="r":
            ans[0]+=(li[j%K]+1)//2
    if S[j]=="s":
            ans[1]+=(li[j%K]+1)//2
    if S[j]=="p":
            ans[2]+=(li[j%K]+1)//2
    
    
print(p*ans[0]+r*ans[1]+s*ans[2])

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
cnt=[0 for _ in range(N)]
score={'r':P,'s':R,'p':S}
for i in range(N):
    if i<K:
        cnt[i]=score[T[i]]
    else:
        if T[i-K]!=T[i]:
            cnt[i]=score[T[i]]
        else:
            if cnt[i-K]==0:
                cnt[i]=score[T[i]]
print(sum(cnt))

Submission status: Accepted

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

for n in range(N-K):
  if T[n+K]==T[n]:
    T[n+K]=""
    
print(P*T.count("r")+R*T.count("s")+S*T.count("p"))

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
R, S, P = map(int,input().split())
T = input()
ans = 0
check = [0] * N
for i in range(N):
  j = T[i]
  if i >= K:
    if j == check[i-K]: continue
  if j == "r": ans += P
  elif j == "s": ans += R
  else: ans += S
  check[i] = j
 
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())
ans = 0
dic = {'p': s, 's': r, 'r': p}
for i in range(k):
  ans += dic[t[i]]
dic_ls = []
for i in range(k, n):
  if t[i] != t[i - k]:
    ans += dic[t[i]]
  else:
    t[i] = 'j'
print(ans)

Submission status: Accepted

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

tt = list(t)
d = {'r':p, 's': r, 'p':s}
dp = [d[t[i]] for i in range(n)]

for i in range(n-k):
    if tt[i]==tt[i+k]:
        dp[i+k] = 0
        tt[i+k] = 'x'

print(sum(dp))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
S,P,R = map(int,input().split())
point = {'s':S,'p':P,'r':R}
T = str(input())
max_point = 0
for i in range(K):
  while i < N:
    if i+K < N and T[i] == T[i+K]:
      max_point += point[T[i]]
      i = i+2*K
    else:
      max_point += point[T[i]]
      i = i+K
print(max_point)

Submission status: Accepted

Solution code:
import math

def main():
	N, K = [int(n) for n in input().split(" ")]
	R, S, P = [int(p) for p in input().split(" ")]
	T = list(input())
	T.reverse()
	cnt = 0
	for i in range(K):
		tmp = []
		for j in range(math.ceil((N - i) / K)):
			if len(tmp) == 0 or T[i + j * K] != tmp[-1][0]:
				if T[i + j * K] == "r":
					tmp.append(["r", P, 1])
				elif T[i + j * K] == "s":
					tmp.append(["s", R, 1])
				elif T[i + j * K] == "p":
					tmp.append(["p", S, 1])
			else:
				tmp[-1][2] += 1
		for l in range(len(tmp)):
			cnt += tmp[l][1] * math.ceil(tmp[l][2] / 2)

	print(cnt)


main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    hand = [0] * N
    ans = 0
    for i, h in enumerate(T):
        win = (h - 1) % 3
        if i >= K and win == hand[i - K]:
            hand[i] = -1
        else:
            hand[i] = win
            ans += point[win]

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = T.translate(str.maketrans('rsp', '012'))
    T = list(map(int, T))

    ans = 0
    for i in range(K):
        vec = T[i::K]
        M = len(vec)

        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == vec[j]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    point = list(map(int, readline().split()))
    T = readline().strip()

    T = list(map(int, T.translate(str.maketrans('rsp', '012'))))

    ans = 0
    for i in range(K):
        M = (N - i + K - 1) // K
        dp = [[0] * 3 for _ in range(M + 1)]
        for j in range(M):
            for k in range(3):
                dp[j + 1][k] = max(dp[j][(k + 1) % 3], dp[j][(k + 2) % 3])
                if (k + 1) % 3 == T[i + j * K]:
                    dp[j + 1][k] += point[k]
        ans += max(dp[M])

    print(ans)
    return


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

scr = 0
#k回目までの処理
for i in range(k):
    if t[i] == "r":
        scr += p
    elif t[i] == "s":
        scr += r
    elif t[i] == "p":
        scr += s
#print(scr)

#k＋１回目以降の処理
#前に出した手のせいで勝てない場合は、次の手の邪魔にならないような手を出す
for i in range(k, n):
    if t[i] == t[i - k]:
        scr += 0
        t[i] = "q"
    else:
        if t[i] == "r":
            scr += p
        elif t[i] == "s":
            scr += r
        elif t[i] == "p":
            scr += s

print(scr)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
N,K=f()
r,s,p=f()
T=list(input())
F=[0]*N
res=0
for i in range(N):
    if F[i]==1:
        continue
    if i+K<N:
        if T[i]==T[i+K]:
            F[i+K]=1
    if T[i]=='s':
        res+=r
    elif T[i]=='p':
        res+=s
    else:
        res+=p
print(res)

Submission status: Accepted

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

w1=''
w2=''
ans=0
for i in range(N):
    if T[i]=='r':
        w1+='p'
    elif T[i]=='p':
        w1+='s'
    else:
        w1+='r'

for i in range(0,K):
    if T[i]=='r':
        w2+='p'
        ans+=P
    elif T[i]=='p':
        w2+='s'
        ans+=S
    else:
        w2+='r'
        ans+=R

if N>=2:
    for i in range(K,N):
        if w1[i]==w2[i-K]:
            w2+='_'
        else:
            if w1[i]=='p':
                w2+='p'
                ans+=P
            elif w1[i]=='s':
                w2+='s'
                ans+=S
            else:
                w2+='r'
                ans+=R
print(ans)

Submission status: Accepted

Solution code:
import copy

N, K = map(int, input().split())
my_R, my_S, my_P = map(int, input().split())
hand_score = {'r': my_P, 's': my_R, 'p': my_S}
T = input()
ans = 0

for i in range(K):
    cnt = (N - i - 1) // K + 1
    dp = {'R': 0, 'S': 0, 'P': 0}
    new_dp = {'R': 0, 'S': 0, 'P': 0}
    for j in range(cnt):
        check = {'r': 0, 's': 0, 'p': 0}
        check[T[(j * K) + i]] = hand_score[T[(j * K) + i]]

        new_dp['R'] = max(dp['S'] + check['s'], dp['P'] + check['p'])
        new_dp['S'] = max(dp['P'] + check['p'], dp['R'] + check['r'])
        new_dp['P'] = max(dp['R'] + check['r'], dp['S'] + check['s'])

        dp = copy.copy(new_dp)
    ans += max(dp.values())
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

d = {"s":r, "p":s, "r":p}
before = deque([])
ans = 0
for i in range(n):
    if i<k:
        before.append(t[i])
        ans += d[t[i]]
    else:
        b = before.popleft()
        if t[i]!=b:
            before.append(t[i])
            ans += d[t[i]]
        else:
            before.append("n")
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()+'*'*K
h=['']*N
f,n='',''
ans=0
for i in range(N):
    if K<=i:f=h[i-K]
    n=T[i+K]
    if T[i]=='r':
        if f=='p':h[i]=n
        else:ans+=P;h[i]='p'
    if T[i]=='p':
        if f=='s':h[i]=n
        else:ans+=S;h[i]='s'
    if T[i]=='s':
        if f=='r':h[i]=n
        else:ans+=R;h[i]='r'
print(ans)

Submission status: Accepted

Solution code:
N,K =  map(int,input().split())
RST = list(map(int,input().split()))
#r:0 s:1 p:2
# a mod K != b mod Kならばaとbは独立に選ぶことができる。
# よってN個の手をmod KによるK個のグループに分け、各グループにおける最大値を求め、最終的にK個のグループの和が答え
T = [0 if i=="r" else 1 if i=="s" else 2 for i in input()]

dp = [[0]*3 for i in range(N)]

#直後の手を見て決める
for i in range(N):
    if i<K:
        for j in range(3):
            # j:1,2,0
            # j==0 -> RST[0]
            #初期値は勝つ手を出し続ける
            if (j+1)%3==T[i]:
                dp[i][j]=RST[j]
    else:
        #現在の出す手
        for j in range(3):
            #K回前に出した手
            for l in range(3):
                if j!=l:
                    if (j+1)%3 == T[i]:
                        dp[i][j] = max(dp[i][j],RST[j]+dp[i-K][l])
                    else:
                        dp[i][j] = max(dp[i][j],dp[i-K][l])

ans = 0
for i in range(N-K,N):
    ans += max(dp[i])

print(ans)

Submission status: Accepted

Solution code:
# D - Prediction and Restriction
N,K = map(int,input().split())
score = list(map(int,input().split()))
T = input()

# dp[i][j]: i回目のじゃんけんで j を出したときの最大の合計点数
# グー: 0, チョキ: 1, パー: 2
dp = [[0]*3 for _ in range(N+1)]

def judge(a,b):
    # パーで勝つとき
    if a == 'r' and b == 2:
        return score[2]
    # グーで勝つとき
    elif a == 's' and b == 0:
        return score[0]
    # チョキで勝つとき
    elif a == 'p' and b == 1:
        return score[1]
    return 0
    
# i 回目のじゃんけん
for i in range(1,N+1):
    # i回目のじゃんけんで j を出したとき
    for j in range(3):
        # i-K 回目のじゃんけんで k を出したとき
        for k in range(3):
            # i = K 回以上のとき
            if i > K:
                # K 回前と同じ手を出したらスルー
                if j == k:
                    continue
            # i < K 回のとき
            else:
                # 勝ち続けるのが最適
                dp[i][j] = judge(T[i-1],j)
                continue
            # K 回前に k を出したときの得点に、j を出して得た得点を加点
            dp[i][j] = max(dp[i][j],dp[i-K][k] + judge(T[i-1],j))

ans = 0
for a in dp[-K:]:
    ans += max(a)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
d = []
for i in range(N):
    if T[i] == "r":
        if i < K or d[i-K] != "p":
            d.append("p")
        else:
            d.append("a")
    elif T[i] == "s":
        if i < K or d[i-K] != "r":
            d.append("r")
        else:
            d.append("a")
    else:
        if i < K or d[i-K] != "s":
            d.append("s")
        else:
            d.append("a")
ans = d.count("r") * R + d.count("s") * S + d.count("p") * P
print(ans)