stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main(){

   int n;

   cin>>n;

   if(n==1){

    cout<<"Hello World"<<endl;

   }

   else if(n==2){

    int a,b;

    cin>>a>>b;

    cout<<a+b<<endl;

   }

}


// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

    int n,a,b;

    scanf("%d%d%d", &n,&a,&b);



    if(n==1)

        printf("Hello World\n");

    if(n==2)

        printf("%d\n", a+b);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    int n,a,b;

    cin>>n;

    if(n==1)

      cout<<"Hello World"<<endl;

    if(n==2)

    {

        cin>>a>>b;

        cout<<(a+b)<<endl;

    }

}


// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

    int x, a, b;

    scanf("%d", &x);

    if(x==1)

    {

        printf("Hello World");

    }

    else

    {

        scanf("%d%d", &a, &b);

        printf("%d", a+b);

    }

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <string>

#include <vector>

#include <set>

#include <cmath>

#include <stdio.h>

#include <list>

#include <numeric>

#include <stack>

#include <queue>

#include <tuple>

#include <bitset>

#include <map>

#include <math.h>

//　ceil(a/b)   (a + (b - 1))/ b

using namespace std;

#define rep(i,n) for(int i = 0; i < n; i++)

typedef long long ll;

typedef pair<int,int> P;

const int inf = 1012345678;

int dx[] = {1,-1,0,0};

int dy[] = {0,0,1,-1};



// int gcd(int a, int b){

//   if (a%b==0){

//     return(b);

//   }

//   else{

//     return(gcd(b,a%b));

//   }

// }





vector<int> v[100001];

int c[100000];

int y[100000];



int main(){

  int n;

  cin >> n;

  if(n == 1) cout << "Hello World" << endl;

  else {

    int a, b;

    cin >> a >> b;

    cout << a+b << endl;

  }

}




// Optimized solution to the same problem (faster):
#include <algorithm>

#include <bitset>

#include <cmath>

#include <ctime>

#include <iostream>

#include <list>

#include <map>

#include <math.h>

#include <numeric>

#include <queue>

#include <set>

#include <stack>

#include <stdio.h>

#include <string>

#include <tuple>

#include <vector>

using namespace std;

#define rep(i,n) for(int i = 0; i < (n); i++)

#define all(a) begin(a),end(a)

typedef long long ll;

typedef pair<int,int> P;

const int inf=1e+9;

const ll mod=1000000007;

const double PI=3.14159265358979323846;

int dx[] = {1,-1,0,0};

int dy[] = {0,0,1,-1};

ll gcd(ll a, ll b){

  if (b==0) return a;

  else return gcd(b,a%b);

}

int cans(bool f){

  if(f) cout << "Yes" << endl;

  else cout << "No" << endl;

  return 0;

}













int main() {

  int N;

  cin >> N;

  if(N==1) cout << "Hello World" << endl;

  else {

    int a,b;

    cin >> a >> b;

    cout << a + b << endl;

  }

  return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

const int MAXN = 2e5 + 5;



vector<int> e[MAXN];



int main(int argc, char* argv[]) {

    int n;

    scanf("%d", &n);

    long long ans = 1LL * n * (n + 1) * (n + 2) / 6;

    for (int i = 1; i < n; i++) {

        int u, v;

        scanf("%d %d", &u, &v);

        if (u > v) swap(u, v);

        ans -= 1LL * u * (n - v + 1);

    }

    printf("%lld\n", ans);

    return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h> //Ithea Myse Valgulious

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rel register ll

#define rec register char

#define gc getchar

//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

char buf[1<<23],*p1=buf,*p2=buf;

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) pc('-'),x=-x;

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=2e5;

typedef int fuko[yuzu|10];

int main() {

  int i,n=read(),u,v;

  ll lxy=1ll*n*(n+1)*(n+2)/6;

  for (i=1;i<n;++i) {

    read(u),read(v);

    if (u>v) swap(u,v);

    lxy-=1ll*u*(n-v+1);

  }

  printf("%lld\n",lxy);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#pragma GCC optimize("O3")



#define debug(var)  do{std::cerr << #var << " : ";view(var);}while(0)

template<typename T> void view(T e){std::cerr << e << std::endl;}

template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;}

template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

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; }

using namespace std;



typedef long long ll;

typedef long double ld;

typedef vector<ll> v1;

typedef vector<v1> v2;

typedef vector<v2> v3;



typedef unordered_map<ll, unordered_map<ll, ll>> graph;



const ll INF = 1ll << 50;

const ll mod = 1000000007;



long n;

long u,v;

long a;



