def count(t): A=0 res=0 for i in t: if i=='A': A+=1 else: res+=A return res def calc(mid): t=[] for i in range(n): if s[i]=='A' or (s[i]=='?' and i<=B[mid]): t.append('A') else: t.append('C') return count(t) def ternary_search(L,R): # 準下凸関数の最小値 while L+2calc(c2): R=c2 else: L=c1 ans=0 for i in range(max(L,0),min(R+1,m)): ans=max(ans,calc(i)) return ans n=int(input()) s=list(input()) B=[] for i in range(n): if s[i]=='?': B.append(i) if len(B)==0: print(count(s)) exit() m=len(B) print(ternary_search(-1,m))