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: Wrong Answer

Solution code:
def solve(n, s):
    num = 0
    a, b, ab = 0, 0, 0
    for i in range(n):
        num += s[i].count("AB")
        if s[i].endswith("A"):
            a += 1
        if s[i].startswith("B"):
            b += 1
        if s[i].endswith("A") and s[i].startswith("B"):
            ab += 1
    best = 0
    for i in range(ab+1):
        cur = min(a-ab+i, b-i)
        best = max(best, cur)
    return num + best

n = int(input())
s = [input() for i in range(n)]
print(solve(n, s))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
first_B, last_A, both = 0, 0, 0
ans = 0
for s in S:
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A': both += 1
    elif s[0] == 'B': first_B += 1
    elif s[-1] == 'A': last_A += 1
temp = min(first_B, last_A, both)
ans += 2*temp
first_B, last_A, both = first_B - temp, last_A - temp, both - temp
if both == 0: ans += min(first_B, last_A)
else:
    rem =  last_A if first_B == 0 else first_B
    pairs = min(rem, both)
    ans += pairs
    rem, both = rem - pairs, both - pairs
    ans += min(pairs, both) + max(both - pairs, 0) // 2
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(n, s):
    num = 0
    a, b = 0, 0
    for i in range(n):
        num += s[i].count("AB")
        if s[i].endswith("A"):
            a += 1
        if s[i].startswith("B"):
            b += 1
    return num + min(a, b)

n = int(input())
s = [input() for i in range(n)]
print(solve(n, s))

Submission status: Wrong Answer

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 != 0:
        ans += ryouhou - 1

        if rest_katahou > 0:
            ans += 1
    
    print(ans)
   
        

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
n = int(input())
XA ,BA ,BX = 0 ,0 ,0
AB = 0
for i in range(n):
    s = str(input())
    AB += s.count("AB")
    if s[0] == "B":
        if s[-1] == "A":
            BA += 1
        else:
            BX += 1
    else:
        if s[-1] == "A":
            XA += 1

print(AB + min(XA,BX) + min((max(XA,BX)-min(XA,BX)),BA) + (BA-min((max(XA,BX)-min(XA,BX)),BA))//2 )

Submission status: Wrong Answer

Solution code:
N=int(input())
A=0
B=0
ans=0
AB=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        AB+=1
    elif S[0]=="B":
        B+=1
    elif S[-1]=="A":
        A+=1
C=min(A,B)
if A==0 and B==0:
    print(ans+max(0,AB-1))
elif C==0:
    print(ans+AB)

Submission status: Wrong Answer

Solution code:
N=int(input())
Alast=0
Bstart=0
AB=0
ABryo=0
for i in range(N):
    S=input()
    AB+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        ABryo+=1
    elif S[0]=="B":
        Bstart+=1
    elif S[-1]=="A":
        Alast+=1
if Alast==0 and Bstart==0:
    print(max(0,ABryo-1)+AB)
elif Alast==0:
    print(ABryo+AB)
elif Bstart==0:
    print(ABryo+AB)
else:
    if Alast==Bstart:
        print(max(0,ABryo-1)+AB+Alast)
    else:
        print(ABryo+AB+min(Alast,Bstart))

Submission status: Wrong Answer

Solution code:
N=int(input())
Alast=0
Bstart=0
AB=0
ABryo=0
for i in range(N):
    S=input()
    AB+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        ABryo+=1
    elif S[0]=="B":
        Bstart+=1
    elif S[-1]=="A":
        Alast+=1
if Alast==0 and Bstart==0:
    print(max(0,ABryo+AB-1))
elif Alast==0:
    print(ABryo+AB)
elif Bstart==0:
    print(ABryo+AB)
else:
    if Alast==Bstart:
        print(max(0,ABryo+AB+Alast-1))
    else:
        print(ABryo+AB+min(Alast,Bstart))

Submission status: Wrong Answer

Solution code:
N=int(input())
Alast=0
Bstart=0
AB=0
ABryo=0
for i in range(N):
    S=input()
    AB+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        ABryo+=1
    elif S[0]=="B":
        Bstart+=1
    elif S[-1]=="A":
        Alast+=1
if Alast==0 and Bstart==0:
    print(ABryo+AB-1)
elif Alast==0:
    print(ABryo+AB)
elif Bstart==0:
    print(ABryo+AB)
else:
    if Alast==Bstart:
        print(ABryo+AB+Alast-1)
    else:
        print(ABryo+AB+min(Alast,Bstart))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())

count = 0
count_a = 0
count_b = 0
count_ab = 0
for i in range(n):
    if s[i][0] =='B' and s[i][-1] =='A':
        count_ab = count_ab + 1
    elif s[i][0] =='B':
        count_b = count_b + 1
    elif s[i][-1] =='A':
        count_a = count_a + 1
    
    count = count + s[i].count('AB')

if count_b==0 and count_a ==0:
    count =count + count_ab-1
else:
    count = count +min(count_a,count_b) + min(count_ab,n-1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())