int main(){

    scanf("%ld", &n);

    a = n*(n+1)*(n+2)/6;

    for(ll i = 1;i < n;i++){

        scanf("%ld %ld", &u, &v);

        if(u < v) a -= u * (n-v+1);

        else a -= v * (n-u+1);

    }

    printf("%ld\n", a);

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#pragma GCC optimize("O3")

static inline char CH()

{

  static char buf[100000],*ptr1=buf,*ptr2=buf;

  return ptr1==ptr2&&(ptr2=(ptr1=buf)+fread(buf,1,100000,stdin),ptr1==ptr2)?EOF:*ptr1++;

}

static inline int IN(void)

{

  int x=0,f=0,c=CH();while(c<48||c>57){f^=c==45,c=CH();}

  while(c>47&&c<58){x=x*10+c-48,c=CH();}return f?-x:x;

}





int main(){

    int n=IN(),i=0,u,v;

    long x = n;

    long a = x*(x+1)*(x+2)/6;

    while(i++ < n-1){

        u=IN();

        v=IN();

        if(u<v){

            a -= 1l * u * (n-v+1);

        }else{

            a -= 1l * v * (n-u+1);

        }

    }

    return printf("%ld", a), 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#pragma GCC optimize("O3")



#define debug(var)  do{std::cerr << #var << " : ";view(var);}while(0)

template<typename T> void view(T e){std::cerr << e << std::endl;}

template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;}

template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

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; }

using namespace std;



typedef long long ll;

typedef long double ld;

typedef vector<ll> v1;

typedef vector<v1> v2;

typedef vector<v2> v3;



typedef unordered_map<ll, unordered_map<ll, ll>> graph;



const ll INF = 1ll << 50;

const ll mod = 1000000007;



long n;

long u,v;

long a;



int main(){

    scanf("%ld", &n);

    a = n*(n+1)*(n+2)/6;

    for(ll i = 1;i < n;++i){

        scanf("%ld %ld", &u, &v);

        if(u < v) a -= u * (n-v+1);

        else a -= v * (n-u+1);

    }

    printf("%ld\n", a);

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#pragma GCC optimize("O3")

static inline char CH()

{

  static char buf[100000],*ptr1=buf,*ptr2=buf;

  return ptr1==ptr2&&(ptr2=(ptr1=buf)+fread(buf,1,100000,stdin),ptr1==ptr2)?EOF:*ptr1++;

}

static inline int IN(void)

{

  int x=0,f=0,c=CH();while(c<48||c>57){f^=c==45,c=CH();}

  while(c>47&&c<58){x=x*10+c-48,c=CH();}return f?-x:x;

}





int main(){

    int n=IN(),i=0,u,v;

    long x = n;

    long a = x*(x+1)*(x+2)/6;

    while(i++ < n-1){

        u=IN();

        v=IN();

        if(u<v){

            a -= 1l * u * (n-v+1);

        }else{

            a -= 1l * v * (n-u+1);

        }

    }

    return printf("%ld", a), 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#pragma GCC optimize("O3")



#define debug(var)  do{std::cerr << #var << " : ";view(var);}while(0)

template<typename T> void view(T e){std::cerr << e << std::endl;}

template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;}

template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

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; }

using namespace std;



typedef long long ll;

typedef long double ld;

typedef vector<ll> v1;

typedef vector<v1> v2;

typedef vector<v2> v3;



typedef unordered_map<ll, unordered_map<ll, ll>> graph;



const ll INF = 1ll << 50;

const ll mod = 1000000007;



