#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <set>

using namespace::std;




int main() {
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  
  long long best = 0;
  unordered_map<char, long long> t;
  for (char x : s) {
    if (x == 'C' || x == '?') best += t['A'];
    ++t[x];
  }
  
  long long b = best;
  unordered_map<char, long long> c;
  for (char x : s) {
    if (x == '?') {
      b -= c['A'] + c['?'];
      b += (t['C'] - c['C']) + (t['?'] - c['?'] - 1);
    }
    best = max(b, best);
    ++c[x];
  }
  
  cout << best << endl;
  
  

  

  
  return 0;
}