Harryxun/stf_run huggingface dataset:
from bisect import bisect_left
N, M = map(int, input().split())
nums = list(map(int, input().split()))

matchNum = {
    1 : 2,
    2 : 5,
    3 : 5,
    4 : 4,
    5 : 5,
    6 : 6,
    7 : 3,
    8 : 7,
    9 : 6,
}

dp = [''] * (N + 1)
for n in range(1, N + 1):
    for k in nums:
        d = 0
        cnt = n - matchNum[k]
        if cnt >= 0 and (dp[cnt] != '' or (cnt == 0 and dp[n] <= dp[cnt] * 10)):
            oldVal = list(str(dp[cnt]))[:: -1]
            adder = bisect_left(oldVal, str(k))
            newVal = oldVal[:adder] + [str(k)] + oldVal[adder:]
            newVal = int(''.join(newVal[:: -1]))
            d = max(d, newVal)
        if d == 0:
            continue
        elif dp[n] == '':
            dp[n] = d
        else:
            dp[n] = max(dp[n], d)

print(dp[N])

### FIXED CODE ###

N , M = map(int,input().split())
A = list(map(int,input().split()))

A.sort(reverse=True)

match = [2,5,5,4,5,6,3,7,6]

dp = [-float('inf')] * (N + 1)

dp[0] = 0
for i in range(1, N+1) :
    maxDigit = -float('inf')
    for a in A :
        if i - match[a - 1] >= 0 :
            maxDigit = max(maxDigit, dp[i - match[a - 1]] + 1)
    dp[i] = maxDigit

maxDigit = dp[-1]

r = N

for _ in range(maxDigit) :
    for a in A :
        if r - match[a - 1] < 0 or r < 0 :
            continue
        if dp[r - match[a - 1]] == dp[r] - 1 :
            print(a, end='')
            r -= match[a - 1]
            break

print('')
from itertools import combinations
def g(i):
    return chr(i+96)
def f(n,i):
    global Ind
    if n==0:
        B.append("".join(A))
        return
    if n==1:
        j = Ind.pop()
        A[j] = g(i)
        B.append("".join(A))
        A[j]=0
        Ind.append(j)
        Ind = sorted(Ind)
        return
    j = Ind.pop(0)
    A[j] = g(i)
    for k in range(n):
        for x in combinations(Ind,n-1-k):
            tmp = []
            for j in range(n-1-k):
                A[x[j]] = g(i)
                tmp.append(x[j])
            for j in tmp:
                Ind.remove(j)
            f(k,i+1)
            for j in tmp:
                A[j] = 0
                Ind.append(j)
            Ind = sorted(Ind)
    
N = int(input())
A = [0 for _ in range(N)]
Ind = list(range(N))
B = []
f(N,1)
B = sorted(B)
for b in B:
    print(b)

### FIXED CODE ###

from itertools import combinations
def g(i):
    return chr(i+96)
def f(n,i):
    global Ind
    if n==0:
        B.append("".join(A))
        return
    if n==1:
        j = Ind.pop()
        A[j] = g(i)
        B.append("".join(A))
        A[j]=0
        Ind.append(j)
        Ind = sorted(Ind)
        return
    j0 = Ind.pop(0)
    Ind1 = Ind[:]
    A[j0] = g(i)
    for k in range(n):
        for x in combinations(Ind1,n-1-k):
            tmp = []
            for j in range(n-1-k):
                A[x[j]] = g(i)
                tmp.append(x[j])
            for j in tmp:
                Ind.remove(j)
            f(k,i+1)
            for j in tmp:
                A[j] = 0
                Ind.append(j)
            Ind = sorted(Ind)
    Ind.append(j0)
    Ind = sorted(Ind)
    
N = int(input())
A = [0 for _ in range(N)]
Ind = list(range(N))
B = []
f(N,1)
B = sorted(B)
for b in B:
    print(b)
S = input()
n = len(S)
k = int(input())
cnt = 0
itl = sorted(list(set(S)))
ans = set([])
for w in itl:
    for i, s in enumerate(S):
        if s != w:
            continue
        else:
            for j in range(i+1, n+1):
                ans.add(S[i:j])
                cnt += 1
                if cnt == k:
                    break

    ans.clear()

ans = list(ans)
ans.sort()
index = k - 1 - (cnt - len(ans))
print(ans[index])


