import sys input = sys.stdin.readline N=int(input()) S=input().strip() LIST=[] for i in range(N): if S[i]=="?": LIST.append(i) def score(A): ANS=0 now=0 for x in A: if x=="A": now+=1 else: ANS+=now return ANS MIN=0 MAX=len(LIST) while MAX>MIN+1: l=(MIN+MAX)//2 A=[] B=[] for i in range(len(S)): s=S[i] if s!="?": A.append(s) B.append(s) else: if i<=l: A.append("A") else: A.append("C") if i<=l+1: B.append("A") else: B.append("C") if score(A)score(B): MAX=l else: MIN=l MAX=l+1 l=MIN A=[] B=[] for i in range(len(S)): s=S[i] if s!="?": A.append(s) B.append(s) else: if i<=l: A.append("A") else: A.append("C") if i<=l+1: B.append("A") else: B.append("C") print(max(score(A),score(B)))