Problem p02891 (python)

Fixed submission:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

s = list(input().rstrip())
k = int(input())
n = len(s)

group = []

cnt = 0

for i in range(n):
    if i == 0:
        cnt += 1
        continue
    elif i == n - 1:
        if s[i] == s[i - 1]:
            cnt += 1
            group.append(cnt)
        else:
            group.append(cnt)
            group.append(1)
        continue
    
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        group.append(cnt)
        cnt = 1
ans = 0
if k == 1:
    for i in group:
        ans += i//2
    print(ans)
    sys.exit()
from copy import deepcopy
first = deepcopy(group)
last = deepcopy(group)
if s[0] == s[-1]:
    first[-1] += last[0]
    last[0] = 0

for i in first:
    ans += i//2
for i in last:
    ans += i//2
if k == 2:
    print(ans)
    sys.exit()
mid = deepcopy(first)
if s[0] == s[-1]:
    mid[0] = 0
temp = 0
for i in mid:
    temp += i//2
ans += temp*(k - 2)
print(ans)

Buggy submission (verdict: Runtime Error):
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    print(cnt)
    elif a[0] == a[-1]:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)
        temp_4 = 0
        elif temp > 1:
            temp_4 += temp
        elif temp_2 > 2:
            temp_4 += temp_2
        cnt += temp_4 * (K-1)


        print(cnt)

    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import copy
def main():
    S = input()
    K = int(input())
    a = list(S)
    b = copy.copy(a)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        cnt_2 = 0
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
        for j in range(len(b)-1):
            if b[j+1] == b[j]:
                b[j] = str(j)
                cnt_2 += 1
        cnt_2 *= K
        if b[0] == b[-1] and len(b) > 1:
            cnt_2 += K-1

        print(min(cnt,cnt_3))

    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import copy
def main():
    S = input()
    K = int(input())
    a = list(S)
    b = copy.copy(a)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
    elif len(a) ==1:
        cnt += K//2
    else:
        cnt_2 = 0
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
        for j in range(len(b)-1):
            if b[j+1] == b[j]:
                b[j] = str(j)
                cnt_2 += 1
        cnt_2 *= K
        if b[0] == b[-1] and len(b) > 1:
            cnt_2 += K-1

    print(min(cnt,cnt_2))

    
if __name__ == '__main__':
    main()