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



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Runtime Error

Solution code:
N = int(input())

ends_with_A = 0
starts_with_B = 0
both = 0

count = 0

for _ in range(N):
  s = input()
  count += s.count('AB')
  if s.endswith('A') and s.startswith('B'):
    both += 1
  elif s.endswith('A'):
    ends_with_A += 1
  elif s.startswith('B'):
    starts_with_B += 1

if ends_with_A or starts_with_B:
  print(count + both + min(ends_with_A, starts_with_B))
else:  
  print(max(count + both + min(ends_with_A, starts_with_B) - 1), 0)

Submission status: Runtime Error

Solution code:
def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    # _AB_ , B_A , B_ , _A の個数をそれぞれ数える
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  # これは確定
    if sted > 0:
        ans += (sted - 1)  # x個でx-1組のABができる
        if st > 0:
            ans += 1  # B_A + B_ で1組
            st -= 1
        if ed > 0:
            ans += 1  # _A + B_A で1組
            ed -= 1
    ans += min(st, ed)  # _A + B_ で1組
    print(ans)

diverta19_c()

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAX=1e5;
int main()
{
    string a;
    int n;
    cin >> n;
    int b = 0,c = 0,d = 0,sum = 0;
    while(n -- )
    {
        cin >> a;
        int l=a.length();
        if(a[0] == 'B')
            b ++ ;
        if(a[l - 1] == 'A')
            c ++ ;
        if(a[l - 1] == 'A' && a[0] == 'B')
            d ++ ;
        for(int i = 0 ; i < l ; i ++ )
        {
            if(a[i] == 'A' && a[i + 1] == 'B')
                sum ++ ;
        }
    }
    if(b == d && c == d && d != 0) 
        sum -- ;
    cout << sum + min(b,c);
    return 0;
}

Submission status: Runtime Error

Solution code:
N,*L = open('0').read().split()

ans = 0
a = 0
b = 0
for s in L:
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans += 1
  if s[-1]=='A':
    a += 1
  if s[0]=='B':
    b += 1
ans += min(a,b)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B" and s[i][len(s[i])-1] != "A":
        b=b+1
    elif s[i][0] != "B" and s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if a== 0 and b == 0 and c == 0:
elif b == 0 and c == 0:
    out = out + a - 1
else :
    out = out +a+ min(b,c)
    
print(int(out))

Submission status: Runtime Error

Solution code:
N = int(input())
ans = 0
f = False
xx = 0
xA = 0
Bx = 0
BA = 0
A = 0
B = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
if xA != 0 and Bx != 0:
  ans += BA + 1
  xA -= 1
  Bx -= 1
elif xA != 0:
  ans += BA
  xA -= 1
elif Bx != 0:
  ans += AB
  Bx -= 1
else:
  if BA >= 2:
    ans += BA - 1
ans += min(Bx,xA)
print(ans)

Submission status: Runtime Error

Solution code:

n = int(input())
lst = [input() for _ in range(n)]

finA = 0
topB = 0
B2A = 0
cnt = 0

for l in lst:
    cnt += l.count('AB')
    if l[0]=='B' and l[-1]=='A':
        B2A += 1
    else:
        if l[0]=='B':
            topB += 1
        elif l[-1]=='A':
            finA += 1

if BtoA==0:
    cnt += min(finA, topB)
else:
    if finA==0 and topB==0:
        cnt += BtoA - 1
    else:
        cnt += BtoA + min(finA, topB)
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	ans += s.count('AB')
	if  s[0] == 'B' and s[-1] == 'A':
		BA += 1
	elif s[0] == 'B':
		B += 1
	elif s[-1] == 'A':
		A += 1


if A+B >= BA:
	ans += BA
	BA = 0
else:
	ans += A+B
	BA -= A+B

if A >= B:
	ans += B
	C = A - B
else:
	ans += A
	C = B - A

if BA > 0:
	if C >= BA:
		ans += BA
	else:
		s,t = divmod(BA,C)
		ans += s*C
		ans += t//2


print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
s=[input() for _ in range(N)]
cnt=0

A_count=0
B_count=0
BA_count=0
for u in s:
    cnt+=u.count("AB")
    if u[0]=="B":
        B_count+=1
    if u[-1]=="A":
        A_count+=1
    if u[0]=="B" and u[-1]=="A":
        BA_count+=1
if A_count==B_count==BA_count:
    if A_count==0:
        print(cnt)
    else:
        print(cnt+A_count-1)
