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

#include<bits/stdc++.h>
namespace my_std{
	using namespace std;
	#define Rint register int
	#define FOR(i,a,b) for(register int i=(a);i<=(b);++i)
	#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
	#define GO(x) for(register int i=p[x].head,v;i;i=e[i].link)
	#define FILE(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
	#define PII pair<int,int>
	#define MP make_pair
	typedef long long LL;
	typedef double DB;
	inline int read(){
		int ans=0,f=1;char c=getchar();
		while(c>'9'||c<'0'){ f^=(c=='-'); c=getchar(); }
		for(;c<='9'&&c>='0';c=getchar()) ans=ans*10+c-'0'; return f?ans:-ans;
	}
	const int mod = 1e9+7;
	inline void inc(int &x,const int &y){ x+=y; if(x>=mod) x-=mod; }
	inline int ksm(int x,LL y){ int res=1; for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) res=1ll*res*x%mod; return res;}
}
using namespace my_std;

int n,len[10010],ans,bas[10];
char c[10010][12];

int main(){
	n = read();
	FOR(i,1,n){
		scanf("%s",c[i]+1);
		len[i] = strlen(c[i] + 1);
//		FOR(o,1,len[i]) printf("%c",c[i][o]);
//		puts("");
		FOR(j,1,len[i]-1) if(c[i][j] == 'A' && c[i][j+1] == 'B') ++ans;
		++bas[(c[i][1] == 'B') + 2*(c[i][len[i]] == 'A')];
	}
//	printf("%d %d %d %d\n",bas[0],bas[1],bas[2],bas[3]);
	int pre = min(bas[1],bas[2]),tem = min(pre,bas[3]) , y= 0;
	FOR(i,0,bas[3]){
		pre = max(pre , min(bas[1] + bas[3] - i,bas[2] + i));
	}
	FOR(i,1,3) bas[i] -= tem;
	FOR(i,0,bas[3]){
		y = max(y , min(bas[1] + bas[3] - i,bas[2] + i));
	}
	tem <<= 1;
	tem += y;
	printf("%d",ans + max(tem , y));
	return 0;
}