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:
n = int(input())
s = [input() for _ in range(n)]

acount = 0
bcount = 0
abcount = 0
ans = 0

"""
for i in range(len(s)):
    if s[i][0] == 'B':
        bcount += 1
    if s[i][-1] == 'A':
        acount += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        abcount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            ans += 1

if max(abcount,acount,bcount) == abcount:
    print(ans + min(acount,bcount) - 1)
else:
    print(ans + min(acount,bcount))

"""

for i in range(len(s)):
    if s[i][0] == 'B' and s[i][-1] == 'A':
        abcount += 1
    elif s[i][0] == 'B':
        bcount += 1
    elif s[i][-1] == 'A':
        acount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            ans += 1

if acount + bcount == 0:
    print(ans + abcount - 1)
else:
    print(ans + abcount + min(acount,bcount))

Submission status: Wrong Answer

Solution code:
n=int(input())
count1=0
count2=0
count3=0
ans=0
for i in range(n):
	flag=0
	s=str(input())
	for j in range(len(s)-1):
		if(s[j]+s[j+1]=='AB'):
			ans+=1

	if(s[-1]=='A' and s[0]=='B'):
		count1+=1
	elif(s[0]=='B'):
		count2+=1
	elif(s[-1]=='A'):
		count3+=1
#print(ans,count1,count2,count3)
if(count3>0):
	count3-=1
	ans+=count1
	if(count2>0):
		ans+=1
		count2-=1
else:
	ans+=count1-1
	if(count2>0):
		ans+=1
		count2-=1

while(count3>0 and count2>0):
	ans+=1
	count2-=1
	count3-=1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
count1=0
count2=0
count3=0
ans=0
for i in range(n):
	flag=0
	s=str(input())
	for j in range(len(s)-1):
		if(s[j]+s[j+1]=='AB'):
			ans+=1

	if(s[-1]=='A' and s[0]=='B'):
		count1+=1
	elif(s[0]=='B'):
		count2+=1
	elif(s[-1]=='A'):
		count3+=1
#print(ans,count1,count2,count3)
while(count2>0 and count3>0):
	ans+=1
	count2-=1
	count3-=1
if(count2==0 and count1>0):
	if(count3>0):
		ans+=count1
	else:
		ans+=count1-1
elif(count3==0):
	ans+=count1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
count1=0
count2=0
ans=0
for i in range(n):
	flag=0
	s=str(input())
	for j in range(len(s)-1):
		if(s[j]+s[j+1]=='AB'):
			ans+=1

	if(s[-1]=='A'):
		if(count2>0):
			ans+=1
			count2-=1
		else:
			count1+=1
			flag=1
	if(s[0]=='B'):
		if(flag==0 and count1>0):
			ans+=1
			count1-=1
		elif(flag==1 and count1>1):
			ans+=1
			count1-=1
		else:
			count2+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
count1=0
count2=0
ans=0
for i in range(n):
	s=str(input())
	for j in range(len(s)-1):
		if(s[j]+s[j+1]=='AB'):
			ans+=1

	if(s[-1]=='A'):
		if(count2>0):
			ans+=1
			count2-=1
		else:
			count1+=1
	if(s[0]=='B'):
		if(count1>0):
			ans+=1
			count1-=1
		else:
			count2+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import re

n = int(raw_input())

ab = 0
a = 0
b = 0

ans = 0
for i in range(0, n):
    s = raw_input()
    find_list = re.findall("AB", s)
    ans += len(find_list)

    if re.search("^A.*B$", s) is not None:
        ab += 1
        continue

    if re.search(".*A$", s) is not None:
        a += 1

    if re.search("^B.*", s) is not None:
        b += 1

if ab > 0:
    ans += ab - 1
    if a > 0:
        ans += 1
        a -= 1
    if b > 0:
        ans += 1
        b -= 1
    ans += min(a, b)

else:
    ans += min(a, b)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
dicts = {'tail': 0, 'head': 0, 'head_tail': 0}
for i in range(N):
    item = input()
    if 'AB' in item:
        ans += 1
    if item[0] == 'B' and item[-1] == 'A':
        dicts['head_tail'] += 1
    else:
        if item[-1] == 'A':
            dicts['tail'] += 1
        if item[0] == 'B':
            dicts['head'] += 1