else:
    print(cnt+min(A_count,B,count))

Submission status: Runtime Error

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1)+(xa>0)+(bx>0) +min((xa-1,bx-1),0))

Submission status: Runtime Error

Solution code:
import re
count_ab = 0
b_a = 0
__a = 0
b__ = 0

for si in ss:
  count_ab += len(re.findall('AB',si))
  if si[0]=='B' and si[-1]=='A':
    b_a += 1
  elif si[0]=='B':
    b__ += 1
  elif si[-1]=='A':
    __a += 1
  else:
    pass

count_ab += min(__a, b__)
if abs(__a - b__) > 0 and b_a > 0:
  count_ab +=1

if b_a > 0:  
  count_ab += b_a - 1

print(count_ab)

Submission status: Runtime Error

Solution code:
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
elif ba>0:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    else bx+xa==0
        ans+=ba-1

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B"&&S[-1]=="A")
  bx+=(s[0]=="B"&&S[-1]!="A")
  bxa+=(s[0]=="B"&&S[-1]=="A")
print(ans+(bxa-1)+min(xa,bx)+(xa!=bx&&bxa>0))

Submission status: Runtime Error

Solution code:
N = int(input())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!=B and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!=B and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import Counter

n=int(input())
s=[input() for _ in range(n)]

cnt=0
cnt_a=0
cnt_b=0
cnt_ab=0
for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        cnt_ab+=1
    elif s[i][0]=='B':
        cnt_b+=1
    elif s[i][-1]=='A':
        cnt_a+=1
    cnt+=s[i].count('AB')
if cnt_ab=0:
    ans=cnt+min(cnt_a,cnt_b)
elif:
    if cnt_a=0 and cnt_b=0:
        ans=cnt+cnt_ab-1
    else:
        ans=cnt+min(cnt_a,cnt_b)

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
ans=0
a=0;b=0;ab=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B":
    b+=1
    if s[-1]=="A":
      ab+=1
      b-=1
  elif s[-1]=="A":
    a+=1
print(ans+c-1+min(a,b))

Submission status: Runtime Error

Solution code:
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 #q1はBA q2はBの最初のみ q3はAの末尾のみ
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
if q1>=1:
  if q2==0 and q3==0:
    print(q1-1+point)
  elif q2==0 or q3==0
    print(q1-1+1+point)
  else:
    print(q1-1+point+2+min(q2-1,q3-1))
else:
  print(min(q2,q3)+point)

Submission status: Runtime Error

Solution code:
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 #q1はBA q2はAの末尾のみ q3はBの最初のみ
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
A-=1
B-=1
print(q1-1+2+min(q3,q2)+point)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

int AB(string S){
    int N = S.length();
    int res = 0;
    for(int i = 0; i < N - 1; i++){
        if(S[i] == 'A' && S[i + 1] == 'B'){
            res++;
            i++;
        }
    }
    return res;
}

