Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
import sys
import operator
n = int(input())
l = list(map(int,input().split()))
a = reduce(operator.mul,l)
if a > 10**18:
    print("-1")
    sys.exit()
print(a)

Fixed submission:
import sys
n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 1
for i in l:
    count= count*i
    if count == 0:
        print("0")
        sys.exit()
    elif count > 10**18:
        print("-1")
        sys.exit()
if count > 10**18:
    print("-1")
    sys.exit()
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
l = list(map(int,input().split()))
count = 1
for i in l:
    count= count*i
    if i == 0:
        print("0")
        sys.exit()
if count > 10**18:
    print("-1")
    sys.exit()
print(count)

Fixed submission:
import sys
n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 1
for i in l:
    count= count*i
    if count == 0:
        print("0")
        sys.exit()
    elif count > 10**18:
        print("-1")
        sys.exit()
if count > 10**18:
    print("-1")
    sys.exit()
print(count)
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
import itertools

grid = []
for i in range(2*k):
    array = list(map(int, input().strip().split()))
    grid.append(array)
for j in range (k):
    l.append(grid[0+2*j+1])
lis = list(itertools.chain.from_iterable(l))
count = 0
for m in range(n):
    if m+1 in lis:
        count = count +1
print(n - count)

Fixed submission:
n,k = map(int,input().split())
import itertools

grid = []
for i in range(2*k):
    array = list(map(int, input().split()))
    grid.append(array)

l = []
for j in range (k):
    l.append(grid[0+2*j+1])

lis = list(itertools.chain.from_iterable(l))

count = 0
for m in range(n):
    if m+1 in lis:
        count = count +1
print(n - count)
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
for i in range(30):
    if n >= k**(i-1) and n < k**i:
        print(i)

Fixed submission:
n,r = map(int,input().split())
if n>=10:
    print(r)
else:
    print(r+100*(10-n))
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
l = list(map(int,input().split()))
if n <= k:
    print("0")
    sys.exit()
l = sorted(l,reverse = True)
for i in range(k):
    l.remove(l[0])
d = dict.fromkeys(l)
a = 0
for j in d.keys():
    a += j
print(a)

Fixed submission:
import sys
n,k = map(int,input().split())
l = list(map(int,input().split()))
if n <= k:
    print("0")
    sys.exit()
l = sorted(l,reverse = True)
hissatu = 0
for i in range(k):
    hissatu += l[i]
total = 0
for j in l:
    total += j
print(total-hissatu)

Buggy python submission (verdict: Runtime Error):
import sys
n,k = map(int,input().split())
l = list(map(int,input().split()))
if n <= k:
    print("0")
    sys.exit()
l = sorted(l,reverse = True)
for i in range(k):
    l.remove(l[i])
a = 0
for j in l:
    a += j
print(a)

Fixed submission:
import sys
n,k = map(int,input().split())
l = list(map(int,input().split()))
if n <= k:
    print("0")
    sys.exit()
l = sorted(l,reverse = True)
hissatu = 0
for i in range(k):
    hissatu += l[i]
total = 0
for j in l:
    total += j
print(total-hissatu)
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
from fractions import gcd
c = math.gcd(a,b)
answer = a*b/c
print(int(answer))

Fixed submission:
a,b = map(int,input().split())
from fractions import gcd
c = gcd(a,b)
answer = a*b/c
print(int(answer))

Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
from math import gcd
c = math.gcd(a,b)
answer = a*b/c
print(int(answer))

Fixed submission:
a,b = map(int,input().split())
from fractions import gcd
c = gcd(a,b)
answer = a*b/c
print(int(answer))

Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
import math
c = math.gcd(a,b)
answer = a*b/c
print(int(answer))

Fixed submission:
a,b = map(int,input().split())
from fractions import gcd
c = gcd(a,b)
answer = a*b/c
print(int(answer))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split()))
count = 0
for i in range(n):
    a = l[i]
    for j in range(i+1,n):
        b = l[j]
        for k in range(j+1,n):
            c = l[k]
            if a+b>c and a+c>b and b+c>a:
                count += 1
                
print(count)

