Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
    ti=[t[ii] for ii in range(n) if ii%k==i]
    ni=len(ti)
#    dp=[[0]*3 for i in range(ni+1)]
    dpir=0
    dpis=0
    dpip=0
    for i2 in range(ni):
        if ti[i2]=="r":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif ti[i2]=="s":
#            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif ti[i2]=="p":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2
#    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
    vsum+=max(dpir,dpis,dpip)
print(vsum)

Fixed submission:
import sys
input = sys.stdin.readline

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
#    ti=[t[ii] for ii in range(n) if ii%k==i]
#    ni=len(ti)
#    dp=[[0]*3 for i in range(ni+1)]
    dpir=0
    dpis=0
    dpip=0
    for i2 in range(i,n,k):
        if t[i2]=="r":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif t[i2]=="s":
#            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif t[i2]=="p":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2
#    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
    vsum+=max(dpir,dpis,dpip)
print(vsum)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
    ti=[t[ii] for ii in range(n) if ii%k==i]
    ni=len(ti)
    dp=[[0]*3 for i in range(ni+1)]
    for i2 in range(ni):
        if ti[i2]=="r":
            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
        elif ti[i2]=="s":
            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
        elif ti[i2]=="p":
            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
print(vsum)
     

Fixed submission:
import sys
input = sys.stdin.readline

n,k=map(int,input().split())
vr,vs,vp=map(int,input().split())
t=list(input())

rr=0
ss=1
pp=2

vsum=0
for i in range(k):
#    ti=[t[ii] for ii in range(n) if ii%k==i]
#    ni=len(ti)
#    dp=[[0]*3 for i in range(ni+1)]
    dpir=0
    dpis=0
    dpip=0
    for i2 in range(i,n,k):
        if t[i2]=="r":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr]+vp,dp[i2][ss]+vp)
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)+vp
        elif t[i2]=="s":
#            dp[i2+1][rr]=max(dp[i2][ss]+vr,dp[i2][pp]+vr)
#            dp[i2+1][ss]=max(dp[i2][pp],dp[i2][rr])
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)+vr
            dpis2=max(dpip,dpir)
            dpip2=max(dpir,dpis)
        elif t[i2]=="p":
#            dp[i2+1][rr]=max(dp[i2][ss],dp[i2][pp])
#            dp[i2+1][ss]=max(dp[i2][pp]+vs,dp[i2][rr]+vs)
#            dp[i2+1][pp]=max(dp[i2][rr],dp[i2][ss])
            dpir2=max(dpis,dpip)
            dpis2=max(dpip,dpir)+vs
            dpip2=max(dpir,dpis)
        dpir=dpir2
        dpis=dpis2
        dpip=dpip2
#    vsum+=max(dp[ni][rr],dp[ni][ss],dp[ni][pp])
    vsum+=max(dpir,dpis,dpip)
print(vsum)