### FIXED CODE ###

S = input()
n = len(S)
k = int(input())
cnt = 0
itl = sorted(list(set(S)))
ans = set([])
for w in itl:
    for i, s in enumerate(S):
        if s != w:
            continue
        else:
            for j in range(i+1, i+k+1):
                ans.add(S[i:j])
    cnt += len(ans)
    if cnt >= k:
        break
    else:
        ans.clear()

ans = list(ans)
ans.sort()
index = k - 1 - (cnt - len(ans))
print(ans[index])
a = list(input())
b = list(input())+""
print(x+y for x,y in zip(a,b).end="")

### FIXED CODE ###

a = list(input())
b = list(input())+[""]
for x,y in zip(a,b):
  print(x+y, end = "")
N=int(input())
A=[0 for i in range(N)]
A=input().split()
for i in range(N):
    A[i]=(int)(A[i])
for i in range(1,N):
    v=A[i]
    j=i-1
    while j>=0 and A[j]>v:
        A[j+1]=A[j]
        j-=1
        A[j+1]=v
    for i in range(N):
        if i!=N-1:
            print(A[i],end=" ")
        if i==N-1:
            print(A[i])

"""


### FIXED CODE ###

N=int(input())
A=[0 for i in range(N)]
A=input().split()
for i in range(N):
    A[i]=(int)(A[i])
for i in range(N):
    v=A[i]
    j=i-1
    while j>=0 and A[j]>v:
        A[j+1]=A[j]
        j=j-1
        A[j+1]=v
    for i in range(N):
        if i!=N-1:
            print(A[i],end=" ")
        if i==N-1:
            print(A[i])
s=input()
s[3]='8'
print(s)

### FIXED CODE ###

print("2018"+input()[4:])
n, m = [int(item) for item in input().split()]
edge = [[] for _ in range(n+1)]
INF =  2000 * 10**9
for i in range(m):
    a, b, c = [int(item) for item in input().split()]
    edge[a-1].append([b-1,c])
edge[n-1].append([n, 0])

def dfs(prev, prev_c, v):
    if v == n:
        return 0
    max_cost = -INF
    for e in edge[v]:
        if e[0] == prev:
            if prev_c + e[1] > 0:
                max_cost = INF
            continue
        max_cost = max(max_cost, e[1] + dfs(v, e[1], e[0]))
    return max_cost

cost = dfs(-1, 0, 0)
if cost > INF // 2:
    print("inf")
else:
    print(cost)

### FIXED CODE ###

n, m = [int(item) for item in input().split()]
edge = []
inf =  2000 * 10**10
for i in range(m):
    a, b, c = [int(item) for item in input().split()]
    edge.append([a-1, b-1, -c])

def BellmanFord(start):
    dist = [inf for i in range(n)]
    dist[0] = 0
    for i in range(n):
        for u, v, c in edge:
            if dist[u] != inf and (dist[u] + c) < dist[v]:
                dist[v] = dist[u] + c
                if i == n-1 and v == n-1:
                    return "inf"
    return -dist[-1]

ret = BellmanFord(0)
print(ret)
n = int(input())
buttons = [0]
target_index = 0
for i in range(n):
    num = int(input())
    buttons.append(num)

count = 0
next_index = 2
while count < n:
    if len(buttons) - 1 <= next_index <= 0:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)


### FIXED CODE ###

n = int(input())
buttons = [0]
target_index = 0
for _ in range(n):
    buttons.append(int(input()))

count = 0
prev_index = 1
while count < n:
    if prev_index < 1 or len(buttons) <= prev_index:
        count = -1
        break

    next_index = buttons[prev_index]
    count += 1
    if next_index == 2:
        break
    else:
        prev_index = next_index

print(count if count < n else -1)
s = input()


def unbalanced(st):
    for c in st:
        if st.count(c) > len(st) // 2:
            return True
    return False


def test_str(st):
    r = (-1, -1)
    if len(s) == 3:
        if not unbalanced(s):
            return r
        else:
            return (0, 3)
    for i in range(len(s) - 3):
        print(s[i:i+3])
        if unbalanced(s[i:i+3]):
            r = (i+1, i+3)
            return r


print(test_str(s)[0], test_str(s)[1])

### FIXED CODE ###

s = input()


def unbalanced(st):
    for c in st:
        if st.count(c) > (len(st) // 2):
            return True
    return False


def test_str(st):
    r = (-1, -1)
    for j in range(1, 3):
        for i in range(len(s) - j):
            if unbalanced(s[i:i+j+1]):
                r = (i+1, i+j+1)
                return r
    return r


print(test_str(s)[0], test_str(s)[1])
while True:
    try:
        a, b = map(int, raw_input().split())
        def gcd(a, b):
        if b == 0:
            return a
        else:
            return gcd(b, a%b)
        g = gcd(a,b)
        l = a*b/gcd(a,b)
        print g, l
    except:
        break

### FIXED CODE ###

while True:
    try:
        a, b = map(int, raw_input().split())
        def gcd(a, b):
            if b == 0:
                return a
            else:
                return gcd(b, a%b)
        g = gcd(a,b)
        l = a*b/g
        print g, l
    except:
        break
n=int(input())

a=input().split()
b=[]
c=[]
i=0
while i<n:
    b.append(int(a[i]))
    i+=1

while n>0:
    c.append(max(b))
    b.remove(max(b))
    n=n-1

answer=str(c[0])
m=2
while m<i+1:
    answer=answer+" "+str(c[m-1])
    m=m+1
print(answer)



### FIXED CODE ###

n=int(input())

a=input().split()
b=[]
c=[]
i=0
while i<n:
    b.append(int(a[i]))
    i+=1

answer=str(b[n-1])
while n>1:
    answer=answer+" "+str(b[n-2])
    n=n-1
print(answer)
N, M=map(int, input().split())
a=[int(input()) for i in range(M)]

if a[0]==1:
  way=[1, 0]
  a.remove(1)
else:
  way=[1, 1]
  
for i in range(2, N+1):
  if len(a)>0:
    if i==a[0]:
      a.remove(i)
      way=[way[1], 0]
    else:
      way=[way[1], sum(way)]
  else:
    way=[way[1], sum(way)]
    
print(way[1]%1000000007)

### FIXED CODE ###

temp=input().split()
N=int(temp[0])
M=int(temp[1])

index=[int(1) for i in range(N)]
for i in range(M):
  index[int(int(input())-1)]=0

temp=[0, 1]
for i in range(N):
  if index[i]==1:
    temp=[temp[1], temp[0]+temp[1]]
  else:
    temp=[temp[1], 0]
    
print(temp[1]%1000000007)
n=int(input())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(min(x),max(x)):
  now=0
  for x in X:
    now+=(i-x)*(i-x)
  ans=min(now,ans)
print(ans)

### FIXED CODE ###

n=int(input())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(min(X),max(X)+1):
  now=0
  for x in X:
    now+=(i-x)*(i-x)
  ans=min(now,ans)
print(ans)
#-------------------------------------------------------------------------------
# coding: utf-8
# Created:  
import sys

op = { "+" : lambda x, y:  x + y,
        "-": lambda x,y: x - y,
        "/": lambda x,y: x/y,
        "*": lambda x,y: x*y }

while True:
    a, op_, b = sys.readline().split()
    if op_ == "?" : break
    a1, b1 = int(a), int(b)

    print op[op_](a1, b1)

### FIXED CODE ###

#-------------------------------------------------------------------------------
# coding: utf-8
# Created:     16/12/2015
import sys

io = sys.stdin

op = { "+" : lambda x, y:  x + y,
        "-": lambda x,y: x - y,
        "/": lambda x,y: x/y,
        "*": lambda x,y: x*y }


while True:
    a, op_, b = io.readline().split()
    if op_ == "?" : break
    a1, b1 = int(a), int(b)

    print op[op_](a1, b1)
n = int(input())
a = [int(input) for i in range(n)]
dist = []
for i in range(n):
    for j in range(n):
        if i >= j:
            continue
        dist.append(abs(a[i]-a[j]))
print(max(dist))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))

print(max(a)-min(a))
n = list(map(int, list(input())))
k = int(input())

# 上の桁からDP
# 大きさが微妙な場合は分ける
# dp[桁][0の個数]

def solve(n, k):
    if k == 0:
        return 1
    dp = [[0 for _ in range(k+1)] for _ in range(len(n))]
    dp[0][0] = 1
    dp[0][1] = n[0] - 1
    for i in range(len(n) - 1):
        for j in range(k + 1):
            if j == 0:
                dp[i+1][0] = dp[i][0]
            else:
                dp[i+1][j] = dp[i][j-1] * 9 + dp[i][j]
    # first non-0
    i = 1
    while i < len(n) and n[i] == 0:
        i += 1
    return dp[len(n) - 1][k] + solve(n[i:], k-1)

print(solve(n, k))

### FIXED CODE ###

n = list(map(int, list(input())))
k = int(input())

# 上の桁からDP
# 大きさが微妙な場合は分ける
# dp[桁][0の個数]

def solve(n, k):
    if k == 0:
        return 1
    if n == []:
        return 0
    dp = [[0 for _ in range(k+1)] for _ in range(len(n))]
    dp[0][0] = 1
    dp[0][1] = n[0] - 1
    for i in range(len(n) - 1):
        for j in range(k + 1):
            if j == 0:
                dp[i+1][0] = dp[i][0]
            else:
                dp[i+1][j] = dp[i][j-1] * 9 + dp[i][j]
    # first non-0
    i = 1
    while i < len(n) and n[i] == 0:
        i += 1
    return dp[len(n) - 1][k] + solve(n[i:], k-1)

print(solve(n, k))
n, w = map(int, input().split())
W, V = [], []
for i in range(n):
    wi, vi = map(int, input().split())
    W.append(wi)
    V.append(vi)
    
# Dp[i][sum_w] := i-1 番目までの品物から重さがsum_wを超えないように選んだときの、価値の総和の最大値
Dp = [[0] * (w + 1) for _ in range(n+1)]

for i in range(n):
    for sum_w in range(w+1):
        # i番目の商品を選ぶ場合
        if sum_w - W[i] >= 0:
            Dp[i+1][sum_w] = max(Dp[i+1][sum_w], Dp[i][sum_w - W[i]] + V[i])
        # i番目の商品を選ばない場合
        Dp[i+1][sum_w] = max(Dp[i+1][sum_w], Dp[i][sum_w])
print(Dp[-1][-1])

### FIXED CODE ###

n = int(input())
H = list(map(int, input().split()))

DP = [10 ** 9] * n
DP[0] = 0

for i in range(1, n):
    DP[i] = min(DP[i], DP[i-1] + abs(H[i] - H[i-1]))
    if i > 1:
        DP[i] = min(DP[i], DP[i-2] + abs(H[i] - H[i-2]))
print(DP[-1])
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

print(f(P)-f(Q))


### FIXED CODE ###

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  N=len(x)
  for i in range (N):
    k=0
    for j in range(i,N):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(N-i-1)
  return result

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

print(abs(f(P)-f(Q)))
n = int(input())

nums = list(map(int, input().split()))
sums = [nums[0]]
for i in range(1, n):
  sums.append(sums[i-1] + nums[i])
  
min_diff = 100
index = 0
for sum in sums:
  if sum - (sum[-1] - sum) < min_diff:
    min_diff = sum - (sum[-1] - sum)
    
print(min_diff)

### FIXED CODE ###

n = int(input())

nums = list(map(int, input().split()))
sums = [nums[0]]
for i in range(1, n):
  sums.append(sums[i-1] + nums[i])
  
min_diff = 100
index = 0
for sum in sums:
  if abs(sum - (sums[-1] - sum)) < min_diff:
    min_diff = abs(sum - (sums[-1] - sum))
    
print(min_diff)
x = [0] * (N + 1)
y = [0] * (N + 1)
for i in range(1, N + 1):
    t[i], x[i], y[i] = map(int, input().split())
for i in range(1, N + 1):
    if t[i] - t[i - 1] < abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]):
        print('No')
        exit()
    elif t[i] % 2 != (x[i] + y[i]) % 2:
        print('No')
        exit()
print('Yes')

### FIXED CODE ###

N = int(input())
t = [0] * (N + 1)
x = [0] * (N + 1)
y = [0] * (N + 1)
for i in range(1, N + 1):
    t[i], x[i], y[i] = map(int, input().split())
for i in range(1, N + 1):
    if t[i] - t[i - 1] < abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]):
        print('No')
        exit()
    elif t[i] % 2 != (x[i] + y[i]) % 2:
        print('No')
        exit()
print('Yes')
#!/usr/bin/env python3

"""
https://atcoder.jp/contests/agc029/tasks/agc029_a
"""

import sys

def my_rep(old, new, st):
    st2 = st.replace(old, new, 1)

    if st2 == st:
        return 0

    return my_rep(old, new, st2) + 1



s = sys.stdin.readline().rstrip()
print(my_rep('BW','WB',s))


### FIXED CODE ###

#!/usr/bin/env python3

"""
https://atcoder.jp/contests/agc029/tasks/agc029_a
"""

import sys
import re


s = sys.stdin.readline().rstrip()
e = 0
n = 0

for chr in s:
    if chr == 'W':
        e += n

    if chr == 'B':
        n += 1

print(e)
import math
from functools import reduce
import numpy as np
def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = int(input())
A = list(map(int, input().split()))
ans=0
for i in range(N):
    a = A[0]
    del A[0]
    
    b = gcd_list(A)
    if ans < b:
        ans = b
    A.append(a)

print(ans)
        

### FIXED CODE ###

def gcd(x,y):
  if y==0:
    return x
  else:
    return gcd(y, x%y)

N = int(input())
A = list(map(int, input().split()))

left_gcd = [A[0]]
a = A[0]
for i in range(1,N-1):
  a = gcd(a, A[i])
  left_gcd.append(a)

right_gcd = [A[N-1]]
b = A[N-1]
for k in range(N-2, 0, -1):
  b = gcd(b, A[k])
  right_gcd.append(b)

ans_gcd = [right_gcd[N-2], left_gcd[N-2]]

for p in range(N-2):
  ans_gcd.append(gcd(left_gcd[p], right_gcd[N-p-3]))

print(max(ans_gcd))
n,k=map(int,input().split(" "))

flag=0
if(n>k):
    flag=1
    temp=n
    n=k
    k=temp

if(k%n==0):
    if(flag==1):
        print(0)
    else:
        print(n) 
    exit()


a=int(k/n)
b=a+1
if(flag==1):
    if(k-n*a<=n*b-k):
        print(k-n*a)
    else:
        print(n*b-k)
else:
    c=abs(n-k)
    d=abs(c-k)
    if(c>d):
        print(d)
    else:
        print(c)


### FIXED CODE ###

n,k=map(int,input().split(" "))

c=abs(n-k)
d=abs(c-k)
if(c+d==k):
    if(c>d):
        print(d)
    else:
        print(c)
    exit()
if(n>=k):
    if(n%k==0):
        print(0)
        exit()
    else:
        a=n-k*(n//k)
        b=k*(n//k+1)-n
        if(a>b):
            print(b)
        else:
            print(a)
        exit()
else:
    
    while(1):
        a=abs(n-k)
        if(n>a):
            n=a
        else:
            print(n)
            exit()
import bisect
N = input()
L_list = list(map(int,input().split()))
L_list.sort()
ans = 0

for i in range(N-1):
  for j in range(i+1,N-1):
    x = bisect.bisect_left(L_list,L_list[i]+L_list[j])
    ans += x-j-1
print(ans)

### FIXED CODE ###

import bisect
N = int(input())
L_list = list(map(int,input().split()))
L_list.sort()
ans = 0

for i in range(N-1):
  for j in range(i+1,N-1):
    x = bisect.bisect_left(L_list,L_list[i]+L_list[j])
    ans += x-j-1
print(ans)
#C問題
S=str(input())
Slist=list(S)
Slen=len(Slist)
Srev=Slist[::-1]
kari=""
for i in range(Slen):
    if len(kari) == 5:
        if kari == "maerd":
            kari=Srev[i]
        elif kari == "esare":
            kari=Slev[i]
        else:
            kari+=Srev[i]
    elif len(kari) == 6:
        if kari == "resare":
            kari=Srev[i]
        else:
            kari+=Srev[i]
    elif len(kari) == 7:
        if kari == "remaerd":
            kari=Srev[i]
        else:
            break
    else:
        kari+=Srev[i]
if kari == "esare" or kari == "maerd" or kari == "resare" or kari == "remaerd":
    print("YES")
else:
    print("NO")

### FIXED CODE ###

#C問題
S=str(input())
Slist=list(S)
Slen=len(Slist)
Srev=Slist[::-1]
kari=""
for i in range(Slen):
    if len(kari) == 5:
        if kari == "maerd":
            kari=Srev[i]
        elif kari == "esare":
            kari=Srev[i]
        else:
            kari+=Srev[i]
    elif len(kari) == 6:
        if kari == "resare":
            kari=Srev[i]
        else:
            kari+=Srev[i]
    elif len(kari) == 7:
        if kari == "remaerd":
            kari=Srev[i]
        else:
            break
    else:
        kari+=Srev[i]
if kari == "esare" or kari == "maerd" or kari == "resare" or kari == "remaerd":
    print("YES")
else:
    print("NO")
n, k = map(int, input().split())
List = list(map(int, input().split()))
score = [1, 1.5, 2, 2.5, 3, 3.5]
max = 0
index = 0
res = 0

for i in range(n-k+1):
    pre = List[i]
    for j in range(k-1):
        pre += List[i+j]
    

    if max < pre:
        index = i
        pre = 0
    else:
        pre = 0
    
#print(index)
for i in range(index, index+k):
    res += score[List[i]-1]
    #print(List[i])

print(res)

### FIXED CODE ###

n, k = map(int, input().split())
List = list(map(int, input().split()))
score = []
score1 = []

def cal_score(num):
    return 1 + 0.5 * (num - 1)

score.append(cal_score(List[0]))

for i in range(1, n):
    score.append(score[i - 1] + cal_score(List[i]))
    
score1.append(score[k - 1])
m = 0
for i in range(k, n):
    score1.append(score[i] - score[m])
    m += 1

#print(score)    
#print(score1)
print(max(score1))
import sys
S = list(input())
S_odd = S[::2]
S_even = S[1::2]
easy_odd = ['R','U','D']
easy_even = ['L','U','D']

print('Yes') if set(S_odd).issubset(easy_odd) and set(S_even).issubset(easy_even) else 
    print('No'

### FIXED CODE ###

import sys
S = list(input())
S_odd = S[::2]
S_even = S[1::2]
easy_odd = ['R','U','D']
easy_even = ['L','U','D']

print('Yes') if set(S_odd).issubset(easy_odd) and set(S_even).issubset(easy_even) else print('No')
x = int(input())
n = x // 100

if x <= 105 * n:
  print(1)
else:
  print(0)


### FIXED CODE ###

a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]!=b[0]:
    print('1')
else:
    print('0')
N = int(input())
s = [input() for _ in range(N)]

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
elif end_A + end_B == 0:
    print(AB_count + both - 1)

### FIXED CODE ###

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
else:
    print(AB_count + both - 1)
import sys
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[float("inf") for i in range(n)] for j in range(n)]
    for i in range(m):
        a, b, c = map(int, input().split())
        wf[a-1][b-1] = wf[b-1][a-1] = c
    for i in range(n):
        wf[i][i] = 0
    wf = csgraph_from_dense(wf, null_value=10**10)
    wf = floyd_warshall(wf, directed=False)
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = csgraph_from_dense(wf, null_value=10**10)
    wf = floyd_warshall(wf, directed=False)
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if wf[s-1][t-1] < 301:
            ans[i] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall, shortest_path

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[0 for i in range(n)] for j in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        wf[a-1][b-1] = wf[b-1][a-1] = c
    wf = np.array(wf)
    for i in range(n):
        wf[i][i] = 0
    wf = shortest_path(wf, method='FW')
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = shortest_path(wf, method='FW')
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if wf[s-1][t-1] < 301:
            ans[i] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()
s=input()
t=inpur()
arr1=[0]*26
arr2=[0]*26
check='abcdefghijklmnopqrstuvwxyz'
for i in range(26):
  arr1[i]=s.count(check[i])
  arr2[i]=s.count(check[i])
arr1=sorted(arr1)
arr2=sorted(arr2)
for i in range(26):
  if arr1[i]!=arr2[i]:
    print('No')
    break
else:
  print('Yes')

### FIXED CODE ###

s=input()
t=input()
arr1=[0]*26
arr2=[0]*26
check='abcdefghijklmnopqrstuvwxyz'
for i in range(26):
  arr1[i]=s.count(check[i])
  arr2[i]=t.count(check[i])
arr1=sorted(arr1)
arr2=sorted(arr2)
for i in range(26):
  if arr1[i]!=arr2[i]:
    print('No')
    break
else:
  print('Yes')