Problem:
 Score : 600 points    Problem Statement You are given a string s consisting of A , B and C .  Snuke wants to perform the following operation on s as many times as possible:   Choose a contiguous substring of s that reads ABC and replace it with BCA .   Find the maximum possible number of operations.      Constraints  1 \leq |s| \leq 200000  Each character of s is A , B and C .         Input Input is given from Standard Input in the following format:  s       Output Find the maximum possible number of operations.        Sample Input 1 ABCABC      Sample Output 1 3  You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA . This is the maximum result.       Sample Input 2 C      Sample Output 2 0       Sample Input 3 ABCACCBABCBCAABCB      Sample Output 3 6

Solution (Python):
S = input()

S = S.replace("BC", "D")

cnt_A = 0
ans = 0

"""
ABC -> BCA の処理は、BCとAwo入れ替えてAを右に送ると考えると理解しやすかった
ans = Aを何回右に送ることができるか
BCがあると、今あるAをそのBCの右に送ることができる
"""
for c in S:
    if c == "A":
        cnt_A += 1
    elif c == "D":
        ans += cnt_A
    else:
        
        cnt_A = 0
        
print(ans)

Solution (Python):
S = input()
T = ""
N = len(S)
flag = 0
for i in range(N):
  if flag == 1:
    flag = 0
    continue
  elif S[i:i+2] == "BC":
    T += "D"
    flag = 1
  else:
    T += S[i]

M = len(T)
count = 0
ans = 0
for i in range(M):
  if T[i] == "A":
    count += 1
  elif T[i] == "D":
    ans += count
  else:
    count = 0
print(ans)

Solution (Python):
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def Eratosthenes(N):
    r = [True]*(N+1)
    r[0] = False
    r[1] = False
    i = 2
    while i*i<=N:
        if r[i]: 
            j = i
            while i*j<=N:
                prime[i*j]=False
                j+=1
        i+=1
    return(r)

def main():
    s = S()
    N = len(s)
    
    ca = 0
    ans = 0
    i = 0
    while i<N:
        if s[i] == "A":
            ca += 1
            i += 1
        elif i<N-1 and s[i:i+2] == "BC":
            ans += ca
            i += 2
        else:
            ca = 0
            i += 1
    return(ans)
    





if __name__ == "__main__":
    print(main())

Solution (Python):
S = input()
N = len(S)
T = ""
flag = 0
ans = 0
for i in range(N-1):
  if flag == 1:
    flag = 0
    continue
  if S[i:i+2] == "BC":
    T += "D"
    flag = 1
  else:
    T += S[i]
M = len(T)
count = 0
for i in range(M):
  if T[i] == "A":
    count += 1
  elif T[i] == "D":
    ans += count
  else:
    count = 0
print(ans)

Solution (Python):
s = input()
s = s.replace("BC", "D")
ans = 0
a = 0
d = 0
a_bool = False
for i in range(len(s)):
    if not a_bool:
        if s[i] == "A":
            a += 1
            a_bool = True
        else:
            continue
    elif a_bool:
        if s[i] == "D":
            ans += a
        elif s[i] == "A":
            a += 1
        else:
            a_bool = False
            a = 0
            d = 0
print(ans)

Solution (Python):

S = input()
ans = 0
a = 0
i = 0
while (i < len(S)):
  if S[i] == 'A':
    a += 1
    i += 1
  elif S[i:i+2] == 'BC':
    ans += a
    i += 2
  else:
    a = 0
    i += 1
print(ans)

Solution (Python):
s=input()
t=""
i=0
while i<len(s):
    if s[i:i+2]=="BC":
        t+="T"
        i+=2
    else:
        t+=s[i]
        i+=1
ans=0
Acnt=0
for i in t:
    if i=="A":
        Acnt+=1
    elif i=="T":
        ans+=Acnt
    else:
        Acnt=0
print(ans)

Solution (Python):
s = input()[::-1].replace('CB', 'x')


x = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'x':
        x += 1
    elif s[i] == 'A':
        ans += x
    else:
        x = 0


print(ans)

Solution (Python):
S = input()
S = S.replace('BC', 'D').replace('B', ' ').replace('C', ' ').split()

