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

#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
    cout << fixed << setprecision(10);
    int n;
    cin >> n;
    int cnt = 0;
    int a = 0, b = 0, ab = 0;
    string s[n];
    rep(i,n) cin >> s[i];
    rep(i,n){
        rep(j,int(s[i].size()-1)){
            if(s[i][j] == 'A' && s[i][j+1] == 'B') cnt++;
        }
        if(s[i][0] == 'B' && s[i][int(s[i].size())-1]  == 'A') ab++;
        else if(s[i][0] == 'B') b++;
        else if(s[i][int(s[i].size())-1] == 'A') a++;
    }
    // cout << a << " " << b << endl;
    // cout << cnt << endl;
    if(a == b && a == 0){
        cout << cnt + max(ab - 1,0) << endl;
    }else if(a == b && a != 0){
        cout << cnt + ab + min(a,b) << endl;
    }else if(a > b){
        cout << cnt + ab + min(a,b) << endl;
    }else{
        cout << cnt + ab + min(a,b) << endl;
    }
    return 0;
}