Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    s = S[i]
    if j>=0:
        if s=="A":
            i+=1
        elif i+1<l and s == "B" and S[i+1] == "C":
            S[i] = "A"
            S[i+1] = "A"
            S[j] = "B"
            S[j+1] = "C"
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i += 1
    elif s == "A":
        j=i
        i+=1
    else:
        i+=1
print(ans)

Fixed submission:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    if j==-1 and S[i] == "A":
        j=i
        i+=1
    elif j>=0 and S[i] == "A":
        i+=1
    elif j>=0:
        #"BC"が続く回数を数える
        bc = 0
        k = 0
        for c in S[i:]:
            if bc == 0 and c=="B":
                bc=1
            elif bc == 1 and c=="C":
                bc=0
                k+=1
            else:break
        if k > 0 :
            S[j+2*k-1] = "C"
            S[j+2*k:i+2*k] = ["A"]*(i-j)
            ans += (i-j)*k
            i = j+2*k-1
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Fixed submission:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    if j==-1 and S[i] == "A":
        j=i
        i+=1
    elif j>=0 and S[i] == "A":
        i+=1
    elif j>=0:
        if len(S[i:i+2])==2 and S[i] == "B" and S[i+1] == "C":
            S[i] = "A"
            S[i+1] = "A"
            S[j] = "B"
            S[j+1] = "C"
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Fixed submission:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    if j==-1 and S[i] == "A":
        j=i
        i+=1
    elif j>=0 and S[i] == "A":
        i+=1
    elif j>=0:
        if len(S[i:i+2])==2 and S[i] == "B" and S[i+1] == "C":
            S[j:i+2] = list("BC")+["A"]*(i-j)
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Fixed submission:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    if j==-1 and S[i] == "A":
        j=i
        i+=1
    elif j>=0 and S[i] == "A":
        i+=1
    elif j>=0:
        if len(S[i:i+2])==2 and S[i] == "B" and S[i+1] == "C":
            S[j:i+2] = list("BC")+["A"]*(i-j)
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Fixed submission:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)