#include #include #include int main() { std::string S; std::cin >> S; // Read the length (unused) std::cin >> S; // Read the actual string long long A = 0, C = 0, a = 0, n = 0; char L = 'A'; for (char s : S) { if (s == L) A++; else { C++; n += A; } } long long c = C, m = n; for (char s : S) { if (s < L) n += c - 1 - a; c -= s > L; a += s == L; m = std::max(n, m); } std::cout << m << std::endl; return 0; }