Buggy python submission (verdict: Runtime Error):
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    SC+=point(T[i])
    if i+K<N and T[i]==T[i+K]:
        Flag[i+K]=False
return(SC)

Fixed submission:
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def MaxScore(n,t):
    SC = 0
    if t[0]=='r':
        SC+=P
    elif t[0]=='s':
        SC+=R
    else:
        SC+=S
    NC=1
    for i in range(1,n):
        if t[i]=='r':
            if t[i]!=t[i-1]:
                SC+=P
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=P
                NC+=1
            else:
                NC+=1
        elif t[i]=='s':
            if t[i]!=t[i-1]:
                SC+=R
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=R
                NC+=1
            else:
                NC+=1
        else:
            if t[i]!=t[i-1]:
                SC+=S
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=S
                NC+=1
            else:
                NC+=1
    return SC

for i in range(K):
    Ti=[T[j] for j in range(N) if j%K==i]
    if (N-i)%K==0:
        Ni=(N-i)//K
    else:
        Ni=(N-i)//K+1
    SCi=MaxScore(Ni,Ti)
    SC+=SCi
    
print(SC)     

Fixed submission:
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def MaxScore(n,t):
    SC = 0
    if t[0]=='r':
        SC+=P
    elif t[0]=='s':
        SC+=R
    else:
        SC+=S
    NC=1
    for i in range(1,n):
        if t[i]=='r':
            if t[i]!=t[i-1]:
                SC+=P
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=P
                NC+=1
            else:
                NC+=1
        elif t[i]=='s':
            if t[i]!=t[i-1]:
                SC+=R
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=R
                NC+=1
            else:
                NC+=1
        else:
            if t[i]!=t[i-1]:
                SC+=S
                NC=1
            elif t[i]==t[i-1] and NC%2==0:
                SC+=S
                NC+=1
            else:
                NC+=1
    return SC

for i in range(K):
    Ti=[T[j] for j in range(N) if j%K==i]
    Ti=''.join(Ti)
    if (N-i)%K==0:
        Ni=(N-i)//K
    else:
        Ni=(N-i)//K+1
    SCi=MaxScore(Ni,Ti)
    SC+=SCi
    
print(SC)             
    

Fixed submission:
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def MaxScore(n,t):
    SC = 0
    Z=[0]*n
    if t[0]=='r':
        Z[0]='p'
        SC+=P
    elif t[0]=='s':
        Z[0]='r'
        SC+=R
    else:
        Z[0]='s'
        SC+=S
    for i in range(1,n):
        if t[i]=='r':
            if Z[i-1]!='p':
                Z[i]='p'
                SC+=P
            elif t[min([i+1,n-1])]=='p':
                Z[i]='r'
            else:
                Z[i]='s'
        elif t[i]=='s':
            if Z[i-1]!='r':
                Z[i]='r'
                SC+=R
            elif t[min([i+1,n-1])]=='r':
                Z[i]='s'
            else:
                Z[i]='p'
        else:
            if Z[i-1]!='s':
                Z[i]='s'
                SC+=S
            elif t[min([i+1,n-1])]=='s':
                Z[i]='p'
            else:
                Z[i]='r'        
    return Z,SC

for i in range(K):
    Ti=[T[j] for j in range(N) if j%K==i ]
    Ti=''.join(Ti)
    if (N-i)%K==0:
        Ni=(N-i)//K
    else:
        Ni=(N-i)//K+1
    Zi,SCi=MaxScore(Ni,Ti)
    SC+=SCi
    
print(SC)

Fixed submission:
N,K=map(int, input().split()) 
R,S,P=map(int, input().split()) 
T=input()
SC=0

def point(w):
    if w=='r':
        return P
    elif w=='s':
        return R
    else:
        return S
    
Flag=[True for i in range(N)]

for i in range(N):
    if Flag[i]:
        SC+=point(T[i])
        if i+K<N and T[i]==T[i+K]:
            Flag[i+K]=False
print(SC)