s = gets.chomp
sum = 0

while true do
    s.gsub!(/(A+)((?:BC)+)/) do |match|
        abc_size = [ $1.size, $2.size/2 ].min
        aaaa = $1.size <= abc_size   ? "" : "A" *($1.size - abc_size)
        bcbc = $2.size <= 2*abc_size ? "" : "BC"*(($2.size - abc_size*2)/2)
        sum += abc_size ** 2
        aaaa + "BC"*abc_size + "A"*abc_size + bcbc
    end
    break if s.index("ABC") == nil
end

puts sum
