program main implicit none integer::i,j,k,x,y,r,ans integer,parameter::MAX_N=200000 integer::n,ma(-MAX_N:MAX_N),mi(-MAX_N:MAX_N) character(MAX_N)::s read*,s n=len_trim(s) ma(0)=0 mi(0)=0 do i=-n,n ma(i)=-1 mi(i)=n+1 end do r=0 do i=1,n if(s(i:i)=='A')then r=r+1 else r=r-1 end if ma(r)=max(ma(r),i) mi(r)=min(mi(r),i) end do ans=0 do i=-n,n ans=max(ans,ma(i)-mi(i)) end do print*,ans end program