Fixed submission:
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        maxc = l[i]+l[j] #短い2辺の和
        index = bisect.bisect_left(l, maxc)
        if index > j:
            count += index-j-1
print(count)
Buggy python submission (verdict: Runtime Error):
import sys
a,b=map(int,input().split())
if b==1:
  print("0")
  sys.exit()
p=1
ans=0
for i in range(0,20):
  p+=a-1
  ans+=1
  if p>=b:
    print(ans)
    sys.exit()

Fixed submission:
s=input()
t=input()
count=0
for i in range(3):
  if s[i]==t[i]:
    count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
import sys
a,b=map(int,input().split())
if b==1:
  print("0")
  sus.exit()
p=1
ans=0
for i in range(0,20):
  p+=a-1
  ans+=1
  if p>=b:
    print(ans)
    sys.exit()

Fixed submission:
import sys
a,b=map(int,input().split())
if b==1:
  print("0")
  sys.exit()
p=1
ans=0
for i in range(0,20):
  p+=a-1
  ans+=1
  if p>=b:
    print(ans)
    sys.exit()

Buggy python submission (verdict: Runtime Error):
import sys
a,b=map(int,input().split())
if b=1:
  print("0")
  sus.exit()
p=1
ans=0
for i in range(0,20):
  p+=a-1
  ans+=1
  if p>=b:
    print(ans)
    sys.exit()

Fixed submission:
import sys
a,b=map(int,input().split())
if b==1:
  print("0")
  sys.exit()
p=1
ans=0
for i in range(0,20):
  p+=a-1
  ans+=1
  if p>=b:
    print(ans)
    sys.exit()
Buggy python submission (verdict: Runtime Error):
n=int(input())
l=list(map(int,input().split()))
l.sort()
a=n/2
s=l.index(a-1)
t=l.index(a)
if s==t:
  print("0")
else:
  ans=t-s
  print(ans)

Fixed submission:
n=int(input())
l=list(map(int,input().split()))
l.sort()
a=int(n/2)
s=l[a-1]
t=l[a]
if s==t:
  print("0")
else:
  ans=t-s
  print(ans)
Buggy python submission (verdict: Runtime Error):
x,y=map(int,input().split())
printint((x+y/2))

Fixed submission:
x,y=map(int,input().split())
print(int(x+y/2))
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=int(input())
b=int(input())
if n==1:
  print("Hello World")
else:
  print(a+b)

Fixed submission:
n=int(input())
if n==1:
   print("Hello World")
else:
   a=int(input())
   b=int(input())
   print(a+b)
Buggy python submission (verdict: Runtime Error):
import math

n,d = map(int,input().split())
xy = [list(map(float,input().split())) for i in range(n)]
count = 0
for i in range(n):
  for j in range(i+1,n):
    dis=0
    for k in range(d):
      dis += (xy[i][k]-xy[j][k])**2
    dis = math.sqrt(dis)
    if dis.is_integer():
      count +=1
      
print(count)

Fixed submission:
n,a,b = map(int,input().split())
if n*a>b:
  print(b)
else:
  print(a*n)

Buggy python submission (verdict: Runtime Error):
import math

n,d = map(int,input().split())
xy = [list(map(float,input().split())) for i in range(n)]
count = 0
for i in range(n):
  for j in range(i+1,n):
    dis=0
    for k in range(d):
      dis += (xy[i][k]-xy[j][k])**2
    dis = math.sqrt(dis)
    if dis.is_integer():
      count +=1
      
print(count)

Fixed submission:
n,a,b = map(int,input().split())
if n*a>b:
  print(b)
else:
  print(a*n)
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    n=int(input())
    r=range
    a=list(map(int, input().split()))
    q=int(input())
    
    for _ in r(q):
        bi, ci = map(int, input().split())
        c=0
        for i in r(n):
            if a[i] == bi:
                a[i]=ci
            c+=a[i]
        print(c)


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()


Fixed submission:
def solve():
    n=int(input())
    r=range
    a=list(map(int, input().split()))
    q=int(input())

    d={}
    s=0
    for i in r(n):
        cu=a[i]

        if d.get(cu, None):
            d[cu]+=1
        else:
            d[cu]=1
        
        s+=cu
    
    for _ in r(q):
        bi, ci = map(int, input().split())
        diff=ci-bi

        if d.get(ci, None):
            d[ci]+=d.get(bi, 0)
        else:
            d[ci]=d.get(bi, 0)

        s+=diff * d.get(bi, 0)
        print(s)

        if d.get(bi, None):
            d[bi]=0


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-

