Problem p02891 (python)

Fixed submission:
#import sys

#input = sys.stdin.readline

#import heapq
#import math
#import fractions

#def sosuhante(n):
#    for k in range(2, int(math.sqrt(n))+1):
#        if n% k ==0:
#            return False
#    return True

def main():
#    h,w,a,b = map(int, input().split())
    s = input()
    k= int(input())
    a= []
    b= []
    n=len(s)
    for i in range(n):
        a.append(s[i])
        b.append(s[i])

    a[0]="a"
    if n > 1:
        for i in range(1,n):
            if b[i]==b[i-1]:
                a[i]="a" if a[i-1]=="a" else "b"
            else:
                a[i]="b" if a[i-1]=="a" else "a"
#    print(a)
    temp=1
    henka=0
    if n > 1:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    henka+=temp//2
                    temp=1
            else:
                henka+=temp//2
                temp=1
    temp=1
    hasi=0
    mae=1
    usiro=1
    if k > 1 and n > 1 and b[0]==b[n-1]:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    mae=temp
            else:
                mae=temp
                break
        temp=1
        for i in range(n-2,-1,-1):
            if a[i]==a[i+1]:
                temp+=1
                if i==0:
                    usiro=temp
            else:
                usiro=temp
                break
        if (mae+usiro)//2 > mae//2 + usiro//2:
            hasi=1
#    print(henka,"henka")
    count=0
    if k > 1 and n>1:
        count=henka*k + hasi*(k-1)
    if k == 1 and n>1:
        count=henka
    if n==1 and k==1:
        count=0
    if n==1 and k>1:
        count=k//2
    if mae==usiro and mae==n:
        count=(n*k)//2


    print(count)





if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#import sys

#input = sys.stdin.readline

#import heapq
#import math
#import fractions

#def sosuhante(n):
#    for k in range(2, int(math.sqrt(n))+1):
#        if n% k ==0:
#            return False
#    return True

def main():
#    h,w,a,b = map(int, input().split())
    s = input()
    k= int(input())
    a= []
    b= []
    n=len(s)
    for i in range(n):
        a.append(s[i])
        b.append(s[i])

    a[0]="a"
    if n > 1:
        for i in range(1,n):
            if b[i]==b[i-1]:
                a[i]="a" if a[i-1]=="a" else "b"
            else:
                a[i]="b" if a[i-1]=="a" else "a"
#    print(a)
    temp=1
    henka=0
    if n > 1:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    henka+=temp//2
                    temp=1
            else:
                henka+=temp//2
                temp=1
    temp=1
    hasi=0
    mae=1
    usiro=1
    if k > 1 and n > 1 and b[0]==b[n-1]:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    mae=temp
            else:
                mae=temp
                break
        temp=1
        for i in range(n-2,0,-1):
            if a[i]==a[i+1]:
                temp+=1
                if i==0:
                    usiro=temp
            else:
                usiro=temp
                break
        if (mae+usiro)//2 > mae//2 + usiro//2:
            hasi=1
#    print(henka,"henka")
    count=0
    if k > 1 and n>1:
        count=henka*k + hasi*(k-1)
    if k == 1 and n>1:
        count=henka
    if n==1 and k==1:
        count=0
    if n==1 and K>1:
        count=k//2


    print(count)





if __name__ == "__main__":
    main()