Problem p02891 (python)

Fixed submission:
import sys
import collections
import copy
import math
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
S= input()
K =int(input())
count = 1
ans_list = []
ans = 0
if len(S) ==1:
    print(len(S)*K//2)
else:
    if S [0] != S[-1]:
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                count += 1
            else:
                if count ==1:
                    continue
                else:
                    ans_list.append(count)
                    count = 1
        if count !=1:
            ans_list.append(count)
        for j in range(len(ans_list)):
            if ans_list[j]%2 == 1:
                ans += (ans_list[j]-1)//2
            else:
                ans += (ans_list[j])//2
        print(ans*K)
    else:
        a = 0
        while S[a] == S[a+1] :
            a +=1
            if a == len(S)-1:
                break
        a += 1
        b = 0
        while S[-1-b] == S[-2-b]:
            b += 1
            if b == len(S)-1:
                break
        b += 1
        if a != len(S):
            S_alt = S[a:len(S)-b]
            for i in range(len(S_alt)-1):
                if S_alt[i] == S_alt[i+1]:
                    count += 1
                else:
                    if count ==1:
                        continue
                    else:
                        ans_list.append(count)
                        count = 1
            if count !=1:
                ans_list.append(count)
            for j in range(len(ans_list)):
                if ans_list[j]%2 == 1:
                    ans += (ans_list[j]-1)//2
                else:
                    ans += (ans_list[j])//2
            ans = (ans * K)
            ans += a//2 + b//2
            ans += (a+b)//2*(K-1)
            print(ans)
        else:
            ans += len(S)*K//2
            print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import collections
import copy
import math
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
S= input()
K =int(input())
count = 1
ans_list = []
ans = 0
if S [0] != S[-1]:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            if count ==1:
                continue
            else:
                ans_list.append(count)
                count = 1
    if count !=1:
        ans_list.append(count)
    for j in range(len(ans_list)):
        if ans_list[j]%2 == 1:
            ans += (ans_list[j]-1)//2
        else:
            ans += (ans_list[j])//2
    print(ans*K)
else:
    a = 0
    while S[a] == S[a+1] :
        a +=1
        if a == len(S)-1:
            break
    a += 1
    b = 0
    while S[-1-b] == S[-2-b]:
        b += 1
        if b == len(S)-1:
            break
    b += 1
    if a != len(S):
        S_alt = S[a:len(S)-b]
        for i in range(len(S_alt)-1):
            if S_alt[i] == S_alt[i+1]:
                count += 1
            else:
                if count ==1:
                    continue
                else:
                    ans_list.append(count)
                    count = 1
        if count !=1:
            ans_list.append(count)
        for j in range(len(ans_list)):
            if ans_list[j]%2 == 1:
                ans += (ans_list[j]-1)//2
            else:
                ans += (ans_list[j])//2
        ans = (ans * K)
        ans += a//2 + b//2
        ans += (a+b)//2*(K-1)
        print(ans)
    else:
        ans += len(S)*K//2
        print(ans)