#include using namespace std; using ll = long long; int main() { int N; string s; cin >> N >> s; vector sum(3, vector(N+1, 0)); for(int i = 0; i < N; i++) { int t = (s[i] == 'A' ? 0 : (s[i] == 'C' ? 1 : 2)); for(int j = 0; j < 3; j++) sum[j][i + 1] = sum[j][i] + (j == t); } ll now = 0; for(int i = 0; i < N; i++) { if(s[i] == 'A') { now += (sum[1][N] - sum[1][i+1]) + (sum[2][N] - sum[2][i+1]); } } ll ans = now; for(int i = 0; i < N; i++) { if(s[i] == '?') { now -= sum[0][i] + sum[2][i]; now += (sum[1][N] - sum[1][i+1]) + (sum[2][N] - sum[2][i+1]); } ans = max(ans, now); } cout << ans << endl; }