ans = 0
for s in S:
    leftA = 0
    for i, t in enumerate(s[:: -1]):
        if t == 'A':
            ans += max(0, i - leftA)
            leftA += 1

print(ans)

Solution (Python):
def main():
    S = input().replace("BC","D")
    ans = count = 0
    for i in S:
        if i == "A":
            count += 1
        elif i == "D":
            ans += count
        else:
            count = 0

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



s = S()
ans = 0
i = len(s) - 1
a_cnt = 0
bc_cnt = 0
while i >= 0:
    if s[i-1:i+1] == 'BC':
        i -= 2
        bc_cnt += 1
    elif s[i] == 'A':
        i -= 1
        a_cnt += 1
        ans += bc_cnt
    else:
        a_cnt = bc_cnt = 0
        i -= 1




print(ans)

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
n = len(s)
t = ''
i = 0
while i < n-1:
    if s[i:i+2] == 'BC':
        t += 'D'
        i += 2
    else:
        t += s[i]
        i += 1
m = len(t)
ans = 0
a,d = 0,0
for i in range(m):
    if t[i] == 'A':
        a += 1
    elif t[i] == 'D':
        ans += a
    else:
        a,d = 0,0
print(ans)

Solution (Python):
s = input()

count = 0
block = 0
i = len(s) - 1
while i >= 0 :
    if s[i-1] == "B" and s[i] == "C":
        block += 1
        i -= 1
    elif s[i] == "A":
        count += block
    else:
        block = 0
    i -= 1
    
print(count)

Solution (Python):
s = input()
n = len(s)
def count_inv(arr):
    left = 0
    ans = 0
    for i in range(len(arr)):
        if arr[i] == 1:
            left += 1
        else:
            ans += left
    return ans

temp = []
ind = 0
ans = 0
while True:
    if ind == n:
        ans += count_inv(temp)
        break
    c = s[ind]
    if c == "A":
        temp.append(1)
        ind += 1
        continue
    elif c == "B":
        if ind == n-1:
            ans += count_inv(temp)
            break
        if s[ind+1] == "C":
            temp.append(0)
            ind += 2
            continue
        else:
            ans += count_inv(temp)
            temp = []
            ind += 1
            continue
    else:
        ind += 1
        ans += count_inv(temp)
        temp = []
        continue


print(ans)

Solution (Python):
S = input()
idx = 0
a_cnt = 0
cnt = 0
len_s = len(S)
while idx < len_s:
  if S[idx:idx+3] == 'ABC':
    cnt += 1 + a_cnt
    idx += 3
    a_cnt += 1
    bc_cnt = 0
    while idx < len(S):
      if S[idx:idx+2] == 'BC':
        cnt += a_cnt
        idx += 2
      else:
        break
  else:
    if S[idx] == 'A':
      a_cnt += 1
    else:
      a_cnt = 0
    idx += 1
print(cnt)

Solution (Python):
def main():
    S = input()
    li = []
    li_append = li.append
    is_A = False
    now_state = 0
    for ind,s in enumerate(S):
        if(is_A):
            if(now_state==1):
                now_state = 0
                continue
            else:
                if(s=='A'):
                    li[-1].append(0)
                elif(S[ind:ind+2]=='BC'):
                    li[-1][-1] += 1
                    now_state = 1
                else:
                    is_A = False
        elif(s=='A'):
            is_A = True
            now_state = 0
            li_append([0])
    cnt = 0
    for i in li:
        cnt_b = sum(i)
        for j in i:
            cnt += cnt_b
            cnt_b -= j
            
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    S = input().replace("BC", "D")

    ans = a = 0
    for s in S:
        if s == "A":
            a += 1
        elif s == "D":
            ans += a
        else:
            a = 0

    print(ans)

main()

Solution (Python):
S = input().replace("BC", "D")

ans = a = 0
for s in S:
    if s == "A":
        a += 1
    elif s == "D":
        ans += a
    else:
        a = 0

print(ans)

Solution (Python):

