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 Kotlin (1.0.0) (judge verdict: Accepted)

import java.util.*

const val MOD: Long = (1e9+7).toLong()
fun Double.format(digits: Int) = String.format("%.${digits}f", this)
fun <T : Comparable<T>> Iterable<T>.argmax(): Int? = withIndex().maxBy{it.value}?.index
fun <T : Comparable<T>> Iterable<T>.argmin(): Int? = withIndex().minBy{it.value}?.index

fun main(args: Array<String>) {
    val reader = System.`in`.bufferedReader()
    val s = reader.readLine()!!
    val n = s.length

    val a = Array(n, {0L})
    for (i in 1..n-2) {
        if (s.substring(i..i+1) == "BC") {
            var f = false
            for (d in i-1 downTo 0) {
                if (a[d] > 0) {
                    a[i] += a[d]
                    break
                }
                if (f) {
                    break
                }
                if (s[d] == 'A') {
                    a[i] += 1L
                } else if (s[d] == 'B') {
                    break
                } else if (s[d] == 'C') {
                    f = true
                }
            }
        }
    }

    println(a.sum())
}