Harryxun/stf_run huggingface dataset:
from collections import defaultdict

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

for a, b in zip(sorted(A), sorted(B)):
    if a > b:
        print('No')
        exit()

AI = [(a, i) for i, a in enumerate(A)]
BI = [(b, i) for i, b in enumerate(B)]
AI.sort()
BI.sort()

orderA = [-1] * N
orderB = [-1] * N

for k, ((_, i), (_, j)) in enumerate(zip(AI, BI)):
    orderA[i] = k
    orderB[j] = k

aToI = {a : i for i, a in enumerate(A)}

cnt = 0
visited = [False] * N
for i, a in enumerate(A):
    if visited[i]:
        continue
    visited[i] = True

    leng = 0
    now = i
    while True:
        b = B[i]
        now = aToI[b]
        if visited[now]:
            break
        leng += 1
        visited[now] = True
    cnt += leng

if cnt <= N - 2:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

class UnionFind :
    def __init__(self, size) :
        self.parent = list(range(size))
        self.height = [0] * size
        self.size = [1] * size
        self.component = size

    def root(self, index) :
        if self.parent[index] == index :  # 根の場合
            return index
        rootIndex = self.root(self.parent[index])  # 葉の場合親の根を取得
        self.parent[index] = rootIndex  # 親の付け直し
        return rootIndex

    def union(self, index1, index2) :  # 結合
        root1 = self.root(index1)
        root2 = self.root(index2)

        if root1 == root2 :  # 連結されている場合
            return

        self.component -= 1  # 連結成分を減らす

        if self.height[root1] < self.height[root2] :
            self.parent[root1] = root2  # root2に結合
            self.size[root2] += self.size[root1]
        else :
            self.parent[root2] = root1  # root1に結合
            self.size[root1] += self.size[root2]
            if self.height[root1] == self.height[root2] :
                self.height[root1] += 1
        return

    def isSameRoot(self, index1, index2) :
        return self.root(index1) == self.root(index2)

    def sizeOfSameRoot(self, index) :
        return self.size[self.root(index)]

    def getComponent(self) :
        return self.component

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

for a, b in zip(sorted(A), sorted(B)):
    if a > b:
        print('No')
        exit()

# Bの昇順でAを並び替え
BA = list(zip(A, B))
BA.sort()

AI = [(a, i) for i, (_, a) in enumerate(BA)]
AI.sort()

tree = UnionFind(N)
for nessIndex, (_, originalIndex) in enumerate(AI):
    tree.union(nessIndex, originalIndex)

V = set()
for i in range(N):
    V.add(tree.root(i))

if len(V) > 1:  # サイクルが複数の場合
    print('Yes')
    exit()

if any(a <= b for a, b in zip(sorted(A)[1:], sorted(B)[:-1])):
    print('Yes')
    exit()

print('No')
n = int(input())
a = [int(i) for i in input().split(" ")]
q = int(input())

operations = []

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for op in operations:
    target, goto = op

    a.sort()

    try:
        index = a.index(target)
        
        while:
            if a[index] != target:
                break;

            a[index] = target
            index += 1

        print(sum(a))

    except:
        print(sum(a))

### FIXED CODE ###

from collections import defaultdict

d = defaultdict(int)

n = int(input())
a = [int(i) for i in input().split(" ")]
q = int(input())

operations = []

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for hoge in a:
    d[hoge] += 1

initial_sum = 0
for key, value in d.items():
    initial_sum += key * value

for op in operations:
    target, goto = op

    before = goto * d[goto] + target * d[target]
    d[goto] += d[target]
    d[target] = 0

    after = goto * d[goto]
    diff = after - before
    
    initial_sum += diff
    print(initial_sum)
n=input();print(n if n%2==0 else n*2)

### FIXED CODE ###

n=int(input());print(n if n%2==0 else n*2)
str = input()
if str=="SUN":
  print(7)
elif str=="MON":
  print(6)
elif str=="TUE":
  print(5)
elif str=="WED":
  print(4)
elif str=="THU":
  print(3)
elif str=="FRI":
  print(2)
else str=="SAT":
  print(1)

### FIXED CODE ###

str = input()
if str=="SUN":
  print(7)
elif str=="MON":
  print(6)
elif str=="TUE":
  print(5)
elif str=="WED":
  print(4)
elif str=="THU":
  print(3)
