#ifdef LOCAL #include #else #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2") #include #define debug(...) ((void)0) #define postprocess(...) ((void)0) #endif #include "atcoder/segtree.hpp" using namespace std; using ll = long long; using ld = long double; using Sclass = pair; Sclass op(Sclass a, Sclass b) { return {a.first + b.first, a.second + b.second}; } Sclass e() { return {0, 0}; } void solve([[maybe_unused]] ll test) { ll N; cin >> N; string S; cin >> S; vector> a(N); for (ll i = 0; i < N; i++) { switch (S[i]) { case 'A': a[i] = {1, 0}; break; case 'C': a[i] = {0, 1}; break; default: a[i] = {0, 1}; break; } } atcoder::segtree st(a); ll ans = 0; ll tmp = 0; for (ll i = 0; i < N; i++) { if (S[i] == 'A') tmp += st.prod(i + 1, N).second; } ans = max(ans, tmp); for (ll i = 0; i < N; i++) { if (S[i] != '?') continue; st.set(i, {1, 0}); tmp -= st.prod(0, i).first; tmp += st.prod(i + 1, N).second; ans = max(ans, tmp); } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; for (int i = 1; i <= t; i++) { solve(i); } postprocess(); }