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:
# ABC149d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)

    n, k = map(int, input().split())
    r, s, p = map(int, input().split())
    t = input()[:-1]
    flag = [False]*n
    # dp[i][T][J]=k手前にJを出してi番目でTを出すときの最高得点
    # dp[i][0][1]=dp[i-1][0][1]

    def tokuten(aite):
        if aite == 'r':
            return p
        if aite == 's':
            return r
        if aite == 'p':
            return s

    ans = t.count('r')*tokuten('r')+t.count('s') * \
        tokuten('s')+t.count('p')*tokuten('p')
    for i in range(n):
        if i - k >= 0 and t[i] == t[i - k] and not flag[i-k]:
            ans -= tokuten(t[i])
            flag[i] = True
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,k=map(int,readline().split())
r,s,p=map(int,readline().split()) #gu cyoki pa
t=input()
dic={'r':p,'s':r,'p':s}
dic2={'r':'p','s':'r','p':'s'}
dic3={'r':'s','s':'p','p':'r'}
h=''

c=0
for i in range(k):
  c+=dic[t[i]]
  h+=dic2[t[i]]
for i in range(k,n):
  if h[i-k]!=dic2[t[i]]:
      c+=dic[t[i]]
      h+=dic2[t[i]]
  else:
    if i+k<n:
      if dic2[t[i+k]]!=t[i]:
        h+=t[i]
      else:
        h+=dic3[t[i]]
    else:
      h+=t[i]
print(c)

Submission status: Accepted

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

#dp = []*n
v = [0]*n
for idt in range(n):
    if idt >= k:
        if t[idt] == t[idt-k] and v[idt-k] != 0:
            v[idt] = 0
            continue
    if t[idt] == 'r':
        v[idt] = p
    elif t[idt] == 's':
        v[idt] = r
    else:
        v[idt] = s
        

ans = sum(v)
print(ans)

Submission status: Accepted

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

counter = 0
flag = -1

for i in range(k):
    flag = -1
    for j in range(i,n,k):
        if flag != 0 and t[j] == "r":
            flag = 0
            counter += p
            #print(counter,i,j,t[j])

        elif flag != 1 and t[j] == "s":
            flag = 1
            counter += r
            #print(counter,i,j,t[j])
            

        elif flag != 2 and t[j] == "p":
            flag = 2
            counter += s
            #print(counter,i,j,t[j])

        else:
            flag = -1

print(counter)

Submission status: Accepted

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

ans=0
for o in range(K):
    pre = 'X'
    for i in range(o,N,K):
        h=T[i]
        if h!=pre:
            ans+=score[h]
            pre=h
        else:
            pre='X'

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = list(input())
dp = [[0]*3 for _ in range(n)]
for i, t in enumerate(T):
    if t == "r":
        if i < k:
            dp[i][0] += 0
            dp[i][1] += 0
            dp[i][2] += p
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2])
            dp[i][1] = max(dp[i-k][0], dp[i-k][2])
            dp[i][2] = max(dp[i-k][0], dp[i-k][1]) + p
    elif t == "s":
        if i < k:
            dp[i][0] += r
            dp[i][1] += 0
            dp[i][2] += 0
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2]) + r
            dp[i][1] = max(dp[i-k][0], dp[i-k][2])
            dp[i][2] = max(dp[i-k][0], dp[i-k][1])
    else:
        if i < k:
            dp[i][0] += 0
            dp[i][1] += s
            dp[i][2] += 0
        else:
            dp[i][0] = max(dp[i-k][1], dp[i-k][2])
            dp[i][1] = max(dp[i-k][0], dp[i-k][2]) + s
            dp[i][2] = max(dp[i-k][0], dp[i-k][1])
