#include using namespace std; using ll = long long; int main() { int N; string S; cin >> N >> S; // 初期解('?' をすべて 'C' に置き換えたもの)の得点を求める ll tmp = 0; { int LA = 0; // 左にある 'A' の個数 for (int i = 0; i < N; i++) { if (S.at(i) == 'A') LA++; else tmp += LA; } } // 'A' を増やしていきながら、すべての文字列を調べる ll ans = tmp; { int LA = 0; // 左にある 'A' の個数 int RC = count(S.begin() + 1, S.end(), 'C') + count(S.begin() + 1, S.end(), '?'); // 右にある 'C' の個数 for (int i = 0; i < N; i++) { if (S.at(i) == '?') { tmp -= LA; tmp += RC; ans = max(ans, tmp); } if (S.at(i) != 'C') LA++; if (i < N - 1 && S.at(i + 1) != 'A') RC--; } } cout << ans << endl; }