def solve():
    K = int(input())
    a = 7%K
    i = 1
    Kset = set()
    Kset.add(a)
    while a!=0:
        a = (a*10+7)%K
        i += 1
        if a in Kset:
            print(-1)
            return
    print(i)


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    K = int(input())
    a = 7%K
    i = 1
    Kset = set()
    Kset.add(a)
    while a!=0:
        a = (a*10+7)%K
        i += 1
        if a in Kset:
            print(-1)
            return
        Kset.add(a)
    print(i)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    As.sort()

    setA = set(As)
    setDel = set()

    cands = []

    for a in As:
        if a in setDel:
            continue

        i = 1
        while a*i <= As[-1]:
            if a*i in setA:
                setDel.add(a*i)
            i += 1

        cands.append(a)

    # 重複する数字を調べる
    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        elif dic[a] == 1:
            cands.remove(a)
        dic[a] += 1

    print(len(cands))


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    As.sort()

    setA = set(As)
    setDel = set()

    cands = []

    for a in As:
        if a in setDel:
            continue

        i = 1
        while a*i <= As[-1]:
            if a*i in setA:
                setDel.add(a*i)
            i += 1

        cands.append(a)

    # 重複する数字を調べる
    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    ans = 0
    for x in cands:
        if dic[x] > 1:
            continue
        ans += 1

    print(ans)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    S: string = input()

    """
    3桁の数字がijkだとして、それを作れるかを全探索で数えていく
    平均計算量は、(3750*10)*(7500*10)*(15000*10) = O(10**4 * 10**4 * 10**5) = O(10**13)
    """

    ans = 0
    for i in range(0, 10):
        idx = S.find(str(i))  # 平均計算量15000
        if idx == -1:
            continue
        for j in range(0, 10):
            jdx = S[idx+1:].find(str(j))  # 平均計算量7500
            if jdx == -1:
                continue
            for k in range(0, 10):
                if str(k) in S[idx+1+jdx+1:]:  # 平均計算量3750
                    ans += 1

    print(ans)


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    S = input()

    """
    3桁の数字がijkだとして、それを作れるかを全探索で数えていく
    平均計算量は、(3750*10)*(7500*10)*(15000*10) = O(10**4 * 10**4 * 10**5) = O(10**13)
    """

    ans = 0
    for i in range(0, 10):
        idx = S.find(str(i))  # 平均計算量15000
        if idx == -1:
            continue
        for j in range(0, 10):
            jdx = S[idx+1:].find(str(j))  # 平均計算量7500
            if jdx == -1:
                continue
            for k in range(0, 10):
                if str(k) in S[idx+1+jdx+1:]:  # 平均計算量3750
                    ans += 1

    print(ans)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def solve():
    r = input()
    print(r*r)

if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    r = int(input())
    print(r*r)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-
