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().strip().split())
r,s,p = map(int, input().strip().split())
T = input().strip()
resp=[0]*n
score=0

for i,t in enumerate(T):
    if t=="r":
        if i<k or resp[i-k]!="p":
            score+=p
            resp[i]="p"
    if t=="s":
        if i<k or resp[i-k]!="r":
            score+=r
            resp[i]="r"
    if t=="p":
        if i<k or resp[i-k]!="s":
            score+=s
            resp[i]="s"
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
ans = 0
key = [False]*n
for i in range(n):
    if i >= k:
        if t[i] == t[i-k] and key[i-k] == False:
            key[i] = True
            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:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
R, S, P = MAP()
T = list(input())

T2 = [[] for i in range(K)]
for i, t in enumerate(T):
    T2[i%K].append(t)

ans = 0
for k in range(K):
    dp = list2d(len(T2[k])+1, 3, 0)
    for i, t in enumerate(T2[k]):
        for j in range(3):
            for l in range(3):
                if i != 0 and j == l:
                    continue
                nxt = 0
                if t == 'r' and l == 2:
                    nxt = P
                elif t == 's' and l == 0:
                    nxt = R
                elif t == 'p' and l == 1:
                    nxt = S
                dp[i+1][l] = max(dp[i+1][l], dp[i][j] + nxt)
    ans += max(dp[len(T2[k])])
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
R, S, P = map(int, input().split())
score = [P, R, S]
t = input()
INF = 10**9

ans = 0
for i in range(k):
    t2 = ""
    j = 0
    while i + j * k <= n-1:
        t2 += t[i+j*k]
        j += 1
    m = len(t2)
    dp = [[-INF, -INF, -INF] for _ in range(m+1)]
    for j in range(3):
        dp[0][j] = 0
    for x in range(m):
        for y in range(3):
            for z in range(3):
                if y != z:
                    if z == 0 and t2[x] == 'r':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    if z == 1 and t2[x] == 's':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    if z == 2 and t2[x] == 'p':
                        dp[x+1][z] = max(dp[x+1][z], dp[x][y] + score[z])
                    dp[x+1][z] = max(dp[x+1][z], dp[x][y])
    num = 0
    for z in range(3):
        num = max(num, dp[m][z])
    ans += num

print(ans)

Submission status: Accepted

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 = [False]*N
    ans = 0
    # hand = {
    #     'r':
    # }
    score = {
        'r':P,
        's':R,
        'p':S
    }
    for i in range(K):
        # print(T[i],score[T[i]])
        ans += score[T[i]]
        A[i] = True
    for i in range(K,N):
        if T[i] != T[i-K]:
            # print(T[i],score[T[i]])
            ans += score[T[i]]
            A[i] = True
        else:
            if A[i-K] != True:
                ans += score[T[i]]
                A[i] = True
            # print(T[i],0)
    print(ans)
    # 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 += '-'





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve(n, k, r, s, p, t) -> int:
    convert = {opp: me for opp, me in zip('rsp', 'prs')}
    score = {'r': r, 's': s, 'p': p}
    my_hands = tuple(convert[_t] for _t in t)

    ret = 0
    for i in range(k):
        w = my_hands[i::k]
        flg = False
        for j, (_w1, _w2) in enumerate(zip(w[:-1], w[1:])):
            if flg:
                flg = False
                continue

            ret += score[_w1]
            if _w1 == _w2:
                flg = True

        if not flg:
            ret += score[w[-1]]

    return ret


def main():
    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()
    print(solve(n, k, r, s, p, t))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

        else:
            if L[i-K] != "s":
                ans += S
                L.append("s")
            else:
                if i+K<=(N - 1):
                    if T[i + K] == "r":
                        L.append("r")
                    else:
                        L.append("p")
                else:
                    L.append("p")
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N-K):
    if T[i] == "s":
        if T[i+K] == "s":
            T[i+K] = "x"
            ans += R
        else:
            ans += R
    elif T[i] == "p":
        if T[i+K] == "p":
            T[i+K] = "x"
            ans += S
        else:
            ans += S
    elif T[i] == "r":
        if T[i+K] == "r":
            T[i+K] = "x"
            ans += P
        else:
            ans += P
    else:
        pass

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

Submission status: Accepted

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

can_use = []
score = 0

