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())
cnt=0

a,b,c=0,0,0
for i in range(n):
    s=input()
    cnt +=s.count("AB")
    if s[0]=="B" and s[-1]=="A":c +=1
    elif s[0]=="B":b +=1
    elif s[-1]=="A": a +=1

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

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0

a,b,c=0,0,0
for i in range(n):
    s=input()
    cnt +=s.count("AB")
    if s[0]=="B" and s[-1]=="A":c +=1
    elif s[0]=="B":b +=1
    elif s[-1]=="A": a +=1

if a==0 and b==0:print(cnt+min(0,c-1))
else:
    for i in range(c):
        a +=1
        b +=1
    print(cnt+min(a,b))

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0

a,b=0,0
for i in range(n):
    s=input()
    cnt +=s.count("AB")
    if s[0]=="B":b +=1
    if s[-1]=="A": a +=1

print(cnt+min(a,b))

Submission status: Wrong Answer

Solution code:
N = int(input())

prev_B = 0
back_A = 0
AB_num = 0
back_A_prev_B = 0
for i in range(N):
    s = input()
    if s[0] == "B":
        prev_B += 1
    if s[len(s)-1] == "A":
        back_A += 1
    if s[0] == "B" and s[len(s)-1] == "A":
        back_A_prev_B += 1
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            AB_num += 1

if back_A_prev_B == prev_B and back_A_prev_B == back_A:
    print(AB_num + min(prev_B-1, back_A-1))
else:
    print(AB_num + min(prev_B, back_A))

Submission status: Wrong Answer

Solution code:
N = int(input())

prev_B = 0
back_A = 0
AB_num = 0
for i in range(N):
    s = input()
    if s[0] == "B":
        prev_B += 1
    if s[len(s)-1] == "A":
        back_A += 1
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            AB_num += 1

print(AB_num + min(prev_B, back_A))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B,BA = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      BA += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans = 0
if A+B > 0:
  ans = BA + min(A,B)
elif  A+B == 0:
  ans = BA -1 + min(A,B)
