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 <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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; }
//(a+b-1)/b

signed main(){
	int n;cin>>n;
	map<int,int> mp;
	//mp[1]= begin == 'B'
	//mp[2]= end == 'A'
	int p=0;
	int ans=0;
	rep(i,0,n){
		string s; cin>>s;
		rep(j,1,s.size()){
			if(s[j]=='B' && s[j-1]=='A')ans++;
		}
		if(s[0]=='B')mp[1]++;
		if(s[s.size()-1]=='A')mp[2]++;
		if(s[s.size()-1]=='A' && s[0]=='B')mp[3]++;
	}
	if(mp[3]==0)ans+=min(mp[1],mp[2]);
	else if(mp[1]+mp[2]==0)ans+=mp[3]-1;
	else ans+=mp[3]+min(mp[1],mp[2]);
	cout<<ans<<endl;
	return 0;
}