using System; using System.Collections.Generic; using System.Linq; namespace _672 { class Program { static void Main(string[] args) { Dictionary> dic = new Dictionary>(); string s = Console.ReadLine() + '?'; int count = 0; for (int i = 0; i < s.Length; i++) { if (!dic.ContainsKey(count)) dic[count] = new List(); dic[count].Add(i); if (s[i] == 'A') count++; if (s[i] == 'B') count--; } int ret = 0; foreach (var item in dic) { ret = Math.Max(ret, item.Value.Max() - item.Value.Min()); } Console.WriteLine(ret); } } }