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= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p'               
print(s)

Submission status: Accepted

Solution code:
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
#T = np.zeros((K,C),dtype = int)
#print(T)
score=0

for i in range(K):
        mem=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                score+=P
                                mem ='r'
                        elif n[j-K]=='r' and mem=='r': mem =""
                        else:
                                score+=P
                                mem ='r'
                elif n[j] =='s':
                        if j < K:
                                score+=R
                                mem ='s'
                        elif n[j-K]=='s' and mem=='s': mem =""
                        else:
                                score+=R
                                mem ='s'
                elif n[j] =='p':
                        if j < K:
                                score+=S
                                mem ='p'
                        elif n[j-K]=='p' and mem=='p': mem =""
                        else:
                                score+=S
                                mem ='p'
                        
print(score)

Submission status: Accepted

Solution code:
n, k = (int(x) for x in input().split())
r, s, p = (int(x) for x in input().split())
t = input()
list = []
for i in range(k):
    i = int(i)
    list.append(t[i::k])
    cr = 0
    cs = 0
    cp = 0
for i in range(k):
    i = int(i)
    list[i] = list[i].replace("rr","rx")
    list[i] = list[i].replace("ss","sx")
    list[i] = list[i].replace("pp","px")

    cr += list[i].count("r")
    cs += list[i].count("s")
    cp += list[i].count("p")

print(cr*p + cs*r + cp*s)

Submission status: Accepted

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

ans = 0
for i in range(K):
    scorer = 0
    prev = ''
    for j in range(i, N, K):
        if T[j] == "r":
            if prev != 'p':
                scorer += P
                prev = 'p'
            else:
                prev = ''
        elif T[j] == "s":
            if prev != 'r':
                scorer += R
                prev = 'r'
            else:
                prev = ''
        elif T[j] == "p":
            if prev != 's':
                scorer += S
                prev = 's'
            else:
                prev = ''
    ans += scorer

print(ans)

Submission status: Accepted

Solution code:
input1 = input()
input2 = input()
input3 = input()

N = int(input1.split()[0])
K = int(input1.split()[1])
R = int(input2.split()[0])
S = int(input2.split()[1])
P = int(input2.split()[2])
T = []
for i in range(N):
    T.append(input3[i])



for i in range(N):
    if T[i] == "r":
        T[i] = "P"
    if T[i] == "s":
        T[i] = "R"
    if T[i] == "p":
        T[i] = "S"



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

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

Submission status: Accepted

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

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

bef = [-1 for i in range(k)]

ans = 0
for i in range(k):
    ans += score[txt[i]]
    bef[i] = txt[i]



for i in range(k,n):
    sup = i%k
    if bef[sup] != txt[i]:
        ans += score[txt[i]]
        bef[sup] = txt[i]
    else:
        bef[sup] = -1

print(ans)

Submission status: Accepted

Solution code:
def win(hand):
        if hand == 'r':
            return 'p'
        elif hand == 'p':
            return 's'
        else:
            return 'r'

def get_best_hand(hand, last_hand):
    won_hand = win(hand)
    if won_hand == last_hand:
        return 'draw'
    else:
        return won_hand
    
def get_point(hand, R, S, P):
        if hand == 'r':
            return R
        elif hand == 'p':
            return P
        elif hand == 's':
            return S
        else:
            return 0

K = int(input().split()[1])
R, S, P = map(int, input().split())
T = input()

last_hand = ''
history = []
point = 0

for t in T:
    last_hand = history[0] if len(history) >= K else ''
    best_hand = get_best_hand(t, last_hand)
    point += get_point(best_hand, R, S, P)
    history.append(best_hand)
    if len(history) > K:
        del history[0]

print(point)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()

l = [""]*k
for i in range(n):
    l[i%k] += t[i]

res = 0
for x in l:
    prev = ""
    score = 0
    for i in range(len(x)):
        if x[i]=="r":
            if prev!="p":
                score += p
                prev = "p"
            else:
                prev = ""
        elif x[i]=="s":
            if prev!="r":
                score += r
                prev = "r"
            else:
                prev = ""
        else:
            if prev!="s":
                score += s
                prev = "s"
            else:
                prev = ""
    res += score

print(res)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
janken = ['' for i in range(K)]
for i in range(N):
    amari = i % K
    janken[amari] = janken[amari] + T[i]
