#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; string s; cin >> n >> s; vector cntA(n + 1, 0), cntC(n + 1, 0); vector cntQ(n + 1, 0), cntQ2(n + 1, 0); for (int i = 0; i < n; i++) { cntA[i + 1] = cntA[i] + (s[i] == 'A'); } for (int i = n - 1; i >= 0; i--) { cntC[i] = cntC[i + 1] + (s[i] == 'C'); } for (int i = n - 1; i >= 0; i--) { cntQ[i] = cntQ[i + 1] + (s[i] == '?'); } for (int i = 0; i < n; i++) { cntQ2[i + 1] = cntQ2[i] + (s[i] == '?'); } long long ans = 0; long long cur = 0; for (int i = 0; i < n; i++) { if (s[i] == 'A') { cur += cntC[i] + cntQ[i]; } } ans = cur; for (int i = 0; i < n; i++) { if (s[i] == '?') { cur -= cntA[i] + cntQ2[i]; cur += cntQ[i + 1] + cntC[i + 1]; ans = max(ans, cur); } } cout << ans << '\n'; }