N = int(input()) S = list(input()) ans = 0 cnt = 0 for i in range(N): if S[i] == "A" or S[i] == "?": cnt += 1 else: ans += cnt num = ans cntC = 0 for i in range(N - 1, -1, -1): if S[i] == "?": cnt -= 1 num += cnt num -= cntC cntC += 1 elif S[i] == "C": cntC += 1 ans = max(ans, num) print(ans)