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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
#include<numeric>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

#define rep(i,n) for (int i = 0; i < (n); ++i)

int main()
{
  int n;
  cin >> n;
  vector<string> s(n);
  rep (i, n) cin >> s[i];

  ll count = 0;
  ll fb = 0;
  ll ba = 0;
  ll fbba = 0;

  rep (i, n) {
    char c1 = s[i][0];
    char c2 = s[i][s[i].size()-1];

    if (c1 == 'B' && c2 == 'A') fbba++;
    else if (c1 == 'B') fb++;
    else if (c2 == 'A') ba++;

    // AB 探しループ
    bool flag = false;
    rep (j, s[i].size()) {
      if (s[i][j] == 'A') {
        flag = true;
      } else if (s[i][j] == 'B' && flag) {
        flag = false;
        count++;
      } else {
        flag = false;
      }
    }
  }

  for (int i = 0; i < fbba; i++) {
    if (fb > 0) {
      count++;
      fb--;
    } else if (fbba > 0) {
      count++;
      fbba--;
    }
    if (ba > 0) {
      count++;
      ba--;
    } else if (fbba > 0) {
      count++;
      fbba--;
    }
  }
  rep (i, fb) {
    if (ba > 0) {
      count++;
      ba--;
    }
  }
  cout << count << endl;
}