def main():
    S = input().rstrip().replace("BC", "X")
    ans, cur = 0, 0
    for i in S:
        if i == "A":
            cur += 1
        elif i == "X":
            ans += cur
        else:
            cur = 0
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
S = input()
N = len(S)
i = 0
T = []
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T.append('D')
        i += 1
    else:
        T.append(S[i])
    i += 1

N = len(T)
ans = 0
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = []
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp.append(T[r])
        r += 1
    
    M = len(tmp)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if tmp[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if tmp[i] == 'A':
            ans += D_count[i]
    
    if r == N:
        break
    else:
        l = r+1

print(ans)

Solution (Python):
S = input()
N = len(S)
i = 0
T = []
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T.append('D')
        i += 1
    else:
        T.append(S[i])
    i += 1

T = "".join(T)

N = len(T)
ans = 0
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = []
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp.append(T[r])
        r += 1
    
    M = len(tmp)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if tmp[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if tmp[i] == 'A':
            ans += D_count[i]
    
    if r == N:
        break
    else:
        l = r+1

print(ans)

Solution (Python):
s = input().replace("BC", "D")
n = len(s)
ans = 0
l = r = 0
while l < n:
    if s[l] != 'A':
        l += 1
        continue
    r = l + 1
    a = 1
    while r < n:
        if s[r] == 'A':
            a += 1
            r += 1
        elif s[r] == 'D':
            ans += a
            r += 1
        else:
            break
    l = r
print(ans)

Solution (Python):
s = input()
N = len(s)

i = N-1
ret = 0
BC_count = 0
while i >= 0:
    prev = i-1
    if prev < 0:
        if s[i] == 'A':
            ret += BC_count
            
        break
        
    
    if s[i] == 'A':
        ret += BC_count
        if s[prev] == 'B':
            BC_count = 0
    elif s[i]=='C':
        if s[prev]=='B':
            BC_count += 1
        else:
            BC_count = 0
    elif s[i]=='B':
        if s[prev]=='B':
            BC_count = 0

    i-= 1


print(ret)

Solution (Python):
s = input()
arr = list()

i = 0
buf = ''
while i < len(s):
    if s[i] == 'A':
        if buf:
            arr.append(buf)
            buf = ''
        arr.append('A')
        i += 1
    elif s[i: i + 2] == 'BC':
        if buf:
            arr.append(buf)
            buf = ''
        arr.append('BC')
        i += 2
    else:
        buf += s[i]
        i += 1
if buf:
    arr.append(buf)

cnt = 0
stack = 0
for e in reversed(arr):
    if e == 'BC':
        stack += 1
    elif e == 'A':
        cnt += stack
    else:
        stack = 0
print(cnt)

Solution (Python):
a=b=0
for s in input().replace('BC','D'):
    if s=='A':
        a+=1
    elif s=='D':
        b+=a
    else:
        a=0
print(b)

Solution (Python):
S = input()

cnt = 0

S = S.replace("BC", "X")
S = S.replace("B", " ")
S = S.replace("C", " ")

arr = S.split()

for elem in arr:
    i = 0

    for char in elem:
        if char == "A":
            i += 1
        else:
            cnt += i

print(cnt)

Solution (Python):
s=input()
l=s.replace('BC','D')
a=0
ans=0
for i in range(len(l)):
  if l[i]=='A':
    a+=1
  elif l[i]=='D':
    ans+=a
  else:
    a=0
print(ans)

Solution (Python):
s = str(input())
d = s.replace("BC", "D")

count = 0
count_a = 0
for i,c in enumerate(d):
    if c == "A":
        count_a += 1
    elif c == "D":
        count += count_a
    else:
        count_a = 0
print(count)

Solution (Python):
sentence = input()
slist = sentence.split("BC")
res = 0
a = 0
for l in slist:
  if ("B" in l) or ("C" in l) :
    l = l.replace("C", "B")
    al = l.split("B")
    a = len(al[-1])
    res += a
  else:
    a += len(l)
    res += a
else:
  res -= a
print(res)

Solution (Python):
s=input()+"v"
n=len(s)
p=0
r=0
c=0
while(1):
    if c>=n:
        break
    if s[0]=="A":
        p+=1
        s=s[1:]
        c+=1
    elif s[:2]=="BC":
        s=s[2:]
        c+=2
        r+=p
    else:
        s=s[1:]
        c+=1
        p=0
print(r)

Solution (Python):
s = input()
ss = s.split("BC")
ans = 0
a = 0
for abaca in ss:
    if ("B" in abaca) or ("C" in abaca) :
        ababa = abaca.replace("C", "B")
        a_a_a = ababa.split("B")
        a = len(a_a_a[-1])
        ans += a
    else :
        a += len(abaca)
        ans += a
else :
    ans -= a
print(ans)

Solution (Python):
S = input()
N = len(S)

a = 0
ans = 0
nowB = False
for i in range(N):
    if S[i] == 'A':
        if nowB:
            a = 0
        a += 1
        nowB = False
    elif S[i] == 'B':
        if nowB:
            a = 0
        nowB = True
    else:
        if nowB:
            ans += a
            nowB = False
        else:
            a = 0
print(ans)

Solution (Python):



import sys
tmp = input()
limit = len(tmp)

i=0
numa=0
total=0
while(i<limit):
	if(tmp[i]=="A"):
		numa+=1
		i+=1
	elif(i+1==limit):
		break
	elif(tmp[i]=="B")and(tmp[i+1]=="C"):
		total+=numa
		i+=2
	else:
		numa=0
		i+=1



print(total)

Solution (Python):
s = input()
s = s.replace('BC', 'D')
ans = 0
x = 0



for i in range(len(s)):
  if s[i] == 'A': 
    x +=1
  elif s[i] == 'D':
    ans += x
  else:
    x =0
    
print(ans)

Solution (Python):
s = input()
s = s.replace("BC", "D")


temp = 0
ans = 0
for i in s:
    if i == "A":
        temp += 1
    elif i == "D":
        ans += temp
    elif i == "B" or i == "C":
        temp = 0
        
print(ans)

Solution (Python):
s = input()
s = s.replace("BC", 'D')

ans = 0
x = 0
for i in range(len(s)):
    if s[i] == 'A':
        x += 1
    elif s[i] == 'D':
        ans += x
    else:
        x = 0

print(ans)

Solution (Python):
s = input()

ans = 0
x = 0
index = 0

while index < len(s):
    if s[index] == 'A':
        x += 1
        index += 1
    elif s[index:index+2] == "BC":
        ans += x
        index += 2
    else:
         index += 1
         x = 0

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input()
sum = 0
sum_a = 0
expect = 'B'
for i in range(len(S)):
    
    if S[i] == 'A':
        if expect == 'C':
            sum_a = 0
        sum_a += 1
        expect = 'B'
    elif S[i] == expect and expect == 'C':
        sum += sum_a
        expect = 'B'
    elif S[i] == expect and expect == 'B':
        expect = 'C'
    else:
        sum_a = 0
        expect = 'B'

print(sum)

Solution (Python):
s=input()
l=[]
i=0
while i < len(s):
    if i<len(s)-1:
        if s[i]=='B' and s[i+1]=='C':
            l.append('D')
            i+=2
        else:
            l.append(s[i])
            i+=1
    else:
        l.append(s[i])
        i+=1
n=len(l)
ca=0
count=0
for i in range(n):
    if l[i]=='A':
        ca+=1
    elif l[i]=='D':
        count+=ca
    else:
        ca=0
print(count)

Solution (Python):
S = input().replace("BC","D")

count = 0
ans = 0
for i in range(len(S)):
    if S[i] == "A":
        count += 1
    elif S[i] == "D":
        ans += count
    else:
        count = 0
print(ans)

Solution (Python):
s = input().replace('BC','D')
n = len(s)
 
count = 0
res = 0
 
for i in range(n):
    if s[i] == 'A':
        count += 1
    elif s[i] == 'D':
        res += count
    else:
        count = 0
print(res)

Solution (Python):
S = input()
S = S.replace('BC', 'X')

ans = 0
A_cnt = 0
for s in S:
    if s == 'A':
        A_cnt += 1
    elif s == 'X':
        ans += A_cnt
    else:
        A_cnt = 0

print(ans)

Solution (Python):
s = input().replace('BC','D')
n = len(s)

count = 0
ans = 0

for i in range(n):
    if s[i] == 'A':
        count += 1
    elif s[i] == 'D':
        ans += count
    else:
        count = 0

print(ans)

Solution (Python):
S = input()
S = S.replace("BC","X")
count = 0
cntA = 0
for c in S:
    if c == "A":
        cntA += 1
    elif c == "X":
        count += cntA
    else:
        cntA = 0
print(count)

Solution (Python):
S = input() + "$"
L = len(S)
SS = []
E = []
i = 0
while i < L:
    if S[i] == 'A':
        E.append(0)
        i += 1
        continue
    if S[i:i+2] == 'BC':
        E.append(1)
        i += 2
        continue
    if E:
        SS.append(E)
        E = []
    i += 1

ans = 0
for E in SS:
    cur = 0
    for c in reversed(E):
        if c:
            cur += 1
        else:
            ans += cur
print(ans)

Solution (Python):
from functools import reduce


s = input()


ans, _, _ = reduce(
    lambda acc, c:
        (acc[0], (1 - acc[2]) * acc[1] + 1, 0) if c == 'A' else
        (acc[0], (1 - acc[2]) * acc[1], 1 - acc[2]) if c == 'B' else
        (acc[0] + acc[2] * acc[1], acc[2] * acc[1], 0),
    s,
    (0, 0, 0)
)


print(ans)

Solution (Python):




 
from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul,itemgetter
from bisect import* 
from heapq import* 
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)

 
def main():
    s=input().replace("BC","X")

    c=0
    d=0
    for i in s:
        if i=="A":
            c+=1
        elif i=="X":
            d+=c
        else:
            c=0
    print(d)



if __name__ == '__main__':
    main()

Solution (Python):




 
from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul,itemgetter
from bisect import* 
from heapq import* 
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)

 
def main():
    s=input().replace("BC","X")

    c=0
    d=0
    for i in s:
        if i=="A":
            c+=1
        elif i=="X":
            d+=c
        else:
            c=0
    print(d)



