import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); char[] s = br.readLine().toCharArray(); br.close(); int[] a = new int[n + 1]; for (int i = 0; i < n; i++) { a[i + 1] = a[i]; if (s[i] == 'A') { a[i + 1]++; } } int[] c = new int[n + 1]; for (int i = n - 1; i >= 0; i--) { c[i] = c[i + 1]; if (s[i] == 'C') { c[i]++; } } long val = 0; int cnt = 0; int cq = 0; for (int i = 0; i < n; i++) { if (s[i] == 'A') { cnt++; } else { val += cnt; cq++; } } long ans = val; cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == 'A') { cnt++; } else if (s[i] == '?') { val -= cnt; cq--; val += cq; cnt++; ans = Math.max(ans, val); } else { cq--; } } System.out.println(ans); } }