#include int main () { int n = 0; char s[200001] = ""; int res = 0; long long ans = 0LL; long long cnt = 0LL; long long sum = 0LL; res = scanf("%d", &n); res = scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] != 'A') { sum += 1LL; } } for (int i = 0; i < n; i++) { if (s[i] == 'A') { cnt += 1LL; } else if (s[i] == 'C') { ans += cnt; sum -= 1LL; } else if (sum > cnt) { cnt += 1LL; sum -= 1LL; } else { ans += cnt; sum -= 1LL; } } printf("%lld\n", ans); return 0; }