n = int(input()) s = input() tmp = 0 la = 0 for c in s: if c == 'A': la += 1 else: tmp += la ans = tmp la = 0 rc = s[1:n].count('C') + s[1:n].count('?') for i in range(n): if s[i] == '?': tmp -= la tmp += rc ans = max(ans, tmp) if s[i] != 'C': la += 1 if i < n - 1 and s[i + 1] != 'A': rc -= 1 print(ans)