# if dicts['tail'] == 0:
#     ans += min(dicts['head'], dicts['head_tail'])
# else:


a = min(dicts['tail'], dicts['head'])
ans += a
dicts['head'] -= a
dicts['tail'] -= a
# diff = max(dicts['tail'], dicts['head']) - min(dicts['tail'], dicts['head'])

if dicts['head_tail'] > 0:
    ans += dicts['head_tail'] - 1
    if dicts['head'] > 0:
        ans += 1
    if dicts['tail'] > 0:
        ans += 1
print(ans)

'''
-A
B-
B-A
'''

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
dicts = {'tail': 0, 'head': 0, 'head_tail': 0}
for i in range(N):
    item = input()
    if 'AB' in item:
        ans += 1
    if item[0] == 'B' and item[-1] == 'A':
        dicts['head_tail'] += 1
    else:
        if item[-1] == 'A':
            dicts['tail'] += 1
        if item[0] == 'B':
            dicts['head'] += 1

# if dicts['tail'] == 0:
#     ans += min(dicts['head'], dicts['head_tail'])
# else:


a = min(dicts['tail'], dicts['head'])
ans += a
dicts['head'] -= a
dicts['tail'] -= a
diff = max(dicts['tail'], dicts['head']) - min(dicts['tail'], dicts['head'])

if dicts['head_tail'] > 0:
    ans +=  diff if diff <= 2 else 2
    ans += dicts['head_tail'] - 1
print(ans)

'''
-A
B-
B-A
'''

Submission status: Wrong Answer

Solution code:
n = int(input())
a_count = 0
b_count = 0
ab_count = 0

if n == 1:
    s = input()
    ab_count += s.count("AB")
else:
    for i in range(n):
        s = input()
        ab_count += s.count("AB")
        if s[0] == "B":
            b_count += 1
        if s[-1] == "A":
            a_count += 1
    
print(ab_count + min(a_count, b_count))

Submission status: Wrong Answer

Solution code:
n = int(input())
a_count = 0
b_count = 0
ab_count = 0
for i in range(n):
    s = input()
    ab_count += s.count("AB")
    if s[0] == "B":
        b_count += 1
    if s[-1] == "A":
        a_count += 1

print(ab_count + min(a_count, b_count))

Submission status: Wrong Answer

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

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = (list(input() for i in range(N)))
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    if 'AB' in s:
        cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    cot += min(cot_a, cot_b)
elif cot_a + cot_b >0:
    cot += cot_ab + min(cot_a, cot_b)
else:
    cot += cot_ab - 1
print(cot)

Submission status: Wrong Answer

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

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = (list(input() for i in range(N)))
print(S)
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    if 'AB' in s:
        cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    cot += min(cot_a, cot_b)
elif cot_a + cot_b >0:
    cot += cot_ab + min(cot_a, cot_b)
else:
    cot += cot_ab - 1
print(cot)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = (list(input() for i in range(N)))
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    if 'AB' in s:
        cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    print(cot + min(cot_a, cot_b))
elif cot_a + cot_b >0:
    print(cot + cot_ab + min(cot_a, cot_b))
elif cot_a + cot_b == 0:
    print(cot + cot_ab - 1)

Submission status: Wrong Answer

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

    a = 0
    b = 0
    ab = 0

    count = 0

    for i in range(N):
        for j in range(len(s[i])-1):
            if s[i][j:j+2] == 'AB':
                count += 1


    for i in range(N):
        if (s[i][0] == 'B' or s[i][-1] == 'A'):
            print('ok')
            if (s[i][0] == 'B' and s[i][-1] == 'A'):
                ab += 1
            elif (s[i][0] == 'B'):
                b += 1
            else:
                a += 1

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


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = (list(input() for i in range(N)))
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    if 'AB' in s:
        cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

cot += min(cot_a, cot_b)

if abs(cot_a - cot_b) > cot_ab:
    cot += abs(cot_a - cot_b)
else:
    cot += cot_ab
print(cot)