ans = 0
for i in range(k):
    ans += max(dp[n-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())
x = 0
for i in range(len(t)):
    if i >= k and t[i] == t[i-k]:
        t[i] = ' '
    if t[i] == "r":
        x += p
    elif t[i] == "s":
        x += r
    elif t[i] == "p":
        x += s
print(x)

Submission status: Accepted

Solution code:
n, k = map(int, input().split(" "))
r, s, p = map(int, input().split(" "))
t = input()
t = list(t)
ans = 0
key = []

for i in range(k):
    key.append(t[i::k])
m = len(key)
#print(key)
for i in range(m):
    flag = 0
    for j,tem in enumerate(key[i]):
        if j == 0:
            if tem == 'r':
                ans += p
            elif tem == 's':
                ans += r
            else:
                ans += s
        elif j < len(key[i])-1:
            if (flag == 0)and(tem == key[i][j-1]):
                ans = ans
                flag = 1
            elif (flag == 1) and (tem == key[i][j-1]):
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s 
                flag = 0
            else:    
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s
                if tem == key[i][j+1]:
                    flag = 0
        else:
            if (flag == 0)and(tem == key[i][j-1]):
                ans = ans
                flag = 1
            elif (flag == 1) and (tem == key[i][j-1]):
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s 
                flag = 0
            else:    
                if tem == 'r':
                    ans += p
                elif tem == 's':
                    ans += r
                else:
                    ans += s
                              
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 = input()

outputs = ""
score = 0

for i in range(N):
    if (i >= K) and (T[i - K] == T[i]) and (outputs[i - K] == "w"):
        score += 0
        outputs += "l"
        continue

    if T[i] == "r":
        score += P
        outputs += "w"
    elif T[i] == "s":
        score += R
        outputs += "w"
    elif T[i] == "p":
        score += S
        outputs += "w"
    

print(score)

Submission status: Accepted

Solution code:
def points(me, you, R, S, P):
    hand_map = {
        'r':'p',
        's':'r',
        'p':'s'
    }
    if hand_map[you] == me:
        if me == 'r':
            return R
        elif me =='p':
            return P
        elif me =='s':
            return S
    else:
        return 0
    
def main():
    N, K = list(map(int, input().split()))
    R, S, P = list(map(int, input().split()))
    T = input()
    Ts = list(T)

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

    memo = []
    cum = 0
    for i, t in enumerate(Ts):
        hand = hand_map[t]
        
        if i >= K:
            if hand == memo[i - K]:
                hand = 'na'
            

        memo.append(hand)
        cum += points(hand, t, R, S, P)
        # print(cum, i, t, memo)


    print(cum)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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


def get_score(t):
    if t == "r":
        return score["p"]
    if t == "s":
        return score["r"]
    if t == "p":
        return score["s"]

l = [-1] * n

for i in range(n):
    if l[i] >= 0:
        continue

    if i > n - k - 1:
        l[i] = get_score(t[i])
        continue

    if t[i] == t[i + k]:
        l[i] = get_score(t[i])
        l[i + k] = 0
    else:
        l[i] = get_score(t[i])


print(sum(l))

Submission status: Accepted

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

def get_score(te):
    if te == "r":
        return p
    elif te == "s":
        return r
    elif te == "p":
        return s
    else:
        return 0

ans = 0
for i in range(k):
    tmp = list(T[i::k])
    # print(tmp)
    for j in range(len(tmp) -1):
        if tmp[j] == tmp[j+1]:
            tmp[j+1] = "x"
    # print(tmp)
    for j in range(len(tmp)):
        ans += get_score(tmp[j])
    # print(ans)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
act = np.array(list(t.replace('r','P').replace('s','R').replace('p','S')))
for a in range(k, len(act)):
    if act[a] == act[a-k]:
        act[a] = 'Z'
score_dict = {'R':r, 'S':s, 'P':p, 'Z':0}
score = np.array([score_dict[i] for i in act])
print(score.sum())

Submission status: Accepted

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

def get_winpos(dealer):
  if dealer == 'r':
    return 'p'
  elif dealer == 's':
    return 'r'
  else:
    return 's'
def calc_point(rsp):
  if rsp == 'r':
    return R
  elif rsp == 's':
    return S
  else:
    return P
  
winpos = [get_winpos(s) for s in T]
point = 0
check = 'any'
for i, pos in enumerate(winpos):
  if i-K >= 0:
    check = winpos[i-K]
  if winpos[i] == check:
    winpos[i] = 'any'
  else:
    point += calc_point(winpos[i])
print(point)

Submission status: Accepted

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

ans=0
win=[]
for tt in t:
 if tt=='r':
  win+='p'
  ans+=P
 elif tt=='s':
  win+='r'
  ans+=R
 else:
  win+='s'
  ans+=S

for i in range(k):
 l=win[i::k]
# print(l)
 for j in range(1,len(l)):
  if l[j]==l[j-1]:
   if l[j]=='p':
    ans-=P
   elif l[j]=='r':
    ans-=R
   else:
    ans-=S
   l[j]='z'

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())
x=[]
count=0
for i in range(k):
  x.append(t[i::k])

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

