Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import numpy as np
import sys
def input(): return sys.stdin.readline()

def main():
  n=int(input())
  a=np.array(list(map(int,input().split())))
  
  ans=0
  #print(a%(np.array([a for _ in range(n)])).transpose())
  
  for A in a:
    b=A%a==0
    #print(b)
    if np.count_nonzero(b)==1:
      ans+=1
  print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

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

n=int(input())
A=list(map(int,input().split()))
cnt=[0]*(10**6+2)
ans=0
for a in A:
  if cnt[a]!=0:
    cnt[a]=2
    continue
  x=a
  while x<10**6+2:
    cnt[x]+=1
    x+=a
for a in A:
  #print(cnt[a])
  if cnt[a]==1:
    ans+=1
print(ans)
### BUGGY CODE ###

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort(reverse = True)
    f = list(map(int, input().split()))
    f.sort()
    right = 10**12
    left = 0
    while right > left:
        middle = (right + left)//2
        b = 0
        
        for i in range(n):
            b += max(0, a[i]-(middle//f[i]))
        if b <= k:
            right = middle
        else:
            left = middle + 1

    print(right)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():

    import numpy as np
    
    n, k = map(int, input().split())
    a = np.array(input().split(), np.int64)
    f = np.array(input().split(), np.int64)
    a.sort()
    a = a[::-1]
    f.sort()
    right = np.max(a*f)
    left = 0
    while right > left:
        middle = (right + left)//2
        b = 0
        
        if np.sum(np.maximum(a-middle//f, 0)) <= k:
            right = middle
        else:
            left = middle+1

    print(right)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n-1):
  for j in range(n-i-1):
    ans = (ans + a[i]*a[i+j+1]) % (10**9 + 7)
print(ans)

### FIXED CODE ###

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

s = sum(a)
t = sum(map(lambda x:x**2,a))
ans = ((s**2 - t)//2) % (10**9 + 7)
print(ans)
### BUGGY CODE ###

N = raw_input()
N = int(N)

a = []
for i in range(N):
	s_input = raw_input()
	s_input = int(s_input)
	a.append(s_input)

a.sort()
result = 0
for i in range(N):
	num = a[i]
	cnt = a.count(num)
	if(cnt % 2 == 1):
		result += 1
	i += cnt

print(result)

### FIXED CODE ###

n=int(raw_input())
li={}
while n !=0:
    a = raw_input()
    if a in li:
        del li[a]
    else:
        li[a]=a
    n-=1
print(len(li))
### BUGGY CODE ###

n,m,s,t=map(int,input().split())
yen=[[]*n for i in range(n)]
sunuke=[[]*n for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  yen[u-1].append((a,v-1))
  yen[v-1].append((a,u-1))
  sunuke[u-1].append((b,v-1))
  sunuke[v-1].append((b,u-1))


#ダイクストラ法
#n:頂点数　s:始点　es:隣接リスト表現
import heapq
def dijkstra(n,s,es):
  d=[float("INF")]*n
  d[s]=0
  used=[False]*n
  used[s]=True
  edgelist=[]
  for to in es[s]:
    heapq.heappush(edgelist,to)
  while len(edgelist)>0:
    minedge=heapq.heappop(edgelist)
    v=minedge[1]
    if used[v]:
      continue
    used[v]=True
    d[v]=minedge[0]
    for to in es[v]:
      heapq.heappush(edgelist,(d[v]+to[0],to[1]))
  return d

ans=[]
d_yen=dijkstra(n,s-1,yen)
d_sunuke=dijkstra(n,t-1,sunuke)
for i in range(n):
  ans.append(d_yen[i]+d_sunuke[i])

for j in range(n):
  print(10**15-min(ans[j:n]))
  
  
  
  
      
    
    
    
    
  
  


  

### FIXED CODE ###

n,m,s,t=map(int,input().split())
yen=[[]*(n+1) for i in range(n+1)]
sunuke=[[]*(n+1) for i in range(n+1)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  yen[u].append((a,v))
  yen[v].append((a,u))
  sunuke[u].append((b,v))
  sunuke[v].append((b,u))
  
import heapq
#n:頂点数、m:辺の数　s:始点　es:辺のリスト
def dijkstra(n,m,s,es):
  #used:訪問済みかどうかを確認する　false 使ってない
  used=[False]*(n+1)
  d=[float("INF")]*(n+1)
  d[s]=0
  used[s]=True
  q=[]
  for to in es[s]:
    heapq.heappush(q,to)
  while len(q)>0:
    minedge=heapq.heappop(q)
    v=minedge[1]
    if used[v]==True:
      continue
    cost=minedge[0]
    d[v]=cost
    used[v]=True
    for to in es[v]:
      heapq.heappush(q,(d[v]+to[0],to[1]))
  return d


d_yen=dijkstra(n,m,s,yen)
d_sunuke=dijkstra(n,m,t,sunuke)

ans=[float("INF")]*(n+1)
num=float("INF")
for i in range(n,0,-1):
  num=min(d_yen[i]+d_sunuke[i],num)
  ans[i]=num
  
for j in range(1,n+1):
  print(10**15-ans[j])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
rev=0
ST=""
EN=""
S=input()
Q=int(input())
for i in [0]*Q:
 A=list(input().split())
 if int(A[0])==1 :
  rev+=1
 else:
  if (int(A[1])+rev)%2==1:
   ST+=A[2]
  else:
   EN+=A[2]
if rev%2==0:
 print(ST[::-1].strip()+S.strip()+EN.strip())
else:
 print(EN[::-1].strip()+S[::-1].strip()+ST.strip())


### FIXED CODE ###

import sys
input = sys.stdin.readline
rev=0
ST=""
EN=""
S=input()
Q=int(input())
for i in [0]*Q:
 A=list(input().split())
 if int(A[0])==1 :
  rev+=1
 else:
  if (int(A[1])+rev)%2==1:
   ST+=A[2]
  else:
   EN+=A[2]
if rev%2==0:
 print(ST[::-1].strip()+S.strip()+EN.strip())
else:
 print(EN[::-1].strip()+S[::-1].strip()+ST.strip())
### BUGGY CODE ###

def main():
    n = int(input())
    A = list(map(int, input().split()))

    d = dict()

    for a in A:
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:
            temp = d[b]
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

        print(sum(k*v for k, v in d.items()))

main()

### FIXED CODE ###

def main():
    n = int(input())
    A = list(map(int, input().split()))

    sum_a = sum(A)

    d = dict()

    for a in A:
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:

            temp = d[b]         
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

            sum_a += (c - b) * temp
        print(sum_a)
        
main()
### BUGGY CODE ###

A,B,K = map(int,input().split())

for i in range(K):
    if A >= 1:
        A -= 1
    elif B >= 1:
        B -= 1
    else:
        pass
print(A, B)

### FIXED CODE ###

A,B,K = map(int,input().split())
print(A - K, B) if A - K >= 0 else print(0, B + (A - K)) if B + (A - K) >= 0 else print(0, 0)
### BUGGY CODE ###

N = int(input())
A = input().split()

flag = 0
for i in range(len(A)):
    for j in range(i+1,len(A)):
        if(A[i]==A[j]):
            flag = 1

if(flag == 0):
    print("YES")
else:
    print("NO")


### FIXED CODE ###

N = int(input())
A = input().split()

if(len(A)) == len(set(A)):
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

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

b, c = [0] * q, [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

a.sort()

for i in range(q):
    if b[i] in a:
        s = a.index(b[i])
        while s < n and a[s] == b[i]:
            a[s] = c[i]
            s += 1
    print(sum(a))

### FIXED CODE ###

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

b, c = [0] * q, [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

cnt = [0] * (10**5 +  5)
for i in range(n):
    cnt[a[i]] += 1

tot = sum(a)

for i in range(q):
    if cnt[b[i]] > 0:
        tot += cnt[b[i]] * (c[i] - b[i])
        cnt[c[i]] += cnt[b[i]]
        cnt[b[i]] = 0
    print(tot)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n,m,p=map(int,input().split())
edge = [[] for i in range(m)]
for i in range(m):
  a,b,c=map(int,input().split())
  edge[i]=[a-1,b-1,-c+p]

def sp(s,n,w,es):
  d=[float("inf")]*n
  d[s]=0
  count=0
  ret=[]
  while count<n+1:
    update=False
    for i in range(w):
      e=es[i]
      if d[e[0]]!=float("inf") and d[e[1]]>d[e[0]]+e[2]:
        d[e[1]]=d[e[0]]+e[2]
        update=True
    if not update:
      ret.append(d[-1])
      ret.append(d[-1])
      break
    count+=1
    if count>=n:
      ret.append(d[-1])
  return ret
d1,d2=sp(0,n,m,edge)
print(-1 if d1!=d2 else max(0,-d2))

### FIXED CODE ###

import sys
input = sys.stdin.readline
n,m,p=map(int,input().split())
edge = [[] for i in range(m)]
for i in range(m):
  a,b,c=map(int,input().split())
  edge[i]=[a-1,b-1,-c+p]
d=[float("inf")]*n
d[0]=0

for _ in range(n):
  for i in range(m):
    e0,e1,e2=edge[i]
    d[e1]=min(d[e1],d[e0]+e2)
ans1=d[-1]
for _ in range(n):
  for i in range(m):
    e0,e1,e2=edge[i]
    if d[e1]>d[e0]+e2:
      d[e1]=-float("inf")
ans2=d[-1]
print(-1 if ans1!=ans2 else max(0,-ans1))
### BUGGY CODE ###

import copy

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

for i in range(N):
    tmp = copy.copy(A)
    tmp.remove(A[i])
    print(max(tmp))

### FIXED CODE ###

import copy

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

res = sorted(A,reverse=True)
for i in range(N):
    if A[i] == res[0]:
        print(res[1])
    else:
        print(res[0])
### BUGGY CODE ###

import scipy as sp

N = int(input())
S = input()
Q = int(input())
*K, = map(int,input().split())

nD = S.count("D")
nC = S.count("C")
if nD < nC:
    S = S[::-1].replace("D","c").replace("C","d").upper()

S = "A" + S
D = sp.cumsum([s=="D" for s in S])
cntM = sp.cumsum([s=="M" for s in S])
sumM = sp.cumsum([d if s=="M" else 0 for s,d in zip(S,D)])
ans = sp.zeros(Q,int)

for i,s in enumerate(S[1:]):
    if s=="C":
        sMi = sumM[i]
        cMi = cntM[i]
        for j,k in enumerate(K):
            ik = max(0,i-k+1)
            ans[j] += (sMi-sumM[ik]) - D[ik]*(cMi-cntM[ik])
for out in ans:
    print(out)


### FIXED CODE ###

N = int(input())
S = input()
Q = int(input())
*K, = map(int,input().split())

nD = S.count("D")
nC = S.count("C")
if nD < nC:
    S = S[::-1].replace("D","c").replace("C","d").upper()

D = [0]*(N+1)
cntM = [0]*(N+1)
sumM = [0]*(N+1)
ans = [0]*Q
for i,s in enumerate(S):
    D[i+1] = D[i]
    cntM[i+1] = cntM[i]
    sumM[i+1] = sumM[i]
    if s=="D":
        D[i+1] += 1
    elif s=="M":
        cntM[i+1] += 1
        sumM[i+1] += D[i]
    elif s=="C":
        for j,k in enumerate(K):
            ik = max(0,i-k+1)
            d = D[ik]
            ans[j] += (sumM[i]-sumM[ik]) - d*(cntM[i]-cntM[ik])
for out in ans:
    print(out)
### BUGGY CODE ###

n, k = list(map(int,input().split()))
h = list(map(int,input().split()))

inf = n * (10**4)
c = [inf] * n

c[0] = 0
c[1] = abs(h[1]-h[0]) + c[0]

for i in range(2,n):
    for j in range(1, k + 1):
        if i - j < 0:
            continue
        c[i] = min(c[i], abs(h[i] - h[i-j]) + c[i-j])

print(c[n-1])


### FIXED CODE ###

n, k = list(map(int,input().split()))
h = list(map(int,input().split()))

inf = n * (10**4)
c = [inf] * n

c[0] = 0
c[1] = abs(h[1]-h[0]) + c[0]

for i in range(2,n):
    for j in range(1, k + 1):
        if i - j < 0:
            continue
        if c[i] < c[i-j]:
            continue
        c[i] = min(c[i], abs(h[i] - h[i-j]) + c[i-j])

print(c[n-1])
### BUGGY CODE ###

n=int(raw_input())
a=map(int,raw_input().split(' '))

b=[]
for i in range(1,n+1):
    b.append(a[i-1])
    b=b[::-1]

print ' '.join(map(str,b))

### FIXED CODE ###

n=int(raw_input())
a=map(int,raw_input().split(' '))

if len(a)%2==0:
    b=a[1::2][::-1]+a[0::2]
else:
    b=a[0::2][::-1]+a[1::2]

print ' '.join(map(str,b))
### BUGGY CODE ###

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

from scipy.sparse.csgraph import floyd_warshall
wf=floyd_warshall(a)
if not (a==wf).all() :
    print(-1)
    exit()

ans=0
for i in range(n):
    for j in range(n):
        use=True
        for h in range(n):
            if h!=i and h!=j and wf[i][j] == wf[i][h]+wf[h][j]:
                use=False
        if use:
            ans+=wf[i][j]
print(int(ans//2))

### FIXED CODE ###

import sys
input=sys.stdin.readline
n=int(input())
wf=[list(map(int,input().split())) for i in range(n)]

ans=0
for i in range(n):
    for j in range(i+1,n):
        use=True
        for h in range(n):
            if h!=i and h!=j and wf[i][j] > wf[i][h]+wf[h][j]:
                print(-1)
                exit()
            if h!=i and h!=j and wf[i][j] == wf[i][h]+wf[h][j]:
                use=False
        if use:
            ans+=wf[i][j]
print(int(ans))
### BUGGY CODE ###

import itertools, fractions
A, B = map(int, input().split())
cd_list = []
if A >= B:
    cd_list = [n for n in range(1, B+1) if A%n==0 and B%n==0]
else:
    cd_list = [n for n in range(1, A+1) if A%n==0 and B%n==0]
prime_list = [1]
for i in range(1, len(cd_list)):
    if all([1 if fractions.gcd(cd_list[i], n)==1 else 0 for n in cd_list[:i]]):
        prime_list.append(cd_list[i])
print(len(prime_list))


### FIXED CODE ###

import fractions, math
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    if temp != 1:
        arr.append([temp, 1])
    if arr == []:
        arr.append([n, 2])
    return arr
A, B = map(int, input().split())
N = fractions.gcd(A, B)
prime_list = factorization(N)
if N == 1:
    print(1)
else:
    print(len(prime_list)+1)
### BUGGY CODE ###

import bisect

N, M, Q = map(int, input().split())
trains = [list(map(int, input().split())) for i in range(M)]
queries = [list(map(int, input().split())) for i in range(Q)]
trains.sort(key = lambda x: x[1])

A  =[[0] * N for _ in range(N)] #A[i][j] = (i + 1) -> (j+1)なる列車の本数
for train in trains:
    li, ri = train
    A[li - 1][ri - 1] += 1
dp = [[0 for _ in range(N)] for __ in range(N)]

def ans(left, right):
    if left > right:
        return 0
    elif left == right:
        dp[left][right] = A[left][right]
        return dp[left][right]
    else:
        dp[left][right] = ans(left, right-1) + ans(left + 1, right) + A[left][right] - ans(left + 1, right - 1)
        return dp[left][right]

for q in range(Q):
    pi, qi = queries[q]
    print(ans(pi - 1, qi - 1))


### FIXED CODE ###

N, M, Q = map(int, input().split())
trains = [list(map(int, input().split())) for i in range(M)]
queries = [list(map(int, input().split())) for i in range(Q)]

A  =[[0] * N for _ in range(N)] #A[i][j] = (i + 1) -> (j+1)なる列車の本数
for train in trains:
    li, ri = train
    A[li - 1][ri - 1] += 1

dp = [[0] * N for __ in range(N)]
for i in range(N - 1, -1, -1):
    for j in range(i, N):
        if i == j:
            dp[i][j] = A[i][j]
        else:
            dp[i][j] = dp[i][j - 1] + dp[i + 1][j] + A[i][j] - dp[i + 1][j - 1]
for q in range(Q):
    pi, qi = queries[q]
    print(dp[pi - 1][ qi - 1])
### BUGGY CODE ###

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [0]*X*Y
i = 0
for a in A:
    for b in B:
        AB[i] = a + b
        i += 1
AB.sort(reverse=True)

ans = [0]*min(K, len(AB))*Z
i, j = 0, 0
for i in range(min(K, len(AB))):
    for c in C:
        ans[j] = AB[i] + c
        j += 1
ans.sort(reverse=True)

for i in range(min(K, len(AB)*Z)):
    print(ans[i])

### FIXED CODE ###

X, Y, Z, K =map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = []
for i in range(X):
  for j in range(Y):
    AB.append(A[i] + B[j])
AB.sort(reverse=True)

ans = []
for i in range(min(K, len(AB))):
  for j in range(Z):
    ans.append(AB[i] + C[j])
ans.sort(reverse=True)

for i in range(K):
  print(ans[i])
### BUGGY CODE ###

def main():
    import sys
    s = str(input())
    t = str(input())
    s += s
    n = len(s)
    m = len(t)
    result = 0
    point = 0
    counter = 0
    i = t[0]
    c = 1
    while c <= m:
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            if c < m:
                i = t[c]
            c += 1
            counter = 0
        else:
            result += n - point
            point = 0
            counter += 1
            if counter >= 2:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    s = str(input())
    t = str(input())
    if set(t) <= set(s):
        x = -1
        counter = 0
        for i in t:
            x = s.find(i, x+1)
            if x == -1:
                counter += 1
                x = s.find(i)
        print(x+1+counter*len(s))
    else:
        print(-1)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math
from functools import reduce
from itertools import product

n = int(input())

l = list(product(range(1, 1+n), repeat=3))
# print(l)

def gcd(*numbers):
    return reduce(math.gcd, numbers)

print(sum([gcd(*i) for i in l]))

### FIXED CODE ###

import math
from functools import reduce
from itertools import product

def gcd(*numbers):
    return reduce(math.gcd, numbers)

n = int(input())

l = product(range(1, 1+n), repeat=3)

print(sum([gcd(*i) for i in l]))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
tmp=0
for i in range(n-1):
    for j in range(i+1,n):
        tmp += ((a[i] % mod) * (a[j] % mod)) % mod
print(tmp % mod)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
tmp = 0
sum = 0
for i in range(n):
    sum += a[i]
    sum %= mod
for i in range(n-1):
    sum -= a[i]
    if sum<0:
        sum+=mod
    tmp += a[i]*sum
    tmp %= mod 
print(tmp)
### BUGGY CODE ###

s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if (count%2 == 0 and int(q[1]) == 1) or (count%2 == 1 and int(q[1]) == 2):
            left.insert(0, q[2])
        else:
            right.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 1:
    left = left[::-1]
    s = s[::-1]
    right = right[::-1]
    print(right+s+left)
else:
    print(left+s+right)

### FIXED CODE ###

s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if count%2 == 0:
            if int(q[1]) == 1:
                left.append(q[2])
            else:
                right.append(q[2])
        else:
            if int(q[1]) == 1:
                right.append(q[2])
            else:
                left.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 0:
    left = left[::-1]
    print(left+s+right)
else:
    s = s[::-1]
    right = right[::-1]
    print(right+s+left)
### BUGGY CODE ###

import itertools
K, S = map(int, input().split())
ans = 0

for i in itertools.product(range(K+1), repeat=3):
    if sum(i) == S:
        ans += 1
print(ans)

### FIXED CODE ###

K, S = map(int, input().split())

ans = 0
for x in range(K+1):
    for y in range (K+1):
        if  0 <= S - x- y <= K:
            ans +=1

print(ans)
### BUGGY CODE ###

import itertools

n=int(input())
s=list(filter(lambda _s: _s == "M" or _s == "A" or _s == "R" or _s == "C" or _s == "H", (input()[0] for _ in range(n))))

if len(s) < 3:
    print(0)
    quit()

print(len(list(filter(lambda x: len(set(x))==3, itertools.combinations(s, 3)))))


### FIXED CODE ###

import itertools
n = int(input())
s = [input()[0] for _ in range(n)]
c = list(itertools.combinations(set(filter(lambda x: x in "MARCH", s)), 3))

ans = 0
for _c in c:
    ans += s.count(_c[0])*s.count(_c[1])*s.count(_c[2])
print(ans)
### BUGGY CODE ###

#典型dp
n,t=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(key=lambda x:x[0],reverse=True)
m=ab[0][0]
ab.sort(key=lambda x:x[0])
dp=[0]*(t+m)

for i in range(n):
    dp_sub=[0]*(t+m)
    for j in range(t):
        if j==0:
            #print(ab[i][1])
            dp_sub[ab[i][0]]=ab[i][1]
        else:
            if dp[j]!=0:
                #print(ab[i][0])
                #print()
                dp_sub[j+ab[i][0]]=dp[j]+ab[i][1]
    for j in range(t+m):
        dp[j]=max(dp[j],dp_sub[j])
print(max(dp))
#print(len(dp))


### FIXED CODE ###

n,t=map(int,input().split())
dish=[list(map(int,input().split())) for i in range(n)]
dish.sort()
dp=[0]*6001
for i in range(n):
    a,b=dish[i]
    for j in range(t-1,-1,-1):
        dp[j+a]=max(dp[j+a],dp[j]+b)
print(max(dp))
### BUGGY CODE ###

from heapq import*
from collections import*
n,*a=map(int,open(0).read().split())
ans=-float("inf")
b=a[:n]
c=deque(a[n:])
heapify(b)
s=sum(b)
s2=sum(nsmallest(n,c))
red=[s-s2]
for i in range(n,n*2):
    s+=a[i]-heappushpop(b,a[i])
    c.popleft()
    s2=sum(nsmallest(n,c))
    red+=[s-s2]
print(max(red))

#あｈｊｆｋぁｄｓｈｆｌｋじゃｇはｌｋｊｄｊｇｌｋ；あｊｄｇｌｋ；あｄｓｊｇｌ；あｊｓｄｇｌｋ；あｊｓｄｇｌ；かｓ

### FIXED CODE ###

from heapq import*
n,*a=map(int,open(0).read().split())
b=a[:n]
c=[-i for i in a[n:][::-1]]
d=c[:n]
e=c[n:]
heapify(b)
heapify(d)
s=sum(b)
t=sum(d)
red=[s]
blue=[t]
for i in range(n,n*2):
    s+=a[i]-heappushpop(b,a[i])
    t+=c[i]-heappushpop(d,c[i])
    red+=[s]
    blue+=[t]
print(max(map(sum,zip(red,blue[::-1]))))

#あｈｊｆｋぁｄｓｈｆｌｋじゃｇはｌｋｊｄｊｇｌｋ；あｊｄｇｌｋ；あｄｓｊｇｌ；あｊｓｄｇｌｋ；あｊｓｄｇｌ；かｓ
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 0
keisu = [0]*n

for i in range(n):
    keisu[i] += sum(a[:i])


for i in range(n):
    ans += a[i] * keisu[i]

        
        

print(ans%(1000000000+7))


### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 0
keisu = [0]*n
t = 0

for i in range(n):
    
    keisu[i] += t
    t += a[i]


for i in range(n):
    ans += a[i] * keisu[i]

        
        

print(ans%(1000000000+7))
### BUGGY CODE ###

import numpy as np

S = input()
mod = 10**9 + 7
dp = np.zeros(13)
dp[0] = 1

M = np.zeros((13, 13))
for i in range(13):
    for d in range(10):
        M[i][(i * 10 + d) % 13] += 1

for i in range(len(S)):
    ndp = np.zeros(13)
    if S[i] == '?':
        ndp = np.dot(dp, M)
    else:
        d = int(S[i])
        for j in range(13):
            ndp[(j * 10 + d) % 13] += dp[j]
    dp = ndp % mod

print(int(dp[5]))


### FIXED CODE ###

import numpy as np
 
s = input()
p = np.array([0]*13)
p[0] = 1
 
amari = [[0]*13 for _ in range(13)]
for j in range(10):
  for i in range(13):
    amari[i][(i*10+j)%13] += 1
M = np.array(amari)
 
for si in range(len(s)):
  a = np.array([0]*13)
  if s[si] == '?':
    a = np.dot(p,M)
  else:
    c = int(s[si])
    for i in range(13):
      a[(i*10+c)%13] += p[i]
  p = np.mod(a,1000000007)
print(p[5])
### BUGGY CODE ###

n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n-2):
  a=l[i]
  
  for j in range(i+1,n-1):
    b=l[j]
    k=j
    while k<n and l[k]<a+b:
      k+=1
    ans+=k-j-1
print(ans)




### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n-2):
  a=l[i]
  k=i
  for j in range(i+1,n-1):
    b=l[j]
    
    while k<n and l[k]<a+b:
      k+=1
    ans+=k-j-1
print(ans)
### BUGGY CODE ###

mod = 1e9 +7
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n-1):
    for y in range(i+1,n):
        ans += (a[i]*a[y])
        ans%=mod
print(int(ans))

### FIXED CODE ###

from itertools import accumulate
mod = 10**9 +7

def main():
    n = int(input())
    a = list(map(int,input().split()))
    ans =0
    ac = list(accumulate(a))

    for i in range(n):
        ans += a[i]* (ac[-1]-ac[i])
        ans %=mod
    print(ans)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

num = int(input())
yaku = make_divisors(num)
ans = float("inf")
for i in yaku:
    for j in yaku:
        if i*j==num:
            ans=min(ans,i+j-2)
print(ans)

### FIXED CODE ###

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(int(i+n/i-2))
    return divisors
ans = float("inf")
for i in make_divisors(int(input())):
    
    ans = min(ans,i)
print(ans)