count = 0
count_a = 0
count_b = 0
count_ab = 0
for i in range(n):
    if s[i][0] =='B' and s[i][-1] =='A':
        count_ab = count_ab + 1
    elif s[i][0] =='B':
        count_b = count_b + 1
    elif s[i][-1] =='A':
        count_a = count_a + 1
    
    count = count + s[i].count('AB')

if count_ab == 1 and count_b==0 and count_a ==0:
    count =count
else:
    count = count +min(count_a,count_b) + min(count_ab,n-1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())

count = 0
count_a = 0
count_b = 0
count_ab = 0
for i in range(n):
    if s[i][0] =='B' and s[i][-1] =='A':
        count_ab = count_ab + 1
    elif s[i][0] =='B':
        count_b = count_b + 1
    elif s[i][-1] =='A':
        count_a = count_a + 1
    
    count = count + s[i].count('AB')

if count_ab == 1 and count_b==0 and count_a ==0:
    count =count
else:
    count = count +min(count_a,count_b) + count_ab

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# wrong answer...
# 正解はこちら　https://img.atcoder.jp/diverta2019/editorial.pdf

import math
N = int(input())
l = []
for i in range(N):
   l.append(input())

default = sum([i.count('AB') for i in l])

#only末尾Aの数
a = sum([i[-1]=='A' and i[0]!='B' for i in l])
#only先頭Bの数
b = sum([i[0]=='B' and i[-1]!='A' for i in l])
#先頭Bかつ末尾Aの数
ab = sum([i[0]=='B' and i[-1]=='A' for i in l])

if a == b:
    if ab > a:
        num = 2 * a + ab - a -1
    elif ab == a:
        num = 2 * a
    elif ab < a:
        num = 2 * ab + a - ab
else:
    min = min(a, b)
    max = max(a, b)
    gap = abs(a-b)
    if ab <= min:
        num = ab * 2 + min - ab
    elif ab > min:
        num = min * 2 + ab - min - 1

print(num + default)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt == 0:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)
elif endA_cnt > 0 and startB_cnt > 0:
    print(min(endA_cnt, startB_cnt) + B_A_cnt + innerAB_cnt)
else:
    print(B_A_cnt-1 + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt == 0:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)
elif endA_cnt > 0 and startB_cnt > 0:
    print(min(endA_cnt, startB_cnt) -1 + B_A_cnt+1 + innerAB_cnt)
else:
    print(B_A_cnt + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt == 0:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)
elif endA_cnt > 1 and startB_cnt > 1:
    print(min(endA_cnt, startB_cnt) -1 + B_A_cnt+1 + innerAB_cnt)
