Problem p02891 (python)

Fixed submission:
S = list(input())
K = int(input())

if len(S) == 1:
    print( K // 2)
    exit()

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break

sum += count//2 #最後のカウントの加算

#print("check1",sum)

#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")

#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

#print("a is",a)
#print("b is",b)
#print("sum is", sum)
if a == len(S): #１文字のみで文字列構成の場合
    print((len(S)* K)//2)
elif S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
while(1):
    if i == len(S)-1:
        if (S[i] == S[0] and S[0] != S[1]) or (S[i] == S[0] and S[i-1] != S[i]):
            if sum % 2 == 1:
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
while(1):
    if i == len(S)-1:
        if (S[i] == S[0] and S[0] != S[1]) or (S[i-1] != S[i] and S[0] == S[1]) or  (S[i-1] != S[i] and S[0] != S[1] and S[i] == S[0]) :
            if sum % 2 == 1:
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
while(1):
    if i == len(S)-1:
        if (S[i] == S[0] and S[0] != S[1]) or (S[i-1] != S[i] and S[0] == S[1]):
            if sum % 2 == 1:
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
while(1):
    if i == len(S)-1:
        if S[i] == S[0] and S[0] != S[1]:
            if sum % 2 == 1:
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Buggy submission (verdict: Runtime Error):
S = list(input())
K = int(input())

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
while(1):
    if i == len(S)-1:
        if S[i] == S[0] and S[0] != S[1]:
            sum += 1
            sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2

print(total_sum * K + sum_ketu)