import bisect

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()

    # O(N^2 * logN)の全探索で解く
    ans = 0
    for a_i in range(N):
        for b_i in range(a+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-
import bisect

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()

    # O(N^2 * logN)の全探索で解く
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def is_onaji(S):
    pre_moji = S[0]
    for i in range(1, len(S)):
        if S[i] == pre_moji:
            pre_moji = S[i]
            continue
        else:
            return False
    return True


def solve():
    S = input()
    K = int(input())
    N = len(S)

    if N == 1:
        print(K//2)
        return

    # 1回目
    base1 = 0
    pre_moji = S[0]
    for i in range(1, N):
        if S[i] == pre_moji:
            base1 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 1:
        print(base1)
        return

    # 2回目
    base2 = 0
    for i in range(0, N):
        if S[i] = pre_moji:
            base2 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 2:
        print(base1+base2)

    # 3回目
    base3 = 0
    for i in range(0, N):
        if S[i] = pre_moji:
            base3 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 3:
        print(base1+base2+base3)

    if base1 == base2 and base2 == base3:
        print(base1*K)
    elif base1 != base2 and base2 == base3:
        print(base1 + base2*(K-1))
    else:
        # base1とbase2が交互にくる
        print(base1*((K+1)//2) + base2*(K//2))


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def is_onaji(S):
    pre_moji = S[0]
    for i in range(1, len(S)):
        if S[i] == pre_moji:
            pre_moji = S[i]
            continue
        else:
            return False
    return True


def solve():
    S = input()
    K = int(input())
    N = len(S)

    if N == 1:
        print(K//2)
        return

    # 1回目
    base1 = 0
    pre_moji = S[0]
    for i in range(1, N):
        if S[i] == pre_moji:
            base1 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 1:
        print(base1)
        return

    # 2回目
    base2 = 0
    for i in range(0, N):
        if S[i] == pre_moji:
            base2 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 2:
        print(base1+base2)
        return

    # 3回目
    base3 = 0
    for i in range(0, N):
        if S[i] == pre_moji:
            base3 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 3:
        print(base1+base2+base3)
        return

    if base1 == base2 and base2 == base3:
        print(base1*K)
    elif base1 != base2 and base2 == base3:
        print(base1 + base2*(K-1))
    else:
        # base1とbase2が交互にくる
        print(base1*((K+1)//2) + base2*(K//2))


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
"""解説
dp

dp[i][j] := 先頭i文字として考えられるもののうち、13で割ったあまりがjであるものの数

1?2?3 を分解すると、
10000 ≡ 3
 A000 ≡ A*(10**3) % MOD ≡ A*12
  200 ≡ 5
   B0 ≡ B*(10**1) % MOD
    3 ≡ 3
    -----
        11 + ? ≡ 11 + A*12 + B*10

"""


def solve():
    S = input()
    MOD = 10**9+7

    N = len(S)
    dp = [[0 for i in range(13)] for j in range(10**5+1)]
    dp[0][0] = 1

    for i in range(0, N):
        if S[i] == "?":
            c = -1
        else:
            c = int(S[i])
        
        for j in range(0, 10):
            if c != -1 and c != j: continue
            
            for ki in range(0, 13):
                dp[i+1][(ki* 10 + j)%13] += dp[i][ki]
        
        for j in range(0, 13):
            dp[i+1][j] %= MOD

    res = dp[N][5]
    print(res)


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-
"""解説
dp

dp[i][j] := 先頭i文字として考えられるもののうち、13で割ったあまりがjであるものの数

1?2?3 を分解すると、
10000 ≡ 3
 A000 ≡ A*(10**3) % MOD ≡ A*12
  200 ≡ 5
   B0 ≡ B*(10**1) % MOD
    3 ≡ 3
    -----
        11 + ? ≡ 11 + A*12 + B*10

"""


def solve():
    S = input()
    MOD = 10**9+7

    N = len(S)
    dp = [[0 for i in range(13)] for j in range(10**5+1)]
    dp[0][0] = 1

    for i in range(0, N):
        if S[i] == "?":
            c = -1
        else:
            c = int(S[i])
        
        for j in range(0, 10):
            if c != -1 and c != j: continue
            
            for ki in range(0, 13):
                dp[i+1][(ki* 10 + j)%13] += dp[i][ki]
        
        for j in range(0, 13):
            dp[i+1][j] %= MOD

    res = dp[N][5]
    print(res)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-
import sys
from math import gcd

def f(a, x):
    """1以上a以下で、xで割り切れるものの数"""
    return a // x

def lcm(a, b):
    """ aとbの最小公倍数を返す """
    return a*b // gcd(a, b)

def solve():
    A, B, C, D = list(map(int, sys.stdin.readline().split()))
    A -= 1

    # CでもDでも割り切れないもの
    b = B - f(B, C) - f(B, D) + f(B, lcm(C, D))
    a = A - f(A, C) - f(A, D) + f(A, lcm(C, D))
    print(b-a)


if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-
"""解説
B以下の整数のうち、CでもDでも割り切れないものの個数をCountBとする
A以下の整数のうち、CでもDでも割り切れないものの個数をCountAとする

求める答えは、CountB - CountA

・CountBの求め方
B以下の整数のうち、Cで割り切れるものの個数はCountB_C = B//C
B以下の整数のうち、Dで割り切れるものの個数はCountB_D = B//D
B以下の整数のうち、CでもDでも割り切れるものの個数はCountB_CD = B//lcm(C,D)

CountB = B - (CountB_C + CountB_D - CountB_CD)

"""
from fractions import gcd

def solve():
    A, B, C, D = list(map(int, input().split()))
    def lcm(a, b):
        """ aとbの最小公倍数を返す """
        return a*b // gcd(a, b)

    def f(x):
        """ 1以上x以下の整数のうち、CでもDでも割り切れないものの個数を返す """
        return x - (x//C + x//D - x//lcm(C,D))    

    ans = f(B) - f(A-1)

    print(ans)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-
import copy

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    
    ans = 0
    M = min(N,K)
    for l in range(M+1):
        having = []
        for r in range(M+1):
            if l+r > K: break
            if l+r == 0: continue
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort(reverse=True)

            restK = K - (l+r)

            for i in range(restK):
                if having[-1] < 0:
                    having.pop()
                    continue
                break

            ans = max(ans, sum(having))
    
    print(ans)

if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-
from collections import deque
"""解説
全探索

左からL個、右からR個宝石を取り、K-(L+R)個の宝石を筒に詰めるとき方針として、

・筒に詰める宝石は絶対値の大きい負の値のもの

あとは全探索
"""

def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))

    ans = 0
    r = min(N, K)
    for a in range(r+1):  # 左から取る個数
        for b in range(r+1):  # 右から取る個数
            if r < a+b: continue

            t = V[:a] + V[::-1][:b]  # 左からa個、右からb個とった宝石を「持っているリストt」にぶちこむ
            t.sort(reverse=True)

            for _ in range(K-a-b):
                if len(t) > 0 and t[-1] < 0:
                    t.pop()
                    continue
                break
            ans = max(ans, sum(t))

    print(ans)



if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-

def solve():
    R, G, B, N = list(map(int, input().split()))

    ans = 0
    for r in range(0, N+1):
        num1 = r*R
        if num1 > N: break

        for g in range(0, N+1):
            num2 = g*G
            if num1+num2 > N: break

            for b in range(0, N+1):
                num3 = b*B
                if num1+num2+num3 > N: break
                if num1+num2+num3 == N:
                    ans += 1

    print(ans)

if __name__ == "__main__":
    solve()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    R, G, B, N = list(map(int, input().split()))

    # r, g, bのそれぞれに関してforで回すとO(N**3)で間に合わないので、
    # rを固定する。するとO(N**2)で解ける
    ans = 0
    for g in range(N+1):
        if g*G > N: break
        for b in range(N+1):
            if g*G+b*B > N: break
            if (N - (g*G+b*B))%R == 0:
                ans += 1
    print(ans)


if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):
# -*- 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()


Fixed submission:
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    ans = 0
    endA = 0
    startB = 0
    ryouhou = 0
    # Aで終わるs1と、Bで始まるs2を連結させれば1個増える
    for i in range(N):
        s = input()
        if s[-1]=="A" and s[0]=="B":
            ryouhou += 1
        elif s[-1]=="A":
            endA += 1
        elif s[0]=="B":
            startB += 1
        
        ans += s.count("AB")
    

    if endA>=1 and startB>=1 and ryouhou>=1:
        ans += min(endA-1, startB-1)
        ans += ryouhou + 1
        print(ans)
        return
    elif ((endA>=1 and startB==0 and ryouhou>=1) or
          (endA==0 and startB>=1 and ryouhou>=1)):
        ans += ryouhou
        print(ans)
        return
    elif endA==0 and startB==0 and ryouhou>=1:
        ans += ryouhou-1
        print(ans)
        return
    else:
        # ryouhou==0のとき
        ans += min(endA, startB)
        rest = abs(max(endA, startB) - min(endA, startB))
        
        ans += min(rest, ryouhou)
        rest = ryouhou - rest
        if rest <= 0:
            print(ans)
            return
        else:
            ans += rest//2
            print(ans)
            return

if __name__ == "__main__":
    solve()