#print(janken)
count = 0
for i in range(K):
    lenN = len(janken[i])
    now = 1
    jan = janken[i][0]
    for j in range(1,lenN):
        if janken[i][j-1] == janken[i][j]:
            now += 1
        elif janken[i][j-1] != janken[i][j]:
            sho = (now + 1) // 2
            if jan == 'r':
                count += sho * P
            elif jan == 's':
                count += sho * R
            elif jan == 'p':
                count += sho * S
            now = 1
            jan = janken[i][j]
    sho = (now + 1) // 2
    if jan == 'r':
        count += sho * P
    elif jan == 's':
        count += sho * R
    elif jan == 'p':
        count += sho * S
print(count)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
R, S, P = map(int, input().split())
t = input()
 
dp = [0] * n
dic = {'r': P, 's': R, 'p': S}
 
for i in range(n):
  if i >= k and (t[i-k] == t[i]) and dp[i-k] != 0:
    continue
  dp[i] = dic[t[i]]
 
print(sum(dp))

Submission status: Accepted

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

# process
tlist = [[] for _ in range(k)]
for i in range(k):
    j = i
    while j < n:
        tlist[i].append(t[j])
        j += k

ans = 0    
for l in tlist:
    pt = ""
    pr = True
    for i in l:
        if i == pt and pr == True:
            pr = False
        else:
            pr = True
            if i == "r": ans += p
            elif i == "s": ans += r
            elif i == "p": ans += s
        pt = i

# output
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = list(input())
point = 0
hantei = 'x'

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

  if k <= i:
    hantei = t[i-k]

  if t[i] != hantei:
    if t[i] == 'r':
      point += r
    elif t[i] == 's':
      point += s
    elif t[i] == 'p':
      point += p
  else:
    t[i] = 'x'

print(point)

Submission status: Accepted

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

def score(T):
    sa = 0
    x = 0
    for i in range(len(T)):
        if x == 1 and T[i] == T[i-1] :
            if T[i] == "r":
                sa += P
                x = 0
            elif T[i] == "s":
                sa += R
                x = 0
            elif T[i] == "p":
                sa += S
                x = 0
            
        elif i > 0 and T[i] == T[i-1] :
            x = 1
        elif T[i] == "r":
            sa += P
            x = 0
        elif T[i] == "s":
            sa += R
            x = 0
        elif T[i] == "p":
            sa += S
            x = 0
    return sa
ans = 0
for k in range(K):
    ans += score(T[k::K])

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

def takahashiKun(t_i, cnt):
    if cnt>=k and t[cnt-k]==t_i:
        t[cnt] = "X"
        return int(0)
    else:
        if   t_i=="p": return s
        elif t_i=="r": return p
        else         : return r

score = 0
for cnt, t_i in enumerate(t):
    score += takahashiKun(t_i, cnt)

print(score)

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())[:n]

res = 0

for i in range(k):
    dp = [0,0,0]
    idx = i
    while idx < n:
        dp[0],dp[1],dp[2] = max(dp[1],dp[2]),max(dp[0],dp[2]),max(dp[0],dp[1])
        if t[idx] == 's':
            dp[0] += r
        elif t[idx] == 'p':
            dp[1] += s
        else:
            dp[2] += p
        idx += k
    res += max(dp)
print(res)

Submission status: Accepted

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

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

    t = list(input())
    ans = 0
    for i in range(len(t)):
        if i < k:
            if t[i] == "r":

                ans += r
            if t[i] == "s":
                ans += s
            if t[i] == "p":
                ans += p
        else:
            if t[i] != t[i -k]:
                if t[i] == "r":
                    ans += r
                if t[i] == "s":
                    ans += s
                if t[i] == "p":
                    ans += p
            else:
                t[i] = "o"

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

point = 0
histry = []

for i in range(N):
    if i >= K and T[i] == histry[i-K]:
        # print('con')
        histry.append(N)
        continue
    else:
        if T[i] == 'r':
            point += P
            histry.append('r')
            # print('P')
        elif T[i] == 's':
            point += R
            histry.append('s')
            # print('R')
        elif T[i] == 'p':
            point += S
            histry.append('p')
            # print('S')

print(point)

Submission status: Accepted

Solution code:


