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:

import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

#a = list(map(int, input().split()))

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

hands = [[] for i in range(k)]
for i in range(n):
    hands[i%k].append(s[i])
ans = 0    
for i in hands:
    prev = 't'
    for j in i:
        if(prev==j):
            ans+=0
            prev = 't'
        else:
            if(j=='r'): ans+=p
            if(j=='s'): ans+=r
            if(j=='p'): ans+=ss
            prev = j
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby
import math 

n,k = [int(x) for x in stdin.readline().rstrip().split()]
r,s,p = [int(x) for x in stdin.readline().rstrip().split()]
t = stdin.readline().rstrip()
t = [i for i in t]
point = 0

for i in range(k):
    li = t[i::k]
    lin = []
    for key, value in groupby(li):
        lin.append((key,len(list(value))))
    for j in lin:
        if j[0] == "r":
            point += math.ceil(j[1]/2)*p
        elif j[0] == "s":
            point += math.ceil(j[1]/2)*r
        else:
            point += math.ceil(j[1]/2)*s

print(point)

Submission status: Accepted

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

hist = [[0 for i in range(3)] for _ in range(k)]
#0r1s2p
for i,v in enumerate(t):
    md = i%k
    cur_0 = hist[md][0]
    cur_1 = hist[md][1]
    cur_2 = hist[md][2]
    cur_max = max(hist[md])
    hist[md]=[cur_max]*3
    if v=="r":
        hist[md]=[cur_max]*3
        hist[md][2]=max(cur_1,cur_0)+p
    elif v=="s":
        hist[md][0]=max(cur_1,cur_2)+r
    else:
        hist[md][1]=max(cur_0,cur_2)+s

print(sum([max(i) for i in hist]))

Submission status: Accepted

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

def rsp(x):
    if x=='r':
        return 'p'
    elif x=='s':
        return 'r'
    else:
        return 's'

Ban = ['']*k+[ rsp(t[i]) for i in range(n-k) ]
res = 0
for i in range(n):
    if rsp(t[i]) != Ban[i]:
        if rsp(t[i]) == 'r':
            res += r
        elif rsp(t[i]) == 's':
            res += s
        else:
            res += p
    elif i+k<n:
        Ban[i+k] = ''
print(res)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = input()
d = {'r':'p', 'p':'s', 's':'r'}
ans = [d[i] for i in t]
for i in range(k, n):
    if ans[i-k] == 'x':
        continue
    if ans[i-k] == ans[i]:
        ans[i] = 'x'
total = 0
for i in ans:
    if i == 'r':
        total+=r
    elif i == 'p':
        total+=p
    elif i == 's':
        total+=s
print(total)

Submission status: Accepted

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


for i in range(K):
    check = T[i]
    ans += V[check]
    while True:
        i += K
        if i >= N:
            break
        if check == T[i]:
            check = '-1'
        else:
            ans += V[T[i]]
            check = T[i]
print(ans)

Submission status: Accepted

Solution code:
def fu(st):
    global r,s,p
    if(st=='r'):
        return p
    elif(st=='p'):
        return s
    return r
    

n,k=[int(i) for i in input().split()]
r,s,p=[int(i) for i in input().split()]
t=list(input())


dp = [0]*(10**5+11)
res=0
for i in range(k):
    res+=fu(t[i])
    dp[i]=res

for i in range(k,len(t)):
    if(t[i-k]==t[i]):
        dp[i] = max(dp[i-k],dp[i-1])
        t[i]='0'
    else:
        dp[i] = dp[i-1]+fu(t[i])

print(dp[n-1])

Submission status: Accepted

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

Submission status: Accepted

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

def point(x):
  if x == 'r':
    return(p)
  elif x == 's':
    return(r)
  else:
    return(s)

score = 0
for i in range(n):
  score += point(t[i])
  
