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

use std::io::*;
use std::str::FromStr;

#[allow(unused_macros)]
macro_rules! scan {
  ($e:expr; $t:ty) => {
    $e.get::<$t>()
  };
  ($e:expr; $($t:ty), *) => {
    ($($e.get::<$t>(),)*)
  };
  ($e:expr => $i:expr; $t:ty) => {
    (0..$i).map(|_| $e.get::<$t>()).collect::<Vec<$t>>()
  };
  ($e:expr => $row:expr, $col:expr; $t:ty) => {
    (0..$i).map(|_| scan!($e => $col; $t)).collect::<Vec<$t>>()
  }
}

struct Scanner<R: Read> {
  reader: R
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
  fn new(reader: R) -> Scanner<R> {
    Scanner { reader: reader }
  }
  fn safe_get<T: FromStr>(&mut self) -> Option<T> {
    let token = self.reader.by_ref().bytes().map(|c| c.unwrap() as char)
      .skip_while(|c| c.is_whitespace())
      .take_while(|c| !c.is_whitespace())
      .collect::<String>();
    if token.is_empty() { None } else { token.parse().ok() }
  }
  fn get<T: FromStr>(&mut self) -> T {
    if let Some(v) = self.safe_get() { v } else {
      writeln!(stderr(), "Terminated with EOF").unwrap();
      std::process::exit(0);
    }
  }
}

trait Writable {
  fn write(&self, sep: &'static str) -> String;
}

impl<T: ToString> Writable for Vec<T> {
  fn write(&self, sep: &'static str) -> String {
    self.iter().map(|e| e.to_string()).collect::<Vec<String>>().join(sep)
  }
}

fn main() {
  let cin = stdin();
  let cin = cin.lock();
  let mut sc = Scanner::new(cin);
  let n = scan!(sc; i32);
  let s = scan!(sc => n; String);
  println!("{}", solve(&s));
}

type VecStr = Vec<String>;

fn solve(s: &Vec<String>) -> i32 {
  let mut a: VecStr = vec![]; // last 'A'
  let mut b: VecStr = vec![]; // first 'B'
  let mut c: VecStr = vec![]; // both
  let mut d: VecStr = vec![]; // other
  for string in s.clone().into_iter() {
    let (f, l) = get_first_and_last(&string);
    if f != 'B' && l == 'A' {
      a.push(string);
    }
    else if f == 'B' && l != 'A' {
      b.push(string);
    }
    else if f == 'B' && l == 'A' {
      c.push(string);
    }
    else {
      d.push(string);
    }
  }
  let mut string: VecStr = vec![];
  if let Some(v) = a.pop() {
    string.push(v);
  }
  string.push(c.join(""));
  let a_len = a.len();
  let b_len = b.len();
  let to = {
    let temp = min(a_len, b_len);
    if      temp == 0 && b_len == 0 { 0 }
    else if temp == 0 && b_len != 0 { 1 }
    else { temp }
  };
  for i in 0..to {
    if i % 2 == 0 {
      string.push(b.pop().unwrap());
    }
    else {
      string.push(a.pop().unwrap());
    }
  }
  string.push(a.join(""));
  string.push(b.join(""));
  string.push(d.join(""));
  let string = string.join("");
  let mut ans = 0;
  let mut a_flag = false;
  for c in string.chars() {
    if a_flag {
      if c == 'B' {
        ans += 1;
      }
      a_flag = false;
    }
    if c == 'A' {
      a_flag = true;
    }
  }
  ans
}

fn get_first_and_last(s: &String) -> (char, char) {
  let s = s.clone();
  let mut s = s.chars();
  (s.next().unwrap(), s.last().unwrap())
}

fn min<T: Ord>(a: T, b: T) -> T {
  if a < b { a } else { b }
}