Problem p02820 (python)

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

pre = [[] for i in range(k)]

for i in range(n):
    #print(i, pre[i%k], t[i])
    pre[i%k].append(t[i])
#print(pre)

def score(h):
    c_r = 0
    c_s = 0
    c_p = 0
    num = "".join(h)
    count = 1
    result = ''

    # 配列全体のループ
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
                count = count + 1
            else:
                result = result + str(num[i]) + str(count)
                ko = num[i]
                wa = count 
                if wa%2 == 0:
                    if ko == "r":
                        c_r += wa//2
                    elif ko == "s":
                        c_s += wa//2
                    else:
                        c_p += wa//2
                else:
                    if ko == "r":
                        c_r += 1 + (wa-1)//2
                    elif ko == "s":
                        c_s += 1 + (wa-1)//2
                    else:
                        c_p += 1 + (wa-1)//2
                count = 1
        except:
            # 最後の文字セットがOutOfRangeになるので結果に出力
            result = result + str(num[i]) + str(count)
            ko = num[i]
            wa = count 
            if wa%2 == 0:
                if ko == "r":
                    c_r += wa//2
                elif ko == "s":
                    c_s += wa//2
                else:
                    c_p += wa//2
            else:
                if ko == "r":
                    c_r += 1 + (wa-1)//2
                elif ko == "s":
                    c_s += 1 + (wa-1)//2
                else:
                    c_p += 1 + (wa-1)//2

    #print(result,c_r * p + c_s * r + c_p * s )
    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()

pre = [[] for i in range(k)]

for i in range(n):
    #print(i, pre[i%k], t[i])
    pre[i%k].append(t[i])
#print(pre)

def score(h):
    c_r = 0
    c_s = 0
    c_p = 0
    num = "".join(h)
    count = 1
    result = ''

    # 配列全体のループ
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
                count = count + 1
            else:
                result = result + str(num[i]) + str(count)
                count = 1
        except:
            # 最後の文字セットがOutOfRangeになるので結果に出力
            result = result + str(num[i]) + str(count)
    
    for i in range(len(result)//2):
        ko = result[2*i]
        wa = int(result[(2*i)+1])
        if wa%2 == 0:
            if ko == "r":
                c_r += wa//2
            elif ko == "s":
                c_s += wa//2
            else:
                c_p += wa//2
        else:
            if ko == "r":
                c_r += 1 + (wa-1)//2
            elif ko == "s":
                c_s += 1 + (wa-1)//2
            else:
                c_p += 1 + (wa-1)//2


    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()

pre = [[] for i in range(((n-1)//k))]
for i in range(n):
    pre[i%k].append(t[i])
#print(pre)

def score(h):
    c_r = 0
    c_s = 0
    c_p = 0
    num = "".join(h)
    count = 1
    result = ''

    # 配列全体のループ
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
                count = count + 1
            else:
                result = result + str(num[i]) + str(count)
                count = 1
        except:
            # 最後の文字セットがOutOfRangeになるので結果に出力
            result = result + str(num[i]) + str(count)
    
    for i in range(len(result)//2):
        ko = result[2*i]
        wa = int(result[(2*i)+1])
        if wa%2 == 0:
            if ko == "r":
                c_r += wa//2
            elif ko == "s":
                c_s += wa//2
            else:
                c_p += wa//2
        else:
            if ko == "r":
                c_r += 1 + (wa-1)//2
            elif ko == "s":
                c_s += 1 + (wa-1)//2
            else:
                c_p += 1 + (wa-1)//2


    return c_r * p + c_s * r + c_p * s

ans = 0
for i in range(len(pre)):
    ans += score(pre[i])
print(ans)