n = int(input()) s = list(input()) v = 0 hind = [] for i in range(n): if s[i] == '?': v += 1 hind.append(i) nowlft = 0 tmp = 0 for i in range(n): if s[i] == '?' or s[i] == 'A': nowlft += 1 elif s[i] == 'C': tmp += nowlft rui = [0] * n ruil = [0] * n for i in range(n): if s[i] == 'C': rui[i] += 1 if s[i] == 'A': ruil[i] += 1 for i in range(n-2,-1,-1): rui[i] += rui[i+1] for i in range(n-1): ruil[i+1] += ruil[i] ans = tmp m = len(hind) rgtc = 0 lftc = m #print(rui) #print(ruil) for i in range(m-1,-1,-1): # s[i] : "(" -> ")" tmp -= rui[hind[i]] tmp -= rgtc lftc -= 1 rgtc += 1 tmp += lftc tmp += ruil[hind[i]] ans = max(ans, tmp) print(ans)