#include #include using namespace std; int main() { map left; map right; string s; cin >> s; int ans = 0; left[0] = 0; for (int i = 0; i < s.size(); i++) { ans += (s[i] == 'A' ? 1 : -1); if (left.find(ans) == left.end()) { left[ans] = i; } right[ans] = i; } ans = 0; const int N = s.size(); for (int i = -N; i < N; i++) { ans = max(ans, right[i] - left[i]); } cout << ans << endl; return 0; }