Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
temp = []
for i in range(N):
    s.append(sorted(list(input())))
    temp.append(i)

ans = 0
for i in temp:
    for j in temp:
        if not i == j:
            if (s[i] == s[j]):
                ans += 1
print(int(ans/2))

Fixed submission:
N = int(input())
s = []
ans = 0
mydict = {}
for i in range(N):
    s = "".join(sorted(list(input())))
    if (s in mydict.keys()):
        ans += mydict[s]
        mydict[s] += 1
    else:
        mydict[s] = 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
for i in range(N):
    s.append(sorted(list(input())))

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if (s[i] == s[j]):
            ans += 1
print(int(ans))

Fixed submission:
N = int(input())
s = []
ans = 0
mydict = {}
for i in range(N):
    s = "".join(sorted(list(input())))
    if (s in mydict.keys()):
        ans += mydict[s]
        mydict[s] += 1
    else:
        mydict[s] = 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
for i in range(N):
    s.append(sorted(list(input())))

ans = 0
for i in range(N):
    for j in range(N):
        if not i == j:
            if (s[i] == s[j]):
                ans += 1
print(int(ans/2))

Fixed submission:
N = int(input())
s = []
ans = 0
mydict = {}
for i in range(N):
    s = "".join(sorted(list(input())))
    if (s in mydict.keys()):
        ans += mydict[s]
        mydict[s] += 1
    else:
        mydict[s] = 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
s = []
for i in range(N):
    s.append(sorted(list(input())))

ans = 0
for i in range(N):
    for j in range(N):
        if not i == j:
            if (si == sj):
                ans += 1
print(int(ans/2))

Fixed submission:
N = int(input())
s = []
ans = 0
mydict = {}
for i in range(N):
    s = "".join(sorted(list(input())))
    if (s in mydict.keys()):
        ans += mydict[s]
        mydict[s] += 1
    else:
        mydict[s] = 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
for i in range(N):
    s.append(list(input()))

ans = 0
for i in range(N):
    for j in range(N):
        if not i == j:
            si = sorted(s[i])
            sj = sorted(s[j])
            if (si == sj):
                ans += 1
print(int(ans/2))

Fixed submission:
N = int(input())
s = []
ans = 0
mydict = {}
for i in range(N):
    s = "".join(sorted(list(input())))
    if (s in mydict.keys()):
        ans += mydict[s]
        mydict[s] += 1
    else:
        mydict[s] = 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
c, d = a-1, b-1
l = sorted[a,b,c,d][::-1]
print(l[0]+l[1])

Fixed submission:
a, b = map(int, input().split())
c, d = a-1, b-1
l = sorted([a,b,c,d])[::-1]
print(l[0]+l[1])
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    cnt = 0
    l, r = map(int, input().split())
    print(s[l-1:r].count("AC"))

Fixed submission:
N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    cnt = 0
    l, r = map(int, input().split())
    for j in range(r-l):
        if s[l-1:r][j:j+2]=="AC": cnt+=1
    print(cnt)

Fixed submission:
N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
i=0
ans = 0
while i < (len(s)-1):
    if s[i] != s[i+1]:
        ans += 2
        s.pop(i)
        s.pop(i)
        i=-1
    i+=1
print(ans)

Fixed submission:
s = list(input())
print(min(s.count("0"),s.count("1"))*2)
Buggy python submission (verdict: Runtime Error):
import collections
import bisect
n,m = map(int, input().split())
p = [list(map(int,input().split())) for i in range(m)]
a = collections.defaultdict(list)
for x,y in sorted(p):
  a[x]+=[y]