elif str=="FRI":
  print(2)
else :
  print(1)
N,M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = [0 for i in range(M)]
for i in range(M):
    BCs[i] = list(map(int, input().split()))
BCs.sort(key=lambda x: x[1])
mark = 0
ans = 0
for i in reversed(range(M)):
    B = BCs[i][0]
    C = BCs[i][1]
    j = mark
    if As[j] >= C:
        break
    while As[j] < C and B > 0:
        As[j] = C
        j += 1
        B -= 1
    ans += (j - mark)*C
    mark = j
    
for i in range(mark,N):
    ans += As[i]

print(ans)


### FIXED CODE ###

N,M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = [0 for i in range(M)]
for i in range(M):
    BCs[i] = list(map(int, input().split()))
BCs.sort(key=lambda x: x[1])
mark = 0
ans = 0
for i in reversed(range(M)):
    B = BCs[i][0]
    C = BCs[i][1]
    j = mark
    if As[j] >= C:
        break
    while As[j] < C and B > 0:
        As[j] = C
        j += 1
        if j >= N:
            break
        B -= 1
    
    ans += (j - mark)*C
    mark = j
    if j >= N:
        break
    
for i in range(mark,N):
    ans += As[i]

print(ans)
x,k,d=map(int,input().split())
x1=abs(x)%d
x2=abs(abs(x1)-d)
num=abs(x)//d
if((k-num>=0):
	if((k-num)%2==0):
    	print(x1)
	else:
    	print(x2)
else:
   print(abs(x)-k*d)

### FIXED CODE ###

x,k,d=map(int,input().split())
x1=abs(x)%d
x2=abs(abs(x1)-d)
num=abs(x)//d
if((k-num)>=0):
    if((k-num)%2==0):
        print(x1)
    else:
        print(x2)
else:
    print(abs(x)-k*d)
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    XY=[tuple(map(int,input().split())) for _ in range(n)]
    parity=[0]*2
    for x,y in XY:
        parity[(x+y)%2]=1

    # 双方のparityがあれば不可能
    if(parity[0] and parity[1]):
        print(-1)
        return

    # for output
    m=31
    D=[2**i for i in range(m)]
    W=[None]*m
    # parityが偶数ならば、先に長さ1のRを入れることで、奇数の場合に帰着
    if(parity[0]):
        XY=[(x-1,y) for x,y in XY]
        D.insert(0,1)
        m+=1

    # 45 deg 回転
    U=[x-y for x,y in XY]
    V=[x+y for x,y in XY]

    for j in range(n): # 各 U[i],V[i]に対して方向を定める
        res=[0]*m
        u=(U[j]+pow(2,m)-1)//2
        v=(V[j]+pow(2,m)-1)//2
        for i in range(m):
            ub=u&1; vb=v&1
            if(ub and vb):
                res[i]='R'
            elif(ub and (not vb)):
                res[i]='D'
            elif((not ub) and vb):
                res[i]='U'
            else:
                res[i]='L'
            ub>>=1; vb>>=1
        W[j]=''.join(res)

    # output
    print(m)
    print(*D)
    for j in range(n):
        print(W[j] if(parity[1]) else 'R'+W[j])
resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    XY=[tuple(map(int,input().split())) for _ in range(n)]
    parity=[0]*2
    for x,y in XY:
        parity[(x+y)%2]=1

    # それぞれのパリティの点が存在すると不可能
    if(parity[0] and parity[1]):
        print(-1)
        return

    m=31
    D=[pow(2,i) for i in range(m)]
    # 偶数の場合は奇数に帰着させる
    if(parity[0]):
        XY=[(x-1,y) for x,y in XY]
        D=[1]+D

    U=[x-y for x,y in XY]
    V=[x+y for x,y in XY]

    W=[0]*n
    for j in range(n):
        res=[0]*m
        u=(U[j]+pow(2,m)-1)//2
        v=(V[j]+pow(2,m)-1)//2
        for i in range(m):
            ub=u&1; vb=v&1
            if(ub and vb):
                res[i]='R'
            elif(ub and (not vb)):
                res[i]='D'
            elif((not ub) and vb):
                res[i]='U'
            else:
                res[i]='L'
            u>>=1; v>>=1
        ans=''.join(res)
        W[j]=ans

    m=m+parity[0]
    print(m)
    print(*D)
    for j in range(n):
        ans=W[j] if(parity[1]) else 'R'+W[j]
        print(ans)
resolve()
s = input()
q = int(input())
flag = 0b0
for i in range(q) :
    t = input()
    if t == 1 :
        flag ^= 1
        continue
    if (int(t[2]) - flag) == 1 :
        s = t[4] + s
    else :
        s = s + t[4]
if flag == 0 :
    print(s)
else :
    print(s[::-1])

### FIXED CODE ###

right = input()
Q = int(input())
flag = 0b1
left = ""
for i in range(Q):
    q = input()
    if q == "1":
        flag ^= 1
        continue
    m,f,c = q.split(" ")
    f = int(f)
    left += c*(1-flag)*(f-1)
    right += c*flag*(f-1)
    left += c*flag*(2-f)
    right += c*(1-flag)*(2-f)
if flag == 1:
    print(left[::-1]+right)
else:
    print(right[::-1]+left)
import copy

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

ans = 10 ** 12
seen = 1  #[[A[j] for j in range(i,N) if A[j] > A[i]] for i in range(0,N)]

for i in range(2 ** (N - 1)):
  AA = copy.deepcopy(A)
  cost = 0
  con = 0
  for j in range(N - 1):
    m = max(AA[:j + 1])
    W
    #print(AA[:j + 1])
    a = AA[j + 1]
    if ((i >> j) & 1) and A[j + 1] <= m:
      cost += (m - a + 1)
      AA[j + 1] = m + 1
      con += 1
    if con == K - 1:
      break
  if len([AA[i] for i in range(1,N) if AA[i] > A[0]]) == K - 1:
    ans = min(cost,ans)
  
print(ans)

### FIXED CODE ###

import copy

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

ans = [10 ** 12 for i in range(2 ** (N - 1))]
seen = 1  #[[A[j] for j in range(i,N) if A[j] > A[i]] for i in range(0,N)]

for i in range(2 ** (N - 1)):
  AA = copy.deepcopy(A)
  cost = 0
  s = str(bin(i))[2:]
  if s.count("1") >= K:
    continue
    
  for j in range(N - 1):
    m = max(AA[:j + 1])
    a = AA[j + 1]
    if ((i >> j) & 1) and a <= m:
      cost += (m - a + 1)
      AA[j + 1] = m + 1
  
  if len([AA[i] for i in range(1,N) if AA[i] > max(AA[:i])]) >= K - 1:
    ans[i] = cost
    #print(AA,bin(i),cost)
    
print(min(ans))
import numpy as np
N, M, Q = map(int, input().split())
 
#input
lst = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    L, R = map(int, input().split())
    lst[R][L] += 1
 
lst = np.array(lst)
# print (lst)
 
#2次元累積和
for i in range(1, N + 1):
    lst[:,i] += lst[:, i - 1]
for j in range(1, N + 1):
    lst[j,:] += lst[j - 1, :]
 
 
#output
for _ in range(Q):
    p, q = map(int, input().split())
    print (lst[q, q] - lst[p-1, q] - lst[q, p-1] + lst[p-1, p-1])
 

### FIXED CODE ###

import numpy as np
N, M, Q = map(int, input().split())

#input
lst = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    L, R = map(int, input().split())
    lst[R][L] += 1

lst = np.array(lst)
# print (lst)

#2次元累積和
for i in range(1, N + 1):
    lst[:,i] += lst[:, i - 1]
for j in range(1, N + 1):
    lst[j,:] += lst[j - 1, :]


#output
for _ in range(Q):
    p, q = map(int, input().split())
    print (lst[q, q] - lst[p-1, q] - lst[q, p-1] + lst[p-1, p-1])

# print (lst)
import sys

sys.setrecursionlimit(10**6)

a,b,c = map(int, input().split())
k = int(input())

m = [[] for _ in range(7)]
m[0].append([a,b,c])

for i in range(k):
    for j in m[i]:
        m[i+1].append([j[0]*2, j[1],j[2]])
        m[i+1].append([j[0], j[1]* 2, j[2]])
        m[i+1].append([j[0], j[1], j[2]* 2])

ans = 'No'

for t in m[k]:
    if t[0] < t[1] and t[1] < t[2]:
        ans = 'Yes'
        break
print(ans)

### FIXED CODE ###

import sys

sys.setrecursionlimit(10**6)

a,b,c = map(int, input().split())
k = int(input())

m = [[] for _ in range(8)]
m[0].append([a,b,c])

for i in range(k):
    for j in m[i]:
        m[i+1].append([j[0]*2, j[1],j[2]])
        m[i+1].append([j[0], j[1]* 2, j[2]])
        m[i+1].append([j[0], j[1], j[2]* 2])

ans = 'No'

for t in m[k]:
    if t[0] < t[1] and t[1] < t[2]:
        ans = 'Yes'
        break
print(ans)
n,q = map(int,input().split())
s = str(input())
for _ in range(q):
  l,r = map(int,input().split())
  print(s[l-1:r].count("AC")

### FIXED CODE ###

n,q = map(int,input().split())
s = input()
t = [0] * (n + 1)
for i in range(n):
  t[i+1] = t[i] + (1 if s[i:i+2] == "AC" else 0)
for i in range(q):
  l,r = map(int,input().split())
  print(t[r-1] - t[l-1])
a=input()
if a[1]=="+":
  print(a[0]+a[2])
else:
  print(a[0]-a[2])


### FIXED CODE ###

a=input().split()
if a[1]=="+":
  print(int(a[0])+int(a[2]))
else:
  print(int(a[0])-int(a[2]))
N, M = map(int, input().split())
cities = [list(map(int, input().split())) for _ in range(M)]

cities = [[*city, i] for i, city in enumerate(cities)]
city_count = [0]*(N+1)
cities.sort(key=lambda x: x[1])
ids = []
for city in cities:
    city_count[city[0]] += 1
    ids.append(['{:0>6}{:0>6}'.format(city[0], city_count[city[0]]), city[2]])
ids.sort(key=lambda x: x[1])
for city in ids:
    print(city[0])

### FIXED CODE ###

# ABC113 C
N, M = map(int, input().split())
cities = [list(map(int, input().split())) for _ in range(M)]

cities = [[i, city[0], city[1]] for i, city in enumerate(cities)]
city_count = [0]*(N+1)
cities.sort(key=lambda x: x[2])
ids = []
for city in cities:
    city_count[city[1]] += 1
    ids.append([city[0], '{:0>6}{:0>6}'.format(city[1], city_count[city[1]])])
ids.sort(key=lambda x: x[0])
for city in ids:
    print(city[1])
N = int(input())
h = [int(i) for i in input().split()]
cost = [-1]*N
def jump_min(n):
    if n == 1:
        v = 0
    elif n == 2:
        v = abs(h[1]-h[0])
    else:
        if cost[n-1] != -1:
            v = cost[n-1]
        else:
            x = jump_min(n-2)+abs(h[n-1]-h[n-3])
            y = jump_min(n-1)+abs(h[n-1]-h[n-2])
            v = min(x,y)
    cost[n-1] = v
    return v
print(jump_min(N))
        

### FIXED CODE ###

import sys
sys.setrecursionlimit(200000)
N = int(input())
h = [int(i) for i in input().split()]
cost = [-1]*N
def jump_min(n):
    if n == 1:
        v = 0
    elif n == 2:
        v = abs(h[1]-h[0])
    else:
        if cost[n-1] != -1:
            v = cost[n-1]
        else:
            x = jump_min(n-2)+abs(h[n-1]-h[n-3])
            y = jump_min(n-1)+abs(h[n-1]-h[n-2])
            v = min(x,y)
    cost[n-1] = v
    return v
print(jump_min(N))
s = str(input())

nw = []
i = 0

for i in range(0,len(s),1):
  if s[i] == '?' and s[i-1] == 'P':
    nw.append('D')
  elif s[i] == '?' and s[i-1] == 'D':
    nw.append('D')
  elif s[i] == '?' and s[i-1] == '?':
    if nw[-1] == 'P':
      nw.append('D')
    elif nw[-1] == 'D':
      nw.append('D')
    
  else:
    nw.append(s[i])

print(''.join(nw))

### FIXED CODE ###

s = str(input())

nw1 = s.replace("?","D")

print(nw1)
while True:
    n, m = map(int, input().split())
    dpList = []
    if (n,m) == (0,0):
        break
    for i in range(n):
        row = list(map(int,input().split()))
        dpList.append(row)
        dpList = sorted(dpList, key = lambda x:x[1], reverse = True)
    num = 0
    for i in range(n):
        if m >= dpList[i][0]:
            m -= dpList[i][0]
        else:
            num += (dpList[i][0] - m) * dpList[i][1]
    print(num)

### FIXED CODE ###

while True:
    n, m = map(int, input().split())
    dpList = []
    if (n,m) == (0,0):
        break
    for i in range(n):
        row = list(map(int,input().split()))
        dpList.append(row)
    dpList = sorted(dpList, key = lambda x:x[1], reverse = True)
    num = 0
    for i in range(n):
        if m >= dpList[i][0]:
            m -= dpList[i][0]
        else:
            num += (dpList[i][0] - m) * dpList[i][1]
            m = 0
    print(num)
n=int(input())
if n%3!=0:
    r
else:
    for i in range(n):
        if i%3==0:
            print("."*(i)+"a"*2+"."*(n-2-i))
        else:
            print("."*(((i//3)+1)*3-1)+"a"+"."*(n-(((i//3)+1)*3-1)-1))


### FIXED CODE ###

n=int(input())

if n==2:
    print(-1)
else:
    x=[]
    if n%3==0:
        for i in range(n//3):
            x.append("."*(3*i)+"a"+"."*(n-3*i-1))
            x.append("."*(3*i)+"a"+"."*(n-3*i-1))
            x.append("."*(3*i)+".aa"+"."*(n-3*i-3))
    elif n%6==1:
        for i in range(n//6-1):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-7)+".aab.c.")
        x.append("."*(n-7)+"d..b.c.")
        x.append("."*(n-7)+"d..eeff")
        x.append("."*(n-7)+"g..mm.l")
        x.append("."*(n-7)+"gnn...l")
        x.append("."*(n-7)+"h...kkj")
        x.append("."*(n-7)+"hii...j")
    elif n%6==2:
        for i in range(n//6-1):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-8)+".a.bb.cc")
        x.append("."*(n-8)+".a...m.j")
        x.append("."*(n-8)+"..pp.m.j")
        x.append("."*(n-8)+"hh..i.o.")
        x.append("."*(n-8)+"gg..i.o.")
        x.append("."*(n-8)+"..n.ll.k")
        x.append("."*(n-8)+"f.n....k")
        x.append("."*(n-8)+"f.dd.ee.")
    elif n%6==4:
        for i in range(n//6):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-4)+"aacb")
        x.append("."*(n-4)+"ffcb")
        x.append("."*(n-4)+"hgdd")
        x.append("."*(n-4)+"hgee")
    else:
        for i in range(n//6):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-5)+"aabbc")
        x.append("."*(n-5)+"g.h.c")
        x.append("."*(n-5)+"gjh..")
        x.append("."*(n-5)+"dj.ii")
        x.append("."*(n-5)+"deeff")
    for i in range(n):
        print("".join(x[i]))

#また出力が違うやつをやりましたが
s, c = [], []
for i in range(m):
    a, b = map(int, input().split())
    s.append(a), c.append(b)

dicts = {}

ans = [0 for i in range(n)]
b = 0

for i in range(m):
    if s[i] not in dicts:
        dicts[s[i]] = c[i]
    elif s[i] in dicts and dicts[s[i]] != c[i]:
        print('-1')
        break


    if i == m - 1:
        for j in range(n + 1):
            if j in dicts:
                ans[j - 1] = dicts[j]
        # print(ans)
        for j in range(n):
            b += ans[n - j - 1] * (10 ** j)
        
        if b >= 10 ** (n - 1) and b <= (10 ** n) - 1:
            print(b)
        else:
            print('-1')


### FIXED CODE ###

n, m = map(int, input().split())
s, c = [], []
for i in range(m):
    a, b = map(int, input().split())
    s.append(a), c.append(b)


for i in range(0, 10 ** n):
    loop = True
    string = str(i)
    if len(string) != n:
        continue
    for j in range(m):
        if string[s[j] - 1] != str(c[j]):
            loop = False
    if loop:
        print(string)
        exit()

print('-1')
def main()
    import itertools
    n, m = map(int, input().split())
    h_lis = list(map(int, input().split()))

    cnt = 0
    ans = 0
    tmp = 0

    ab_lis = []
    for _ in range(m):
        ab_lis.append(list(map(int, input().split())))
    h_dic = {}

    ab_total = list(itertools.chain.from_iterable(ab_lis))
    #print(len(set(ab_total)))
    ans = n - len(set(ab_total))

    for i in range(n):
        h_dic[i+1] = h_lis[i]
    print(h_dic)

    for mountain in h_dic:
        tmp = 0
        for ab in ab_lis:
            print(mountain, h_dic[mountain], ab, ans, tmp)
            if mountain == ab[0]:
                if h_dic[mountain] < h_dic[ab[1]]:
                    tmp += 1
                    print(mountain, h_dic[mountain], ab, ans, tmp)
            elif mountain == ab[1]:
                if h_dic[mountain] < h_dic[ab[0]]:
                    tmp+=1
                    print(mountain, h_dic[mountain], ab, ans, tmp)
        if tmp == 0:
            ans += 1

    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    n, m = map(int,input().split())
    hlis = list(map(int, input().split()))
    hdic = {}
    rdic = {}
    for i in range(1, len(hlis)+1):
        hdic[i] = hlis[i-1]
    for j in range(m):
        a, b = map(int, input().split())
        if a in rdic:
            if rdic[a] < hdic[b]:
                rdic[a] = hdic[b]
        else:
            rdic[a] = hdic[b]
        if b in rdic:
            if rdic[b] < hdic[a]:
                rdic[b] = hdic[a]
        else:
            rdic[b] = hdic[a]
    ans = 0
    #print(hdic, rdic)
    for key in hdic:
        if key in rdic:
            if hdic[key] > rdic[key]:
                ans += 1
                #print(key)
        else:
            ans += 1
            #print(key)
    print(ans)



    
    
if __name__ == "__main__":
    main()
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
from math import sqrt, gcd
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]

# n = int(input())



def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A'
        b += s[0] == 'B'
        ans += s.replace('AB', "*").count('*')
    print(ans + min(a, b))
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()




"""




"""



### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import deque
from math import sqrt
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]

# n = int(input())



def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    c = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A' and s[0] != 'B'
        b += s[0] == 'B' and s[-1] != 'A'
        c += s[0] == 'B' and s[-1] == 'A'
        ans += s.replace('AB', "*").count('*')
    if (a+b) > 0:
        print(ans + c + min(a, b))
    else:
        if c:
            c -= 1
        print(ans + c)
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()




"""




"""
def main():
    N = int(input())
    P = [None] * N
    for i in range(N):
        x, y = map(int, input().split())
        P[i] = (x, y)
    P.sort()
    cnt = {}
    for i in range(N):
        for j in range(i+1, N):
            ix, iy = P[i]
            jx, jy = P[j]
            d = (jx - ix, jy - iy)
            if d not in cnt:
                cnt[d] = 0
            cnt[d] += 1
    print(N - max(cnt.values()))


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    N = int(input())
    if N == 1:
        print(1)
        return
    P = [None] * N
    for i in range(N):
        x, y = map(int, input().split())
        P[i] = (x, y)
    P.sort()
    cnt = {}
    for i in range(N):
        for j in range(i+1, N):
            ix, iy = P[i]
            jx, jy = P[j]
            d = (jx - ix, jy - iy)
            if d not in cnt:
                cnt[d] = 0
            cnt[d] += 1
    print(N - max(cnt.values()))


if __name__ == "__main__":
    main()
L=input()
a=int(L[0])
b=int(L[1])
c=int(L[2])
d=int(L[3])

import itertools

A=(1,-1)

def c(x):
	if x==1:
		return '+'
	else:
		return '-'

for v in itertools.product(A,repeat=3):
	s=a+b*v[0]+c*v[1]+d*v[2]
	if s==7:
		print(L[0]+c(v[0])+L[1]+c(v[1])+L[2]+c(v[2])+L[3]+'=7')
		exit()

### FIXED CODE ###

L=input()
a=int(L[0])
b=int(L[1])
c=int(L[2])
d=int(L[3])

import itertools

A=(1,-1)

def e(x):
	if x==1:
		return '+'
	else:
		return '-'

for v in itertools.product(A,repeat=3):
	s=a+b*v[0]+c*v[1]+d*v[2]
	if s==7:
		print(L[0]+e(v[0])+L[1]+e(v[1])+L[2]+e(v[2])+L[3]+'=7')
		exit()