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 in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using static MyIO;


public class B
{
	public static void Main()
	{
		string[] s = Regex.Replace(GetString(), "BC", "D").Split('B', 'C');
		long ans = 0;
		for(int i = 0; i < s.Length; i++)
		{
			var bit = new BinaryIndexedTree<int>(s[i].Length, 0, (x, y) => x + y);
			int iD = 1;
			int iA = s[i].ToCharArray().Count(x => (x == 'D')) + 1;
			for(int j = 0; j < s[i].Length; j++)
			{
				if(s[i][j] == 'A')
				{
					ans += j - bit.Query(iA);
					bit.Update(iA, 1);
					iA++;
				}
				else
				{
					ans += j - bit.Query(iD);
					bit.Update(iD, 1);
					iD++;					
				}
			}
		}
		Console.WriteLine(ans);
	}
}


public class BinaryIndexedTree<T>
{
	private int N;
	private T INI;
	private Func<T, T, T> Func;
	private T[] bit;

	public BinaryIndexedTree(int n, T ini, Func<T, T, T> func)
	{
		N = n;
		INI = ini;
		Func = func;
			
		bit = new T[N + 1];  // 1-index
		for(int i = 0; i <= N; i++)
			bit[i] = INI;
	}

	public void Update(int i, T x)
	{
		while(i <= N)
		{
			bit[i] = Func(bit[i], x);
			i += i & -i;
		}
	}

	public T Query(int i)
	{
		T s = INI;
		while(i > 0)
		{
			s = Func(s, bit[i]);
			i -= i & -i;
		}
		return s;
	}
}


public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt(){ return int.Parse(getArg()); }
	public static long GetLong(){ return long.Parse(getArg()); }
	public static double GetDouble(){ return double.Parse(getArg()); }
	public static string GetString(){ return getArg(); }
}