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: Wrong Answer)

#![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();
    if xa_is_bigger {
        for i in 0..BXs.len() {
            ans_vv.push(XAs[i].clone());
            ans_vv.push(BXs[i].clone());
        }

        let rest = XAs.len() - BXs.len();
        for i in 0..min(rest, BAs.len()) {
            ans_vv.push(XAs[BXs.len() + i].clone());
            ans_vv.push(BAs[i].clone());
        }

        let rest_rest = if rest >= BAs.len() {
            rest - BAs.len()
        } else {
            BAs.len() - rest
        };
        if rest >= BAs.len() {
            for i in 0..rest_rest {
                ans_vv.push(XAs[BXs.len() + BAs.len() + i].clone());
            }
        } else {
            for i in 0..rest_rest {
                ans_vv.push(BAs[rest + i].clone());
            }
        }
    } else {
        for i in 0..XAs.len() {
            ans_vv.push(XAs[i].clone());
            ans_vv.push(BXs[i].clone());
        }

        let rest = BXs.len() - XAs.len();
        for i in 0..min(rest, BAs.len()) {
            ans_vv.push(BAs[i].clone());
            ans_vv.push(BXs[XAs.len() + i].clone());
        }

        let rest_rest = if rest >= BAs.len() {
            rest - BAs.len()
        } else {
            BAs.len() - rest
        };
        if rest >= BAs.len() {
            for i in 0..rest_rest {
                ans_vv.push(BXs[XAs.len() + BAs.len() + i].clone());
            }
        } else {
            for i in 0..rest_rest {
                ans_vv.push(BAs[rest + i].clone());
            }
        }
    }

    // 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);
}