Problem p02891 (python)

Fixed submission:
s = input()
k = int(input())

n = len(s)
s2 = s + s

renzoku = False
taisho = 0
aida = False
for i in range(2*n-1):
    if s2[i] == s2[i+1]:
        if not renzoku:
            renzoku = True
            start_i = i
            count = 0
        count += 1

    elif renzoku:
        renzoku = False
        end_i = i
        count += 1
        #print(start_i, end_i)
        if count % 2 == 1:
            indexs = list(range(start_i+1, end_i, 2))
            taisho += len(indexs)
            #print(indexs)
            if (n-1 in indexs) or (n in indexs):
                aida = True
        elif count % 2 == 0:
            indexs1 = list(range(start_i, end_i, 2))
            indexs2 = list(range(start_i+1, end_i, 2))
            taisho += len(indexs1)
            #print(indexs1)
            all_indexs = indexs1 + indexs2
            if (n-1 in all_indexs) or (n in all_indexs):
                aida = True

last_renzoku = False
if renzoku:
    last_renzoku = True
    renzoku = False
    end_i = 2*n - 1
    count += 1
    if count % 2 == 1:
        indexs = list(range(start_i+1, end_i, 2))
        taisho += len(indexs)
        #print(indexs)
        if (n-1 in indexs) or (n in indexs):
            aida = True
        last_odd = True
    elif count % 2 == 0:
        indexs1 = list(range(start_i, end_i, 2))
        indexs2 = list(range(start_i+1, end_i, 2))
        taisho += len(indexs1)
        #print(indexs1)
        all_indexs = indexs1 + indexs2
        if (n-1 in all_indexs) or (n in all_indexs):
            aida = True
        last_odd = False


#if True:
if not aida:
    assert taisho % 2 == 0
    hitotsu = taisho // 2
    out = hitotsu * k
else:
    if taisho % 2 == 0: 
        hitotsu = taisho // 2
        out = hitotsu * k
    else:
        if not last_renzoku:
            hitotsu = (taisho+1) // 2
            out = hitotsu * k - 1
        else:
            x = (taisho - 1) // 2
            #print("last_odd", last_odd)
            if last_odd:
                out = x + (x+1) * (k-1)
            else:
                if k % 2 == 0:
                    out = taisho * (k // 2)
                else:
                    out = taisho * ((k-1) // 2) + x


print(out)

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

n = len(s)
s2 = s + s

renzoku = False
taisho = 0
aida = False
for i in range(2*n-1):
    if s2[i] == s2[i+1]:
        if not renzoku:
            renzoku = True
            start_i = i
            count = 0
        count += 1

    elif renzoku:
        renzoku = False
        end_i = i
        count += 1
        #print(start_i, end_i)
        if count % 2 == 1:
            indexs = list(range(start_i+1, end_i, 2))
            taisho += len(indexs)
            #print(indexs)
            if (n-1 in indexs) or (n in indexs):
                aida = True
        elif count % 2 == 0:
            indexs1 = list(range(start_i, end_i, 2))
            indexs2 = list(range(start_i+1, end_i, 2))
            taisho += len(indexs1)
            #print(indexs1)
            all_indexs = indexs1 + indexs2
            if (n-1 in all_indexs) or (n in all_indexs):
                aida = True

if renzoku:
    renzoku = False
    end_i = 2*n - 1
    count += 1
    if count % 2 == 1:
        indexs = list(range(start_i+1, end_i, 2))
        taisho += len(indexs)
        #print(indexs)
        if (n-1 in indexs) or (n in indexs):
            aida = True
    elif count % 2 == 0:
        indexs1 = list(range(start_i, end_i, 2))
        indexs2 = list(range(start_i+1, end_i, 2))
        taisho += len(indexs1)
        #print(indexs1)
        all_indexs = indexs1 + indexs2
        if (n-1 in all_indexs) or (n in all_indexs):
            aida = True


#if not aida:
if True:
    assert taisho % 2 == 0
    hitotsu = taisho // 2
    out = hitotsu * k
#else:

print(out)