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 joined = tailA.Intersect(headB).ToList();
        var freeA = tailA.Except(joined).ToList();
        var freeB = headB.Except(joined).ToList();
        int commonCount = Math.Min(freeA.Count, freeB.Count);
        var common = freeA.Take(commonCount).Zip(freeB.Take(commonCount), (p, q) => new { A = p, B = q }).ToList();
        var commons = common.SelectMany(p => new[] { p.A, p.B }).ToList();
        var remainingA = freeA.Except(commons).ToList();
        var remainingB = freeB.Except(commons).ToList();
        var gab = Enumerable.Range(0, n).Except(tailA).Except(tailA).ToList();

        var sb = new StringBuilder();
        var seq = remainingA.Concat(joined).Concat(remainingB).Concat(commons).Concat(gab);
        foreach (int i in seq) 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;
    }
}