/* -*- coding: utf-8 -*- * * 2276.cc: No.2276 I Want AC - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ typedef long long ll; /* global variables */ char s[MAX_N + 4]; /* subroutines */ ll check(int n, int x) { ll sum = 0; int a = 0, c = 0; for (int i = 0; i < n; i++) { if ((s[i] == '?' && i < x) || s[i] == 'A') a++; else sum += a, c++; } return sum; } /* main */ int main() { int n; scanf("%d%s", &n, s); int x0 = 0, x1 = n; while (x0 + 2 < x1) { int xx0 = (x0 * 2 + x1) / 3; int xx1 = (x0 + x1 * 2) / 3; ll c0 = check(n, xx0); ll c1 = check(n, xx1); if (c0 < c1) x0 = xx0; else x1 = xx1; } ll maxc = 0; for (int x = x0; x <= x1; x++) maxc = max(maxc, check(n, x)); printf("%lld\n", maxc); return 0; }