Problem p02820 (python)

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

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

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

def DFS(cnt, point, string) : 
    if cnt == N : 
        return(point)
    else : 
        if cnt >= K : 
            r1 = 0
            r2 = 0
            r3 = 0
            r4 = 0
            r5 = 0
            r6 = 0
            s1 = 0
            s2 = 0
            s3 = 0
            s4 = 0
            s5 = 0
            s6 = 0
            p1 = 0
            p2 = 0
            p3 = 0
            p4 = 0
            p5 = 0
            p6 = 0

            if T[cnt] == 'r' : 
                if string[-K] == 'r' : 
                    r1 = DFS(cnt+1, point + P, string+'p') # p
                    r2 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 'p' : 
                    r3 = DFS(cnt+1, point, string+'r') # r
                    r4 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 's' : 
                    r5 = DFS(cnt+1, point, string+'r') # r
                    r6 = DFS(cnt+1, point + P, string+'p') # p
            elif T[cnt] == 's' : 
                if string[-K] == 'r' : 
                    s1 = DFS(cnt+1, point, string+'p') # p
                    s2 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 'p' : 
                    s3 = DFS(cnt+1, point + R, string+'r') # r
                    s4 = DFS(cnt+1, point, string+'s') # s
                elif string[-K] == 's' : 
                    s5 = DFS(cnt+1, point + R, string+'r') # r
                    s6 = DFS(cnt+1, point, string+'p') # p
            elif T[cnt] == 'p' : 
                if string[-K] == 'r' : 
                    p1 = DFS(cnt+1, point, string+'p') # p
                    p2 = DFS(cnt+1, point + S, string+'s') # s
                elif string[-K] == 'p' : 
                    p3 = DFS(cnt+1, point, string+'r') # r
                    p4 = DFS(cnt+1, point + S, string+'s') # s
                elif string[-K] == 's' : 
                    p5 = DFS(cnt+1, point, string+'r') # r
                    p6 = DFS(cnt+1, point, string+'p') # p
                    
            return(max(r1, r2, r3, r4, r5, r6, s1, s2, s3, s4, s5, s6, p1, p2, p3, p4, p5, p6))

        else :         
            r1 = 0
            r2 = 0
            r3 = 0
            s1 = 0
            s2 = 0
            s3 = 0
            p1 = 0
            p2 = 0
            p3 = 0
            
            if T[cnt] == 'r' : 
                r1 = DFS(cnt+1, point, string+'r') # r
                r2 = DFS(cnt+1, point + P, string+'p') # p
                r3 = DFS(cnt+1, point, string+'s') # s
            elif T[cnt] == 's' : 
                s1 = DFS(cnt+1, point + R, string+'r') # r
                s2 = DFS(cnt+1, point, string+'p') # p
                s3 = DFS(cnt+1, point, string+'s') # s
            elif T[cnt] == 'p' : 
                p1 = DFS(cnt+1, point, string+'r') # r
                p2 = DFS(cnt+1, point, string+'p') # p
                p3 = DFS(cnt+1, point + S, string+'s') # s
               
            return(max(r1, r2, r3, s1, s2, s3, p1, p2, p3))
        
print(DFS(0, 0, ''))