Submission status: Accepted

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

for num,i in enumerate(t):
    if num<k:
        continue  
    if i==t[num-k]:
        t[num]=0

rl=[]
sl=[]
pl=[]
count=0
for num,i in enumerate(t):
    if i=="r":
        count+=p
        #rl.append(num)
    elif i=="s":
        count+=r
        #sl.append(num)
    elif i=="p":
        count+=s
        #pl.append(num)

print(count)

Submission status: Accepted

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

for i in range(N):
  if i<=K-1:
    if T[i]=="r":
      score+=P
      hand.append("p")
    elif T[i]=="s":
      score+=R
      hand.append("r")
    else:
      score+=S
      hand.append("s")
  elif i<=N-K-1:
    if T[i]=="r":
      if hand[i-K]=="p":
        if T[i+K]=="r":
          hand.append("r")
        elif T[i+K]=="s":
          hand.append("s")
        elif T[i+K]=="p":
          hand.append("r")
      else:
        score+=P
        hand.append("p")
    if T[i]=="s":
      if hand[i-K]=="r":
        if T[i+K]=="r":
          hand.append("s")
        elif T[i+K]=="s":
          hand.append("s")
        elif T[i+K]=="p":
          hand.append("p")
      else:
        score+=R
        hand.append("r")
    if T[i]=="p":
      if hand[i-K]=="s":
        if T[i+K]=="r":
          hand.append("r")
        elif T[i+K]=="s":
          hand.append("p")
        elif T[i+K]=="p":
          hand.append("p")
      else:
        score+=S
        hand.append("s")
  else:
    if T[i]=="r" and hand[i-K]!="p":
      score+=P
    if T[i]=="s" and hand[i-K]!="r":
      score+=R
    if T[i]=="p" and hand[i-K]!="s":
      score+=S

print(score)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, K: int, R: int, S: int, P: int, T: str):
    s = ['b']*(N)
    
    l = [
        ['s', S],
        ['r', R],
        ['p', P]
    ]
    l = sorted(l, key=lambda x:-x[1])

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

    for i in range(N):
        s[i] = d[T[i]]


    for a, b in l:
        for i in range(N):
            if s[i] == a and i >= K and s[i-K] == a:
                s[i] = 'b'

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


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    R = int(next(tokens))  # type: int
    S = int(next(tokens))  # type: int
    P = int(next(tokens))  # type: int
    T = next(tokens)  # type: str
    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()

# 相手の手がkeyだった時に勝つと得られるポイント
score = {"s":R,"p":S,"r":P}
# K手前が同じ手だったフラグ
flag = [False for i in range(N)]
ans = 0
for i in range(N):
    if (flag[i]):
        pass
    else:
        ans += score[T[i]]
        if (N-i-1 >= K):
            if (T[i] == T[i+K]):
                flag[i+K] = True

print(ans)

Submission status: Accepted

Solution code:
N,K = map(lambda x: int(x),input().split())
r,s,p = map(lambda x: int(x),input().split())
T = input()
dic={}
for i in range(K):
  txt=""
  idx=i
  while idx<N:
    txt = txt+T[idx]
    idx += K
  dic[i] = txt
toWin = {"r":"p","s":"r","p":"s"}
scoretable = {"r":{"r":0,"s":0,"p":p},
              "s":{"r":r,"s":0,"p":0},
              "p":{"r":0,"s":s,"p":0},
             }
def katite(txt):
  best = txt.translate(str.maketrans({"r":toWin["r"],"s":toWin["s"],"p":toWin["p"]}))
  return best

def Score(txt):
  first = txt[0]
  prev_score = scoretable[first]
  nexthand = {"r":["s","p"],"s":["r","p"],"p":["s","r"]}
  
  for c in txt[1:]:
    next_score = {k:v for k,v in prev_score.items()}
    score = scoretable[c]#achievable score
    for prevhand in prev_score.keys():
      for nh in nexthand[prevhand]:
        s = score[nh]
        next_score[nh] = max(prev_score[prevhand]+s,next_score[nh])
    prev_score = next_score
  return prev_score
    
    
    