else:
    print(B_A_cnt + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
B_start, A_end, B__A, AB_in = 0, 0, 0, 0
for n in range(N) :
    star = input()
    if star[0] == "B" :
        B_start += 1
    if star[-1] == "A" :
        A_end += 1
        if star[0] == "B" :
            B__A += 1
    AB_in += star.count("AB")
    
if B_start == A_end and A_end == B__A :
    print(B__A+AB_in-1)
else :
    print(min(B_start, A_end)+AB_in)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt > 0:
    print(min(endA_cnt, startB_cnt)+ max(remainder, 1) + B_A_cnt-1 + innerAB_cnt)
else:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
print(min(endA_cnt, startB_cnt)+ max(remainder, 1) + B_A_cnt-1 + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
B_start, A_end, B__A, AB_in = 0, 0, 0, 0
for n in range(N) :
    star = input()
    if star[0] == "B" :
        B_start += 1
    if star[1] == "A" :
        A_end += 1
        if star[0] == "B" :
            B__A += 1
    AB_in += star.count("AB") 
    
if B_start == A_end and A_end == B__A :
    print(B__A+AB_in-1)
else :
    print(min(B_start, A_end)+AB_in)

Submission status: Wrong Answer

Solution code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1

if endA_cnt == 0 and startB_cnt == 0:
    print(B_A_cnt-1 + innerAB_cnt)
else:
    print(min(endA_cnt, startB_cnt) + B_A_cnt + innerAB_cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: "List[str]"):
    cnt = 0
    
    cnt_ba = 0
    cnt__a = 0
    cnt_b_ = 0

    for s in S:
        cnt += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            cnt_ba += 1
        elif s[0] == "B":
            cnt_b_ += 1
        elif s[-1] == "A":
            cnt__a += 1

    cnt_ab = min(cnt__a,cnt_b_)
    cnt += cnt_ab 
    if cnt_ba > 0:
        cnt += cnt_ba - 1

    if abs(cnt__a-cnt_b_) > 0 and cnt_ba > 0:
        cnt += 1

    print(cnt)



    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [ next(tokens) for _ in range(N) ]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: "List[str]"):
    cnt = 0
    
    cnt_ba = 0
    cnt__a = 0
    cnt_b_ = 0

    for s in S:
        cnt += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            cnt_ba += 1
        elif s[0] == "B":
            cnt_b_ += 1
        elif s[-1] == "A":
            cnt__a += 1

    cnt_ab = min(cnt__a,cnt_b_)
    cnt += cnt_ab 
    if cnt_ba > 0:
        cnt += cnt_ba - 1

    if cnt__a > 0 and cnt_ba > 0:
        cnt += 1

    print(cnt)



    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [ next(tokens) for _ in range(N) ]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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:
        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: Wrong Answer

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 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
    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: Wrong Answer

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
        elif ba:
            x = ba.popleft()
            for j in x:
                ans[i] = j
                i += 1
        else:
            x = c.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: Wrong Answer

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:
        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
    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: Wrong Answer

Solution code:
import numpy as np

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


ABarray = np.zeros((N, 2), dtype=int)
count = 0
for i, alp in enumerate(s):
    ABarray[i, 0] = 1 if (alp[-1] == 'A') else 0
    ABarray[i, 1] = 1 if (alp[0] == 'B') else 0
    count += alp.count('AB')

both = np.count_nonzero(np.all(ABarray == 1, axis=0))
ABsum = np.sum(ABarray, axis=0)
Aonly = ABsum[0] - both
Bonly = ABsum[1] - both

if Aonly == 0 and Bonly == 0 and both >= 1:
    count += both - 1
else:
    count += min(Aonly, Bonly) + both
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

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


ABarray = np.zeros((N, 2), dtype=int)
count = 0
for i, alp in enumerate(s):
    ABarray[i, 0] = 1 if (alp[-1] == 'A') else 0
    ABarray[i, 1] = 1 if (alp[0] == 'B') else 0
    count += alp.count('AB')

both = np.count_nonzero(np.all(ABarray == 1, axis=0))
ABsum = np.sum(ABarray, axis=0)
Aonly = ABsum[0] - both
Bonly = ABsum[1] - both
if Aonly == 0 and Bonly == 0:
    count += both - 1
else:
    count += min(Aonly, Bonly) + both
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

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


ABarray = np.zeros((N, 2), dtype=int)
count = 0
for i, alp in enumerate(s):
    ABarray[i, 0] = 1 if (alp[-1] == 'A') else 0
    ABarray[i, 1] = 1 if (alp[0] == 'B') else 0
    count += alp.count('AB')

both = np.count_nonzero(np.all(ABarray == 1, axis=0))
ABsum = np.sum(ABarray, axis=0)
Aonly = ABsum[0] - both
Bonly = ABsum[1] - both
count += min(Aonly, Bonly) + both
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N = int(input())

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

Max = 0

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

Max += min(end.count('A'),start.count('B'))

print(Max)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = 0
sb = ea = both = 0
for i in range(N):
    s = input()
    c += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        ea += 1

if both > 0:
    c += both//2
    if sb > 0:
        c += 1
        sb -= 1
    if ea > 0:
        c += 1
        ea -= 1
    c += min(sb, ea)
else:
    c += min(sb, ea)

print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = 0
sb = ea = both = 0
for i in range(N):
    s = input()
    c += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        ea += 1
print(c + (sb+ea+2*both)//2)

Submission status: Wrong Answer

Solution code:
n = int(input())
enda = 0
startb = 0
ab = 0
for i in range(n):
    s = input()
    if s[0] == "B":
        startb += 1
    if s[-1] == "A":
        enda += 1
    ab += s.count("AB")
print(ab + min([enda, startb, n - 1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
enda = 0
startb = 0
ab = 0
for i in range(n):
    s = input()
    if s[0] == "B":
        startb += 1
    if s[-1] == "A":
        enda += 1
    ab += s.count("AB")
print(ab + min(enda, startb))

Submission status: Wrong Answer

Solution code:
N=int(input())
cnt=0
ba=0
a=0
b=0

for i in range(N):
    chr=input()
    cnt+=chr.count('AB')
    if chr[0]=='B' and chr[-1]=='A':
        ba+=1
    elif chr[-1]=='A':
        a+=1
    elif chr[0]=='B':
        b+=1

cnt+=(min(a,b)+ba)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
counta=0
countb=0
countspec=0
re=0
for i in range(n):
    s=input()
    re+=s.count("AB")
    if (s[0]=='B') and s[len(s)-1]=='A':
        countspec+=1
    else:
        if s[0]=='B':
            countb+=1
        else:
            if (s[len(s)-1]=='A'):
                counta+=1
if counta==0 and countb==0:
    get=countspec-1
else:
    if counta!=0 and countb!=0:
        get=countspec+1
    else:
        get=countspec
re+=max(min(counta,countb)+countspec,get)
print(re)

Submission status: Wrong Answer

Solution code:
n=int(input())
counta=0
countb=0
countspec=0
re=0
for i in range(n):
    s=input()
    re+=s.count("AB")
    if (s[0]=='B') and s[len(s)-1]=='A':
        countspec+=1
    else:
        if s[0]=='B':
            countb+=1
        else:
            if (s[len(s)-1]=='A'):
                counta+=1
get=max(counta,countb)
re+=max(min(counta,countb)+countspec,get)
print(re)