N=int(input()) S=list(input()) AC=[False]*N for i in range(N): if S[i]=="?": AC[i]=True S[i]="C" cnt=0 a=0 for s in S: if s=="A": a+=1 else: cnt+=a ans=cnt a=0 c=S.count("C") for i in range(N): if AC[i]: S[i]="A" c-=1 cnt-=a cnt+=c if S[i]=="A": a+=1 else: c-=1 ans=max(ans,cnt) print(ans)