ans=0    

for i in range(K):
  #print(dic[i])
  ans += max(Score(dic[i]).values())

print(ans)

Submission status: Accepted

Solution code:
r_p_s_sum, K = map(int, input().split())
R,S,P = map(int, input().split())
enemy = input()
enemy_list = list(enemy)

# 全勝のポイント
max_points = enemy.count('r')*P + enemy.count('p') * S + enemy.count('s') * R

for i in range(K,len(enemy_list),1):
    if (enemy_list[i] == enemy_list[i-K]):
        if enemy_list[i] == 'r':
            max_points -= P
        elif enemy_list[i] == 'p':
            max_points -= S
        else:
            max_points -= R
        enemy_list[i] = 'NULL'
print(max_points)

Submission status: Accepted

Solution code:
n,k=(int(i) for i in input().split())
r,s,p=(int(i) for i in input().split( ))
T=input()
hand=[]
point=0

for i in range(n):
	if k-1<i:
		if hand[i-k]!=T[i]:
			if T[i]=='r':
				point+=p
			elif T[i]=='s':
				point+=r
			else:
				point+=s
			hand.append(T[i])
			
		else:
			hand.append('atcoder')
	else:
		if T[i]=='r':
			point+=p
		elif T[i]=='s':
			point+=r
		else:
			point+=s
		hand.append(T[i])

print(point)

Submission status: Accepted

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

mine = []
score = 0
for n in range(1, N + 1):
    if n <= K:
        if T[n - 1] == "r":
            score += P
            mine.append("p")

        elif T[n - 1] == "s":
            score += R
            mine.append("r")

        else:
            score += S
            mine.append("s")

    else:
        if T[n - 1] == "r":
            if mine[n - 1 - K] == "p":
                mine.append("any")
                continue
            else:
                score += P
                mine.append("p")

        elif T[n - 1] == "s":
            if mine[n - 1 - K] == "r":
                mine.append("any")
                continue
            else:
                score += R
                mine.append("r")

        else:
            if mine[n - 1 - K] == "s":
               mine.append("any")
               continue
            else:
               score += S
               mine.append("s")


print(score)

Submission status: Accepted

Solution code:
import sys

n, k = map(int, sys.stdin.readline().split())
r, s, p = map(int, sys.stdin.readline().split())
t = sys.stdin.readline().rstrip()
point = dict([('r', r), ('s', s), ('p', p)])

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

def main():
    res = 0
    for i in range(k):
        cur = hand(t[i])
        res += point[cur]
        prev = cur
        for j in range(i+k, n, k):
            cur = hand(t[j])
            if cur == prev:
                cur = 'any'
            else:
                res += point[cur]
            prev = cur
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

score = 0
result = ""
def judge(i):
    if i >= k and t[i] == t[i-k] and result[i-k] == "w":
        return False
    else:
        return True

for i in range(n):
    if judge(i):
        result += "w"
        if t[i] == "r":
            score += p
        elif t[i] == "s":
            score += r
        else:
            score += s
    else:
        result += "l"
print(score)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
r, s, p = list(map(int, input().split()))
make = {"r": "s", "s": "p", "p": "r"}
_t = input()
patterns = ["p" if x == "r" else "r" if x == "s" else "s" for x in _t]
scores = [p if x == "r" else r if x == "s" else s for x in _t]
for i in range(n):
    if i > k - 1:
        if patterns[i - k] == patterns[i]:
            if scores[i - k] == 0:
                patterns[i - k] = make[_t[i - k]]
            else:
                patterns[i] = _t[i]
                scores[i] = 0
print(sum(scores))

Submission status: Accepted

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

score = 0
flag = [0] * N

for i in range(N):
    if i < K:
        if T[i] == 'r':
            score = score + P
            flag[i] = 1
        elif T[i] == 's':
            score = score + R
            flag[i] = 1
        else:
            score = score + S
            flag[i] = 1
    elif i >= K:
        if T[i] == T[i - K] and flag[i - K] == 1:
            score = score
        elif T[i] == 'r':
            score = score + P
            flag[i] = 1
        elif T[i] == 's':
            score = score + R
            flag[i] = 1
        else:
            score = score + S
            flag[i] = 1