Submission status: Wrong Answer

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:
    cnt=cnt+i.count('AB')
    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: Wrong Answer

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

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':cnt_b+=1
    if i[-1]=='A':cnt_a+=1
    if i[0]=='B' and i[-1]=='A':count+=1
if N==count:print (N-1)
else:print (cnt+min(cnt_a,cnt_b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
l=list(map(str,input().split()))
del l[0]
cnt=cnt_a=cnt_b=0
for i in l:
    if 'AB' in i:cnt+=1
    if  i[0]=='B':cnt_b+=1
    if i[-1]=='A':cnt_a+=1
print (cnt+min(cnt_a,cnt_b))

Submission status: Wrong Answer

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

if A==0 and B==0:
  ans+=AB-1
else:
  ans+=(AB+min(A,B))
  
print(ans)

Submission status: Wrong Answer

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

if A==B and A==AB:
  ans+=(AB*2)
elif A==B:
  if A > AB:
    ans+=AB*2
    ans+=A-AB
  else:
    ans+=A*2
    ans+=((AB-A)-1)
else:
  ans+=min(A,B,AB)*2
  A-=min(A,B,AB)
  B-=min(A,B,AB)
  AB-=min(A,B,AB)
  if AB==0:
    ans+=min(A,B)
  else:
    if max(A,B)>AB:
      ans+=AB
    else:
      ans+=(AB//max(A,B))*max(A,B)
      ans+=(AB%max(A,B))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  while "AB" in word:
    count += 1
    word = word.replace("AB","X")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if ab == 0:
  print(min([a,b])+count)
else:
  if a == 0 and b == 0:
    print(ab-1+count)
  elif a == 0 or b == 0:
    print(ab+count)
  else:
    print(ab+1+min([a-1,b-1])+count)

Submission status: Wrong Answer

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

_a_cnt = 0
b__cnt = 0
b_a_cnt = 0
other_cnt = 0

ans = 0
for s in str_list:
    if s[0] == "B" and s[-1] == "A":
        b_a_cnt += 1
    elif s[-1] == "A":
        _a_cnt += 1
    elif s[0] == "B":
        b__cnt += 1
    else:
        other_cnt += 1
    ans += s.count("AB")

if _a_cnt > 0 and b__cnt > 0:
    ans += b_a_cnt + 1 + min(_a_cnt - 1, b__cnt - 1)
elif _a_cnt > 0 or b__cnt > 0:
    ans += b_a_cnt
else:
    ans += b_a_cnt - 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[-1] == "A":
        ca += 1
    elif tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    if tmp[-1] == "A":
        ca += 1
    if tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    if tmp[-1] == "A":
        ca += 1
    if tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if ca == cb == 0:
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
 
b_start = []
a_end = []
count = 0
dob = 0
for i in s:
  b_flag = False
  a_flag = False
  if "AB" in i:
    count += 1
  if i[0] == "B":
    b_start.append(i)
    b_flag = True
  if i[-1] == "A":
    a_end.append(i)
    a_flag = True
  if b_flag and a_flag:
    dob += 1
    
if dob == 0:
  count += min(len(b_start), len(a_end))
elif len(b_start) + len(a_end) > 0:
  count += min(len(b_start), len(a_end)) + dob
else:
  count += dob - 1
  
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1

if xa == bx:
	if xa == 0:
		print(ans+bxa-1)
	else:
		print(ans+xa+bxa)
else:
	print(ans+bxa+min(xa, bx))

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1

if xa == bx:
	if xa == 0:
		print(bxa-1)
	else:
		print(ans+xa+bxa)
else:
	print(ans+bxa+min(xa, bx))

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1

if xa == bx:
	print(ans+xa+bxa)
else:
	print(ans+bxa+min(xa, bx))

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
elif xax == 0 and bxx == 0:
	res1 += bxax-1
else:
	res1 += bxax
print(ans+res1)

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
if xax == 0 and bxx == 0:
	res1 += bxax-1
else:
	res1 += bxax
print(ans+res1)

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
else:
	res1 += bxax
print(ans+res1)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[]
ans=0
A=0
B=0
AB=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
        if L[i][-1]=="A" and L[i][0]=="B":
                AB += 1
if A>=B:
        ans+=B
        if AB==A:
                ans-=1
else:
        ans+=A
        if AB==B:
                ans-=1            
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
else:
	res1 += bxax
	#m = max(xax, bxx)
	#if bxax <= m:
	#	res1 += bxax
	#else:
	#	res1 += m + bxax - 1
	##print(ans)
res2 = bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		res2 += 1
	if bx > 0:
		bx -= 1
		res2 += 1
res2 += min(xa, bx)
print(ans+max(res1, res2))

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
ans += bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		ans += 1
	if bx > 0:
		bx -= 1
		ans += 1
ans += min(xa, bx)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
else:
	m = max(xax, bxx)
	if bxax <= m:
		res1 += bxax
	else:
		res1 += m + bxax - 1
	#print(ans)
res2 = bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		res2 += 1
	if bx > 0:
		bx -= 1
		res2 += 1
res2 += min(xa, bx)
print(ans+max(res1, res2))

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
res1 = num*2
bxax = bxa - num
xax = xa - num
bxx = bx - num
if bxax == 0:
	res1 += min(xax, bxx)
	#print(ans)
else:
	m = max(xax, bxx)
	if bxax <= m:
		res1 += bxax
	else:
		res1 += m + bxax - 1
	#print(ans)
res2 = bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		res2 += 1
	if bx > 0:
		bx -= 1
		res2 += 1
res2 += min(xa, bx)
print(ans+max(res1, res2))

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[]
ans=0
A=0
B=0
AB=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
        if L[i][-1]=="A" and L[i][0]=="B":
                AB += 1
if A>=B:
        ans+=B
        if AB==B:
                ans+=-1
else:
        ans+=A
        if AB==A:
                ans+=-1
print(ans)

Submission status: Wrong Answer

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,AB)*2
A-=min(A,B,AB)
B-=min(A,B,AB)
AB-=min(A,B,AB)


ans+=min(A,B)
tmp=abs(A-B)

if tmp == 0 and AB>0:
  ans+=AB-1
elif AB <= tmp:
  ans+=AB
else:
  ans+=AB//tmp*tmp
  ans+=AB%tmp
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
num = min(bxa, xa, bx)
ans += num*2
bxa -= num
xa -= num
bx -= num
if bxa == 0:
	ans += min(xa, bx)
	print(ans)
else:
	m = max(xa, bx)
	if bxa <= m:
		ans += bxa
	else:
		ans += m + bxa - 1
	print(ans)

Submission status: Wrong Answer

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,AB)*2
A-=min(A,B,AB)
B-=min(A,B,AB)
AB-=min(A,B,AB)


ans+=min(A,B)
tmp=abs(A-B)

if tmp == 0:
  ans+=AB-1
elif AB <= tmp:
  ans+=AB
else:
  ans+=AB//tmp*tmp
  ans+=AB%tmp
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ 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":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1
#print(xa, bx, bxa)
ans += bxa-1
if bxa > 0:
	if xa > 0:
		xa -= 1
		ans += 1
	if bx > 0:
		bx -= 1
		ans += 1
ans += min(xa, bx)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[]
ans=0
A=0
B=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
if A>=B:
        ans+=B
else:
        ans+=A
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
  n = int(input())
  s = [input() for _ in range(n)]
  ans = 0
  righta = 0
  leftb = 0
  double = 0
  
  for i in range(n):
    tmp = s[i]
    targetlength = len(tmp)
    if tmp[0] =='B':leftb += 1
    if tmp[-1] == 'A': righta += 1
    if tmp[0] =='B' and tmp[-1] =='A':
      leftb -= 1
      righta -= 1
      double += 1
    for j in range(targetlength-1):
      if tmp[j] =='A' and tmp[j+1] == 'B': ans += 1
  #print(leftb,righta,double,ans)
  for i in range(double):
    if righta >= leftb:
      leftb += 1
    else:
      righta += 1
  ans += double//2
  print(ans+min(righta,leftb))
solve()

Submission status: Wrong Answer

Solution code:
def solve():
  n = int(input())
  s = [input() for _ in range(n)]
  ans = 0
  righta = 0
  leftb = 0
  for i in range(n):
    tmp = s[i]
    targetlength = len(tmp)
    if tmp[0] =='B':leftb += 1
    if tmp[-1] == 'A': righta += 1
    
    for j in range(targetlength-1):
      if tmp[j] =='A' and tmp[j+1] == 'B': ans += 1
  print(ans+min(righta,leftb))
solve()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

i  = lambda: int(input())
s  = lambda: input()

b_start = 0
a_end = 0
count = 0
for _ in range(i()):
    ts = s()
    if ts[0] == 'B':
        b_start += 1
    if ts[-1] == 'A':
        a_end += 1
    count += ts.count('AB')

print(count + min([b_start, a_end]))

Submission status: Wrong Answer

Solution code:
n = int(input())
last_a = 0
first_b = 0
both = 0
ans = 0
for i in range(n):
  s = input()
  a, b = s[-1], s[0]
  if a == "A" and b == "B":
    both += 1
  elif a == "A":
    last_a += 1
  elif b == "B":
    first_b += 1
  for j in range(len(s)-1):
    if s[j] == "A" and s[j+1] == "B":
      ans += 1
if last_a == 0 and first_b == 0:
  ans += both
else:
  ans += min(last_a,first_b) + both
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
AB,B_A, _A,B_ = 0,0,0,0
for _ in range(n):
    s = input()
    AB +=s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        B_A +=1
    elif s[0] == 'B':
        B_ +=1
    elif s[-1] == 'A':
        _A +=1
print(AB + min(_A,B_) +min(B_A,abs(_A-B_)) )

Submission status: Wrong Answer

Solution code:
n = int(input())
last_a = 0
first_b = 0
both = 0
ans = 0
for i in range(n):
  s = input()
  a, b = s[-1], s[0]
  if a == "A" and b == "B":
    both += 1
  elif a == "A":
    last_a += 1
  elif b == "B":
    first_b += 1
  for j in range(len(s)-1):
    if s[j] == "A" and s[j+1] == "B":
      ans += 1
if last_a == 0 and first_b == 0:
  ans += both - 1
else:
  ans += min(last_a,first_b) + both
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
last_a = 0
first_b = 0
both = 0
ans = 0
for i in range(n):
  s = input()
  a, b = s[-1], s[0]
  if a == "A" and b == "B":
    both += 1
  elif a == "A":
    last_a += 1
  elif b == "B":
    first_b += 1
  for j in range(len(s)-2):
    if s[j] == "A" and s[j+1] == "B":
      ans += 1
if last_a == 0 and first_b == 0:
  ans += both - 1
else:
  ans += min(last_a,first_b) + both
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
if top >0:
    ans +=1
    top -=1
    MAX -=1
if fin >0:
    ans +=1
    fin -=1
    MAX -=1
ans += min(top,fin,MAX)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward + dbl-1
MAX = N-1 - (dbl-1)
if top >0:
    ans +=1
    top -=1
    MAX -=1
if fin >0:
    ans +=1
    fin -=1
    MAX -=1
ans += min(top,fin,MAX)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward + dbl-1
if top >0:
    ans += 1
    top -=1
if fin >0:
    ans += 1
    fin -= 1
ans += min(top,fin)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
inward = 0
top = 0
fin = 0

for n in range(N):
    s = input()
    for i in range(len(s)):
        if i == 0 and s[i] == 'B':
            fin += 1
        if i == len(s)-1:
            if s[i] == 'A':
                top += 1
            continue
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1


print(inward+min(top,fin,N-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
inward = 0
top = 0
fin = 0

for n in range(N):
    s = input()
    for i in range(len(s)):
        if i == 0 and s[i] == 'B':
            fin += 1
        if i == len(s)-1:
            if s[i] == 'A':
                top += 1
            continue
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1


print(inward+min(top,fin))

Submission status: Wrong Answer

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

ans = 0
for i in s:
	ans += i.count("AB")

# print(ans)

A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1

if AB == 0:
	ans = min(A,B)
else:
	ans = AB + min(A,B)
	if A+B == 0:
		ans -= 1

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in s:
	ans += i.count("AB")

# print(ans)

A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1

if AB == 0:
	ans = min(A,B)
else:
	if A+B == 0:
		ans = AB-1
	else:
		ans = AB + min(A,B)

print(ans)