# ?の数を数えて、前半何個までをAとするかで全探索してみるか N = int(input()) S = input() q_count = S.count('?') ans = 0 for q in range(q_count+1): subans = 0 A_count = 0 used = 0 for i in range(N): if S[i] == 'A': A_count += 1 elif S[i] == 'C': subans += A_count elif S[i] == '?': if used < q: # treat as A used += 1 A_count += 1 else: # treat as C subans += A_count #print('q', q, 'subans', subans) ans = max(ans, subans) print(ans)