print(score)

Submission status: Accepted

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

ans=0
jibun=""
for i in range(n):
  if i<k:
    if t[i]=="s":
      ans+=r
      jibun+="r"
    elif t[i]=="p":
      ans+=s
      jibun+="s"
    else:
      ans+=p
      jibun+="p"
  else:
    if t[i]==t[i-k]:
      if (t[i]=="s" and jibun[i-k]=="r") or (t[i]=="p" and jibun[i-k]=="s") or (t[i]=="r" and jibun[i-k]=="p"):
        jibun+="n"
      else:
        if t[i]=="s":
          ans+=r
          jibun+="r"
        elif t[i]=="p":
          ans+=s
          jibun+="s"
        else:
          ans+=p
          jibun+="p"
    else:
        if t[i]=="s":
          ans+=r
          jibun+="r"
        elif t[i]=="p":
          ans+=s
          jibun+="s"
        else:
          ans+=p
          jibun+="p"
print(ans)

Submission status: Accepted

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

def score(t):
    if t == "r": return p
    if t == "s": return r
    if t == "p": return s

hist = [False] * n

ans = 0
for i in range(n):
    if i < k or (t[i] != t[i-k] or not hist[i-k]): 
        ans += score(t[i])
        hist[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())

results = []
score = 0
for idx, t in enumerate(T):
    if idx - K < 0:
        if t == "r":
            results.append("p")
            score += P
        elif t == "s":
            results.append("r")
            score += R
        elif t == "p":
            results.append("s")
            score += S
    else:
        if t == "r" and results[idx - K] == "p":
            results.append("?")
        elif t == "r":
            results.append("p")
            score += P
        if t == "s" and results[idx - K] == "r":
            results.append("?")
        elif t == "s":
            results.append("r")
            score += R
        if t == "p" and results[idx - K] == "s":
            results.append("?")
        elif t == "p":
            results.append("s")
            score += S
print(score)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

win={"r":"p","s":"r","p":"s"}
score={"r":r,"s":s,"p":p}
total=0
already=[]
for i in range(k):
    win_hand=win[tl[i]]
    already.append(win_hand)
    total+=score[win_hand]
temp=["r","s","p"]
for j in range(k,n):
    temp=["r","s","p"]
    win_hand=win[tl[j]]
    #print(j-k)
    if already[j-k]==win_hand:
        temp.remove(win_hand)
        if j+k <= n-1:
            keep=win[tl[j+k]]
            if not keep in temp:
                already.append(temp[0])
            else:
                temp.remove(keep)
                already.append(temp[0])
        else:
            already.append(temp[0])
        pass
    else:
        already.append(win_hand)
        total+=score[win_hand]
print(total)

Submission status: Accepted

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

points = {'rr': 0, 'rs': R, 'rp': 0, 'sr': 0, 'ss': 0, 'sp': S, 'pr': P, 'ps': 0, 'pp': 0}
hands = ['r', 's', 'p']
ans = 0
for i in range(K):
    subT = T[i::K]
    n = len(subT)
    dp = [{h: 0 for h in hands} for _ in range(n)]
    for j, t in enumerate(subT):
        for hand in hands:
            dp[j][hand] = max(dp[j-1][h] for h in set(hands) - set([hand])) + points[hand + t]
    ans += max(dp[-1][h] for h in hands)
print(ans)

Submission status: Accepted

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

def score_p(t):
    if t == 'r':
        return P
    if t == 's':
        return R
    if t == 'p':
        return S

A = [None] * N
for k in range(K):
    for i in range(k, N, K):
        if i == k:
            A[i] = score_p(T[i])
        else:
            if T[i] == T[i-K]:
                if A[i-K] > 0:
                    A[i] = 0
                else:
                    A[i] = score_p(T[i])
            else:
                A[i] = score_p(T[i])

