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 static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        int count = int.Parse(ReadLine());
        stringData[] stringDatas = new stringData[count];

        for (int i = 0; i < count; i++)
        {
            var input = ReadLine();
            stringDatas[i] = new stringData(input);
        }

        int a_and_b = stringDatas.Count(sd => sd.IsEndWithA && sd.IsStartWithB);
        int a = stringDatas.Count(sd => sd.IsEndWithA) - a_and_b;
        int b = stringDatas.Count(sd => sd.IsStartWithB) - a_and_b;
        int min = new[] { a, b }.Min();
        if (a_and_b == count)
            min = min - 1;

        int Ab = stringDatas.Sum(sd => sd.ABCount);

        WriteLine(Ab + min + a_and_b);

        ReadKey();
    }
}

public struct stringData
{
    public bool IsStartWithB;
    public bool IsEndWithA;
    public int ABCount;

    public stringData(string s)
    {
        ABCount = 0;

        IsStartWithB = s[0] == 'B';
        int i = 1;

        bool isA = false;
        for (i = 0; i < s.Length; i++)
        {
            if (s[i] == 'A')
                isA = true;
            else if (s[i] == 'B' && isA)
            {
                ABCount++;
                isA = false;
            }
            else
            {
                isA = false;
            }
        }
        IsEndWithA = s[s.Length - 1] == 'A';
    }
}

static class MyUtil
{
    public static long[] ReadlongArray()
    {
        return ReadLine().Split(' ').Select(s => long.Parse(s)).ToArray();
    }

    /// <summary>
    /// <see cref="IEnumerable{T}"/>の各要素を並べてコンソールに出力します。各要素の間には半角スペースが一つ入ります。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ts"></param>
    public static void WriteToConsole<T>(this IEnumerable<T> ts)
    {
        StringBuilder stringBuilder = new StringBuilder();
        foreach (T t in ts)
        {
            stringBuilder.Append(t.ToString());
            stringBuilder.Append(" ");
        }
        stringBuilder.Remove(stringBuilder.Length - 1, 1);
        Console.WriteLine(stringBuilder.ToString());
    }
}