n = int(input()) s = input().strip() max_right_c = [0] * (n + 1) for i in range(n - 1, -1, -1): if s[i] == 'C' or s[i] == '?': max_right_c[i] = max_right_c[i + 1] + 1 else: max_right_c[i] = max_right_c[i + 1] a = 0 total = 0 for i in range(n): if s[i] == 'A': a += 1 elif s[i] == 'C': total += a else: # It's '?', decide between A or C current_max_right = max_right_c[i + 1] if i + 1 < n else 0 if a >= current_max_right: total += a else: a += 1 print(total)