Problem p02820 (python)

Fixed submission:
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)

Buggy submission (verdict: Runtime Error):
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(score)

Buggy submission (verdict: Runtime Error):
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(score)