def resolve():
    N,K=(int(i) for i in input().split())
    R,S,P=(int(i) for i in input().split())
    pdic={'r':P,'s':R,'p':S}
    T=list(input())
    points=sum([pdic[i] for i in T[:K]])
    for i in range(K):
        for j in range(i+K,N,K):
            if T[j]!=T[j-K]:
                points+=pdic[T[j]]
            else:
                T[j]='x'
    print(points)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

ans = 0
win = [1] * N

for i in range(N):
  if T[i] == "r":
    ans += P
    x = P
  elif T[i] == "s":
    ans += R
    x = R
  else:
    ans += S
    x = S
  if i >= K:
    if (T[i] == T[i - K]) and (win[i - K] == 1):
      ans -= x
      win[i] = 0
  #print(ans)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
point = list(map(int,input().split()))
t = input().translate(str.maketrans('rsp','201'))
c = 0
for i in range(k):
  dp = [0]*3
  for j in range(i,n,k):
    pdp,dp = dp,[0]*3
    for l in range(3):
      if l == int(t[j]):
        dp[l] = max(pdp[(l+1)%3],pdp[(l+2)%3]) + point[l]
      else:
        dp[l] = max(pdp[(l+1)%3],pdp[(l+2)%3])
  c += max(dp)
print(c)

Submission status: Accepted

Solution code:
num, back = map(int, input().split(' '))
r_point, s_point, p_point = map(int, input().split(' '))
t = input()
j_dict = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': r_point, 's': s_point, 'p': p_point, 'e': 0}
result = [j_dict[i] for i in t]
for i in range(back, num):
    if result[i] == result[i - back]:
        result[i] = 'e'
print(sum(score[i] for i in result))

Submission status: Accepted

Solution code:
n, k = tuple(map(int, input().split(' ')))
r, s, p = tuple(map(int, input().split(' ')))
t = (input())
point = {'r':p, 's':r, 'p':s, 'any':0}
st = {i:t[i] for i in range(k)}
ans1 = sum(map(lambda x: point[x], t[:k]))
ans2 = 0
for i in range(k, n):
    h = t[i]
    m = i%k
    if st[m] != h:
        ans2 += point[h]
        st[m] = h
    else:
        st[m] = 'any' 
print(ans1 + ans2)

Submission status: Accepted

Solution code:
N, K = [int(_) for _ in input().split()]
points = [int(_) for _ in input().split()]
T = list(input())

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

ans = 0
for k in range(K):
    pre = -1
    for i in range(k, N, K):
        win_i = (PAT.index(T[i]) + 2) % 3
        if pre != win_i:
            ans += points[win_i]
            pre = win_i
        else:
            pre = -1

print(ans)

Submission status: Accepted

Solution code:
n, k = tuple(map(int, input().split(' ')))
r, s, p = tuple(map(int, input().split(' ')))
t = (input())
def char2int(c):
    if c == 'r':
        return p
    elif c == 's':
        return r
    else:
        return s
st = {i:t[i] for i in range(k)}
ans1 = sum(map(char2int, t[:k]))
ans2 = 0
for i in range(k, n):
    h = t[i]
    m = i%k
    if st[m] != h:
        ans2 += char2int(h)
        st[m] = h
    else:
        st[m] = 'any' 
print(ans1 + ans2)

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

Submission status: Accepted

Solution code:
def calc(x):
    if x == 'r': n = P
    elif x == 's': n = R
    else: n = S
    
    return n

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

T = input()

ans = 0
prev = [False] * K

for i in range(N):
    if i < K: ans += calc(T[i])
    else:
        if T[i] == T[i-K]:
            if not prev[i%K]: prev[i%K] = True
            else:
                ans += calc(T[i])
                prev[i%K] = False
            
        else: 
            ans += calc(T[i])
            prev[i%K] = False
            
print(ans)

Submission status: Accepted

Solution code:
n1, n2 = [int(i) for i in input().split()]
n3, n4, n5= [int(i) for i in input().split()]
s6 = input()

r8 = 0
a9 = [0] * n1
for i7 in range(n1):
    if i7 >= n2:
        if s6[i7] == s6[i7 - n2]:
            if a9[i7 - n2] == 0:
                a9[i7] = 1
                continue
    if s6[i7] == "r":
        r8 += n5
    elif s6[i7] == "s":
        r8 += n3
    else:
        r8 += n4
