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)-1 while MAX>MIN+1: l=(MIN+MAX)//2 A=list(S) B=list(S) for i in range(len(LIST)): if i<=l: A[LIST[i]]="A" else: A[LIST[i]]="C" if i<=l+1: B[LIST[i]]="A" else: B[LIST[i]]="C" if score(A)score(B): MAX=l else: MIN=l MAX=l+1 l=MIN A=list(S) B=list(S) for i in range(len(LIST)): if i<=l: A[LIST[i]]="A" else: A[LIST[i]]="C" if i<=l+1: B[LIST[i]]="A" else: B[LIST[i]]="C" if score(A)score(B): MAX=l else: MIN=l MAX=l+1 print(max(score(A),score(B)))