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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    void Run()
    {
        var sc = new Scanner();
        int n = sc.NextInt();
        string[] s = new string[n];
        var tailA = new HashSet<int>();
        var headB = new HashSet<int>();
        for (int i = 0; i < n; i++)
        {
            s[i] = sc.NextString();
            if (s[i][0] == 'B') headB.Add(i);
            if (s[i][s[i].Length - 1] == 'A') tailA.Add(i);
        }
        var common = tailA.Intersect(headB).ToList();
        var freeA = tailA.Except(common).ToList();
        var freeB = headB.Except(common).ToList();
        var sb = new StringBuilder();
        if (freeA.Count - freeB.Count > 0)
        {
            for (int i = freeA.Count - freeB.Count - 1; i < freeA.Count; i++)
                sb.Append(s[freeA[i]]);
        }
        for (int i = 0; i < Math.Min(freeA.Count, freeB.Count); i++)
        {
            sb.Append(s[freeA[i]]);
            sb.Append(s[freeB[i]]);
        }
        foreach (int i in common) sb.Append(s[i]);
        if (freeB.Count - freeA.Count > 0)
        {
            for (int i = freeB.Count - freeA.Count - 1; i < freeB.Count; i++)
                sb.Append(s[freeB[i]]);
        }
        foreach (int i in Enumerable.Range(0, n).Except(tailA).Except(headB)) sb.Append(s[i]);
        string r = sb.ToString();
        int res = 0;
        for (int i = 1; i < r.Length; i++)
            if (r.Substring(i - 1, 2) == "AB") res++;
        Console.WriteLine(res);
    }

    static void Main(string[] args)
    {
        new Program().Run();
    }

    static long Gcd(long a, long b)
    {
        if (a < b) return Gcd(b, a);
        if (b == 0) return a;
        return Gcd(b, a % b);
    }
}

class Scanner
{
    int index = -1;
    string[] buf;

    public Scanner()
    {
    }

    public int NextInt()
    {
        CheckNext();
        return int.Parse(buf[index++]);
    }

    public long NextLong()
    {
        CheckNext();
        return long.Parse(buf[index++]);
    }

    public double NextDouble()
    {
        CheckNext();
        return double.Parse(buf[index++]);
    }

    public string NextString()
    {
        CheckNext();
        return buf[index++];
    }

    private void CheckNext()
    {
        if (index >= 0 && index < buf.Length) return;

        buf = Console.ReadLine().Split(' ');
        index = 0;
    }
}