int main(){



    long n;

    long u,v;

    long a;



    scanf("%ld", &n);

    a = n*(n+1)*(n+2)/6;

    for(ll i = 1;i < n;i++){

        scanf("%ld %ld", &u, &v);

        if(u < v) a -= u * (n-v+1);

        else a -= v * (n-u+1);

    }

    printf("%ld\n", a);

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#pragma GCC optimize("O3")

static inline char CH()

{

  static char buf[100000],*ptr1=buf,*ptr2=buf;

  return ptr1==ptr2&&(ptr2=(ptr1=buf)+fread(buf,1,100000,stdin),ptr1==ptr2)?EOF:*ptr1++;

}

static inline int IN(void)

{

  int x=0,f=0,c=CH();while(c<48||c>57){f^=c==45,c=CH();}

  while(c>47&&c<58){x=x*10+c-48,c=CH();}return f?-x:x;

}





int main(){

    int n=IN(),i=0,u,v;

    long x = n;

    long a = x*(x+1)*(x+2)/6;

    while(i++ < n-1){

        u=IN();

        v=IN();

        if(u<v){

            a -= 1l * u * (n-v+1);

        }else{

            a -= 1l * v * (n-u+1);

        }

    }

    return printf("%ld", a), 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



string s[11000];



int cnt[10];



int main() {

#ifdef Hwang_Baek_I

	freopen("in.txt", "r", stdin);

	freopen("out.txt", "w", stdout);

#endif

	ios::sync_with_stdio(0);

	cin.tie(0); cout.tie(0);

	int n; cin >> n;

	for (int i = 0; i < n; i++) cin >> s[i];

	int ans = 0;

	for (int i = 0; i < n; i++) {

		for (int j = 1; j < s[i].length(); j++) if (s[i][j - 1] == 'A' && s[i][j] == 'B') ans++;

		if (s[i][0] == 'B') cnt[1]++;

		if (s[i][s[i].length() - 1] == 'A') cnt[0]++;

		if (s[i][0] == 'B' && s[i][s[i].length() - 1] == 'A') cnt[2]++;

	}

	cnt[0] -= cnt[2];

	cnt[1] -= cnt[2];

	ans += max(cnt[2] - 1, 0);

	if (cnt[2]) {

		if (cnt[0]) ans++, cnt[0]--;

		if (cnt[1]) ans++, cnt[1]--;

	}

	ans += min(cnt[0], cnt[1]);

	cout << ans << endl;

	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;

using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,

		tree_order_statistics_node_update> ordered_set;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,

		tree_order_statistics_node_update> indexed_multiset;





int n ;

long long ans;

vector<int>b,a,ab;

bool vis[10004];

int main(){	

	cin>>n;

	for(int i = 0; i < n ; i++){

		string s ;cin>>s;

		for(int j = 1 ; j<(int)s.size() ;j++){

			if(s[j]=='B'&& s[j-1]=='A'){

				ans++;

			}

		}

		if(s[0]=='B' &&s[(int)s.size() - 1]=='A')

			ab.push_back(i);

		else if(s[0]=='B')b.push_back(i);

		else if(s[(int)s.size() - 1] == 'A')a.push_back(i);

	}

	int aa = a.size() , bb = b.size() , aabb = ab.size();

	if(aa){

		aa--;

		while(aabb){

			aabb--;

			ans++;

		}

		// last = a

		if(bb){

			bb--;

			ans++;

		}

		//last = b;

		while(aa&&bb){

			aa--;bb--;

			ans++;

		}

	}else{

		if(aabb){

			ans+=(aabb-1);

			if(bb)ans++;

		}

	}

	cout<<ans;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int n;

string s[10000];



int main()

{

	cin>>n;

	for(int i=0;i<n;i++)

	cin>>s[i];

	int ans=0;

	for(int i=0;i<n;i++)

		for(int j=0;j<s[i].size();j++)

		if(s[i][j]=='A'&&s[i][j+1]=='B')

		ans++;

	int a=0,b=0;

	int d=0;

	for(int i=0;i<n;i++)

	{

		if(s[i][0]=='B')

		b++;

		if(s[i][s[i].size()-1]=='A')

		a++;

		if(s[i][0]=='B'&&s[i][s[i].size()-1]=='A')

		d++;

	}

	if(d==n||(d==a&&d==b&&d!=0))

	ans--;

	

	ans+=min(a,b);

	cout<<ans;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int main()

{

	string str;

	int n,ans=0,a=0,b=0,ba=0;

	cin>>n;

	for(int i=0;i<n;i++)

	{

		cin>>str;

		for(int j=0;j<str.size()-1;j++)

		if(str[j]=='A'&&str[j+1]=='B')

		ans++;

		if(str[0]=='B')

		a++;

		if(str[str.size()-1]=='A')

		b++;

		if(str[str.size()-1]=='A'&&str[0]=='B')

		ba++;

	}

	ans+=min(a,b);

	if(ba==a&&ba==b&&ba!=0)

	ans--;

	cout<<ans;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<cmath>

#include<ctime>

#include<iostream>

#include<algorithm>

#include<map>

#include<stack>

#include<queue>

#include<vector>

#include<set>

#include<string>

#define ll long long

#define dd double

using namespace std;



int main(){

	string s[10005];

	string st="";

	ll a[10005][3]={0};

	memset(a,0,sizeof(a));

	ll n;cin>>n;

	for(ll i=0;i<n;i++){

		cin>>s[i];

		if(s[i][0]=='B'){

			a[i][0]++;

		}

		if(s[i][s[i].size()-1]=='A'){

			a[i][1]++;

		}

	}

	ll flag=0;

	for(ll i=0;i<n;i++){

		if(a[i][0]==0&&a[i][1]!=0){

			flag=1;

			st+=s[i];

			a[i][2]=-1;

			break;

		}

	}

	ll q=0;

	if(flag==1){

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

					break;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	if(flag==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	ll count = 0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			count++;

		}

	}

	cout<<count<<endl;

} 

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>

#include <algorithm>

using namespace std;

char dt[30],l,r,s;

int n,t[4],k,f,m;

int main(){

	scanf("%d",&n);

	memset(t,0,sizeof(t));

	f=0;

	while(n--){

		scanf("%s",dt);

		s=strlen(dt);

		l=dt[0];

		r=dt[s-1];

		for(int i=1;i<s;i++)if(dt[i]=='B'&&dt[i-1]=='A')f++;

		k=0;

		if(l=='B')k++;

		if(r=='A')k+=2;

		t[k]++;

	}

//		printf("%d\n",f);

	m=t[1]+t[2];

	k=min(t[2],min(t[1],t[3]));

	f+=k<<1;

	t[1]-=k;t[2]-=k;t[3]-=k;

//	printf("%d %d %d\n",t[1],t[2],t[3]);

//	printf("%d\n",f);

	f+=min(t[1],t[2]);

//	printf("%d\n",min(t[1],t[2]));

	if(m)f+=t[3];

	else f+=max(0,t[3]-1);

	printf("%d\n",f);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long 



#define maxn 10005

using namespace std;

ll a[maxn][3]={0};

string s[maxn];

string st="";

int main(){

	ll n;

	cin>>n;

	

	for(ll i =0;i<n;i++){

	cin>>s[i];

	if(s[i][0] == 'B')

	   a[i][0]++;

	if(s[i][s[i].size()-1]=='A')

	   a[i][1]++;

	   }

	   

	ll flag = 0;

	

	for(ll i=0;i<n;i++){

		if(a[i][0] == 0&&a[i][1]!=0){

			flag = 1;

			st+=s[i];

			a[i][2] = -1;

			break;

		}

	}

	

	ll q = 0;

	//1

	if(flag == 1){

		for(ll i=0;i<n;i++){

			if(a[i][2] == -1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2] = -1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

			if(a[i][0]!=0){

				st+=s[i];

				a[i][2]=-1;

				break;

			}	

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(q%2==0){

					if(a[i][1]!=0){

						st+=s[i];

						a[i][2] =-1;

						i=-1;

						q++;

					}

				}

				else{

					if(a[i][0]!=0){

						st+=s[i];

						a[i][2]=-1;

						i=-1;

						q++;

					}

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	//2

	if(flag ==0){

		for(ll i=0;i<n;i++){

			if(a[i][1]!=0&&a[i][2]!=-1){

				st+=s[i];

				a[i][2] =-1;

				break;

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0&&a[i][1]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]==-1){

				continue;

			}

			else{

				if(a[i][0]!=0){

					st+=s[i];

					a[i][2]=-1;

				}

			}

		}

		for(ll i=0;i<n;i++){

			if(a[i][2]!=-1){

				st+=s[i];

				a[i][2]=-1;

			}

		}

	}

	

	

	

	ll num =0;

	for(ll i=0;i<st.size()-1;i++){

		if(st[i]=='A'&&st[i+1]=='B'){

			num++;

		}

	}

	cout<<num<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<string>

using namespace std;

int main()

{

	int n,len,x=0;

	int nA=0,nB=0,nAB=0;

	cin>>n;

	while(n--)

	{

		string arr;

		cin>>arr;

		len=arr.size();

		for(int i=0;i<len-1;i++)

		{

			if(arr[i]=='A'&&arr[i+1]=='B')

				nAB++;

		}

		if(arr[0]=='B')

			nB++;

		if(arr[len-1]=='A')

			nA++;

		if(arr[0]=='B'&&arr[len-1]!='A'||arr[0]!='B'&&arr[len-1]=='A')

			x=1;

	}

	if(x==0&&nA>0)

		nA--;

	cout<<nAB+min(nA,nB);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

typedef long long ll;

string s[11111];

int main(){

	int n,count=0,ba=0,a=0,b=0;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>s[i];

		int len=s[i].size();

		for(int j=0;j<len-1;j++){

			if(s[i][j]=='A'&&s[i][j+1]=='B') count++;

		}

		if(s[i][0]=='B'&&s[i][len-1]=='A') ba++;

		else if(s[i][0]=='B') b++;

		else if(s[i][len-1]=='A') a++;

	}

	int c=min(a,b);

	if(a==0&&b==0&&ba>0) ba--;

	cout<<count+c+ba<<endl;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<string>

#include<string.h>

using namespace std;

typedef long long ll;

int main()

{

	int n;

	int l;

	int ant=0,ab=0,a=0,b=0;

	int ans=0;

	scanf("%d",&n);

	for(int i=0;i<n;i++)

	{

		char c[15];

		scanf("%s",c);

//		printf("%s\n",c);

		l=strlen(c);

		for(int j=0;j<l-1;j++)

		{

			if(c[j]=='A'&&c[j+1]=='B')

				ant++;

		}

		if(c[0]=='B'&&c[l-1]=='A')

			ab++;

		else if(c[0]=='B')

			b++;

		else if(c[l-1]=='A')

			a++;

	}

//	printf("ant:%d ab:%d a:%d b:%d\n",ant,ab,a,b);

	ans+=ant;

	if(ab!=0)

		ans+=(ab-1);

	if(a!=0&&ab!=0)

	{

		ans++;

		a--;

	}

	if(b!=0&&ab!=0)

	{

		ans++;

		b--;

	}

	ans+=min(a,b);

	printf("%d\n",ans);

	return 0;

}

/*

5

AB

AB

AB

AB

BB

6

*/

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int a[3];

	memset(a,0,sizeof(a));

	int cnt = 0;

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') a[0] ++;

		if(s[i][l-1] == 'A') a[1]++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') a[2] ++;

	}

	if(a[0] < a[1])  cnt += a[0];

	else cnt += a[1];

	if(a[0] == a[1] && a[1] == a[2] && a[2] != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



int main()

{

    char a[15];

    int n;

    cin >> n;

    int b = 0,c = 0,d = 0,e = 0;

    while(n -- )

    {

        memset(a,'\0',sizeof(a));

        cin >> a;

        if(a[0] == 'B')

            b ++ ;

        if(a[strlen(a) - 1] == 'A')

            c ++ ;

        if(a[strlen(a) - 1] == 'A' && a[0] == 'B')

            d ++ ;

        for(int i = 0 ; i < strlen(a) ; i ++ )

        {

            if(a[i] == 'A' && a[i + 1] == 'B')

                e ++ ;

        }

    }

    int aa = min(b,c);

    if(b == d && c == d && d != 0)

        e -- ;

    cout << e + aa << endl;

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;

const int N= 100100;

string s[N];

//map<int,map<char,char>>mp;

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	ll n;

	cin>>n;

	int t;

	ll res;

	map<char,ll>m;

	map<char,ll>p;

	int flag=0;

	for(ll i=0;i<n;i++) 

	{

		cin>>s[i];

		m[s[i][0]]++;

		p[s[i][s[i].size()-1]]++;

		if(s[i][0]=='B'&&s[i][s[i].size()-1]=='A')

			flag++;

	}

	ll a=m['B'],b=p['A'];

	ll c=a-flag,d=b-flag;

	ll ans=0;

	for(ll i=0;i<n;i++)

	{

		t=s[i].size();

		for(ll j=0;j<t;j++)

		{

			if(s[i][j+1]=='B'&&s[i][j]=='A')

				ans++;

		}

	}

	res=min(b,a);

	ans+=res;

//	cout<<flag<<endl;

//	cout<<ans<<endl;

	if(c==0&&d==0&&flag!=0) ans--; 

	cout<<ans<<endl;

	return 0;

 } 

  

// Optimized solution to the same problem (faster):
#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<string>

#define ios ios::sync_with_stdio(false)

using namespace std;

typedef long long ll;

const int maxn=1e4+100;

string a[maxn];

int main()

{

	ios;

	int n,A=0,B=0,AB=0; cin>>n;

	ll sum=0;

	for(int i=0;i<n;i++){

		cin>>a[i];

		int len=a[i].size()-1;

		for(int j=1;j<=len;j++){

			if(a[i][j]=='B'&&a[i][j-1]=='A') sum++;

		}

		if(a[i][0]=='B'&&a[i][len]=='A'){

			AB++; continue;

		}

		if(a[i][0]=='B'){

			B++; continue;

		}

		if(a[i][len]=='A'){

			A++; continue;

		}

	}

	ll ans=0;

	if(AB>0){

		if(A>0||B>0){

			A-=1; B-=1;

			ans+=2;AB-=1;			

		}

		else AB-=1;

	}

	ans+=min(A,B)+AB+sum;

	cout<<ans<<endl;

	return 0;

}

// Fastest known solution for this problem: