# ?の数を数えて、前半何個までをAとするかで全探索してみるか N = int(input()) S = input() q_count = S.count('?') if q_count == 0: 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 print(subans) exit() ans = 0 width = 100 for q in range(max(0, (q_count//2)-width), min(q_count, (q_count//2)+width)+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)