#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; int main() { lint N; string S; cin >> N >> S; lint a=0, b=0, c=0, x, y, z; lint ng = -1, m1, m2, ok = N; while (abs(ok - ng) > 2) { m1 = (ok + ng*2) / 3; m2 = (ok*2 + ng) / 3; rep(_, 2) { string t; rep(i, N) { if (S[i] == '?') { if (i < m1) t += 'A'; else t += 'C'; } else { t += S[i]; } } vi v(N); rep(i, N) { if (t[N-1-i] == 'C') v[N-1-i]++; } rep(i, N-1) v[N-2-i] += v[N-1-i]; a = 0; rep(i, N) { if (t[i] == 'A') a += v[i]; } swap(a, b); swap(m1, m2); } if (a < b) ng = m1; else ok = m2; chmax(c, a); chmax(c, b); } repx(j, ng-100, ok+100) { string t; rep(i, N) { if (S[i] == '?') { if (i < j) t += 'A'; else t += 'C'; } else { t += S[i]; } } vi v(N); rep(i, N) { if (t[N-1-i] == 'C') v[N-1-i]++; } rep(i, N-1) v[N-2-i] += v[N-1-i]; a = 0; rep(i, N) { if (t[i] == 'A') a += v[i]; } chmax(c, a); } cout << c << endl; }