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; 
typedef long long ll;
typedef long double ld;
#define fr first
#define sc second
#define pb push_back
const ll mod = 998244353;
const int N = 1e6+205;
void solve(){
	int n;
	cin>>n; 
	int bx = 0;
	int xa = 0;
	int ba = 0;
	int ans2 = 0;
	for(int i=1;i<=n;i++){
		string s; 
		cin>>s;
		int z = s.size();
		if(s[0]=='B' and s[z-1]=='A')ba++;
		else if(s[0]=='B') bx++;
		else if(s[z-1]=='A')xa++;
		for(int j=0;j<z-1;j++){
			if(s[j]=='A' and s[j+1]=='B')ans2++;
		}
	}	
	int ans = min(xa,bx);
	xa-=ans;
	bx-=ans;
	/// BA + BA + BA + BA + BA + BA 
	if(ba>0){
		ans+=ba-1;
		ba = 1;
	}
	if((xa>0 or bx>0))ans+=ba;
	cout<<ans+ans2<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1; 
//	cin>>t; 
	while(t--){
		solve();
	}
	return 0;
}