Language: C++

#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(int i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define clr(ar, val) memset(ar, val, sizeof(ar))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
	ll n,l[10000]={},x[4]={};//0..x 1..end a 2...start b 3...both
	ll ans=0;
	string s[10000];
	cin >> n;
	rep(i,n){
		cin>>s[i];
		l[i]=s[i].size();
		if(s[i].back()=='A' && s[i].front()=='B') x[3]++;
		else if(s[i].back()=='A') x[1]++;
		else if(s[i].front()=='B') x[2]++;
		else x[0]++;
		rep(j, l[i]-1){
			if(s[i][j]=='A' && s[i][j+1]=='B') ans++;
		}
	}
	ans+=(x[3]+min(x[1],x[2]))-(x[3]!=0&&x[1]+x[2]==0);
	cout << ans << endl;
	return 0;
}
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(int i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define clr(ar, val) memset(ar, val, sizeof(ar))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
	ll n,l[10001]={},x[4]={};//0..x 1..end a 2...start b 3...both
	ll ans=0;
	string s[10001];
	cin >> n;
	rep(i,n){
		cin>>s[i];
		l[i]=s[i].size();
		if(s[i].back()=='A' && s[i].front()=='B') x[3]++;
		else if(s[i].back()=='A') x[1]++;
		else if(s[i].front()=='B') x[2]++;
		else x[0]++;
		rep(j, l[i]-1){
			if(s[i][j]=='A' && s[i][j+1]=='B') ans++;
		}
	}
	ans+=(x[3]+min(x[1],x[2]))-(x[3]!=0&&x[1]+x[2]==0);
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc:44:25: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
   44 | template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
      |                         ^~~~~
a.cc:7:25: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
    7 | template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
      |                         ^~~~~
a.cc:45:25: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
   45 | template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
      |                         ^~~~~
a.cc:8:25: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
    8 | template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
      |                         ^~~~~
a.cc:46:5: error: redefinition of 'int gcd(int, int)'
   46 | int gcd(int a,int b){return b?gcd(b,a%b):a;}
      |     ^~~
a.cc:9:5: note: 'int gcd(int, int)' previously defined here
    9 | int gcd(int a,int b){return b?gcd(b,a%b):a;}
      |     ^~~
a.cc:54:5: error: redefinition of 'int main()'
   54 | int main(void)
      |     ^~~~
a.cc:17:5: note: 'int main()' previously defined here
   17 | int main(void)
      |     ^~~~