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.42.0) (judge verdict: Runtime Error)

#![allow(non_snake_case)]
#![allow(unused_imports)]
use ascii::AsciiChar;
use ascii::AsciiStr;
use itertools::Itertools as _;
use permutohedron::LexicalPermutation as _;
use petgraph::algo::dijkstra;
use petgraph::algo::min_spanning_tree;
use petgraph::data::FromElements;
use petgraph::graph::NodeIndex;
use petgraph::graph::UnGraph;
use petgraph::unionfind::UnionFind;
use proconio::fastout;
use proconio::input;
use proconio::marker::Chars;
use proconio::marker::Usize1;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use superslice::Ext as _;

#[fastout]
fn main() {
    input! {
        N: usize,
        Ss: [Chars; N],
    }

    // let B_A = Ss.iter().filter(|s| s[0] == 'B' && s[s.len()-1] == 'A').count();
    // let B_X =Ss.iter().filter(|s| s[0] == 'B' ).count() - B_A;
    // let X_A =     Ss.iter().filter(|s| s[s.len()-1] == 'A').count() - B_A;

    let BAs: Vec<Vec<char>> = Ss
        .clone()
        .into_iter()
        .filter(|s| s[0] == 'B' && s[s.len() - 1] == 'A')
        .collect();
    let BXs: Vec<Vec<char>> = Ss
        .clone()
        .into_iter()
        .filter(|s| s[0] == 'B' && s[s.len() - 1] != 'A')
        .collect();
    let XAs: Vec<Vec<char>> = Ss
        .clone()
        .into_iter()
        .filter(|s| s[0] != 'B' && s[s.len() - 1] == 'A')
        .collect();
    let others: Vec<Vec<char>> = Ss
        .clone()
        .into_iter()
        .filter(|s| s[0] != 'B' && s[s.len() - 1] != 'A')
        .collect();

    let mut ans_vv: Vec<Vec<char>> = others;

    let xa_is_bigger = XAs.len() >= BXs.len();
    let XAs_len = XAs.len();
    let BXs_len = BXs.len();
    let BAs_len = BAs.len();

    // 方針: インデックスで制御するのはバグの温床なので、イテレータ化して個数で管理する
    let mut BAs_iter = BAs.into_iter();
    let mut BXs_iter = BXs.into_iter();
    let mut XAs_iter = XAs.into_iter();

    if xa_is_bigger {
        let rest = XAs_len - BXs_len;

        for _ in 0..min(XAs_len, BXs_len) {
            let xa = XAs_iter.next().unwrap();
            let bx = BXs_iter.next().unwrap();

            ans_vv.push(xa);
            ans_vv.push(bx);
        }

        if rest >= BAs_len {
            for _ in 0..BAs_len {
                let xa = XAs_iter.next().unwrap();
                let ba = BAs_iter.next().unwrap();

                ans_vv.push(xa);
                ans_vv.push(ba);
            }

            for _ in 0..rest - BAs_len {
                let xa = XAs_iter.next().unwrap();

                ans_vv.push(xa);
            }
        } else {
            for _ in 0..rest {
                let xa = XAs_iter.next().unwrap();
                let ba = BAs_iter.next().unwrap();

                ans_vv.push(xa);
                ans_vv.push(ba);
            }

            for _ in 0..BAs_len - rest {
                let ba = BAs_iter.next().unwrap();
                ans_vv.push(ba);
            }
        }
    } else {
        let rest = BXs_len - XAs_len;

        for _ in 0..min(XAs_len, BXs_len) {
            let xa = XAs_iter.next().unwrap();
            let bx = BXs_iter.next().unwrap();

            ans_vv.push(xa);
            ans_vv.push(bx);
        }

        if rest >= BAs_len {
            for _ in 0..BAs_len {
                let ba = BAs_iter.next().unwrap();
                let bx = BXs_iter.next().unwrap();
                ans_vv.push(ba);
                ans_vv.push(bx);
            }

            for _ in 0..rest - BAs_len {
                let bx = BXs_iter.next().unwrap();

                ans_vv.push(bx);
            }
        } else {
            for _ in 0..rest {
                {
                    let ba = BAs_iter.next().unwrap();
                    let bx = BXs_iter.next().unwrap();
                    ans_vv.push(ba);
                    ans_vv.push(bx);
                }

                for _ in 0..BAs_len - rest {
                    let ba = BAs_iter.next().unwrap();
                    ans_vv.push(ba);
                }
            }
        }
    }

    // dbg!(&ans_vv);

    let ans_v = ans_vv.concat();

    let mut ans: i64 = 0;
    for i in 1..ans_v.len() {
        if ans_v[i - 1] == 'A' && ans_v[i] == 'B' {
            ans += 1;
        }
    }

    println!("{}", ans);
}