#include using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; string s; cin >> n >> s; LL ans = 0, c = 0; for (int i = 0, a = 0; i < n; i += 1) { if (s[i] == 'A') { a += 1; } else { c += 1; ans += a; } } for (int i = 0, a = 0; i < n; i += 1) { if (s[i] != 'A') { c -= 1; } if (s[i] == '?') { ans += max(c - a, 0LL); } if (s[i] != 'C') { a += 1; } } cout << ans; }