int main(){
    int N;
    cin >> N;
    vector<string> S(N);
    vector<int> len(N);
    for(int i = 0; i < N; i++){
        cin >> S[i];
        len[i] = S[i].length();
    }
    
    int ans = 0;
    int A = 0, B = 0, C = 0;
    vector<bool> TOP(N, false);
    vector<bool> BAC(N, false);
    for(int i = 0; i < N; i++){
        ans += AB(S[i]);
        if(S[i][0] == 'B')TOP[i] = true;
        if(S[i][len[i] - 1] == 'A')BAC[i] = true;
    }
    
    //cout << ans << endl;
    for(int i = 0; i < N; i++){
        if(TOP[i] == true && BAC[i] == false)B++;
        if(TOP[i] == false && BAC[i] == true)A++;
        if(TOP[i] && BAC[i])C++;
    }
    
    ans += min(A, B);
    int dis = abs(A - B);
    C += dis;
    ans += (C + 1) / 2;
    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
n=int(input())
ba,a,b,ab=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1

print(min(a,b)+x+ab)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
"""
.....A
B.....
を連結させればABは一個増える
"""
def solve():
    N = int(input())

    ans = 0
    b_start = 0
    a_end = 0
    ryouhou = 0
    for i in range(N):
        s = input()
        ans += s.count("AB")
        
        if s[0] == "B" and S[-1] == "A":
            ryouhou += 1
        elif s[0] == "B":
            b_start += 1
        elif s[-1] == "A":
            a_end += 1
    
    add_ab_num = min(b_start, a_end)
    ans += add_ab_num

    rest_katahou = (max(b_start, a_end) - add_ab_num)

    if ryouhou >= rest_katahou:
        ans += rest_katahou
        rest_ryouhou = (ryouhou - rest_katahou)
        ans += rest_ryouhou//2
    else:
        ans += ryouhou
    
    print(ans)
   
        

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
N = int(input())
count = 0
end = 0
start = 0
double = 0
for i in range(N):
    line = input()
    if "AB" in line:
        count += 1
    if line[0] == "B":
        if line[-1] == "A":
            double +=1
        else:
            start += 1
    if line[-1] == "A":
        if line[0] == "B":
            double +=1
        else:
            end += 1
if double == 0:
    count += min(start, end)
elif start + end > 0:
    counr += double + min(start, end)
elif start + end == 0:
    count += double - 1
print(count)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    print(n-k+1)
    return

#B
def B():
    r,g,b,n = LI()
    ans = 0
    for i in range(n+1):
        x = r*i
        for j in range(n+1-x):
            y = g*j
            z = n-x-y
            if z%b != 0 or z < 0:continue
            ans += 1
    print(ans)
    return

#C
def C():
    n = I()
    s = SR(n)
    f = [0 for i in range(n)]
    a = deque()
    b = deque()
    ba = deque()
    c = deque()
    l = 0
    for i in range(n):
        l += len(s[i])
        if s[i][-1] == "A":
            f[i] += 1
        if s[i][0] == "B":
            f[i] += 2
        if f[i] == 0:
            c.append(s[i])
        elif f[i] == 1:
            a.append(s[i])
        elif f[i] == 2:
            b.append(s[i])
        else:
            ba.append(s[i])
    ans = [None for i in range(l)]
    i = 0
    while a or b or c or ba:
        if a:
            x = a.popleft()
            for j in x:
                ans[i] = j
                i += 1
            while ba:
                x = ba.popleft()
                for j in x:
                    ans[i] = j
                    i += 1
            if b:
                x = b.popleft()
                for j in x:
                    ans[i] = j
                    i += 1
        elif b:
            x = b.popleft()
            for j in x:
                ans[i] = j
                i += 1
        else:
            x = c.popleft()
            for j in x:
                ans[i] = j
                i += 1
        while ba:
            x = ba.popleft()
            for j in x:
                ans[i] = j
                i += 1
    s = 0
    for i in range(l-1):
        if ans[i] == "A" and ans[i+1] == "B":
            s += 1
    print(s)
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Runtime Error

Solution code:
from itertools import permutations

N = int(input())

inputs = []
end = []
start = []
pair = []

Max = 0
B_count = 0
A_count = 0

for i in range(0,N):
    tex = input()
    inputs.append(tex)
    head = tex[0] 
    tail = tex[-1]
    if head == 'B' or tail == 'A':
        end.append(tail)
        start.append(head)
        pair.append(tex[0]+tex[len(tex) - 1])
    Max += tex.count('AB')


print(end)
print(start)
print(pair)
count = 0
# while pair != []:
#     count+=1
#     end.pop(0)
#     start.pop()
perm = permutations(pair)
for p in perm:
    print(p)
    tex = ''.join(p)
    c = tex.count('AB')
    if count < c:
        count = c
print(c+Max)

Submission status: Runtime Error

Solution code:
n = int(input())
ss = [input() for _ in range(n)]
ans = 0
count_ab = 0 # if ab exists
count_a = 0 # if end in a
count_b = 0 # if start in b
count_ba = 0 # if start in b and end in a
for s in ss:
  slen = len(s)
  if s[0] == 'B' and  s[-1] == 'A':
    count_ba +=1
  else:
      if s[0] == 'B':
        count_b +=1
      if s[-1] == 'A':
        count_a +=1
  count_ab += s.count("AB")

if count_ba == 0:
    print(count_ab + min(count_a, count_b))
elif count_a == 0 and count_b == 0:
    print(count_ab + count+ba -1)
elif count_a > 0 and count_b > 0:
    print(count_ab +  min(count_a, count_b) + count_ba)
else:
    print(count_ab + count_ba)

Submission status: Runtime Error

Solution code:
n = int(input())
t = []
a = 0
b = 0
total = 0
for i in range(n):
	t.append(input())
    total += t[-1].count("AB")
    a += t[-1][-1] == "A"
    b += t[-1][0] == "B"
print(min(a,b) + total)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [list(input()) for i range(n)]
cnt = 0
cnt_A=0
cnt_B=0
cnt_C=0
for i in range(n):
  if "AB" in a[i]:
    cnt+= 1
for i in range(n):
  if a[i][-1] == "A" and a[i][0] =="B":
    cnt_C+=1  
  elif a[i][-1] == "A" and a[i][0] !="B":
    cnt_A +=1
  elif a[i][0] =="B"and a[i][-1] !="A":
    cnt_B+=1

print(cnt+cnt_A+cnt_B+cnt_C)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [list(input()) for i range(n)]
cnt = 0
cnt_A=0
cnt_B=0
cnt_C=0
for i in range(n):
  if "AB" in a[i]:
    cnt+= 1
for i in range(n):
  if a[i][-1] == "A" and a[i][0] =="B":
    cnt_C+=1  
  elif a[i][-1] == "A": 
    cnt_A +=1
  elif a[i][0] =="B":
    cnt_B+=1

print(cnt+cnt_A+cnt_B+cnt_C)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [list(input()) for i range(n)]
cnt = 0
cnt_A=0
cnt_B=0
cnt_C=0
for i in range(n):
  if "AB" in a[i]:
    cnt+= 1
for i in range(n):
  if a[i][-1] == "A" 
    cnt_A +=1
  elif a[i][0] =="B":
    cnt_B+=1
  elif a[i][-1] == "A" and a[i][0] =="B":
    cnt_C+=1
print(cnt+cnt_A+cnt_B-cnt_C)

Submission status: Runtime Error

Solution code:
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
 
doubleAB = 0
 
for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if(str[-1] == 'A' and str[0] == 'B'):
  	doubleAB = doubleAB + 1
  elif (str[-1] == 'A'):
    tail_a = tail_a + 1
  elif (str[0] == 'B'):
    head_b = head_b + 1
 
if(tail == 0 and head_b == 0):
  total_cand = inside_str + doubleAB - 1
else:
  total_cand = inside_str + doubleAB + min(tail_a, head_b)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
S = []
counterBX = 0
counterXA = 0
counterBXA = 0
counterAB = 0
for i in range(N):
    S.append(input())
    if S[i][0] == 'B' and S[i][-1] != 'A':
        counterBX +=1
    if S[i][0] != 'B' and S[i][-1] == 'A':
        counterXA +=1
    if S[i][0] == 'B' and S[i][-1] == 'A':
        counterBXA +=1
    if len(S[i]) > 1:
        for j in range(len(S[i]) - 1):
            if S[i][j] == 'A' and S[i][j+1] == 'B':
                counterAB +=1
minval = min(counterBX,counterXA)
sa = abs(counterBX - counterXA)

if counterBXA == 0:
    ans = counterAB + min(counterXA,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX == 0:
    ans = counterAB + counterBXA - 1
elif counterBXA >=1 and counterXA >=1 and counterBX == 0:
    ans = counterAB + (counterBXA - 1) + 1 + min(counterXA - 1,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX >=1:
    ans = counterAB + (counterBXA - 1) + 1 +min(counterXA,counterBX - 1)
else:
    ans = counter(BXA - 1) + 2 + min(counterXA - 1, counterBX - 1)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = sum([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = sum([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = sum([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Submission status: Runtime Error

Solution code:
#c
n = int(input())
stringsA = 0
stringsB = 0
both = 0
ans = 0
if n == 1:
    string = input()
    print(string.count("AB"))
else:
    for i in range(n):
        string = input()
        ans += string.count("AB")
        if string[-1] == 'A' and string[0] == 'B':
            both += 1
        elif string[-1] == 'A':
            stringsA += 1
        elif string[0] == 'B':
            stringsB += 1
    if both == 0:
		print(ans + min(stringsA,stringsB))
    elif stringsA + stringsB == 0 and both > 1:
        both -= 1
    elif stringsA == stringsB and both == 1:
        both = 0
    else:
    	print(ans + min(stringsA,stringsB) + both)

Submission status: Runtime Error

Solution code:
N = int(input())
s = [input() for _ in range(N)]

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
elif end_A + end_B == 0:
    print(AB_count + both - 1)

Submission status: Runtime Error

Solution code:
N = int(input())
s = [input() for i in range(N)]

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
elif end_A + end_B == 0:
    print(both - 1)

Submission status: Runtime Error

Solution code:
N=int(input())
l=[]
for i in range(N):
    l.append(input())
cnt=cnt_a=cnt_b=count=0
for i in l:
    if 'AB' in i:cnt+=1
    if i[0]=='B' and i[-1]=='A':count+=1
    if i[0]=='B':cnt_b+=1
    if i[-1]=='A':cnt_a+=1
if N==count:print (cnt+N-1)
else:print (cnt+min(cnt_a,cnt_b)

Submission status: Runtime Error

Solution code:
n = int(input())
ba, xa, bx = 0, 0, 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        ba += 1
    if s[0] == "B" and s[-1] != "A":
        bx += 1
    if s[0] != "B" and s[-1] == "A":
        xa += 1

if xa == 0 and bx == 0:
    ans += max(0, ba-1)
else:
    ans += ba
    ans += min(xa, bx)

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=0
B=0
AB=0
ans=0

for i in range(N):
  s=input()
  for j in range(len(s)-1):
    if s[j]=="A" and s[j+1]=="B":
      ans+=1
      
  if s[0]=="B" and s[-1]=="A":
    AB+=1
  elif s[0]=="B":
    B+=1
  elif s[-1]=="A":
    A+=1
  else:
    continue
  
ans+=min(A,B)
tmp=abs(A-B)

if AB <= tmp:
  ans+=AB
else:
  ans+=AB//tmp*tmp
  ans+=AB%temp
  
print(ans)

Submission status: Runtime Error

Solution code:
tBeA = 0
tBeNA = 0
tNBeA = 0
for i in range(N):
    flag = False
    if a[i][0] == "B":
        if a[i][-1] == "A":
            tBeA += 1
        else:
            tBeNA += 1
    else:
        if a[i][-1] == "A":
            tNBeA += 1

    for j in range(len(a[i])):
        # print(a[i][j])
        if a[i][j] == "B" and flag:
            cnt += 1
        flag = False

        if a[i][j] == "A":
            flag = True

print(
    cnt + min(tBeNA, tNBeA) if tBeA == 0 else cnt + min(tBeNA, tNBeA) + tBeA if tNBeA + tBeNA > 0 else cnt + min(tBeNA,
                                                                                                                 tNBeA) + tBeA - 1)

Submission status: Runtime Error

Solution code:
tailA = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[0] =='B' and t[-1]=='A':
        both+=1
    elif t[0] == 'B':
        headB+=1
    elif t[-1] == 'A':
        tailA +=1
    c+=t.count('AB')
if both>0:
    c+=both-1
    headB+=1
    tailA+=1
print(c+min(headB,tailA))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(input())
count = 0
bcount = 0
acount = 0
bacount = 0
for j in s:
    count += j.count("AB")
    if j[0] == "B" and j[-1] == "A":
        bacount += 1
    elif j[0] == "B":
        bcount += 1
    elif j[-1] == "A":
        acount += 1
if min(bcount,acount) >= bacount:
	count += min(bcount,acount) + bacount
    print(count)
else:
    count += min(bcount,acount)*2
    if abs(acount-bcount) > 0 and bacount - min(bcount,acount) > 0:
        count += min(abs(acount-bcount),bacount - min(bcount,acount))
        print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
S = []
result = 0
A_count = 0
B_count = 0
BA_count = 0

for i in range(n):
    Si = str(input())
    S.append(Si)

for i in range(n):
    Si = S[i]
    result += Si.count("AB")
    if Si[0] == "B":
        B_count += 1
    if Si[-1] == "A":
        A_count += 1
    if Si[0] == "B" and Si[-1] == "A"
        BA_count += 1 

if BA_count >= 2:
    result += (BA_count - 1)

if BA_count >= 1
    if B_count >= 1:
        result += 1
        B_count -= 1
    if A_count >= 1:
        result += 1
        A_count -= 1

if B_count >= 1 and A_count >= 1:
    result += min(B_count, A_count)

print(result)

Submission status: Runtime Error

Solution code:
n = int(input())
 
g1 = []
g2 = []
g3 = []
 
cnt = 0
 
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        g3.append(s)
    elif s[0] == "B":
        g1.append(s)
    elif s[-1] == "A":
        g2.append(s)
        
a = len(g1)
b = len(g2)
c = len(g3)
 
if a == b:
    if a == c:
        cnt += c * 2
    elif a > c:
        cnt += c
        cnt += a
    elif a < c:
        if a == 0:
            cnt += c - 1
        else]:
            cnt += a
            cnt += c
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c
 
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB = 0, 0
ans = 0

for _ in range(N):
    S = input()

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            endA += 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1

ans += min(endA, startB)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
    S = [input() for i in range(N)]

    a = b = ab = 0
    ans = 0
    for s in S:
        n = len(s)
        for j in range(n-1):
            if s[j] == "A" and s[j+1] == "B":
                ans += 1
        if s[0] == "B" and s[-1] == "A":
            ab += 1
        elif s[0] == "B":
            b += 1
        elif s[-1] == "A":
            a += 1

    if a == 0 and b == 0:
        print(ans+ab-1)
    else:
        print(ans+ab+min(a, b))

Submission status: Runtime Error

Solution code:
N = int(input())
S = [input() for i in range(N)]

# 'AB'の数を数える
cnt_ab = sum([s.count('AB') for s in S])

# L: 先頭が'B'、終わりが'A'の数を数える
cnt_b_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

# M: 先頭が'B'、終わりが'A'じゃないの数を数える
cnt_b_na = sum([(s[0] == 'B') and (s[-1] != 'A') for s in S])

# N : 先頭が'B'じゃなく、終わりが'A'の数を数える
cnt_nb_a = sum([(s[0] != 'B') and (s[-1] == 'A') for s in S])

count = cnt_ab
if (cnt_b_a == 0): # MとNの数の小さい方の分だけ追加できる
    ans += min(cnt_b_na, cnt_nb_a)
elif (cnt_b_na + cnt_nb_a == 0): # Lの個数 - 1個だけ追加できる
    ans += (cnt_b_a - 1)
else:
    ans += (cnt_b_a + min(cnt_b_na, cnt_nb_a))
print(ans)

Submission status: Runtime Error

Solution code:
a = int(input())
b = [input() for i in range(a)]
c = []
d = []
f = []
e = 0
p =[]
x ='0'
for j in range(a):
    if b[j][0] == 'B':
        c.append(b[j])
    elif b[j][-1] == 'A':
        d.append(b[j])
    else:
        p.append(b[j])
for h in range(a):
    if not (len(c) == 0) & (len(d) == 0):
        if e%2 == 0:
            f.append(d[0])
            d.remove(d[0])
            e += 1
        else:
            f.append(c[0])
            c.remove(c[0])
            e += 1
    else:
        break
f = f + p
for h in range(len(f)):
    x += f[h]
print(x.count('AB'))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in range(len(s)):
	c += s[j].count("AB")
a = 0
b = 0
for k in range(len(s)):
	if s[k][-1] == "A":
        a += 1
for l in range(len(s)):
	if s[l][0] == "B":
        b += 1
print(c+min(a,b))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in range(len(s)):
	c += s[i].count("AB")
a = 0
b = 0
for k in range(len(s)):
	if s[i][-1] == "A":
        a += 1
for l in range(len(s)):
	if s[i][0] == "B":
        b += 1
print(c+min(a,b))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in range(len(s)):
	c += s[i].count("AB")
a = 0
b = 0
for k in range(len(s)):
	if s[i][-1] == "A":
        a += 1
for l in range(len(s)):
	if s[i][0] == "B":
        b += 1
print(c+Math.min(a,b))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in len(s):
	c += s[i].count("AB")
a = 0
b = 0
for k in range(len(s)):
	if s[i][-1] == "A":
    	a += 1
for l in range(len(s)):
	if s[i][0] == "B":
    	b += 1
print(c+Math.min(a,b))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in len(s):
	c += s[i].count("AB")
a = 0
b = 0
for k in len(s):
	if s[i][-1] == "A":
    	a += 1
for l in len(s):
	if s[i][0] == "B":
    	b += 1
print(c+Math.min(a,b))

Submission status: Runtime Error

Solution code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
ct += max(0,c -1)
if c !=0:
  if a > 0 and b >0:
    ct += 2 + min(a-1,b-1)
  elif head>0:
    ct += 1 + min(a-1,b)
  elif last>0:
    ct += 1 + min(a,b-1)
else:
  ct += min(a,b)
print(ct)

Submission status: Runtime Error

Solution code:
N = int(input())
res = 0
both = 0
h = 0
b = 0
for i in range(N):
  inp = input()
  if 'AB' in inp:
    res += 1
  if 'B'==inp[0] and 'A'==inp[-1]:
    both+=1
  elif 'B'==inp[0]:
    # print (inp)
    h+=1
  elif 'A'==inp[-1]:
    # print (inp)
    b += 1
if max(h,b)>0 or both==0:
    print(res+min(h,b)+both)
elif max(a,b)==0:
    print(res+both-1)