print(r8)

Submission status: Accepted

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

d = {'r': p, 's': r, 'p': s}
ans = 0

for i in range(k):
    last = ''
    j = i
    while j < n:
        if t[j] != last:
            ans += d[t[j]]
            last = t[j]
        else:
            last = ''
        j += k

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
R,S,P = map(int, input().split())
T = [c for c in input()]

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

def win(enemy_hand):
  if enemy_hand == "r":
    return "p", P
  elif enemy_hand == "s":
    return "r", R
  elif enemy_hand == "p":
    return "s", S
  else:
    raise ValueError("HAND IS WRONG")

total_point = 0
my_hands = []
for enemy_hand in T:
  my_hand, point = win(enemy_hand)
  total_point += point
  my_hands.append(my_hand)

for i in range(K, N):
  my_hand = my_hands[i]
  if my_hand == my_hands[i-K]:
    total_point -= hand_points[my_hand]
    my_hands[i] = "any"

print(total_point)

Submission status: Accepted

Solution code:
N, K = input().split()
N, K = int(N), int(K)

R, S, P = input().split()
R, S, P = int(R), int(S), int(P)

T = input()


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

def seq_score(T):
    score = 0
    cnt = 1
    c0 = T[0]
    for c in T[1:]:
        if c == c0:
            cnt += 1
        else:
            score += d[c0] * ((cnt+1) // 2)   
            c0 = c
            cnt = 1

    score += d[c0] * ((cnt+1) // 2)
    return score

score = 0
for k in range(K):
    score += seq_score(T[k::K])

print(score)

Submission status: Accepted

Solution code:
import math

if __name__=="__main__":
    M=0
    n,k=input().split()
    r,s,p=input().split()
    t=input()
    t=list(t)
    n,k,r,s,p=int(n),int(k),int(r),int(s),int(p)
    for i in range(0,n):
       if t[i]=='r':
           t[i]='p'
           M+=p
       elif t[i]=='s':
           t[i]='r'
           M+=r
       else:
           t[i]='s'
           M+=s
    for i in range(k,n):
        if t[i]==t[i-k]:
            if t[i]=='r':
                M-=r
            elif t[i]=='s':
                M-=s
            else:
                M-=p
            t[i]='u'
    print(M)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
point = 0
win = [0 for i in range(len(t))]

for i in range(len(t)):
  if t[i] == 'r':
    if (t[i-k] != 'r') or (win[i-k] != 1):
      point += p
      win[i] = 1

  if t[i] == 's':
    if (t[i-k] != 's') or (win[i-k] != 1):
      point += r
      win[i] = 1
    
  if t[i] == 'p':
    if (t[i-k] != 'p') or (win[i-k] != 1):
      point += s
      win[i] = 1

print(point)

Submission status: Accepted

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

score = 0
char_list = list(t)
v_list = []
janken = {0:r, 1:s, 2:p}
dp = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
  if char_list[i] == 'r':
    v_list.append(2)
  elif char_list[i] == 's':
    v_list.append(0)
  else:
    v_list.append(1)

for i in range(k):
  dp[i][v_list[i]] = janken[v_list[i]]
  for j in range(i+k, n, k):
    for l in range(3):
      if v_list[j] == l:
        dp[j][l] = max(dp[j-k][(l+1)%3], dp[j-k][(l+2)%3]) + janken[l]
      else:
        dp[j][l] = max(dp[j-k][(l+1)%3], dp[j-k][(l+2)%3])

for i in range(1, k+1):
  score += max(dp[n-i][0], max(dp[n-i][1], dp[n-i][2]))

print(score)

Submission status: Accepted

Solution code:
store_score = {}
win_hands = {"s": "r", "r": "p", "p": "s"}
scores = {}

def resolve():
    # k飛ばしで文字列を抽出し、それらの文字列ごとに最適なpathを考える
    N, K = map(int, input().split())
    R, S, P = map(int, input().split())
    T = input()

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

    total_score = 0
    for i in range(K):
        # print(T[i::K])
        target_text = T[i::K]
        # print(target_text)
        target_text += "o"

        previous_select = "o"
        local_score = 0
        for j in range(len(target_text) - 1):
            previous_select, score = judge(previous_select, target_text[j], target_text[j+1])
            local_score += score    

        total_score += local_score

    print(total_score)
                        

def judge(previous_select, current_machine, next_machine):
    # print(previous_select, current_machine, next_machine)
    ideal_hand = win_hands[current_machine]
    if previous_select == ideal_hand:
        # もし前回と同じハンドが理想ハンドならば、次回のハンドに邪魔しない手を選ぶ
        select = next_machine
        score = 0
    else:
        select = ideal_hand
        score = scores[select]
    # print(select, score)
    return select, score


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
r,s,p=map(int, input().split())
t=list(input())
hl=[]
ans=0
for i in range(n):
		if i<k:
			if t[i]=='r':
				ans+=p
				hl.append('r')
			elif t[i]=='p':
				ans+=s
				hl.append('p')
			elif t[i]=='s':
				ans+=r
				hl.append('s')
		elif i>=k:
			if t[i]==hl[i-k]:
				hl.append('a')
			else: 
				if t[i]==('r'):
					ans+=p
					hl.append('r')
				elif t[i]=='p':
					ans+=s
					hl.append('p')
				elif t[i]=='s':
					ans+=r
					hl.append('s')
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
win = [True] * len(T)

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

sum = 0
for i in range(K):
  sum += dic[T[i]]
for i in range(K, len(T)):
  if T[i] == T[i - K] and win[i - K]:
    win[i] = False
  else:
    sum += dic[T[i]]
print(sum)

Submission status: Accepted

Solution code:
"""
N K
R S P
T
"""

ln1 = input().split()
ln2 = input().split()
ln3 = input().split()

N, K = int(ln1[0]), int(ln1[1])
R, S, P = int(ln2[0]), int(ln2[1]), int(ln2[2])
T = ln3[0]

point = 0
result = []

def janken_result(te):
    if te == "r":
        return P
    elif te == "s":
        return R
    elif te == "p":
        return S
    else:
        exit(-1)

for i in range(N):
    if i < K:
        point += janken_result(T[i])
        result.append(1)
    elif T[i] != T[i-K]:
        point += janken_result(T[i])
        result.append(1)
    else:
        if result[i-K] == 0:
            point += janken_result(T[i])
            result.append(1)
        else:
            result.append(0)

print(point)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
R,S,P = map(int,input().split()) #point
T = input() #r or s or p

def janken_win(x):
    if x == "r": 
        return "p"
    if x == "s": 
        return "r"
    if x == "p": 
        return "s"

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


point = 0
janken = []
## mod group
for text in range(len(T)):
    if text < K : 
        janken.append(janken_win(T[text]))
        point = point + win_point(T[text])

    elif T[text] == T[text-K]:
        if janken[text-K] == janken_win(T[text]):
            janken.append("a")
        
        else:
            janken.append(janken_win(T[text]))
            point = point + win_point(T[text])
    
    else:
        janken.append(janken_win(T[text]))
        point = point + win_point(T[text])



print(point)

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 t[i] == 'r':
        ans += p
        if i+k < n and t[i+k] == 'r':
            t[i+k] = '0'
    elif t[i] == 's':
        ans += r
        if i+k < n and t[i+k] == 's':
            t[i+k] = '0'
    elif t[i] == 'p':
        ans += s
        if i+k < n and t[i+k] == 'p':
            t[i+k] = '0'
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())

A = []
for i in range(N) :
  if T[i] == "r" :
    A.append("P")
  elif T[i] == "s" :
    A.append("R")
  else :
    A.append("S")
#print (A)

for i in range(N) :
  if i+K < N :
    if A[i] == A[i+K] :
      A[i+K] = "N"
#print (A)

sum = 0
for i in range(N) :
  if A[i] == "P" :
    sum += P
  elif A[i] == "R" :
    sum += R
  elif A[i] == "S" :
    sum += S    
print (sum)

Submission status: Accepted

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

h = {'r': 'p', 's': 'r', 'p': 's'}
score = {'r': r, 's': s, 'p': p, 'e': 0}

z = [h[c] for c in t]
for i in range(k, n):
    if z[i]==z[i-k]: z[i] = 'e'

x = sum(score[v] for v in z)

print(x)

Submission status: Accepted

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

ans = 0
for i in range(n):
    if i-k >= 0:
        if t[i] == t[i-k]:
            t[i] = 'change'
        else:
            continue

for j in range(n):
    if t[j] == 'r':
        ans += s[2]
    elif t[j] == 's':
        ans += s[0]
    elif t[j] == 'p':
        ans += s[1]
    else:
        continue
print(ans)

Submission status: Accepted

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

for i in range(n):
  if i < k:
    val += dict[t[i]]
  elif t[i-k] != t[i]:
    val += dict[t[i]]
  else:
    t[i] = " "

print(val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
m = dd(int)
m1 = dd(int)

m['r']='p'
m['p']='s'
m['s']='r'

m1['r']=p
m1['p']=s
m1['s']=r
res=0
for i in range(k):
	res+=m1[t[i]]
	t = t[:i]+m[t[i]]+t[i+1:]
	
	
#print(res)
	
for i in range(k,n):
	#lose
	#print(t)
	if t[i-k] == m[t[i]]:
		t = t[:i]+'x'+t[i+1:]
		#print(t)
	else:
		res+=m1[t[i]]
		t = t[:i]+m[t[i]]+t[i+1:]
	#print(res)	
print(res)

Submission status: Accepted

Solution code:
n_matches, k = map(int, input().split())
point_r, point_s, point_p = map(int, input().split())
ts = list(input())

total_point = 0
t_of_mine = []

for i in range(len(ts)):
    t = ts[i]

    if i < k:
        k_prev = '*'
    else:
        k_prev = t_of_mine[i - k]

    if t == 'r' and k_prev != 'p':
        total_point = total_point + point_p
        t_of_mine.append('p')
    elif t == 's' and k_prev != 'r':
        total_point = total_point + point_r
        t_of_mine.append('r')
    elif t == 'p' and k_prev != 's':
        total_point = total_point + point_s
        t_of_mine.append('s')
    else:
        t_of_mine.append('*')

print(total_point)

Submission status: Accepted

Solution code:
#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, K = getlist()
	R, S, P = getlist()
	T = input()
	ans = 0
	DPr = [0] * N
	DPs = [0] * N
	DPp = [0] * N
	#初期化
	for k in range(K):	
		if T[k] == "r":
			DPp[k] = P
		elif T[k] == "s":
			DPr[k] = R
		else:
			DPs[k] = S
	for i in range(K, N):
		now = i
		pre = i - K
	
		if T[now] == "r":
			DPr[now] = max(DPs[pre], DPp[pre])
			DPs[now] = max(DPr[pre], DPp[pre])
			DPp[now] = max(DPr[pre] + P, DPs[pre] + P)
		elif T[now] == "s":
			DPr[now] = max(DPs[pre] + R, DPp[pre] + R)
			DPs[now] = max(DPr[pre], DPp[pre])
			DPp[now] = max(DPr[pre], DPs[pre])
		else:
			DPr[now] = max(DPs[pre], DPp[pre])
			DPs[now] = max(DPr[pre] + S, DPp[pre] + S)
			DPp[now] = max(DPr[pre], DPs[pre])
	# print(DPr)
	# print(DPs)
	# print(DPp)
	for i in range(K):
		ans += max(DPr[N - 1 - i], DPs[N - 1 - i], DPp[N - 1 - i])

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:


def solve_sub(t, r, s, p):
    prev = "@"
    res = 0
    win = {
        "r": p,
        "s": r,
        "p": s,
    }
    for c in t:
        if c == prev:
            prev = "@"
            continue
        else:
            res += win[c]
            prev = c
    return res


def main():
    N, K = [int(x) for x in input().split()]
    R, S, P = [int(x) for x in input().split()]
    T = input()

    groups = [""] * K
    for i in range(len(T)):
        groups[i%K] += T[i]
    # print(groups)

    ans = 0
    for g in groups:
        ans += solve_sub(g, R, S, P)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve(string):
    *nkrsp, t = string.split()
    t = list(t)
    n, k, *rsp = map(int, nkrsp)
    b = {k: v for k, v in zip("spr", rsp)}
    ans = sum([b[k] for k in t[:k]])
    for i, _t in enumerate(t[k:]):
        if _t == t[i]:
            t[i + k] = "x"
        else:
            ans += b[_t]
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))