Problem:
Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let mut s = Vec::new();
    for _ in 0..n {
        s.push(read::<String>().chars().collect::<Vec<_>>());
    }

    let mut ans = 0;
    for i in 0..n {
        for j in 0..s[i].len() - 1 {
            if s[i][j] == 'A' && s[i][j + 1] == 'B' {
                ans += 1;
            }
        }
    }
    let mut end_with_a_count = 0;
    let mut start_with_b_count = 0;
    let mut both = 0;

    for i in 0..n {
        if s[i][0] == 'B' && s[i][s[i].len() - 1] == 'A' {
            both += 1;
        } else if s[i][0] == 'B' {
            start_with_b_count += 1;
        } else if s[i][s[i].len() - 1] == 'A' {
            end_with_a_count += 1;
        }
    }

    ans += min(end_with_a_count, start_with_b_count);
    if end_with_a_count == start_with_b_count {
        ans += max(both - 1, 0);
    } else if end_with_a_count > start_with_b_count {
        end_with_a_count -= start_with_b_count;
        if both <= end_with_a_count {
            ans += both;
            assert!(false);
        } else {
            ans += end_with_a_count;
            both -= end_with_a_count;
            ans += both;
            assert!(false);
        }
    } else {
        start_with_b_count -= end_with_a_count;
        if both <= start_with_b_count {
            ans += both;
            assert!(false);
        } else {
            ans += start_with_b_count;
            both -= start_with_b_count;
            ans += both;
            assert!(false);
        }
    }
    println!("{:?}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}