for i in range(k):
  a = i
  b = 1
  while a+k < n:
    if t[a] == t[a+k]:
      b += 1
    else:
      score -= point(t[a])*(b//2)
      b = 1
    a += k
  else:
    score -= point(t[a])*(b//2)
print(score)

Submission status: Accepted

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

dasita_te = []

score = 0

for i, c in enumerate(t):
    if c == "r":
        if (i >= k and dasita_te[i - k] != "p") or i < k:
            score += p
            dasita_te.append("p")
        else:
            dasita_te.append("-")
    elif c == "s":
        if (i >= k and dasita_te[i - k] != "r") or i < k:
            score += r
            dasita_te.append("r")
        else:
            dasita_te.append("-")
    elif c == "p":
        if (i >= k and dasita_te[i - k] != "s") or i < k:
            score += s
            dasita_te.append("s")
        else:
            dasita_te.append("-")

print(score)

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
scores = list(map(int,input().split()))
cards = input()
cards = list(cards)
ans = 0

for i in range(n[0]):
    if i < n[1] or cards[i-n[1]] != cards[i]:
        if cards[i] == 'r':
            ans += scores[2]
        elif cards[i] == 's':
            ans += scores[0]
        else:
            ans += scores[1]
    else:
        cards[i] = 'k'

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
score=0
m=[]
for i in range(k):
    if t[i]=='r':
        score+=p
        m.append('p')
    elif t[i]=='s':
        score+=r
        m.append('r')
    else:
        score+=s
        m.append('s')
for i in range(k,n):
    if t[i]=='r' and m[i-k]!='p':
        score+=p
        m.append('p')
    elif t[i]=='s' and m[i-k]!='r':
        score+=r
        m.append('r')
    elif t[i]=='p' and m[i-k]!='s':
        score+=s
        m.append('s')
    else:
        m.append('penis')

print(score)

Submission status: Accepted

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

ans = 0
hands = []
for i in range(N):
    t = T[i]
    if t == 'r':
        if i < K or hands[i-K] != 'p':
            ans += P
            hands.append('p')
        else:
            if i+K >= N:
                hands.append('r')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
    elif t == 's':
        if i < K or hands[i-K] != 'r':
            ans += R
            hands.append('r')
        else:
            if i+K >= N:
                hands.append('s')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
    elif t == 'p':
        if i < K or hands[i-K] != 's':
            ans += S
            hands.append('s')
        else:
            if i+K >= N:
                hands.append('p')
            else:
                if T[i+K] == 'r':
                    hands.append('r')
                elif T[i+K] == 's':
                    hands.append('s')
                elif T[i+K] == 'p':
                    hands.append('p')
print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
R, S, P = list(map(int, input().split()))
T = input()
X = {'r':P, 's':R, 'p':S}
win = [False]*K

ans = 0
for i in range(N):
    if i<K or T[i]!=T[i-K] or not win[i%K]:
        ans += X[T[i]]
        win[i%K]=True
    else:
        win[i%K]=False

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
rsp = list(map(int, input().split()))
t = input()
ans = 0
d = {'r': 2, 's':0, 'p': 1}
for c in t:
  ans += rsp[d[c]]
for i in range(k):
  prev = t[i]
  seq = 1
  for j in range(i+k, n, k):
    if t[j] == prev:
      seq += 1
    else:
      ans -= rsp[d[prev]] * (seq // 2)
      prev = t[j]
      seq = 1
  ans -= rsp[d[prev]] * (seq // 2)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split(" "))
    R, S, P = map(int, input().split(" "))
    strings = input()
    T = []
    for s in strings:
        T.append(s)
    score = {'r': P, 's': R, 'p': S}
    total = 0

    for i in range(K):
        total += score[T[i]]

    for j in range(K, N):
        if T[j] != T[j - K]:
            total += score[T[j]]
        else:
            T[j] = ' '
            continue

    print(total)

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

hands = ['r', 's', 'p']
for i,t in enumerate(T):
    if t == 'r':
        T[i] = 'p'
    elif t == 's':
        T[i] = 'r'
    else:
        T[i] = 's'
point = 0
for i,t in enumerate(T):
    if i+1 <= K:
        if t == 'r':
            point += R
        elif t == 's':
            point += S
        else:
            point += P
        continue
    if t == T[i-K]:
        a = 0
        if len(T)-1 < i+K:
            next_hand = T[i]
        else:
            next_hand = T[i+K]
        while T[i] == T[i-K] or T[i] == next_hand:
            T[i] = hands[a]
            a += 1
    else:
        if t == 'r':
            point += R
        elif t == 's':
            point += S
        else:
            point += P
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
# count for 0 ~ K
for i in range(N-K):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point
 
    if(T[i+K] == T[i]):
        T[i+K] = 'n'
 
# count for K ~ N
for i in range(N-K, N):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point
 
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

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

#print(N,K)
#print(R,S,P)
#print(T)

def maxp(s):
    vr = 0
    vs = 0
    vp = 0
    for i in range(0,len(s)):
        if s[i] == 'r':
            nvr = 0 + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = P + max(vr,vs)
        elif s[i] == 's':
            nvr = R + max(vs,vp)
            nvs = 0 + max(vp,vr)
            nvp = 0 + max(vr,vs)
        else:
            nvr = 0 + max(vs,vp)
            nvs = S + max(vp,vr)
            nvp = 0 + max(vr,vs)
        vr = nvr
        vs = nvs
        vp = nvp
        #print(s[i],vr,vs,vp)
    return(max(vr,vs,vp))
ans=0
for i in range(0,K):
    p = ""
    for k in range(i,len(T),K):
        p = p + T[k]
    #print(p)
    ans  = ans + maxp(p)
print(ans)

Submission status: Accepted

Solution code:
def main():
  N,K = input().split()
  N = int(N)
  K = int(K)
  R,S,P = input().split()
  R = int(R)
  S = int(S)
  P = int(P)
  T = input()
  #r -> a -> p 
  #s -> b -> r
  #p -> c -> s
  T = T.replace("r","a")
  T = T.replace("s","b")
  T = T.replace("p","c")
  T = T.replace("a","p")
  T = T.replace("b","r")
  T = T.replace("c","s")
  #print(T)
  #r_count = T.count("r")
  #s_count = T.count("s")
  #p_count = T.count("p")
  #print(r_count, s_count, p_count)
  
  T = list(T)
  score=0
  
  for i in range(N-K):
    if(T[i]=="r"):
      score = score+R
    elif(T[i]=="s"):
      score = score+S
    elif(T[i]=="p"):
      score = score+P
    if(T[i]==T[i+K]):
      T[i+K]="n"
          
  for i in range(N-K,N,1):
    if(T[i]=="r"):
      score = score+R
    elif(T[i]=="s"):
      score = score+S
    elif(T[i]=="p"):
      score = score+P

  print(score)
  

if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
r,s,p = inm()
t = input().strip()
fail = [False]*n
acc = 0
for i in range(n):
    ddprint("i {}".format(i))
    if t[i]=='r':
        if not (i>=k and t[i-k]=='r') or fail[i-k]:
            acc += p
            ddprint("add {}".format(p))
        else:
            fail[i] = True
    elif t[i]=='s':
        if not (i>=k and t[i-k]=='s') or fail[i-k]:
            acc += r
            ddprint("add {}".format(r))
        else:
            fail[i] = True
    else:
        if not (i>=k and t[i-k]=='p') or fail[i-k]:
            acc += s
            ddprint("add {}".format(s))
        else:
            fail[i] = True
print(acc)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
R,S,P=map(int,input().split())
string=input()
ans=0
for i in range(k):
    F=""
    for j in range(n):
        if i+j*k<=n-1:
            F+=string[i+j*k]
        else:
            break
    length=len(F)
    gu=[0 for i in range(length+1)]
    tyoki=[0 for i in range(length+1)]
    pa=[0 for i in range(length+1)]
    for index in range(1,length+1):
        gu[index]=max(tyoki[index-1],pa[index-1])
        tyoki[index]=max(gu[index-1],pa[index-1])
        pa[index]=max(gu[index-1],tyoki[index-1])
        if F[index-1]=="r":
            pa[index]+=P
        elif F[index-1]=="p":
            tyoki[index]+=S    
        elif F[index-1]=="s":
            gu[index]+=R
    ans+=max(tyoki[length],pa[length],gu[length])
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
# count for 0 ~ K
for i in range(N-K):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point

    if(T[i+K] == T[i]):
        T[i+K] = 'n'

# count for K ~ N
for i in range(N-K, N):
    if(T[i] == 'r'):
        point = P
    elif(T[i] == 's'):
        point = R
    elif(T[i] == 'p'):
        point = S
    else: # already used
        point = 0
    ans += point

print(ans)

Submission status: Accepted

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

def score(R,S,P,T):
    if T == "r":
        return P
    elif T == "s":
        return R
    else:
        return S

sum = 0
flag = [0 for i in range(K)]
for i in range(N):
    if i < K:
        sum += score(R,S,P,T[i])
    else:
        if T[i] == T[i-K]:
            if not flag[i%K]:#フラグが立っていなければ
                flag[i%K] = 1
            else:
                flag[i%K] = 0
                sum += score(R, S, P, T[i])
        else:
            flag[i % K] = 0
            sum += score(R, S, P, T[i])





print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

dp = [''] * n
a = 0
for i in range(k):
    a += m[t[i]]
    dp[i] = t[i]
    
for i in range(k, n):
    if t[i] != dp[i-k]:
        a += m[t[i]]
        dp[i] = t[i]

print(a)

Submission status: Accepted

Solution code:
def main():
	# a,b,k = map(int,input().split())
	n,k = map(int,input().split())
	# s = str(input())
	a = list(map(int,input().split()))
	rsp = list(str(input()))
	# l = [list(map(int, input().split())) for _ in range(n-1)]
	# s = list(s)
	ans = 0
	jan = []
	for i in range(n):
		# print("---i = {}---".format(i))
		if i < k:
			if rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		elif rsp[i] == "s" and jan[i-k] == "r":
			jan.append("z")
		elif rsp[i] == "r" and jan[i-k] == "p":
			jan.append("z")
		elif rsp[i] == "p" and jan[i-k] == "s":
			jan.append("z")
		else:
			if rsp[i] == "s":
				ans += a[0]
				jan.append("r")
			elif rsp[i] == "r":
				ans += a[2]
				jan.append("p")
			elif rsp[i] == "p":
				ans += a[1]
				jan.append("s")
		# print(jan)
		# print(len(jan))
	
	print(ans)

	
 
 
if __name__ == '__main__':
	main()

Submission status: Accepted

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

sum = 0
flag = [0 for i in range(N)]
countP, countR, countS = 0, 0, 0

for i in range(K):
    if T[i]=='r':
        countP += 1

    elif T[i]=='s':
        countR += 1

    elif T[i]=='p':
        countS += 1
for i in range(K,N):
    if T[i]=='r':
        if T[i-K]!='r':
            countP += 1
        else:
            if flag[i-K] == 1:
                countP += 1
                flag[i] = 0
            else:
                flag[i] = 1

    if T[i]=='s':
        if T[i-K]!='s':
            countR += 1
        else:
            if flag[i-K] == 1:
                countR += 1
                flag[i] = 0
            else:
                flag[i] = 1

    if T[i]=='p':
        if T[i-K]!='p':
            countS += 1
        else:
            if flag[i-K] == 1:
                countS += 1
                flag[i] = 0
            else:
                flag[i] = 1

sum = countR * R+ countP * P + countS * S
print(sum)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = str(input())
syo = n//k 
total = 0
for i in range(k):
  te = ''
  score = 0
  for j in range(i, n, k):
    if score == 0 or t[j] != te:
      if t[j] == 'r':
        score = p
        total += p
        te = 'r'
      elif t[j] == 's':
        score = r
        total += r
        te = 's'
      else:
        score = s
        total += s
        te = 'p'
    else:
      score = 0
      
  
print(total)

Submission status: Accepted

Solution code:
N, k = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
already = set()
for i in range(len(T)):
    t_now = T[i]
    point = 0
    if t_now == "r":
        point = P
    elif t_now == "p":
        point = S
    else:
        point = R

    if i >= k:
        t_before = T[i - k]
        if t_now == t_before and (i - k) not in already:
            point = 0
            already.add(i)
    ans += point
    # print(point)
print(ans)

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}
result = 0
wins = [False] * K
for i in range(len(T)):
    if not wins[i % K] or T[i] != T[i - K]:
        result += d[T[i]]
        wins[i % K] = True
    else:
        wins[i % K] = False
print(result)

Submission status: Accepted

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

draws = [True] * K
d = {'r': P, 's': R, 'p': S}
result = 0
for i in range(len(T)):
    if draws[i % K] or T[i] != T[i - K]:
        result += d[T[i]]
        draws[i % K] = False
    else:
        draws[i % K] = True
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
r, s, p = map(int, input().split())
t = list(input())
hand = {"r" : p, "s" : r, "p" : s}
ans = 0
for n_ in range(n):
	temp = n_ - k
	if temp >= 0 and t[n_] == t[temp]:
		t[n_] = ""
	else:
		ans += hand[t[n_]]
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
#dp[i][ｊ]:=i番目までで、最後にｊを出したときの点数の最大値
dp = [[-10**9]*3for i in range(n+k)]
for i in range(k):
    dp[i][0],dp[i][1],dp[i][2] = 0,0,0
    
    
 
for i in range(k):
    for j in range(n//k+3):
        if i > n-1:continue
        if t[i] == "r":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0]+p,dp[i][1]+p)
        
        elif t[i] == "s":
            dp[i+k][0] = max(dp[i][1]+r,dp[i][2]+r)
            dp[i+k][1] = max(dp[i][0],dp[i][2])
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        
        elif t[i] == "p":
            dp[i+k][0] = max(dp[i][1],dp[i][2])
            dp[i+k][1] = max(dp[i][0]+s,dp[i][2]+s)
            dp[i+k][2] = max(dp[i][0],dp[i][1])
        i += k
ans = 0    
for i in range(n,n+k):
    ans += max(dp[i])
print(ans)

Submission status: Accepted

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


def pon(computer, mine):
    if computer == 'r' and mine == 2: return P
    if computer == 's' and mine == 0: return R
    if computer == 'p' and mine == 1: return S
    return 0


dp = [[0 for _ in range(3)] for _ in range(N+10)]

ans = 0
for st in range(K):
    for i in range(st, N, K):
        for j in range(3):
            for k in range(3):
                if j == k:
                    continue
                if i+1-K >= 0:
                    dp[i+1][k] = max(dp[i+1][k], dp[i+1-K][j] + pon(T[i], k))
                else:
                    dp[i+1][k] = max(dp[i+1][k], pon(T[i], k))
    ans += max(dp[i+1])

print(ans)

Submission status: Accepted

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

subS = ""
for i in range(len(t)):
    if (t[i] == 'r'): subS += 'p'
    elif (t[i] == 'p'): subS += 's'
    else: subS += 'r'

sums = 0
already = [True]
# print(subS)
# print(r)
# print(len(t))
for i in range(len(t)):
    if (i == 0):
        if(subS[i]== 'r'): sums += r
        elif(subS[i] == 'p'): sums += p
        else: sums += s
        continue   
    if (t[i] != t[i - k]):
        sums += (subS[i] == 'r')*r + (subS[i] == 'p')*p + (subS[i] == 's')*s
        already.append(True)
    else:
          if(i >= k and already[i - k]): already.append(False)
          else:
                already.append(True)
                sums += (subS[i] == 'r')*r + (subS[i] == 'p')*p + (subS[i] == 's')*s
print(sums)

Submission status: Accepted

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

ans = 0
pre = ""
for i in range(N):
    if i >= K and T[i - K]==T[i]:
        if T[i] == "r" and pre[i - K] == "p":
            pre += "x"
            continue
        if T[i] == "s" and pre[i - K] == "r":
            pre += "x"
            continue
        if T[i] == "p" and pre[i - K] == "s":
            pre += "x"
            continue

    if T[i] == "r":
        ans += P
        pre += "p"
    if T[i] == "s":
        ans += R
        pre += "r"
    if T[i] == "p":
        ans += S
        pre += "s"
print(ans)

Submission status: Accepted

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

result = 0

if len(T) > K:
    for i in range(K):
        if T[i] == 'r':
            result += P;
        elif T[i] == 's':
            result += R
        else:
            result += S
        M.append('win')
    for i in range(K, len(T)):
        if T[i - K] != T[i] or (T[i - K] == T[i] and M[i - K] == 'lose'):
            if T[i] == 'r':
                result += P
            elif T[i] == 's':
                result += R
            else:
                result += S
            M.append('win')
        else:
            M.append('lose')
            continue
else:
    for i in range(len(T)):
        if T[i] == 'r':
            result += P
        elif T[i] == 's':
            result += R
        else:
            result += S

print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

# 初期値
score = 0

# K個のグループに分ける
string = ['' for i in range(K)]

for i in range(N) : 
    string[i%K] += T[i]
    
# 動的計画法を使用, 0:r, 1:s, 2:p
for h in range(K) : 
    n = len(string[h])
    
    dp = [[0 for j in range(3)] for i in range(n)] 
    
    for i in range(n) : 
        if i==0 : 
            if string[h][i] == 'r' : 
                dp[i][2] += P
            elif string[h][i] == 's' : 
                dp[i][0] += R
            elif string[h][i] == 'p' : 
                dp[i][1] += S
        else : 
            if string[h][i] == 'r' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2]))
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2]))
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1])+P)
            elif string[h][i] == 's' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2])+R)
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2]))
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1]))
            elif string[h][i] == 'p' : 
                dp[i][0] = max(dp[i][0], max(dp[i-1][1], dp[i-1][2]))
                dp[i][1] = max(dp[i][1], max(dp[i-1][0], dp[i-1][2])+S)
                dp[i][2] = max(dp[i][2], max(dp[i-1][0], dp[i-1][1]))
                    
    score += max(dp[i][0], dp[i][1], dp[i][2])

print(score)

Submission status: Accepted

Solution code:
n = input()    
m = n.split()
p = [int(i) for i in m]
n = input()    
m = n.split()
P = [int(i) for i in m]
n = input()
list = [""]*p[1]
for i, value in enumerate(n):
    list[i%p[1]] =list[i%p[1]] + value
A = 0
for i in list:
    able = [True,True,True]
    for j in i:
        if j == "r":
            if able[2]:
                A = A + P[2]
                able = [True,True,False]
            else:
                able = [True,True,True]
        if j == "s":
            if able[0]:
                A = A + P[0]
                able = [False,True,True]
            else:
                able = [True,True,True]
        if j == "p":
            if able[1]:
                A = A + P[1]
                able = [True,False,True]
            else:
                able = [True,True,True]

print(A)