if __name__ == '__main__':
    main()

Solution (Python):
s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i + 2] == "BC":
        ans += cntA
    elif i > 0 and s[i - 1: i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)

Solution (Python):
s = input() + "_"

a = 0
r = 0
i = 0

while i < len(s):
    if s[i] == "A":
        a += 1
        i += 1
    elif s[i] == "B":
        if s[i + 1] == "C":
            r += a
            i += 2
        else:
            a = 0
            i += 1
    else:
        a = 0
        i += 1

print(r)

Solution (Python):

"""
Created on Sat Apr 13 09:53:12 2019
 
@author: Owner
"""
import collections
 

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """
 
    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx
 
def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
 
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
 
    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
 
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
 
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
 
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    
 
"""
N, X = map(int, input().split())
 
x = list(map(int, input().split()))
 
P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
 

dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')
 

ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)
 

(x >> i) & 1
 

dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

add = 1 
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)
 

li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    
 

org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)


 

    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)
 
"""
 
 
 
s = input()
S = collections.deque(s)
def main():     
    res = 0
    
    past = ""
    now = ""
    cnt_bc = 0
    flg_bc = False
    for n,s in enumerate(reversed(S)):
        if s == "A" and flg_bc:
            res += cnt_bc
            continue
        now = s 
        if past == "C" and now == "B":
            cnt_bc += 1
            flg_bc = True
        elif past == "C" and now == "C":
            cnt_bc = 0
            flg_bc = False
        elif past == "B" and now == "B":
            cnt_bc = 0
            flg_bc = False
        elif past == "B" and now == "C":
            flg_bc = False
        else:
            cnt_bc = 0
            flg_bc = False
        past = now
        
    
    print(res)
                
                
                
            
            
                
 
    
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    S = input()
    N = len(S)
    A = 0
    ans = 0
    i = 0
    while i < N:
        if S[i] == "A":
            A += 1
            i += 1
        elif i+1 < N and S[i] == "B" and S[i+1] == "C":
            ans += A
            i += 2
        else:
            A = 0
            i += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
cnt, tmp = 0, 0
for c in input().replace("BC", "X"):
  if c == "A":
    tmp += 1
  elif c == "X":
    cnt += tmp
  else:
    tmp = 0
print(cnt)

Solution (Python):
s = "Z".join(input().split("BC"))
l = len(s)
count = 0
cue = 0
for i in range(l):
	if s[i] == "A":
		cue += 1
	elif s[i] == "Z":
		count += cue
	else:
		cue = 0
print(count)

Solution (Python):
s = input()

inversion = 0
count_a = 0
for si in s.replace("BC", "D"):
    if si == "A":
        count_a += 1
    elif si == "D":
        inversion += count_a
    else:
        count_a = 0

print(inversion)

Solution (Python):
s = input().strip()
len_s = len(s)
i = a = bc = ans = 0
while i < len_s:
    if s[i] == 'A':
        a += 1
        i += 1
    elif s[i:i+2] == 'BC':
        bc = 1
        ans = ans + bc * a
        i += 2
    else:
        a = bc = 0
        i += 1

print(ans)

Solution (Python):







import sys
from itertools import combinations, permutations 
from math import factorial
def combinations_count(n, r):
    
    
    
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result
def combination_with_repetition_count(n, r):
    return combinations_count(n + r - 1, r)
from collections import deque, Counter 
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
from copy import deepcopy, copy 
from functools import reduce
from fractions import gcd 
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers):
    return reduce(lcm, numbers, 1)

    
    
    
    
    
    
    
    
    


s = input()
s = s.replace('A','0')
s = s.replace('BC','1')
s = s.replace('B',' ')
s = s.replace('C', ' ')
l = s.split()
ans = 0
for i in l:
    countbc = 0
    for j in i[::-1]:
        if j == '1':
            countbc += 1
        else:
            ans += countbc

print(ans)

Solution (Python):
S = input()
S = S.replace("BC", "D")

c = 0
total = 0

for s in S:
    if s == "A":
        c += 1
    elif s == "D":
        total += c
    else:
        c = 0

print(total)

Solution (Python):
from functools import reduce


s = input()


ans, _, _ = reduce(
    lambda acc, c: (
        (acc[0], (1 - acc[2]) * acc[1] + 1, 0) if c == 'A' else
        (acc[0], (1 - acc[2]) * acc[1], 1 - acc[2]) if c == 'B' else
        (acc[0] + acc[2] * acc[1], acc[2] * acc[1], 0)
    ),
    s,
    (0, 0, 0)
)


print(ans)

Solution (Python):
s = input()
s = s.replace('BC','D')

ans = 0
cnta = 0
for n in range(len(s)):
    if s[n] == 'A':
        cnta += 1
    elif s[n] == 'D':
        ans += cnta
    elif s[n] == 'B' or s[n] == 'C':
        cnta = 0

print(ans)

Solution (Python):
s = input()
n = len(s)
ans = 0
numa = 0
i = 0
while i < n-1:  
    if s[i] == 'A':
        numa += 1   
    elif s[i:i+2] == 'BC':
        ans += numa 
        i += 1  
    else:
        numa = 0
    i += 1
print(ans)

Solution (Python):
s = input()
s = s.split('BC')
count = [0]
count_all = 0
Icount = 0

for i in range(0,len(s)-1):
    Icount = 0
    j = 1
    while(j<=len(s[i]) and s[i][-j]=='A'):
        Icount = Icount + 1
        j = j + 1
        
    if (s[i]=='' or s[i]=='A'*len(s[i])):
        Icount = Icount + count[-1]
   
    count.append(Icount)
  
        
count_all = sum(count)  
print(count_all)

Solution (Python):
S=input()

cnt=[]

def solve(s):
  pos = len(s)-sum(s)
  ct=0
  res=0
  for i in range(len(s)):
    if s[i]==1:
      res+=(pos+ct-i)
      ct+=1
  return res

n=0
while n < len(S):
  if S[n]=='A':
    cnt.append(1)
    n+=1
  elif (n < len(S)-1) and (S[n]=='B') and (S[n+1]=='C'):
    cnt.append(0)
    n+=2
  else:
    cnt.append(2)
    n+=1

r=0
l=0
ans=0
while l < len(cnt):
  if cnt[l] != 2:
    r=l
    while r<len(cnt) and cnt[r]!=2:
      r+=1
    ans+=solve(cnt[l:r])
    l=r
  else:
    l+=1

    
print(ans)

Solution (Python):
s = input()
n = len(s)

ans = 0
cnt = 0

for i in range(n-1):
    if s[i] == "A":
        cnt += 1
    if s[i]+s[i+1] == "BC":
        ans += cnt
    if s[i]+s[i+1] in ("AC","BA","BB","CC"):
        cnt = 0

print(ans)

Solution (Python):
s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i + 2] == "BC":
        ans += cntA
    elif i > 0 and s[i - 1: i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)

Solution (Python):
S = input().replace('BC', 'D')
ans, n = 0, 0

for s in S:
    if s == 'A':
        n += 1
    elif s == 'D':
        ans += n
    else:
        n = 0
print(ans)

Solution (Python):
s = input().replace("BC", 'X')
a, x, ans = 0, 0, 0
for char in s:
    if char == 'A':
        a += 1
    elif char == 'X':
        ans += a
    else:
        a, x = 0, 0
print(ans)

Solution (Python):
s = str(input())
s = s.replace("BC","X")

retn = 0
cnt = 0

for i in range(len(s)):

    if (s[i] == "A"):
        cnt += 1
    elif (s[i] == "X"):
        retn += cnt
    else:
        cnt = 0

print(retn)

Solution (Python):
s = input()
s= s.replace('BC','D')
a=0
d=0
ans=0
t=''
for i in range(len(s)):
    if s[i] =='A':
        a+=1
    elif s[i]=='D':
        ans+=a
    else:
        a=0
print(ans)

Solution (Python):
import copy
S = input()

S2 = S.replace("BC", "D")

count = 0
Acount = 0
Dcount = 0
for i in range(len(S2)):
    if S2[i] == 'A' or S2[i] == 'D':
        if S2[i] == 'A':
            Acount += 1
        else:
            count += Acount
    else:
        Acount = 0
    
print(count)

Solution (Python):
s = input().strip()
n = len(s)
ret = 0
a = 0
i = 0
while i < n:
    if s[i] == 'A':
        a += 1
        i += 1
    elif s[i] == 'B':
        if i < n - 1 and s[i + 1] == 'C':
            ret += a
            i += 2
        else:
            a = 0
            i += 1
    else:
        a = 0
        i += 1
print(ret)

Solution (Python):
S = input()+"D"

i = 0
ans = 0
A = 0
while i < len(S)-1:
    if A > 0 and S[i] == "B" and S[i+1] == "C":
        i += 2
        ans += A
    elif S[i] == "A":
        A += 1
        i += 1
    else:
        A = 0
        i += 1
print(ans)

Solution (Python):
s = input()
n = len(s)
st = ''
i = 0
while i < n:
    if i < n - 1 and s[i] == 'B' and s[i + 1] == 'C':
        st += 'D'
        i += 1
    else:
        st += s[i]
    i += 1
ans = 0
ac = 0
for c in st:
    if c in ('B', 'C'):
        ac = 0
    elif c == 'A':
        ac += 1
    elif c == 'D':
        ans += ac
print(ans)

Solution (Python):
s=input()

S=[s[0]]

for i in range(1,len(s)):
    if S[len(S)-1]=="B" and s[i]=="C":
        S[len(S)-1]="D"
    else:
        S.append(s[i])


L=[]
if S[len(S)-1]=="D":
    L.append(1)
else:
    L.append(0)
    
for i in range(1,len(S)):
    if S[len(S)-1-i]=="D":
        L.append(L[len(L)-1]+1)
    elif S[len(S)-1-i]=="A":
        L.append(L[len(L)-1])
    else:
        L.append(0)
L=L[::-1]

ANS=0
for i in range(len(S)):
    if S[i]=="A":
        ANS+=L[i]
print(ANS)