using System; using System.Collections.Generic; using System.ComponentModel; namespace ConsoleApp2 { class Program { static void Main(string[] args) { String s = Console.ReadLine(); Int32 n = s.Length; Int32[] sum = new Int32[n + 1]; for (Int32 i = 0; i < n; i++) { if (s[i] == 'A') sum[i + 1] = sum[i] + 1; else sum[i + 1] = sum[i] - 1; } var mp = new Dictionary(); for (Int32 i = -200000; i <= 200000; i++) { mp.Add(i,(1000,0)); } Int32 ans = 0; for (Int32 i = 0; i <= n; i++) { var (a,b) = mp[sum[i]]; if (a > i) a = i; if (b < i) b = i; mp[sum[i]] = (a, b); ans = Math.Max(ans, b - a); } Console.WriteLine(ans); /*for (Int32 i = 0; i < n + 1; i++) { Console.WriteLine(sum[i]); }*/ } } }