import sys input = sys.stdin.readline N = int(input()) S = input().rstrip() right = [0] * (N+1) cnt = 0 for i in range(N)[::-1]: right[i] = right[i+1] if S[i] == 'A': right[i] += cnt else: cnt += 1 left = [0] * (N+1) cnt = 0 for i in range(N): left[i+1] = left[i] if S[i] == 'C': left[i+1] += cnt else: cnt += 1 ans = 0 A = 0 C = 0 for c in S: if c != 'A': C += 1 for i in range(N+1): ans = max(ans, left[i] + right[i] + A * C) if i < N: if S[i] == 'A': A += 1 elif S[i] == '?': A += 1 C -= 1 else: C -= 1 print(ans)