Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	st=[]
	s=f4()
	n1=0
	ans=0
	for i in s:
		n1+=1
		st.append(i)
		#print st
		if n1>=3 and st[n1-3:]==['A','B','C']:
			#print 123
			s1=[]
			n2=n1-4
			ans+=1
			st[n1-1]="A"
			while n2>-1 and st[n2]=="A":
				ans+=1
				st[n2+2]="A"
				n2-=1
			st[n2+1],st[n2+2]="B","C"
			#print st,12
	print ans


if __name__ == '__main__':
	main()


Fixed submission:
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	
	s=f4()
	n1=0
	ans=0
	i=0
	while i<len(s)-1:
		if s[i]=="A":
			n1+=1
		elif s[i:i+2]=="BC":
			ans+=n1
			i+=1
		else:
			n1=0
		i+=1
	print ans

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	st=[]
	s=f4()
	n1=0
	ans=0
	for i in s:
		n1+=1
		st.append(i)
		if i=="C" and n1>=3:
			s1=[]
			n2=n1-1
			for i in xrange(3):
				s1.append(st[n2])
				n2-=1
			s1=s1[::-1]
			if s1==["A","B","C"]:
				ans+=1
				st[n1-1]="A"
				while n2>-1 and st[n2]=="A":
					ans+=1
					st[n2+2]="A"
					n2-=1
				st[n2+1],st[n2+2]="B","C"
	print ans


if __name__ == '__main__':
	main()


Fixed submission:
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	
	s=f4()
	n1=0
	ans=0
	i=0
	while i<len(s)-1:
		if s[i]=="A":
			n1+=1
		elif s[i:i+2]=="BC":
			ans+=n1
			i+=1
		else:
			n1=0
		i+=1
	print ans

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()

st=[]
s=f4()
n1=0
ans=0
for i in s:
	n1+=1
	st.append(i)
	if n1>=3:
		s1=""
		for i in xrange(3):
			s1+=st.pop()
		s1=s1[::-1]
		if s1=="ABC":
			ans+=1
			s1="BCA"
			while st and st[-1]=="A":
				s1+="A"
				ans+=1
				st.pop()
			st.extend(list(s1))

		else:
			st.extend(list(s1))

print ans


Fixed submission:
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	
	s=f4()
	n1=0
	ans=0
	i=0
	while i<len(s)-1:
		if s[i]=="A":
			n1+=1
		elif s[i:i+2]=="BC":
			ans+=n1
			i+=1
		else:
			n1=0
		i+=1
	print ans

if __name__ == '__main__':
	main()