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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;

class Main{
    public static void main(String argv[]) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String[] str=new String[N];
        sc.nextLine();
        for(int i=0;i<N;i++){
            str[i] = sc.nextLine();
        }

        int max_AB = 0;
        int[] checkA=new int[str.length];
        int[] checkB=new int[str.length];
        for(int i=0;i<str.length;i++){
            if(str[i].substring(0, 1).equals("B")){
                checkB[i]=1;
            }
            if(str[i].substring(str[i].length()-1, str[i].length()).equals("A")){
                checkA[i]=1;
            }
            for(int j=0;j<str[i].length()-1;j++){
                if(str[i].substring(j, j+2).equals("AB")){
                    max_AB++;
                }
            }
        }
        for(int i=0;i<str.length;i++){
            if(checkA[i]==1){
                for(int j=0;j<str.length;j++){
                    if(i==j)continue;
                    if(checkB[j]==1){
                        checkB[j]=checkB[i];
                        checkB[i]=0;
                        max_AB++;
                        break;
                    }
                }
                checkA[i]=0;
            }
        }
        System.out.println(max_AB);
    }
}