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.io.IOException;
import java.io.InputStream;

public class Main {

	static FastReaderR in = new FastReaderR(System.in) ;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int N = in.nextInt() ;
		int a = 0 ;
		int b = 0 ;
		int ab = 0 ;
		int ans = 0 ;
		for(int i=0;i<N;i++){
			String line = in.next() ;
			if(line.charAt(0)!='B'&&line.charAt(line.length()-1)!='A'){
				int index = 0 ;
				while(true){
//					System.out.println("1");
					index = line.indexOf("AB",index) ;
					if(index!=-1)
						ans++ ;
					else
						break ;
					if(index+2>=line.length())
						break ;
					index += 2 ;
				}
			}else{
				if(line.charAt(0)=='B'&&line.charAt(line.length()-1)=='A'){
					ab++ ;
					int index = 0 ;
					while(true){
						index = line.indexOf("AB",index) ;
						if(index!=-1)
							ans++ ;
						else
							break ;
						if(index+2>=line.length())
							break ;
						index += 2 ;
					}
				}else{
					if(line.charAt(line.length()-1)=='A'){
						a++ ;
						int index = 0 ;
						while(true){
							index = line.indexOf("AB",index) ;
							if(index!=-1)
								ans++ ;
							else
								break ;
							if(index+2>=line.length())
								break ;
							index += 2 ;
						}
					}
						if(line.charAt(0)=='B'){
							b++ ;
							int index = 0 ;
							while(true){
								index = line.indexOf("AB",index) ;
								if(index!=-1)
									ans++ ;
								else
									break ;
								if(index+2>=line.length())
									break ;
								index += 2 ;
							}
						}
					
				}
			}
		}
		
//		System.out.println(a+" "+b+" "+ab+ " "+ans) ;
		
		if(b>a){
			if((b-a)<=ab){
				ans += b ;
				
				ab -= (b-a) ;
				ab++ ;
				if(ab>=1){
					ans += (ab-1) ;
				}
			}else{
				ans += (a+ab) ;
				
				ab++ ;
				ab -= (b-a) ;
				if(ab>=1){
					ans += (ab-1) ;
				}
			}
		}else{
			if((a-b)<=ab){
				ans += a ;
				
				ab -= (a-b) ;
				ab++ ;
				if(ab>=1){
					ans += (ab-1) ;
				}
			}else{
				ans += (b+ab) ;
				
				ab -= (a-b) ;
				ab++;
				if(ab>=1){
					ans += (ab-1) ;
				}
			}
		}
		System.out.println(ans);
	}

}

class FastReaderR {
    byte[] buf = new byte[2048];
    int index, total;
    InputStream in;
 
    FastReaderR(InputStream is) {
        in = is;
    }
    int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) {
                return -1;
            }
        }
        return buf[index++];
    }
    int nextInt() throws IOException {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan());
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
    String next() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan());
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }
}