for x,y in p:
  print("%06d%06d"%(x,bisect.bisect(a[x],y))

Fixed submission:
import collections
import bisect
n,m = map(int, input().split())
p = [list(map(int,input().split())) for i in range(m)]
a = collections.defaultdict(list)
for x,y in sorted(p):
  a[x]+=[y]
for x,y in p:
  print("%06d%06d"%(x,bisect.bisect(a[x],y)))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [[i] + list(map(int, input().split())) for i in range(m)]
r = [0]*n
p.sort(key=lambda x:(x[1],x[2]))
j = 0
k = 0
q = []
for i in range(m):
    if j==p[i][1]:
        k+=1
    else:
        k = 1
    q.append([p[i][0],k])
    j = p[i][1]
q.sort()
p.sort()
for i in range(m):
    print(f"{p[i][1]:0>6}{q[i][1]:0>6}")

Fixed submission:
import collections
import bisect
n,m = map(int, input().split())
p = [list(map(int,input().split())) for i in range(m)]
a = collections.defaultdict(list)
for x,y in sorted(p):
  a[x]+=[y]
for x,y in p:
  print("%06d%06d"%(x,bisect.bisect(a[x],y)))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [list(map(int, input().split())) for _ in range(m)]
r = [0]*n
for i in range(m):
    r[p[i][0]-1]+=1
    print(f"{p[i][0]:0>6}{r[p[i][0]-1]:0>6}")

Fixed submission:
import collections
import bisect
n,m = map(int, input().split())
p = [list(map(int,input().split())) for i in range(m)]
a = collections.defaultdict(list)
for x,y in sorted(p):
  a[x]+=[y]
for x,y in p:
  print("%06d%06d"%(x,bisect.bisect(a[x],y)))
Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
l = [i for i in range(a,b+1)]
l = l[:k] + l[-k:]
for i in sorted(list(set(l))):
    print(i)

Fixed submission:
a,b,k = map(int, input().split())
l=range(a,b+1)
for i in sorted(set(l[:k])|set(l[-k:])):
    print(i)
Buggy python submission (verdict: Runtime Error):
s = [input() for _ in range(int(input()))]
t = [input() for _ in range(int(input()))]
ls = list(set(s))
temp=0
for l in ls:
    ans=0
    ans += 1 for ss in s if ss == l
    ans -= 1 for tt in t if tt == l
    temp = max(temp, ans)
print(temp)

Fixed submission:
n=[input() for _ in range(int(input()))]
m=[input() for _ in range(int(input()))]
l=list(set(n))
print(max(0,max(n.count(l[i])-m.count(l[i]) for i in range(len(l)))))

Buggy python submission (verdict: Runtime Error):
s = [input() for _ in range(int(input()))]
t = [input() for _ in range(int(input()))]
ls = list(set(s))
temp=0
for l in ls:
    ans=0
    ans += 1 for ss in s if ss == l
    ans -= 1 for tt in t if tt == l
    temp = ans if ans>temp
print(temp)


Fixed submission:
n=[input() for _ in range(int(input()))]
m=[input() for _ in range(int(input()))]
l=list(set(n))
print(max(0,max(n.count(l[i])-m.count(l[i]) for i in range(len(l)))))

Buggy python submission (verdict: Runtime Error):
s = [input() for _ in range(int(input()))]
t = [input() for _ in range(int(input()))]
ls = list(set(s))
temp=0
for l in ls:
    ans=0
    ans += 1 for ss in s if l == ss
    ans-=1 for tt in t if tt == l
    temp = ans if ans>temp
print(temp)


Fixed submission:
n=[input() for _ in range(int(input()))]
m=[input() for _ in range(int(input()))]
l=list(set(n))
print(max(0,max(n.count(l[i])-m.count(l[i]) for i in range(len(l)))))

Buggy python submission (verdict: Runtime Error):
s = [input() for _ in range(int(input()))]
t = [input() for _ in range(int(input()))]
ls = set(s)
temp=0
for l in ls:
    ans=0
    ans += 1 for ss in s if l == ss
    ans-=1 for tt in t if tt == l
    temp = ans if ans>temp
print(temp)


Fixed submission:
n=[input() for _ in range(int(input()))]
m=[input() for _ in range(int(input()))]
l=list(set(n))
print(max(0,max(n.count(l[i])-m.count(l[i]) for i in range(len(l)))))

Buggy python submission (verdict: Runtime Error):
s = [input() for _ in range(input())]
t = [input() for _ in range(input())]
ls = set(s)
ans=0
temp=0
for l in ls:
    for ss in s:
      ans += 1if l == ss
    ans-=1for tt in t if tt == l
    temp = ans if ans>temp
print(temp)

Fixed submission:
n=[input() for _ in range(int(input()))]
m=[input() for _ in range(int(input()))]
l=list(set(n))
print(max(0,max(n.count(l[i])-m.count(l[i]) for i in range(len(l)))))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(input().split())
b = []
if n%2==1:
    for i in a:
        if int(i)%2==1: b.insert(0,i)
        else:  b.append(i)
else:
    for i in a:
        if int(i)%2==0: b.insert(0,i)
        else:  b.append(i)
print(" ".join(b))

Fixed submission:
n = int(input())
a = list(input().split())
b = []
if n%2==1:
    b = a[::-2]+a[1::2]
else:
    b = a[::-2]+a[::2]
print(" ".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(input().split())
b = []
for i in a:
    if n%2==1:
        if int(i)%2==1: b.insert(0,i)
        else:  b.append(i)
    else:
        if int(i)%2==0: b.insert(0,i)
        else:  b.append(i)
print(" ".join(b))

Fixed submission:
n = int(input())
a = list(input().split())
b = []
if n%2==1:
    b = a[::-2]+a[1::2]
else:
    b = a[::-2]+a[::2]
print(" ".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(input().split())
b = []
for i in a:
    b.append(i)
    b = b[::-1]
print(" ".join(b))

Fixed submission:
n = int(input())
a = list(input().split())
b = []
if n%2==1:
    b = a[::-2]+a[1::2]
else:
    b = a[::-2]+a[::2]
print(" ".join(b))
Buggy python submission (verdict: Runtime Error):
print("A"+{input()[8]}+"C")

Fixed submission:
print("A"+input()[8]+"C")

Buggy python submission (verdict: Runtime Error):
print(f"A{input()[8]}C")

Fixed submission:
print("A"+input()[8]+"C")
Buggy python submission (verdict: Runtime Error):
S = {i:list(input()) for i in "abc"}
while S[s]: s=S[s].pop(0)
print(s.upper())

Fixed submission:
a = list(input())
b = list(input())
c = list(input())
s = a.pop(0)
ans = "a"
while 1:
    if s == "a":
        if a: s = a.pop(0)
        else: break
    elif s=="b":
        if b: s = b.pop(0)
        else: break
    else:
        if c: s = c.pop(0)
        else: break
print(s.upper())
Buggy python submission (verdict: Runtime Error):
n = int(input())

if(n <= 9):
    print n

elif(n <= 99):
    print 9

elif(n <= 999):
    print (n - 90)

elif(n <= 9999):
    print 909

elif(n <= 99999):
    print (n - 9090)

else:
    print 90909

Fixed submission:
n = int(input())

if(n <= 9):
    print(n)

elif(n <= 99):
    print(9)

elif(n <= 999):
    print(n - 90)

elif(n <= 9999):
    print(909)

elif(n <= 99999):
    print(n - 9090)

else:
    print(90909)

Buggy python submission (verdict: Runtime Error):
n = int(input())

if(n <= 9):
    return n

elif(n <= 99):
    return 9

elif(n <= 999):
    return (n - 90)

elif(n <= 9999):
    return 909

elif(n <= 99999):
    return (n - 9090)

else:
    return 90909

Fixed submission:
n = int(input())

if(n <= 9):
    print(n)

elif(n <= 99):
    print(9)

elif(n <= 999):
    print(n - 90)

elif(n <= 9999):
    print(909)

elif(n <= 99999):
    print(n - 9090)

else:
    print(90909)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []

for i in range(n):
    a.append(int(input()))

for i in range(n):
    tmp = a[:]
    tmp.pop(i)
    print(max(tmp))

Fixed submission:
n = int(input())

a = []

for i in range(n):
    a.append(int(input()))

tmp = a[:]
max1 = max(tmp)
tmp.remove(max1)
max2 = max(tmp)

if(max1 == max2):
    for i in range(n):
        print(max1)
else:
    for i in range(n):
        if(a[i] != max1):
            print(max1)
        else:
            print(max2)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []

for i in range(n):
    a.append(int(input()))

for i in range(n):
    tmp = a[:]
    tmp.pop(i)
    print(max(tmp))

Fixed submission:
n = int(input())

a = []

for i in range(n):
    a.append(int(input()))

tmp = a[:]
max1 = max(tmp)
tmp.remove(max1)
max2 = max(tmp)

if(max1 == max2):
    for i in range(n):
        print(max1)
else:
    for i in range(n):
        if(a[i] != max1):
            print(max1)
        else:
            print(max2)
Buggy python submission (verdict: Runtime Error):
ans = 0
for i, a in enumerate(A):
    ans += B[a-1]
    if a - A[i-1] == 1 and i-1 > 0:
        ans += C[i-2]
print(ans)

Fixed submission:
import numpy as np

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i, a in enumerate(A):    
    ans += B[a-1]
    
A_ = [-100] + A[:-1]
A_ = np.array(A_)
A = np.array(A)

A_diff = A - A_

for j in A[A_diff == 1] -1:
    ans += C[j-1]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
s = input()
s = s.replace('LR', 'L,R')
s = s.replace('RL', 'R,L')
s = s.split(',')
s = list(map(len, s))
l = 2*k+1
ans = -1

s_ = list(np.array(s) - 1)
if l >= len(s):
    ans = sum(s)-1
else:
    
    for i in range(len(s)-l+1):
        cnt = 0
        cnt += sum(s_[:i])
        cnt += sum(s[i:l+i])-1
        cnt += sum(s_[l+i:])
        ans = max(ans, cnt)   
print(ans)

Fixed submission:
n, k = map(int,input().split())
s = input()
ans=0
for i in range(n-1):
    if s[i] == s[i+1]:
        ans += 1

ans += k*2
if ans > n-1:
    ans = n-1
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
s = input()
s = s.replace('LR', 'L,R')
s = s.replace('RL', 'R,L')
s = s.split(',')
s = list(map(len, s))
l = 2*k+1
ans = -1

if l >= len(s):
    ans = sum(s)-1
else:
    for i in range(len(s)-l+1):
        cnt = 0
        cnt += sum(list(np.array(s[:i])-1))
        cnt += sum(s[i:l+i])-1
        cnt += sum(list(np.array(s[l+i:])-1))
        ans = max(ans, cnt)   
print(ans)

Fixed submission:
n, k = map(int,input().split())
s = input()
ans=0
for i in range(n-1):
    if s[i] == s[i+1]:
        ans += 1

ans += k*2
if ans > n-1:
    ans = n-1
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k = map(int, input().split())
s = input()
s = s.replace('LR', 'L,R')
s = s.replace('RL', 'R,L')
s = s.split(',')
s = list(map(len, s))
l = 2*k+1
ans = -1

if l >= len(s):
    ans = sum(s)-1
else:
    for i in range(len(s)-l+1):
        cnt = 0
        cnt += sum(np.array(s[:i])-1)
        cnt += sum(s[i:l+i])-1
        cnt += sum(np.array(s[l+i:])-1)
        ans = max(ans, cnt)
        
print(ans)

Fixed submission:
n, k = map(int,input().split())
s = input()
ans=0
for i in range(n-1):
    if s[i] == s[i+1]:
        ans += 1

ans += k*2
if ans > n-1:
    ans = n-1
    
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
arr = np.array([[1,2,3],
                [1,2,3],
                [1,2,2]])
uniq, cnt = np.unique(arr, return_counts=True, axis=0)

a,b = map(int, input().split())
ans = max(a+b,a-b,a*b)
print(ans)

Fixed submission:
a,b = map(int, input().split())
ans = max(a+b,a-b,a*b)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
S = input().replace('LR', 'L,R').replace('RL', 'R,L').split(',')
ans = []
for i in range(len(S)//2):
    r = len(S[2*i])
    l = len(S[2*i+1])
    
    if (r+l) % 2 == 0:
        nums = [(r+l)//2] * 2
    else:
        diff = r - l
        if diff>0 and r%2==0 or diff<0 and l%2==1:
            nums = [(r+l-1)//2, (r+l-1)//2+1]
        else:
            nums = [(r+l-1)//2+1, (r+l-1)//2]
                
    li = [0]*(r-1) + nums + [0]*(l-1)
    ans.append(li)    
    
print(*sum(ans, []))

Fixed submission:
import numpy as np
S = input()
n = len(S)
rl_li = []
for i in range(n-1):
    if S[i]+S[i+1]=='RL':
        rl_li.append(i) 

ans = [0] * n
rl_idx = 0
for i in range(n):
    rl = rl_li[rl_idx]
    distance = abs(i - rl)
    
    if distance % 2 == 0:
        ans[rl] += 1
    else:
        ans[rl+1] += 1
    
    if i == n-1:
        break
        
    if S[i] == 'L' and S[i+1] == 'R':
        rl_idx += 1

print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
S = input().replace('LR', 'L,R').replace('RL', 'R,L').split(',')
ans = []
for i in range(len(S)//2):
    
    r = len(S[2*i])
    l = len(S[2*i+1])
    
    if (r+l) % 2 == 0:
        nums = [(r+l)//2] * 2
    else:
        diff = r - l
        if diff > 0:
            if r % 2 == 0:
                nums = [(r+l-1)//2, (r+l-1)//2+1]
            else:
                nums = [(r+l-1)//2+1, (r+l-1)//2]
        else:
            if l % 2 == 0:
                nums = [(r+l-1)//2+1, (r+l-1)//2]
            else:
                nums = [(r+l-1)//2, (r+l-1)//2+1]
    ans.append([0]*(r-1))
    ans.append(nums)
    ans.append([0]*(l-1))
    
print(*sum(ans, []))

Fixed submission:
import numpy as np
S = input()
n = len(S)
rl_li = []
for i in range(n-1):
    if S[i]+S[i+1]=='RL':
        rl_li.append(i) 

ans = [0] * n
rl_idx = 0
for i in range(n):
    rl = rl_li[rl_idx]
    distance = abs(i - rl)
    
    if distance % 2 == 0:
        ans[rl] += 1
    else:
        ans[rl+1] += 1
    
    if i == n-1:
        break
        
    if S[i] == 'L' and S[i+1] == 'R':
        rl_idx += 1

print(*ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
n, m  = map(int, input().split())

cards = np.array([])
for i in range(m):
    a, b = map(int, input().split())
    cards = np.append(cards, np.arange(a, b+1))
    
count = np.unique(cards, return_counts=True)
ans = sum(count[1] == m)
print(ans)

Fixed submission:
n, m  = map(int, input().split())

a_max = -1
b_min = 999999999999999

for i in range(m):
    a, b = map(int, input().split())
    
    a_max = max(a_max, a)
    b_min = min(b_min, b)

ans = max(b_min - a_max + 1, 0)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

for i in range(m):
    b, c = map(int, input().split())
    
    if c > min(nums):
        nums.remove(min(nums))
        nums.extend([c] * b)
    
nums.sort(reverse=True)
print(sum(nums[:length]))

Fixed submission:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

length = len(nums)

for i in range(m):
    b, c = map(int, input().split())
    
    if c > min(nums):
        nums.remove(min(nums))
        nums.extend([c] * b)
    
nums = sorted(nums, reverse=True)
print(sum(nums[:length]))

Fixed submission:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

for i in range(m):
    b, c = map(int, input().split())
    
    if c > min(nums):
        nums.extend([c] * b)
    
nums = sorted(nums, reverse=True)
print(sum(nums[:length]))

Fixed submission:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

for i in range(m):
    b, c = map(int, input().split())
    nums.extend([c] * b)
    
nums = sorted(nums, reverse=True)
print(sum(nums[:length]))

Fixed submission:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))
Buggy python submission (verdict: Runtime Error):
# BA=0, B=0, A=0, 0
# BA=2, B=0, A=0, 1
# BA=0, B=1, A=0, 0
# BA=0, B=0, A=1, 0
# BA=2, B=1, A=0, 2
# BA=2, B=0, A=1, 2
# BA=2, B=1, A=1, 2

n = int(input())
 
ans = 0
A = 0
B = 0
AB = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    
    if s[-1] == 'A' and s[0] != 'B':
        A += 1 
    if s[0] == 'B' and s[-1] != 'A':
        B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1
    
ans += min(A+AB, B+AB)


if AB>0 and A+B==0:
    ans -= 1

print(ans)

Fixed submission:
n, k = map(int, input().split())
print(n-k+1)

Buggy python submission (verdict: Runtime Error):
n = int(input())
 
ans = 0
A = 0
B = 0
AB = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    
    if s[-1] == 'A' and s[0] != 'B':
        A += 1 
    if s[0] == 'B' and s[-1] != 'A':
        B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1
    
ans += min(A+AB, B+AB)


if AB>0 and A==0 and B==0:
    ans -= 1

print(ans)

Fixed submission:
n, k = map(int, input().split())
print(n-k+1)