print(count + ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B,BA = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      BA += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans = min(A,B)
if BA > 0:
  ans = max(A,B)-ans+BA-1 + min(A,B)
print(count + ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B,AB = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      AB += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans1 = min(A,B)
ans2 = (max(A,B) - ans1) + AB
ans = 0
if A-B == 0 and AB <= 1:
  ans = count + ans1
else:
  ans = count + ans1 + AB + (max(A,B) - ans1-1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B,AB = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      AB += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans1 = min(A,B)
ans2 = (max(A,B) - ans1)*AB

print(count + ans1 + ans2)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B,AB = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      AB += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans = min(A+AB,B+AB)
print(count + ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B = 0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A":
      A += 1
    if s[0] == "B":
      B += 1
ans = min(A,B)
print(count + ans)

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    cnt = 0
    ba = 0
    a = 0
    b = 0
    for s in S:
        for i in range(1,len(s)):
            if s[i - 1:i + 1] == 'AB':
                cnt += 1
        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[0] == 'B':
            b += 1
        elif s[-1] == 'A':
            a += 1
    
    if a > 0 and b > 0:
        cnt += ba + 1 + min(a - 1,b - 1)
    elif a > 0:
        cnt += ba
    elif b > 0:
        cnt += ba
    else:
        cnt += ba - 1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += st.count('AB')
    if st.endswith('A'):
        if st.startswith('B'):
            bxxxa += 1
        else:
            xxxa += 1
    elif st.startswith('B'):
        bxxx += 1

while min(xxxa,bxxx,bxxxa) > 0:
    xxxa -= 1
    bxxx -= 1
    bxxxa -= 1
    count += 2

count += bxxxa
count += min(xxxa,bxxx)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += st.count('AB')
    if st.endswith('A'):
        if st.startswith('B'):
            bxxxa += 1
        else:
            xxxa += 1
    elif st.startswith('B'):
        bxxx += 1

while min(xxxa,bxxx,bxxxa) > 0:
    xxxa -= 1
    bxxx -= 1
    bxxxa -= 1
    count += 2

if bxxxa == 0:
    count += min(xxxa,bxxx)
else:
    count += bxxxa

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += st.count('AB')
    if st.endswith('A'):
        if st.startswith('B'):
            bxxxa += 1
        else:
            xxxa += 1
    elif st.startswith('B'):
        bxxx += 1

while min(xxxa,bxxx,bxxxa) > 0:
    xxxa -= 1
    bxxx -= 1
    bxxxa -= 1
    count += 2

if bxxxa == 0:
    count += min(xxxa,bxxx)
else:
    count += bxxxa - 1
    count += min(xxxa+bxxx,1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += 1
    if st.endswith('A'):
        if st.startswith('B'):
            bxxxa += 1
        else:
            xxxa += 1
    elif st.startswith('B'):
        bxxx += 1

while min(xxxa,bxxx,bxxxa) > 0:
    xxxa -= 1
    bxxx -= 1
    bxxxa -= 1
    count += 2

if bxxxa == 0:
    count += min(xxxa,bxxx)
else:
    count += bxxxa - 1
    count += min(xxxa+bxxx,1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += 1
    if st.endswith('A'):
        if st.startswith('B'):
            bxxxa += 1
        else:
            xxxa += 1
    elif st.startswith('B'):
        bxxx += 1

while xxxa * bxxx * bxxxa > 0:
    xxxa -= 1
    bxxx -= 1
    bxxxa -= 1
    count += 2

if bxxxa == 0:
    count += min(xxxa,bxxx)
else:
    count += bxxxa - 1
    count += min(xxxa+bxxx,1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_l = [input() for _ in range(n)]

count = 0
xxxa = 0
bxxx = 0
# bxxxa = 0
for st in s_l:
    if 'AB' in st:
        count += 1
    if st.endswith('A'):
#        if st.startswith('B'):
#            bxxxa += 1
#        else:
        xxxa += 1
    if st.startswith('B'):
        bxxx += 1

count += min(xxxa,bxxx)
print(count)

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
    elif si[0] == "B":
        b += 1
    elif si[-1] == "A":
        a += 1
print(ans + max(ab-1, 0)) if a == b == 0 else print(ans +  min(a, b))

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
print(ans + max(ab-1, 0)) if a == b == 0 else print(ans +  min(a, b))

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
print(ans + min(ab-1, 0)) if a == b == 0 else print(ans +  min(a, b))

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
print(ans + max(ab-1, 0)) if a == b == 0 else print(ans + ab + min(a, b))

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ab = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
    if si[0] == "B" and si[-1] == "A":
        ab += 1
print(ans + min(ab-1, 0)) if a == b == 0 else print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:
s = [input() for _ in range(int(input()))]
a = b = ans = 0
for si in s:
    if si[0] == "B":
        b += 1
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            ans += 1
    if si[-1] == "A":
        a += 1
print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]
ans=sum([si.count('AB') for si in s])
endA=len([si for si in s if si[-1]=='A'])
startB=len([si for si in s if si[0]=='B'])
AB=len([si for si in s if si[0]=='B' and si[-1]=='A'])
if AB==endA and AB==startB:
      print(max(0,ans+AB-1))
else:
      print(ans+min(endA,startB))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]
ans=sum([si.count('AB') for si in s])
endA=len([si for si in s if si[-1]=='A'])
startB=len([si for si in s if si[0]=='B'])
AB=len([si for si in s if si[0]=='B' and si[-1]=='A'])
if AB==endA and AB==startB:
      print(ans+AB-1)
else:
      print(ans+min(endA,startB))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]
ans=sum([si.count('AB') for si in s])
endA=len([si for si in s if si[-1]=='A'])
startB=len([si for si in s if si[0]=='B'])
print(ans+min(endA,startB))

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    s = []
    ans = 0
    acnt = 0
    bcnt = 0
    bacnt = 0
    for i in range(n):
        s_i = input()
        ans += s_i.count('AB')
        s_i.replace('AB', '')
        if s_i[0] == 'B' and s_i[-1] == 'A':
            bacnt += 1
        elif s_i[0] == 'B':
            bcnt += 1
        elif s_i[-1] == 'A':
            acnt += 1

    # bacntをa,bに変換
    if acnt > bcnt:
        acnt,bcnt = bcnt,acnt

    if bcnt-acnt > bacnt:
        acnt += bacnt
        ans += bacnt
    else:
        ans += (bacnt-1)
        bacnt -= (bcnt-acnt)
        acnt += (bcnt-acnt)
        acnt += bacnt // 2
        bcnt += bacnt // 2

    print(ans + min(acnt, bcnt))

solve()

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    s = []
    ans = 0
    acnt=0
    bcnt=0
    for i in range(n):
        s_i = input()
        ans += s_i.count('AB')
        s_i.replace('AB', '')
        if s_i[0] == 'B':
            bcnt+=1
        if s_i[-1] == 'A':
            acnt += 1
    print(ans + min(acnt, bcnt))

solve()

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[0,0,0,0]#non,~a,b~,b~a
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    l[2*int(s[0]=="B")+1*int(s[-1]=="A")]+=1

if l[-1]:
    ans+=l[-1]//2
    if l[1]:
        l[1]-=1
        ans+=1
    if l[2]:
        l[2]-=1
        ans+=1
print(ans+min(l[1:3]))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
# sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


# show_flg = True
show_flg = False


def main():
    N = I()
    s = []

    for i in range(N):
        s.append(S())

    ans = 0
    a = 0
    b = 0
    dup = 0

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

    if dup == max(a, b):
        # print(dup, a, b)
        print(ans + dup - 1)
    else:
        print(ans + min(a, b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0
c=0

for i in range(n):
    s = ns()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1

ans += min(n-1,a,b)+c
if a+b == 0 and n > 1:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0
c=0

for i in range(n):
    s = ns()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s= list(input() for i in range(n))

acnt =0
bcnt =0
ecnt =0
cnt=0
nomal =0

for i in range(n):
    nomal += s[i].count('AB')
    t=0
    if s[i][0] == 'B':
        bcnt += 1
        cnt+=1
        t+=1
    if s[i][-1] == 'A':
        acnt += 1
        if not t:
            cnt+=1
        t+=1
    if t==2 :
        ecnt+=1

if cnt == ecnt:
    print(nomal + acnt-1)
else:
    print(nomal + min(acnt,bcnt))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

n = int(input())

strings = []
for _ in range(n):
    strings.append(input())

ab = 0 # ABが内部にあるもの
tail_a = 0 # 末尾がAのもの
head_b = 0 # 先頭がBのもの
both = 0
for s in strings:
    ab += s.count('AB')
    if s[-1] == 'A':
        tail_a += 1
    if s[0] == 'B':
        head_b += 1
    if s[-1] == 'A' and s[0] == 'B':
        both += 1
ans = ab 
if not(both == 1 and tail_a == 1 and head_b == 1):
    ans += min(tail_a, head_b)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

n = int(input())

strings = []
for _ in range(n):
    strings.append(input())

ab = 0 # ABが内部にあるもの
tail_a = 0 # 末尾がAのもの
head_b = 0 # 先頭がBのもの
for s in strings:
    ab += s.count('AB')
    if s[-1] == 'A':
        tail_a += 1
    if s[0] == 'B':
        head_b += 1
ans = ab + min(tail_a, head_b)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

n = int(input())

strings = []
for _ in range(n):
    strings.append(input())

ab = 0 # ABが内部にあるもの
tail_a = 0 # 末尾がAのもの
head_b = 0 # 先頭がBのもの
for s in strings:
    if 'AB' in s:
        ab += 1
    if s[-1] == 'A':
        tail_a += 1
    if s[0] == 'B':
        head_b += 1
ans = ab + min(tail_a, head_b)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
ans=0
ab=a=b=0
for ss in s:
  ans+=ss.count("AB")
  if ss[0]=="B" and ss[-1]=="A":
    ab+=1
  elif ss[0]=="B" and ss[-1]!="A":
    b+=1
  elif ss[0]!="B" and ss[-1]=="A":
    a+=1
# print(ans)
cnt_min=min(a,b)
ans+=cnt_min
a-=cnt_min
b-=cnt_min
cnt_min=min(ab,a+b)
ans+=cnt_min
if ab>=2:
  ans+=1
ab-=cnt_min
ans+=ab//2
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
su=0
w1=0
w2=0
w3=0
for i in range(n):
    s=input()
    for i in range(len(s)-1):
        if(s[i] in 'A' and s[i+1] in 'B'):
            su+=1
    if(s[0] in 'B' and s[-1] not in 'A'):
        w1+=1
    elif(s[-1] in 'A' and s[0] not in 'B'):
        w2+=1
    elif(s[0] in 'B' and s[-1] in 'A'):
        w3+=1
ww1=w1
ww2=w2
ww3=w3
sm=su
m=min(w1,w2,w3)
su+=(2*m)
w1-=m
w2-=m
w3-=m
if(w3==0):
    su+=min(w1,w2)
else:
    if(max(w1,w2)>0):
        su+=w3
    else:
        su+=w3-1

if(ww3>=2):
    sm+=(ww3-1)
    ww3=1
sm+=min(ww1,ww2)
if(ww3!=0 and ww1!=ww2):
    sm+=1

print(max(su,sm))

Submission status: Wrong Answer

Solution code:
n=int(input())
su=0
w1=0
w2=0
w3=0
for i in range(n):
    s=input()
    for i in range(len(s)-1):
        if(s[i] in 'A' and s[i+1] in 'B'):
            su+=1
    if(s[0] in 'B' and s[-1] not in 'A'):
        w1+=1
    elif(s[-1] in 'A' and s[0] not in 'B'):
        w2+=1
    elif(s[0] in 'B' and s[-1] in 'A'):
        w3+=1
m=min(w1,w2,w3)
su+=(2*m)
w1-=m
w2-=m
w3-=m
if(w3==0):
    su+=min(w1,w2)
else:
    if(max(w1,w2)>0):
        su+=w3
    else:
        su+=w3-1
'''
if(w3>=2):
    su+=(w3-1)
    w3=1
su+=min(w1,w2)
if(w3!=0 and w1!=w2):
    su+=1
'''
print(su)

Submission status: Wrong Answer

Solution code:
n=int(input())
su=0
w1=0
w2=0
w3=0
for i in range(n):
    s=input()
    for i in range(len(s)-1):
        if(s[i] in 'A' and s[i+1] in 'B'):
            su+=1
    if(s[0] in 'B' and s[-1] not in 'A'):
        w1+=1
    elif(s[-1] in 'A' and s[0] not in 'B'):
        w2+=1
    elif(s[0] in 'B' and s[-1] in 'A'):
        w3+=1
if(w3>=2):
    su+=w3-1
    w3=1

su+=min(w1,w2)
if(w3!=0 and w1!=w2):
    su+=1
print(su)

Submission status: Wrong Answer

Solution code:
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    N = II()
    S = [0]*N

    numA = 0
    numB = 0
    ans = 0
    for i in range(N):
        s = input()
        ans += s.count('AB')
        if s[-1] == 'A':
            numA += 1
        if s[0] == 'B':
            numB += 1
    print(ans + min(numA,numB))


        

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    S = list(input() for _ in range(n))

    cnt_AB = 0
    cnt_A = 0
    cnt_B = 0
    res = 0
    for s in S:
        res += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            cnt_AB += 1
        elif s[0] == "B":
            cnt_B += 1
        elif s[-1] == "A":
            cnt_A += 1

    if cnt_AB > 1:
        res += cnt_AB - 1
        cnt_AB = 1

    if cnt_AB > 0 and cnt_A > 0 and cnt_A > 0:
        res += 2
        cnt_A -= 1
        cnt_B -= 1

    print(res + min(cnt_A, cnt_B))



if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    S = list(input() for _ in range(n))

    cnt_AB = 0
    cnt_A = 0
    cnt_B = 0
    res = 0
    for s in S:
        if "AB" in s:
            res += 1
        if s[0] == "B" and s[-1] == "A":
            cnt_AB += 1
        elif s[0] == "B":
            cnt_B += 1
        elif s[-1] == "A":
            cnt_A += 1

    if cnt_AB == 0:
        print(res + min(cnt_A, cnt_B))
    else:
        if cnt_A + cnt_B > 0:
            print(res + cnt_AB + min(cnt_A, cnt_B))
        else:
            print(res + cnt_AB - 1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    S = list(input() for _ in range(n))

    cnt_AB = 0
    cnt_A = 0
    cnt_B = 0
    res = 0
    for s in S:
        if "AB" in s:
            res += 1
        if s[0] == "B" and s[-1] == "A":
            cnt_AB += 1
        elif s[0] == "B":
            cnt_B += 1
        elif s[-1] == "A":
            cnt_A += 1

    if cnt_AB > 1:
        res += cnt_AB - 1
        cnt_AB = 1

    if cnt_AB > 0 and cnt_A > 0 and cnt_A > 0:
        res += 2
        cnt_A -= 1
        cnt_B -= 1

    print(res + min(cnt_A, cnt_B))



if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
ab=0
ans=0
for i in range(n):
    s=input()
    m=len(s)
    m-=len(s.replace("AB",""))
    m//=2
    ans+=m
    if s[0]=="B" and s[-1]=="A":
        ab+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
if abs(a-b)>0:
    print(ans+min(a,b)+ab)
else:
    print(ans+a+max(ab-1,0))

Submission status: Wrong Answer

Solution code:
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == '__main__':
    N = int(input())
    S = [input() for _ in range(N)]

    cnt = 0
    cnt_xa = 0
    cnt_bx = 0
    cnt_ba = 0
    for s in S:
        for i in range(0, len(s) - 1):
            if s[i] == 'A' and s[i + 1] == 'B':
                cnt += 1
        if 'B' != s[0] and 'A' == s[-1]:
            cnt_xa += 1
        if 'B' == s[0] and 'A' != s[-1]:
            cnt_bx += 1
        if 'B' == s[0] and 'A' == s[-1]:
            cnt_ba += 1

    cnt += max(cnt_ba - 1, 0)
    cnt_ba = 1 if cnt_ba > 0 else 0
    ans1 = min(cnt_xa, cnt_bx) + \
        min((max(cnt_xa, cnt_bx) - min(cnt_xa, cnt_bx)), cnt_ba)
    ans2 = min(cnt_xa, cnt_ba) + \
        min((max(cnt_xa, cnt_ba) - min(cnt_xa, cnt_ba)), cnt_bx)
    ans3 = min(cnt_bx, cnt_ba) + \
        min((max(cnt_bx, cnt_ba) - min(cnt_bx, cnt_ba)), cnt_xa)

    print(cnt + max(ans1, ans2, ans3))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input()for i in range(n)]
ans = 0
resa = 0
resb = 0
for x in s:
    for i in range(len(x)-1):
        if x[i:i+2] == 'AB':ans +=1
    if x[-1] == 'A':resa +=1
    if x[0] == 'B':resb +=1
print(ans+resa+resb)

Submission status: Wrong Answer

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

ans = 0
a = 0
b = 0
c = 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' and s[0]=='B':
    c += 1
  elif s[-1]=='A':
    a += 1
  elif s[0]=='B':
    b += 1
if c!=0:
  ans += c-1
  c = 1
if a==b:
  ans += a
elif a<b:
  ans += a
  ans += min(b-a,c)
else:
  ans += b
  ans += min(a-b,c)
print(ans)

Submission status: Wrong Answer

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

ans = 0
a = 0
b = 0
c = 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' and s[0]=='B':
    c += 1
  elif s[-1]=='A':
    a += 1
  elif s[0]=='B':
    b += 1
ans += c-1
c = 1
if a==b:
  ans += a
elif a<b:
  ans += a
  ans += min(b-a,c)
else:
  ans += b
  ans += min(a-b,c)
print(ans)

Submission status: Wrong Answer

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

Solution code:

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

hoge  = ""
PHASEA = 0
PHASEB = 1
phase  = 0
while ba != 0 or xa != 0 or bx != 0:
    if phase % 2 == PHASEA:
        if xa > 0:
            xa  -= 1
            hoge += "XA"
        elif ba > 0 :
            ba -= 1
            hoge += "BA"
    else:
        if bx > 0:
            bx  -= 1
            hoge += "BX"
        elif ba > 0 :
            ba -= 1
            hoge += "BA"
    phase += 1
print(hoge.count("AB") + ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
b = []
ans = ""
key = [False]*n
for i in range(n):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    ans = ans+s
  elif s[0] == "B":
    b.append(s)
  elif s[-1] == "A":
    a.append(s)
  else:
    key[i] = s

for i in range(max([len(a), len(b)])):
  if i == 0:
    if a:
      ans = a[i]+ans
    if b:
      ans = ans+b[i]
    continue
  if i >= len(a) or i >= len(b):
    break
  ans = ans+a[i]+b[i]
  
for i in range(n):
  if key[i]:
    ans = ans + key[i]
answer = 0
for i in range(len(ans)-1):
  if ans[i:i+2] == "AB":
    answer += 1
print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ans = ""
key = [False]*n
f, b = False, False
ans_f, ans_b = "", ""
for i in range(n):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    ans = ans+s
  elif s[0] == "B" and not f:
    f = True
    ans_f = s
  elif s[-1] == "A" and not b:
    b = True
    ans_b = s
  else:
    key[i] = s

ans = ans_b+ans+ans_f
for i in range(n):
  if key[i]:
    ans = ans + key[i]
answer = 0
for i in range(len(ans)-1):
  if ans[i:i+2] == "AB":
    answer += 1
print(answer)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ab = 0
lastA = 0
onlylastA = 0
firstB = 0
onlyfirstB = 0
for s in S:
    ab += s.count('AB')
    if s[-1] == 'A' and s[0] != 'B':
        lastA += 1
        onlylastA += 1
    if s[0] == 'B' and s[-1] != 'A':
        firstB += 1
        onlyfirstB += 1
    if s[-1] == 'A' and s[0] == 'B':
        lastA += 1
        firstB += 1

ans = ab + min(lastA,firstB)
if onlylastA == 0 and onlyfirstB == 0 and lastA == firstB:
    ans = max(ans-1,0)
print(ans)

Submission status: Wrong Answer

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

ab = 0
lastA = 0
onlylastA = 0
firstB = 0
onlyfirstB = 0
for s in S:
    ab += s.count('AB')
    if s[-1] == 'A' and s[0] != 'B':
        lastA += 1
        onlylastA += 1
    if s[0] == 'B' and s[-1] != 'A':
        firstB += 1
        onlyfirstB += 1
    if s[-1] == 'A' and s[0] == 'B':
        lastA += 1
        firstB += 1

ans = ab + min(lastA,firstB)
if onlylastA == 0 and onlyfirstB == 0:
    ans = max(ans-1,0)
print(ans)

Submission status: Wrong Answer

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

ab = 0
lastA = 0
firstB = 0
for s in S:
    ab += s.count('AB')
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B':
        firstB += 1
ans = ab + min(lastA,firstB,N-1)
print(ans)

Submission status: Wrong Answer

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

ab = 0
lastA = 0
firstB = 0
for s in S:
    ab += s.count('AB')
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B':
        firstB += 1
ans = ab + min(lastA,firstB)
print(ans)

Submission status: Wrong Answer

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

ab = 0
lastA = 0
firstB = 0
for s in S:
    if 'AB' in s:
        ab += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B':
        firstB += 1
ans = ab + min(lastA,firstB)
print(ans)