print(sum(A))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = str(input())
t += "e"*k
lst = ["r","s","p"]
lst2 = [p,r,s]
score  = 0
for i in range(k):
    now = i
    s = ""
    while now < n:
        s += t[now]
        now += k
    l = []
    count = 1
    for j in range(len(s)-1):
        if s[j] != s[j+1]:
            l.append([s[j],count])
            count = 1
        else:
            count += 1
    l.append([s[len(s)-1],count])
    for arr in l:
        score += lst2[lst.index(arr[0])]*((arr[1]+1)//2)
print(score)

Submission status: Accepted

Solution code:
n,k = (int(i) for i in input().split())
r,s,p = (int(i) for i in input().split())
S = input()

ans_li = []
ans = 0
for t in S:
    if t == 'r':
        ans_li.append('p')
        ans += p

    elif t == 'p':
        ans_li.append('s')
        ans += s
    
    else:
        ans_li.append('r')
        ans += r

for i in range(k,n):
    
    if ans_li[i] == ans_li[i-k]:
       
       if ans_li[i] == 'r': ans -= r
       elif ans_li[i] == 'p': ans -= p
       else: ans -= s

       ans_li[i] = 'x'

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
t=t.replace('r','P')
t=t.replace('s','R')
t=t.replace('p','S')
t=t.lower()
ans=r*t.count('r')+s*t.count('s')+p*t.count('p')

l=int(n/k)
for i in range(k):
  j=i
  while j+k<=n-1:
    if t[j]==t[j+k]:
      ans-=r*(t[j+k]=='r')+s*(t[j+k]=='s')+p*(t[j+k]=='p')
      t=t[:j+k]+'x'+t[j+k+1:]
    j+=k
      



print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
T = input()
out = 0
L = [0 for i in range(n)]

def score(a):
  if a == "r":
    ret = p
  elif a == "s":
    ret = r
  else:
    ret = s
  return (ret)

for i in range(n):
  if i < k:
    out += score(T[i])
  else:
    if T[i] != T[i-k]:
      out += score(T[i])
    elif L[i-k] == 1:
      out += score(T[i])
    else:
      L[i] = 1
  #print("{0},{1}".format(i, out))
print(out)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t=input()
text = {"r":"p","s":"r","p":"s"}
tokutenn = 0
def dasu(te):
  if te=="r":
    return r
  if te=="s":
    return s
  if te=="p":
    return p
nons = list(range(n))
for i in range(n):
  non = "" if (i < k or nons[i-k][1]=="2") else nons[i-k][0]
  tei = text[t[i]]
  ts=dasu(tei)
  if k==i+1:
    tokutenn+=ts
    nons[i]=text[t[i]]+"0"
  else:
    if tei!=non:
      tokutenn+=ts
      nons[i]=text[t[i]]+"1"
    else:
      nons[i]=text[t[i]]+"2"
print(tokutenn)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = str(input())
win_point_dict = {
    "r": R,
    "s": S,
    "p": P
}

from collections import deque
last_hand_queue = deque("a" * K)
last_result_queue = deque("a" * K)

def get_winning_hand(opponents_hand):
    if opponents_hand == "r":
        return("p")
    elif opponents_hand == "s":
        return("r")
    elif opponents_hand == "p":
        return("s")

ans = 0
for i in range(N):
    last_k_hand = last_hand_queue.pop()
    last_k_result = last_result_queue.pop()
    if get_winning_hand(T[i]) == last_k_hand and last_k_result == "w":
        last_hand_queue.appendleft(T[i])
        last_result_queue.appendleft("e")
    else:
        last_hand_queue.appendleft(get_winning_hand(T[i]))
        last_result_queue.appendleft("w")
        ans += win_point_dict[get_winning_hand(T[i])]

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 = input()
ans = 0
x = []
for i in range(n):
  if i<k:
    if t[i] == "r":
      x.append("p")
      ans += p
    elif t[i] == "s":
      x.append("r")
      ans += r
    elif t[i] == "p":
      x.append("s")
      ans += s
  else:
    if t[i] == "r":
      if x[i-k] != "p":
        x.append("p")
        ans += p
      else:
        x.append(-1)
    elif t[i] == "s":
      if x[i-k] != "r":
        x.append("r")
        ans += r
      else:
        x.append(-1)
    elif t[i] == "p":
      if x[i-k] != "s":
        x.append("s")
        ans += s
      else:
        x.append(-1)
print(ans)