#include using namespace std; const int N = 4e5 + 7; // ??? int pos[N], a[N], b[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); s = " " + s; for (int i = 1; i <= n; ++ i) { a[i] = a[i - 1] + (s[i] == 'A'); b[i] = b[i-1] + (s[i] == 'B'); } int eps = 200000; // ??? for (int i = 0; i <= 400000; ++ i) pos[i] = n + 1; for (int i = 1; i <= n; ++ i) { int x = a[i] - b[i] + eps; // pos[x] = min(pos[x], i); if (pos[x] == n + 1) pos[x] = i; } int ans = 0; // ??????? for (int r = 1; r <= n; ++ r) { int x = a[r] - b[r] + eps; if (pos[x] < r) { // ????? ans = max(ans, r - pos[x]); } } cout << ans; return 0; }