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())
T = input()
def jan(a):
    if a == 'r':
        return P
    elif a == 's':
        return R
    else:
        return S
ans = 0
for i in range(K):
    check = 'n' + T[i::K]
    h = 0
    for i in range(len(check)-1):
        if check[i] != check[i+1]:
            ans += jan(check[i+1])
            h = 0
        else:
            if h == 0:
                h = 1
            else:
                ans += jan(check[i+1])
                h = 0
print(ans)

Submission status: Accepted

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

for i in range(N-K):
  if T[i] == T[i+K]:
    if len(T) > i+K:
      T = T[:i+K] + '*' + T[i+K+1:]
    else:
      T = T[:i+K] + '*'

score = 0
for i in range(N):
  if T[i] == 'r':
    score += P
  elif T[i] == 's':
    score += R
  elif T[i] == 'p':
    score += S
    
print(score)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, R, S, P, T):
    k_list = [[] for _ in range(K)]
    for i in range(len(T)):
        k_list[i % K].append(T[i])

    ans = 0
    for kl in k_list:
        dp = [[0, 0, 0] for _ in range(len(kl) + 1)]
        for i in range(len(kl)):
            # rock
            dp[i + 1][0] = max(dp[i][1], dp[i][2])
            dp[i + 1][0] += R if kl[i] == 's' else 0
            # scissors
            dp[i + 1][1] = max(dp[i][0], dp[i][2])
            dp[i + 1][1] += S if kl[i] == 'p' else 0
            # paper
            dp[i + 1][2] = max(dp[i][0], dp[i][1])
            dp[i + 1][2] += P if kl[i] == 'r' else 0
        ans += max(dp[-1])

    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()

    # N, K = 10 ** 5, 10 ** 3
    # R, S, P = 100, 90, 5
    # T = 'r' * 10 ** 5

    solve(N, K, R, S, P, T)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
P = {'s':r, 'p':s, 'r':p}
 
mx = r*T.count('s')+s*T.count('p')+p*T.count('r')
if len(T)<k:
    print(mx)
else:
    for i in range(n-k):
        if T[i]==T[i+k]:
            mx -= P[T[i]]
            T[i+k] = '_'
print(mx)

Submission status: Accepted

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

POINT = {"r":P,"s":R,"p":S}
GET = [False] * N

ANS = 0
for i in range(N):
  t = T[i]
  if i <= K-1:
    ANS += POINT[t]
    GET[i] = True
  else:
    if not GET[i-K] or t != T[i-K]:
      ANS += POINT[t]
      GET[i] = True

print(ANS)

Submission status: Accepted

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

commands = [""]*n
ans = 0

for i, t in enumerate(T):
    if t == "p":
        command = "s"
        point = s
    elif t == "s":
        command = "r"
        point = r
    elif t == "r":
        command = "p"
        point = p

    if(i - k) >= 0 and (commands[i-k] == command):
        command = ""
        point = 0

    ans += point
    commands[i] = command
print(ans)

Submission status: Accepted

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

li = [-1]*N #グー、チョキ、パー
se = set(["r","s","p"])
dic = {"r":"p","s":"r","p":"s"}
score = {"r":0,"s":0,"p":0}
#iはあまりでi+k*a
num = N//K
for i in range(K):
    for a in range(num+1):
        nex = i + K*a
        if nex>N-1:
            break 

        rival = T[nex]
        kati = dic[rival]
        
         
        if nex<K:
            li[nex]=kati
            score[kati]+=1
        elif not kati==li[nex-K]:
            li[nex]=kati
            score[kati]+=1
        #elif kati==li[nex-k]:
        # if nex+k>N-1:
        # else:
print(score["r"]*R+score["s"]*S+score["p"]*P)

Submission status: Accepted

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

ans = 0
commands = [''] * N

for i, t in enumerate(T):
    if t == 's':
        command = 'r'
        score = R
    elif t == 'r':
        command = 'p'
        score = P
    elif t == 'p':
        command = 's'
        score = S

    if (i-K >= 0) and (commands[i-K] == command):
        command = ''
        score = 0

    ans += score
    commands[i] = command

print(ans)

Submission status: Accepted

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

ans = []
for i in range(N):
    if T[i] == 's':
        ans.append('r')
    elif T[i] == 'r':
        ans.append('p')
    elif T[i] == 'p':
        ans.append('s')

    if i >= K:
        if ans[i-K] == ans[i]:
            ans[i] = ''

count = 0
for i in range(N):
    # あいこ処理　
    if (T[i] == ans[i]) or ('' == ans[i]):
        continue

    if ans[i] == 'r':
        count += R
    elif ans[i] == 's':
        count += S
    elif ans[i] == 'p':
        count += P

print(count)

Submission status: Accepted

Solution code:
# abc149_d.py
def janken(idx,mine):
    if i >= K:
        if flg[i-K] and T[i-K]==T[i]:
            return 0
    flg[i] = True
    if mine=="r":
        return R
    if mine=="p":
        return P
    if mine=="s":
        return S

N, K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
flg = [False]*N
ans = 0
for i,v in enumerate(T):
    if v=="s":
        ans += janken(i,"r")
    elif v=="r":
        ans += janken(i,"p")
    elif v=="p":
        ans += janken(i,"s")
print(ans)

Submission status: Accepted

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

d = {i:[] for i in range(K)}
score = {"r":P, "s":R, "p":S}

for i in range(len(T)):
    d[i%K].append(T[i])
  
ans = 0
cnt = 1
for val in d.values():
    cnt = 1
    for i in range(len(val)-1):
        if(val[i] == val[i+1]):
            cnt += 1
            
        else:
            if(cnt > 1):
                if(cnt%2 == 0):
                    ans += score[val[i]]*(cnt//2)
                    cnt = 1
                else:
                    ans += score[val[i]]*(cnt//2+1)
                    cnt = 1
            else:
                ans += score[val[i]]
    if(cnt > 1):
        if(cnt%2 == 0):
            ans += score[val[i]]*(cnt//2)
        else:
            ans += score[val[i]]*(cnt//2+1)
    
    else:
        ans += score[val[-1]]
    
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 = [i for i in input().split()[0]]
ts = [t[0+i::k] for i in range(k)]
for t in ts:
  for i in range(len(t)-1):
    if t[i] == t[i + 1]:
      t[i + 1] = 'n'
  for i in range(len(t)):
    if t[i] == 'r':
      t[i] = p
    if t[i] == 's':
      t[i] = r
    if t[i] == 'p':
      t[i] = s
    if t[i] == 'n':
      t[i] = 0
sc = [sum(t) for t in ts]
print(sum(sc))

Submission status: Accepted

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

Submission status: Accepted

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

def main(n,k,r,s,p,t):
    rireki=[]
    tokuten=0
    for i in range(n):
        c=t[i]
        if i<k:
            if(c=='r'):
                rireki.append('p')
                tokuten+=p
                #print('パー',tokuten)
            elif(c=='s'):
                rireki.append('r')
                tokuten+=r
                #print('グー',tokuten)
            elif(c=='p'):
                rireki.append('s')
                tokuten+=s
                #print('チョキ',tokuten)
        else:
            #print(rireki[i-k])
            if c=='r':
                if rireki[i-k]=='p':
                    rireki.append('x')
                    #print('パー以外',tokuten)
                else:
                    rireki.append('p')
                    tokuten+=p
                    #print('パー',tokuten)
            elif c=='s':
                if rireki[i-k]=='r':
                    rireki.append('x')
                    #print('グー以外',tokuten)
                else:
                    rireki.append('r')
                    tokuten+=r
                    #print('グー',tokuten)
            elif c=='p':
                if rireki[i-k]=='s':
                    rireki.append('x')
                    #print('チョキ以外',tokuten)
                else:
                    rireki.append('s')
                    tokuten+=s
                    #print('チョキ',tokuten)
        #print(rireki)
    return tokuten

if __name__=='__main__':
    n,k,r,s,p,t=readinput()
    ans=main(n,k,r,s,p,t)
    print(ans)

Submission status: Accepted

Solution code:
def main():
    N,K = map(int, input().split())
    R,S,P = map(int, input().split())
    T = list(input())
    score=0
    for i in range(N):
        if T[i] == 'r':
            score+=P
            if i+K<N and T[i+K]=='r':
                T[i+K]='x'
        if T[i]=='s':
            score+=R
            if i+K<N and T[i+K]=='s':
                T[i+K]='x'
        if T[i]=='p':
            score+=S
            if i+K<N and T[i+K]=='p':
                T[i+K]='x'
    print(score)







if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
ans = 0
vstd = [0]*n
def jungkeng(x):
  global ans
  if x=='r':
    ans += p
  elif x=='s':
    ans += r
  else:
    ans += s
  return ans
  
for i in range(k):
  ans = jungkeng(t[i])
  vstd[i]=1
for i in range(k,n):
  if t[i]==t[i-k] and vstd[i-k]:
    continue
  ans = jungkeng(t[i])
  vstd[i] = 1
print(ans)

Submission status: Accepted

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

mx = r*T.count('s')+s*T.count('p')+p*T.count('r')
if len(T)<k:
    print(mx)
else:
    for i in range(n-k):
        if T[i]==T[i+k]:
            mx -= P[T[i]]
            T[i+k] = '_'
print(mx)

Submission status: Accepted

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

ans = 0
for i in range(len(t)):
    if i < k:
        if t[i] == "r":
            ans += p
        elif t[i] == "s":
            ans += r
        else:
            ans += s
    else:
        if t[i] == "r":
            if t[i - k] != "r":
                ans += p
            else:
                t[i] = "x"
        if t[i] == "s":
            if t[i - k] != "s":
                ans += r
            else:
                t[i] = "x"
        if t[i] == "p":
            if t[i - k] != "p":
                ans += s
            else:
                t[i] = "x"
                
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t = input()
d = [0]*3*10**5
for i in range(n):
  if t[i] == 'r':
    d[i] = (10**4+1)+p
  if t[i] == 's':
    d[i] = 2*(10**4+1)+r
  if t[i] == 'p':
    d[i] = 3*(10**4+1)+s
ans = 0
for i in range(n):
  if d[i] == d[k+i]:
    d[k+i] = 0
  ans += d[i]%(10**4+1)
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

n,k = iim()
r,s,p = iim()
t = list(input())
ans = 0
flag = [True]*n
for i,item in enumerate(t):
    if i >= k and flag[i-k]:
        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:
            flag[i] = False

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

ans = 0
hands = ["" for _ in range(N)]

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

print(ans)

Submission status: Accepted

Solution code:
def solve():
    def max2(x, y): return x if x >= y else y

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

    Uss = [Ts[i::K] for i in range(K)]

    ans = 0
    for Us in Uss:
#        print('\nUss:', Uss)
        lenU = len(Us)
        dp = [[0]*(3) for _ in range(lenU+1)]
        for i, U in enumerate(Us, start=1):
            # グー
            dp[i][0] = max2(dp[i-1][1], dp[i-1][2])
            if U == 's':
                dp[i][0] += R
            # チョキ
            dp[i][1] = max2(dp[i-1][0], dp[i-1][2])
            if U == 'p':
                dp[i][1] += S
            # パー
            dp[i][2] = max2(dp[i-1][0], dp[i-1][1])
            if U == 'r':
                dp[i][2] += P
#            print(dp[i])

        ans += max(dp[-1])

    print(ans)


solve()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input() + "0"*K
#T = T.replace("r",P).replace("s",R).replace("p",S)
#print(T)
#rn, sn, pn = 0, 0, 0
r, s, p = 0, 0, 0

for i in range(N):
    i = int(i)
    if T[i] == T[i+K]:
        T = T[:i+K] + "0" + T[i+K+1:]
        """
        if T[i] == "r":
            rn += 1


        elif T[i] == "s":
            sn += 1

        else:
            pn += 1
        """
    else:
        if T[i] == "r":
            r += 1

        elif T[i] == "s":
            s += 1

        elif T[i] == "p":
            p += 1

#for i in [rn, sn, pn,r, s, p]:
    #print(i)
#sum = P*(r+(rn//2)) + R*(s+(sn//2)) + S*(p+(pn//2))
#print(T)
sum = P*T.count("r") + R*T.count("s") + S*T.count("p")
print(sum)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    n, k = input_list()
    # R = グー, S = チョキ, P = パー
    R, S, P = input_list()
    t = input()
    tt = list(t)
    for i in range(n - k):
        if tt[i] == t[i+k]:
            tt[i+k] = "x"
    r = tt.count("r") * P
    s = tt.count("s") * R
    p = tt.count("p") * S
    print(r + s + p)


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

did = "-" * k
ans = 0
for i in t:
    if i == "r":
        if did[-k] == "p":
            did += "-"
        else:
            did += "p"
            ans += p
    elif i == "s":
        if did[-k] == "r":
            did += "-"
        else:
            did += "r"
            ans += r
    else:
        if did[-k] == "s":
            did += "-"
        else:
            did += "s"
            ans += s

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
def score(x):
    if x=='r':
        return p
    elif x=='s':
        return r
    else:
        return s
totalscore=0
zero=[0]*(n+1)
for i in range(n):
    if i<=k-1:
        totalscore+=score(t[i])
    else:
        if t[i]!=t[i-k]:
            totalscore+=score(t[i])
        else:
            if zero[i-k]!=0:
                totalscore+=score(t[i])
            else:
                zero[i]=1
                
                
print(totalscore)

Submission status: Accepted

Solution code:
n,k = (int(a) for a in input().split())
r,s,p = (int(a) for a in input().split())
T = list(input())
point = 0
for i in range(n) :
    if i < k :
        pass
    elif  T[i] == T[i-k] :
        T[i] = 0
a = T.count("r")
b = T.count("s")
c = T.count("p")
print(a*p + r*b + c*s)

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

for i in range(K):
  for j in range(i,N,K): 
    if j>=K:
      if T[j]=="r":
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])+P
      elif T[j]=="s":
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])+R
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])
      else:
        dp[j][0]=max(dp[j-K][1],dp[j-K][2])
        dp[j][1]=max(dp[j-K][0],dp[j-K][2])+S
        dp[j][2]=max(dp[j-K][0],dp[j-K][1])
           
    else:
      if T[j]=="r":
        dp[j][2]=P
      elif T[j]=="s":
        dp[j][0]=R
      else:
        dp[j][1]=S
ans=0
for i in range(K): 
  ans+=max(dp[-i-1])
    
print(ans)

Submission status: Accepted

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

history = []
cnt = 0
for i, t in enumerate(T):
    if i < K:
        u = hand[t]
        flag = 1
    elif K <= i < N - K:
        l = i - K
        r = i + K
        if hand[t] == history[l]:
            u = T[r]
            flag = 0
        else:
            u = hand[t]
            flag = 1
    else:
        l = i - K
        if hand[t] == history[l]:
            u = t
            flag = 0
        else:
            u = hand[t]
            flag = 1
    cnt += score[u] * flag
    history.append(u)

print(cnt)

Submission status: Accepted

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

ans = 0
memo = [0]*(n+1)
for i in range(n):
    if t[i] == "r":
        if i-k >= 0 and memo[i-k] == 1:
            pass
        else:
            ans += p
            memo[i] = 1
    elif t[i] == "s":
        if i-k >= 0 and memo[i-k] == 2:
            pass
        else:
            ans += r
            memo[i] = 2
    else:
        if i-k >= 0 and memo[i-k] == 3:
            pass
        else:
            ans += s
            memo[i] = 3
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
D - Prediction and Restriction
https://atcoder.jp/contests/abc149/tasks/abc149_d

"""
import sys


def solve(N, K, R, S, P, T):
    d = {'r': P, 's': R, 'p': S}
    lost = set()
    ans = 0
    for i, ch in enumerate(T):
        if i < K:
            ans += d[ch]
        else:
            if T[i-K] != ch or i-K in lost:
                ans += d[ch]
            else:
                lost.add(i)
    return ans


def main(args):
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()
    ans = solve(N, K, R, S, P, T)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:

#<D>wa
n, k = map(int,input().split())
r, s, p = map(int,input().split())
q = input()
d = {"r":p,"s":r, "p":s}
ans = 0
wins = [False] * k
for i in range(len(q)):
    if not wins[i % k] or q[i] != q[i - k]:
        ans += d[q[i]]
        wins[i % k] = True
    else:
        wins[i % k] = False
print(ans)

Submission status: Accepted

Solution code:
#abc149_d
from sys import stdin

#stdin = open("test/abc149_d_sample-2.in")

N, K = map(int, stdin.readline().strip().split(" "))
R, S, P = map(int, stdin.readline().strip().split(" "))
T = stdin.readline().strip()

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

winmap = {
  "r": "p",
  "s": "r",
  "p": "s"
}

point = 0
T2 = list(T)
#print(N,K)

for i,t in enumerate(T):
  #print(t)
  wintp = "x"
  if i>K-1:
    tp = T2[i-K]
    if tp!="x":
      wintp = winmap[tp]
  
  wint = winmap[t]
  if wint!=wintp:
    point += pmap[wint]
  else:
    T2[i] = "x"

#print(T2)
print(point)

Submission status: Accepted

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

a=[[] for i in range(k)]
for i in range(n):
    a[i%k].append(T[i])

d={'r':p,'s':r,'p':s}
score=0
for j in range(k):
    b=a[j]
    flg=0
    for l in range(len(b)):
        if l==0:
            score+=d[b[l]]
        else:
            if b[l-1]==b[l] and flg==0:
                flg=1
                continue
            elif b[l-1]==b[l] and flg==1:
                score+=d[b[l]]
            elif b[l-1]!=b[l]:
                score+=d[b[l]]
            flg=0
print(score)

Submission status: Accepted

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

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

T=list(input())

a=[[] for i in range(k)]

for i in range(n):
    a[i%k].append(T[i])


score=0
for j in range(k):
    b=a[j]
    flg=0
    for l in range(len(b)):
        if l==0:
            if b[l]=='r':
                score+=p
            elif b[l]=='s':
                score+=r
            elif b[l]=='p':
                score+=s
        else:
            if b[l-1]==b[l] and flg==0:
                flg=1
                continue
            elif b[l-1]==b[l] and flg==1:
                if b[l]=='r':
                    score+=p
                elif b[l]=='s':
                    score+=r
                elif b[l]=='p':
                    score+=s
                flg=0
            elif b[l-1]!=b[l]:
                if b[l]=='r':
                    score+=p
                elif b[l]=='s':
                    score+=r
                elif b[l]=='p':
                    score+=s
                flg=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 = list(input())
points = {"r":r,"s":s,"p":p}
d = {"r":"p","s":"r","p":"s"}
que = deque([None]*k)
ans = 0
for ti in t:
    x = que.popleft()
    if d[ti] != x:
        #print(d[ti])
        que.append(d[ti])
        ans+=points[d[ti]]
    else:
        #print(None)
        que.append(None)
    #que.popleft()
    #print(que,ti)
print(ans)

Submission status: Accepted

Solution code:
def main():
    # r:ぐー,s:ちょき,p:ぱー
    # 相手のn回目の手についてn%Kの値によってグループ分け
    # 連続して同じ手を出せずに最高点を出す方法を知れば良い
    # 最後に全てを足し合わせる
    N,K = map(int, input().split())
    R,S,P = map(int, input().split())
    T = input()
    hands = [[] for i in range(K)]
    for i in range(N):
        if T[i]=='r': hands[i%K].append(0)
        elif T[i]=='s': hands[i%K].append(1)
        else: hands[i%K].append(2)
    # 貪欲に前から勝てば良い..わけではなさそう
    ans = 0
    points = [R,S,P]
    for i in range(K):
        # dp[j][k] := j回目にkを出した時の最大得点
        dp = [[0]*3 for _ in range(len(hands[i])+1)]
        for j in range(len(hands[i])):
            for k in range(3):
                # kが勝ち手かどうかで場合分け
                if hands[i][j]==(k+1)%3:
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+1)%3]+points[k])
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+2)%3]+points[k])
                else:
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+1)%3])
                    dp[j+1][k] = max(dp[j+1][k], dp[j][(k+2)%3])
        ans += max(dp[-1])
    print(ans)

main()

Submission status: Accepted

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

ans = 0
for k in range(K):
    t = T[k::K]
    b = 'a'
    for i in range(len(t)):
        if t[i] == b:
            b = 'a'
        else:
            if t[i] == 'r':
                ans += P
            elif t[i] == 's':
                ans += R
            else:
                ans += S
            b = t[i]
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
t=""
count=0
for i in range(n):
    if i>k-1:
        if T[i]=="r":
            if t[i-k]!="p":
                t=t+"p"
                count+=P
            else:
                t=t+" "
        if T[i]=="s":
            if t[i-k]!="r":
                t=t+"r"
                count+=R
            else:
                t=t+" "
        if T[i]=="p":
            if t[i-k]!="s":
                t=t+"s"
                count+=S
            else:
                t=t+" "
    else:
        if T[i]=="r":
            t=t+"p"
            count+=P
        if T[i]=="p":
            t=t+"s"
            count+=S
        if T[i]=="s":
            t=t+"r"
            count+=R
print(count)

Submission status: Accepted

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

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

t = input()
l = [[] for i in range(k)]

ans = 0

for i in range(n):
    mod = i%k
    l[mod].append(t[i])
        
for i in range(k):
    h = ""
    for j in l[i]:
        if j == "r" and h != "p":
            ans += p
            h = "p"
        elif j == "s" and h != "r":
            ans += r
            h = "r"
        elif j == "p" and h != "s":
            ans += s
            h = "s"
        else:
            h = ""
print(ans)

Submission status: Accepted

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

import numpy as np

#0: g, R
#1: c, S
#2: p, P

ans = 0
L = [[] for _ in range(K)]
for i in range(K):
    L[i] = T[i:N:K]
for l in L:
#    print()
#    print(l)
    lenl = len(l)
    Dp = np.zeros([lenl+1, 3], dtype=int)
    Dp[:][:] = -1
    Dp[0][:] = 0
#    print(Dp)
    ll = l[0]
    if ll == 'r':
        Dp[1][0] = 0
        Dp[1][1] = 0
        Dp[1][2] = P
    elif ll == 's':
        Dp[1][0] = R
        Dp[1][1] = 0
        Dp[1][2] = 0
    elif ll == 'p':
        Dp[1][0] = 0
        Dp[1][1] = S
        Dp[1][2] = 0

    for i in range(1, lenl):
        ll = l[i]
        if ll == 'r':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2])
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0])
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1]) + P
        elif ll == 's':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2]) + R
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0])
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1])
        elif ll == 'p':
            Dp[i+1][0] = max(Dp[i][1], Dp[i][2])
            Dp[i+1][1] = max(Dp[i][2], Dp[i][0]) + S
            Dp[i+1][2] = max(Dp[i][0], Dp[i][1])
#    print(Dp)
    ans += max(Dp[-1][:])
print(ans)

Submission status: Accepted

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

use = [1]*n
for i in range(n-k):
    if not use[i]:
        continue
    if t[i] == t[i+k]:
        use[i+k] = 0

ans = 0
for i in range(n):
    if use[i]:
        x = t[i]
        if x == 'r':
            ans += p
        elif x == '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()
# 相手の手に対して勝ったときの得点
rsp_dic = {"r":P,"s":R,"p":S}
# 相手の手に対して
rsp_dic2 = {0:P,1:R,2:S}
# 自分の手に対して勝てる相手の手
rsp_win = ["s","p","r"]
# 相手の手に対して勝てる自分の手
rsp_lose = {"r":2,"s":0,"p":1}

anslist = [0] * K

modcount = [N//K]*K
for i in range(1,N%K+1):
    modcount[i] += 1

for k in range(K):
    count = modcount[k]
    dp = [[0,0,0] for _ in range(count+1)]
    for i in range(1, count+1):
        dp[i][0] = max(dp[i - 1][1], dp[i - 1][2])
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][2])
        dp[i][2] = max(dp[i - 1][0], dp[i - 1][1])
        if k != 0:
            dp[i][rsp_lose[T[(i-1) * K + k - 1]]] += rsp_dic[T[(i-1) * K + k - 1]]
        else:
            dp[i][rsp_lose[T[i * K + k - 1]]] += rsp_dic[T[i * K + k - 1]]

    anslist[k] = max(dp[-1])

print(sum(anslist))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 14 17:18:33 2020

@author: NEC-PCuser
"""

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

score = 0
your_hand = [-1] * N
# グー 0 チョキ 1 パー 2
score_dict = {"r":P, "s":R, "p":S}


for i in range(len(T)):
    if T[i] != your_hand[i - K]:
        score += score_dict[T[i]]
        your_hand[i] = T[i]

print(score)

Submission status: Accepted

Solution code:
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+K] == T[i]:
        T[i+K] = 0

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

Submission status: Accepted

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

conv_winhand = {'r':'p', 's':'r', 'p':'s'}
conv_score = {'r':R, 's':S, 'p':P}
poi = []

ans = 0
for i in range(N):
    if i < K:
        poi.append(conv_winhand[T[i]])
        ans += conv_score[conv_winhand[T[i]]]
    else:
        if conv_winhand[T[i]] == poi[i-K]:
            tmp = T[i]
            if i + K < N and tmp == conv_winhand[T[i+K]]: tmp = conv_winhand[poi[i-K]]
            poi.append(tmp)
        else:
            poi.append(conv_winhand[T[i]])
            ans += conv_score[conv_winhand[T[i]]]
print(ans)