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: Accepted)

// Try diverta 2019 Programming Contest
// author: Leonardone @ NEETSDKASU

fn main() {
	let mut stdin = String::new();
    std::io::Read::read_to_string(
    	&mut std::io::stdin(),
        &mut stdin).unwrap();
	let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
    	($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    
    let n = get!();
    let mut ans = 0;
    let mut rem = vec![0; 4];
    for _ in 0..n {
    	let s = get().as_bytes();
        for i in 1..s.len() {
        	if s[i-1]==b'A' && s[i]==b'B' {
                ans += 1;
            }
        }
        let a = if s[s.len()-1]==b'A' { 1 } else { 0 };
        let b = if s[0]==b'B' { 1 } else { 0 };
        rem[a*2+b] += 1;
    }
    if rem[3] > 0 {
        ans += rem[3] - 1;
        if rem[1] > 0 {
            rem[1] -= 1;
            ans += 1;
        }
        if rem[2] > 0 {
            rem[2] -= 1;
            ans += 1;
        }
    }
    ans += std::cmp::min(rem[1], rem[2]);
    println!("{}", ans);
}