fn solve(scanner: &mut Scanner) { let n: usize = scanner.next(); let s: Vec = scanner.next::().chars().collect(); let s = s .iter() .map(|&c| { if c == 'A' { 0 } else if c == 'C' { 1 } else { 2 } }) .collect::>(); let mut sum = vec![vec![0; n + 1]; 3]; for (i, &c) in s.iter().enumerate() { for j in 0..3 { sum[j][i + 1] = sum[j][i]; } sum[c][i + 1] += 1; } let mut ans = 0; for (i, &c) in s.iter().enumerate() { if c == 1 || c == 2 { ans += sum[0][i]; } } let mut now = ans; for (i, &c) in s.iter().enumerate() { if c == 2 { now -= sum[0][i] + sum[2][i]; now += sum[1][n] - sum[1][i + 1] + sum[2][n] - sum[2][i + 1]; } ans = ans.max(now); } println!("{}", ans); } fn main() { let mut scanner = Scanner::new(); let t: usize = 1; for _ in 0..t { solve(&mut scanner); } } pub struct Scanner { buf: Vec, } impl Scanner { fn new() -> Self { Self { buf: vec![] } } fn next(&mut self) -> T { loop { if let Some(x) = self.buf.pop() { return x.parse().ok().expect(""); } let mut source = String::new(); std::io::stdin().read_line(&mut source).expect(""); self.buf = Self::split(source); } } fn split(source: String) -> Vec { source .split_whitespace() .rev() .map(String::from) .collect::>() } }