stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 6.545180492532793x):
#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);

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.251543538858794x):
#include<stdio.h>

#pragma GCC optimize("O2")

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;

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.251543538858794x):
#include<stdio.h>

#pragma GCC optimize("O2")

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;

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.250081007875717x):
#include<stdio.h>

#pragma GCC optimize("O2")

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;

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.253172848686953x):
#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;

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.253172848686953x):
#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;

}
// Slower C++ solution:
#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);

}


// Faster C++ solution for the same problem (measured speedup: 5.251709863948502x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.3670573778589716x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.3509537236771256x):
/**

 * code generated by JHelper

 * More info: https://github.com/AlexeyDmitriev/JHelper

 * @author YahiaAshraf74

 */



#include <iostream>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set>

#include <map>

#include <unordered_set>

#include <unordered_map>

#include <queue>

#include <stack>

#include <complex>

#include <string>

#include <bitset>

#include <stdio.h>

#include <string.h>

#include <fstream>

#include <iomanip>

#include <assert.h>



using namespace std;

#define el  '\n'

#define ll long long

#define clr(a, b)memset(a,b,sizeof(a))

#define sz(a) (int)(a).size()

const long long N = 2e5 + 74, OO = 0x3f3f3f3f, MOD = (long long) 1e9 + 7;



class CABSubstrings {

public:

    void solve(istream &cin, ostream &cout) {

        int n;

        cin >> n;

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

        int ans = 0;

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

            string s;

            cin >> s;

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

                ++ba;

            } else if (s[sz(s) - 1] == 'A') {

                ++a;

            } else if (s[0] == 'B') {

                ++b;

            }

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

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

                    ++ans;

            }

        }

        if (ba > 1) {

            ans += (ba - 1);

            if (a > 0)

                ++ans, --a;

            if (b > 0)

                ++ans, --b;

        } else if (ba == 1) {

            if (a > 0)

                ++ans, --a;

            if (b > 0)

                ++ans, --b;

        }

        ans += min(a, b);

        cout << ans << el;

    }

};



int main() {

    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    CABSubstrings solver;

    std::istream &in(std::cin);

    std::ostream &out(std::cout);

    solver.solve(in, out);

    return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.323128204896494x):
#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;

}
// Slower C++ solution:
#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;

} 

// Faster C++ solution for the same problem (measured speedup: 8.24476966877701x):
#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);

}