def get_score(forwin):
    if forwin == "r":
        score = R
    elif forwin == "s":
        score = S
    elif forwin == "p":
        score = P
    return score

def can_use_hand(forwin):
    if forwin == "r":
        hands = ["s", "p"]
    elif forwin == "s":
        hands = ["r", "p"]
    elif forwin == "p":
        hands = ["r", "s"]
    return hands

for i in range(N):
    if T[i] == "r":
        forwin = "p"
    elif T[i] == "s":
        forwin = "r"
    elif T[i] == "p":
        forwin = "s"
    
    if len(can_use) < K:
        score += get_score(forwin)
        can_use.append(can_use_hand(forwin))
    else:
        can_use_hands = can_use[-K]
        if forwin in can_use_hands:
            score += get_score(forwin)
            can_use.append(can_use_hand(forwin))
        else:
            score += 0
            can_use.append(["r","s","p"])
print(score)

Submission status: Accepted

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

dp = [False] * N

for i in range(N):
    if i >= K and dp[i - K] and T[i] == T[i - K]:
        continue
    dp[i] = True
    
ans = 0
for i in range(N):
    if not dp[i]:
        continue
        
    if T[i] == "r":
        ans += P
    elif T[i] == "s":
        ans += R
    else:
        ans += S
        
print(ans)

Submission status: Accepted

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

score=0
for k in range(K):
    temp_t = T[k::K]
    if len(temp_t)==1:
        if temp_t=='r':
            score += P
        elif temp_t=='s':
            score += R
        else:
            score += S
    else:
        prev = 'x'
        prev_count = 0
        for itr in range(len(temp_t)):
            if temp_t[itr] != prev:
                prev = temp_t[itr]
                prev_count = 1
                if temp_t[itr]=='r':
                    score += P
                elif temp_t[itr]=='s':
                    score += R
                else:
                    score += S
            else:
                prev_count += 1
                if prev_count % 2 == 1:
                    if temp_t[itr]=='r':
                        score += P
                    elif temp_t[itr]=='s':
                        score += R
                    else:
                        score += S
print(score)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
next_r = [1] * N
next_s = [1] * N
next_p = [1] * N
ans = 0
for i in range(len(T)):
        if T[i] == 'r' and next_r[i - K]:
                ans += P
                next_r[i] = 0
        elif T[i] == 's' and next_s[i - K]:
                ans += R
                next_s[i] = 0
        elif T[i] == 'p' and next_p[i - K] :
                ans += S
                next_p[i] = 0
print(ans)

Submission status: Accepted

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

A=[[R,"r","s"],[S,"s","p"],[P,"p","r"]]
A.sort()

rec=[0]*N
for i in range(N):
  if T[i]==A[0][2]:
    if i<K:
      rec[i]=A[0][1]
    elif rec[i-K] !=A[0][1]:
      rec[i]=A[0][1]
for i in range(N):
  if T[i]==A[1][2]:
    if i<K:
      rec[i]=A[1][1]
    elif rec[i-K] !=A[1][1]:
      rec[i]=A[1][1]
for i in range(N):
  if T[i]==A[2][2]:
    if i<K:
      rec[i]=A[2][1]
    elif rec[i-K] !=A[2][1]:
      rec[i]=A[2][1]


x=0
for i in range(N):
  if rec[i]=="r":
    x+=R
  elif rec[i]=="s":
    x+=S
  elif rec[i]=="p":
    x+=P
print(x)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
dic = {"r": p, "s": r, "p": s}
t = list(input())
ans = 0

for i in range(len(t)):
    if i < k:
        ans += dic[t[i]]
    else:
        if not t[i-k] == t[i]:
            ans += dic[t[i]]
        else:
            t[i] = "x"
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N,K=LI()
SC=LI()
T=input()

DP=[[[0]*3 for _ in range(K)] for _ in range(N//K + (1 if N%K else 0))]

for i in range(N):
    cur = i//K
    rest = i%K
    v = 0 if T[i] == 'r' else (1 if T[i] == 's' else 2)
    if cur > 0:
        # 負ける場合
        DP[cur][rest][v] = max(DP[cur-1][rest][(v+1)%3], DP[cur-1][rest][(v+2)%3])
        DP[cur][rest][(v+1)%3] = max(DP[cur-1][rest][v], DP[cur-1][rest][(v+2)%3])
        # 勝つ場合
        DP[cur][rest][(v+2)%3] = max(DP[cur-1][rest][v], DP[cur-1][rest][(v+1)%3]) + SC[(v+2)%3]
    else:
        # 負ける場合
        DP[cur][rest][v] = 0
        DP[cur][rest][(v+1)%3] = 0
        # 勝つ場合
        DP[cur][rest][(v+2)%3] = SC[(v+2)%3]

res = 0

for i in range(N-K, N):
    cur = i//K
    rest = i%K
    # print(DP[cur][rest])
    res += max(DP[cur][rest])
print(res)

Submission status: Accepted

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############

N,K = ip()
R,S,P = ip()
T = input()
ans = 0

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

for i in range(K):
    ans += p[T[i]]
    # print(ans)

for i in range(K,N):
    # print(T[i])
    if T[i] == T[i-K]:
        T = T[:i] + 'c' + T[i+1:]
    else:
        ans += p[T[i]]
    # print(ans)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
cnt=0
t=[[0]for i in range(K)]
for i in range(N):
    if T[i]=="r":
        if t[i%K]!="p":
            cnt+=P
            t[i%K]="p"
        else:
            t[i%K]="0"
    if T[i]=="p":
        if t[i%K]!="s":
            cnt+=S
            t[i%K]="s"
        else:
            t[i%K]="0"
    if T[i]=="s":
        if t[i%K]!="r":
            cnt+=R
            t[i%K]="r"
        else:
            t[i%K]="0"
    

print(cnt)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
i=0
ans=0
hand_dic={'r':'p','s':'r','p':'s'}
point_dic={'r':R,'s':S,'p':P}

for i in range(K):
  _T=T[i::K]
  before=''
  _ans=0
  for t in _T:
    if before != hand_dic[t]:
      _ans+=point_dic[hand_dic[t]]
      before=hand_dic[t]
    else:
      before=''
  ans+=_ans
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
point=list(map(int,input().split()))
t=input()
t+="x"*k
l=[[t[i],1] for i in range(k)]
ans=0
for i in range(k,n+k):
    if l[i%k][0]==t[i]:
        l[i%k][1]+=1
    else:
        ans+=point["spr".index(l[i%k][0])]*((l[i%k][1]+1)//2)
        l[i%k][0]=t[i]
        l[i%k][1]=1
print(ans)

Submission status: Accepted

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

ans = 0

r_ = p
s_ = r
p_ = s

for i in range(k):
    if t[i] == 'r':
        ans += r_
    if t[i] == 's':
        ans += s_
    if t[i] == 'p':
        ans += p_

for i in range(k,n):
    if t[i] == t[i-k]:
        t[i] = 'z'
    else:
        if t[i] == 'r':
            ans += r_
        if t[i] == 's':
            ans += s_
        if t[i] == 'p':
            ans += p_

print(ans)

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
def main():
    N, K = map( int, input().split())
    rsp = list(map( int, input().split()))
    T = input()
    TmodK = [[] for _ in range(K)]
    for i in range(N):
        if T[i] == 'r':
            TmodK[i%K].append(0)
        elif T[i] == 's':
            TmodK[i%K].append(1)
        elif T[i] == 'p':
            TmodK[i%K].append(2)
    ans = 0
    cnt = N//K
    red = N%K
    s = 0
    for i in range(K):
        if i < red:
            s = 1
        else:
            s = 0
        dp = [[0,0,0] for _ in range(cnt+s)]
        dp[0][(TmodK[i][0] + 2)%3] = rsp[(TmodK[i][0] + 2)%3]
        for j in range(1, cnt+s):
            t = TmodK[i][j]

            win = (t+2)%3
            # print(t, win)
            for q in range(3):
                if q == win:
                    dp[j][q] = max(dp[j-1][(q+1)%3] + rsp[win], dp[j-1][(q+2)%3] + rsp[win])
                else:
                    dp[j][q] = max(dp[j-1][(q+1)%3], dp[j-1][(q+2)%3])
#        print(dp)
        ans += max(dp[cnt+s-1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K=map(int, input().split())
R, S, P=map(int, input().split())
T=input()
dp=[[0 for _ in range(N)] for _ in range(3)]
for i in range(N):
  if i>=K:
    dp[0][i]+=max(dp[1][i-K], dp[2][i-K])
    dp[1][i]+=max(dp[0][i-K], dp[2][i-K])
    dp[2][i]+=max(dp[0][i-K], dp[1][i-K])
  dp[0][i]+=R if T[i]=='s' else 0
  dp[1][i]+=S if T[i]=='p' else 0
  dp[2][i]+=P if T[i]=='r' else 0
ans=0
for i in range(N-K, N):
  ans+=max(dp[0][i], dp[1][i], dp[2][i])
print(ans)

Submission status: Accepted

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

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

vD = ["X",]*K
res = 0
for i,s in enumerate(T):
	np = vD[i]
	if s == "r":
		p, pt = "P", P
	if s == "s":
		p, pt = "R", R
	if s == "p":
		p, pt = "S", S

	if p != np:
		res += pt
	else:
		p = "X"
	vD.append(p)

print(res)

Submission status: Accepted

Solution code:
n, k = [ int(v) for v in input().split() ]
r, s, p = [ int(v) for v in input().split() ]
dp_list = [ [0, 0, 0] for i in range(n+1) ]
t = input()
hand = "rsp"
win = [r, s, p]

t_list = [t[i::k] for i in range(k)]
ans = 0
for z in t_list:
    dp_list = [ [0, 0, 0] for i in range(len(z)+1) ]
    for i, v in enumerate(z):
        a = (hand.index(v)-1)%3
        for j in range(3):
            dp_list[i+1][j] = max(dp_list[i][(j+1)%3], dp_list[i][(j-1)%3])
        dp_list[i+1][a] += win[a]

    ans += max(dp_list[-1])

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# abc149/abc149_d
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    R, S, P = i2nn()
    table = {'r': P, 's': R, 'p': S}
    T = list(i2s())
    score = 0
    for i in range(K):
        score += table[T[i]]
    for i in range(K, len(T)):
        if T[i] == T[i - K]:
            T[i] = 'x'
        else:
            score += table[T[i]]
    print(score)
    return

main()

Submission status: Accepted

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

def get_point(computer, player):
    if computer == 'r' and player == 'p':
        return P
    if computer == 's' and player == 'r':
        return R
    if computer == 'p' and player == 's':
        return S
    return 0

ans = 0

import math
for j in range(K):
    computer = [T[i] for i in range(j, N, K)]
    C = len(computer)
    dp = [{'r': 0, 's': 0, 'p': 0} for _ in range(C)]

    for player in ('r', 's', 'p'):
        dp[0][player] = get_point(computer[0], player)

    for i in range(1, C):
        dp[i]['r'] = max(dp[i-1]['s'], dp[i-1]['p']) + get_point(computer[i], 'r')
        dp[i]['s'] = max(dp[i-1]['r'], dp[i-1]['p']) + get_point(computer[i], 's')
        dp[i]['p'] = max(dp[i-1]['s'], dp[i-1]['r']) + get_point(computer[i], 'p')

    ans += max(dp[C-1]['r'], dp[C-1]['s'], dp[C-1]['p'])

print(ans)

Submission status: Accepted

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のときのKおきの和のmax
dp=[[0 for j in range(N)] for i in range(4)]
for j in range(K):
    if T[j]=="r":
        dp[p][j]=P
    elif T[j]=="s":
        dp[r][j]=R
    else:
        dp[s][j]=S
    dp[m][j]=max(dp[r][j],dp[s][j],dp[p][j])
for j in range(K,N):
    for i in range(3):
        dp[i][j]=max(dp[(i+1)%3][j-K],dp[(i+2)%3][j-K])
    if T[j]=="r":
        dp[p][j]+=P
    elif T[j]=="s":
        dp[r][j]+=R
    else:
        dp[s][j]+=S
    dp[m][j]=max(dp[r][j],dp[s][j],dp[p][j])
ans=0
for i in range(K):
    ans+=dp[m][-i-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, K = rl()
R, S, P = rl()
T = list(rs())
dict = {'r': P, 's': R, 'p': S}
def count(X):
    ret = 0
    cur = None
    for a in X:
        if a != cur:
            ret += dict[a]
            cur = a
        else:
            cur = None
    
    return ret

answer = 0
for i in range(1, K+1):
    x = T[i-1::K]
    answer += count(x)

print(answer)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
tt = [[] for _ in range(k)]
# print(n, k, r, s, p, t)

def win(c):
	if c == 'r': return p
	elif c == 's': return r
	elif c == 'p': return s
	return 0

for i, c in enumerate(t):
	tt[i%k].append(c)
# print(tt)

score = 0
for ttt in tt:
	preC = 'X'
	for i, c in enumerate(ttt):
		if c != preC:
			score += win(c)
			preC = c
		else:
			preC = 'X'
print(score)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
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:
        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)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

ten = {'r':p, 's':r, 'p':s}
ans = 0
add = []
for i in range(n):
    if i + 1 > k and t[i - k] == t[i] and add[i - k]:
        add.append(False)
    else:
        ans += ten[t[i]]
        add.append(True)
print(ans)

Submission status: Accepted

Solution code:
def run(N, K, R, S, P, T):
    ans = 0
    main = ''
    for i in range(len(T)):
        t = T[i]
        if i < K:
            if t == 'r':
                ans += P
                main += 'p'
            elif t == 's':
                ans += R
                main += 'r'
            elif t == 'p':
                ans += S
                main += 's'
        else:
            pre_t = main[i - K]
            if t == 'r' and pre_t != 'p':
                ans += P
                main += 'p'
            elif t == 's' and pre_t != 'r':
                ans += R
                main += 'r'
            elif t == 'p' and pre_t != 's':
                ans += S
                main += 's'
            else:
                main += ' '
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

r_flg, s_flg, p_flg = [0]*k,[0]*k,[0]*k

r_cnt,s_cnt,p_cnt = 0,0,0

for i in range(n):
	if t[i] == "r":
		if r_flg[i%k] == 0:
			r_cnt += 1
			r_flg[i%k] = 1
			s_flg[i%k] = 0
			p_flg[i%k] = 0
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0
	if t[i] == "s":
		if s_flg[i%k] == 0:
			s_cnt += 1
			r_flg[i%k] = 0
			s_flg[i%k] = 1
			p_flg[i%k] = 0
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0
	if t[i] == "p":
		if p_flg[i%k] == 0:
			p_cnt += 1
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 1
		else:
			r_flg[i%k] = 0
			s_flg[i%k] = 0
			p_flg[i%k] = 0

ans = r_cnt*P + s_cnt*R + p_cnt*S
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
points = {'r': p, 's': r, 'p': s}
t = input()
ans = 0
for i in range(k):
  f = t[i::k]
  ans += points[f[0]]
  cnt = 1
  for j in range(1, len(f)):
    if f[j] != f[j-1]:
      ans += points[f[j]]
      cnt = 1
    else:
      cnt += 1
      if cnt % 2 == 1:
        ans += points[f[j]]
print(ans)

Submission status: Accepted

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

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

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

        if j + K >= N:
            tmp = -(-tmp // 2)
            if T[j] == "r":
                tmp *= P
            elif T[j] == "s":
                tmp *= R
            else:
                tmp *= S
            ans += tmp
    if i + K >= N:
        if T[i] == "r":
            tmp *= P
        elif T[i] == "s":
            tmp *= R
        else:
            tmp *= S
        ans += tmp

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
 if(i <= K-1):
   if(T[i] == 'r'):   ans += P
   elif(T[i] == 's'): ans += R
   elif(T[i] == 'p'): ans += S
 else:
   if(T[i-K] != T[i]):
     #上から Pでwin #Rでwin #Sでwin
     if(T[i] == 'r'): ans += P
     elif(T[i] == 's'): ans += R
     elif(T[i] == 'p'): ans += S
   if(T[i-K] == T[i]):
     T[i] = 'A'
      
print(ans)

Submission status: Accepted

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

def reverse_rocks(s):
    if 'r' == s:
        return 'p'
    elif 's' == s:
        return 'r'
    elif 'p' == s:
        return 's'

def except_rocks(s):
    if 'r' == s:
        return ['s','p']
    elif 's' == s:
        return ['r','p']
    elif 'p' == s:
        return ['s','r']

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

sum_score = 0
chechs = []
for i in range(N):
    must_not_char = ''
    if i >= K: 
        flag = True
        for c in chechs[i-K]:
            if c != reverse_rocks(T[i]):
                sum_score += score(reverse_rocks(T[i]))
                chechs.append([reverse_rocks(T[i])])
                flag = False
                break
        if flag:
            chechs.append(except_rocks(reverse_rocks(T[i])))
        continue
            
    sum_score += score(reverse_rocks(T[i]))
    chechs.append([reverse_rocks(T[i])])
            
print(sum_score)