#include using namespace std; int main(){ int N; cin >> N; string S; cin >> S; vector A(N + 1, 0), C(N + 1, 0); for (int i = 0; i < N; i++){ A[i + 1] = A[i]; C[i + 1] = C[i]; if (S[i] == 'A'){ A[i + 1]++; } if (S[i] == 'C'){ C[i + 1]++; } } long long x = 0; for (int i = 0; i < N; i++){ if (S[i] == 'C'){ x += A[i]; } } vector X, Y; int M = 0; for (int i = 0; i < N; i++){ if (S[i] == '?'){ M++; X.push_back(C[N] - C[i]); Y.push_back(A[i]); } } long long sumy = 0; for (int i = 0; i < M; i++){ sumy += Y[i]; } long long mx = sumy; for (int i = 0; i < M; i++){ sumy -= Y[i]; sumy += Y[i]; mx = max(mx, sumy + (long long) (i + 1) * (M - 1 - i)); } cout << mx + x << endl; }