Harryxun/stf_run huggingface dataset:
# import sys
# input = sys.stdin.readline
# import re
# import itertools
import collections

def main():
    n = int(input())
    s = input_list()
    sc = collections.Counter(s)
    q = int(input())
    ans = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        sc.pop(b)
        a = 0
        for k, v in sc.items():
            a += k*v
        ans.append(a)
    for a in ans:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

def input_list():
    return list(map(int, input().split()))

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()


### FIXED CODE ###

# import sys
# input = sys.stdin.readline
# import re
# import itertools
import collections

def main():
    n = int(input())
    s = input_list()
    sc = collections.Counter(s)
    q = int(input())
    data = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        if i > 0:
            data.append(c*sc[b] - b * sc[b])
        if b in sc:
            sc.pop(b)
    ans = 0
    for k, v in sc.items():
        ans += k*v
    aa = [ans]
    for i, v in enumerate(data[::-1]):
        aa.append(aa[i] - v)
    for a in aa[::-1]:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

def input_list():
    return list(map(int, input().split()))

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()
#python3
INF = int(1e9)
dd = [(-1, 0), (1, 0), (0, 1), (0, -1)]

def main():
    h, w = map(int, input().split())
    maze = []
    ans = 0
    for _ in range(h):
        data = input()
        maze.append(data)    
    
    for i in range(h):
        for j in range(w):
            if maze[i][j] == "#":
                continue
            start = (i, j)
            from collections import deque         
            g = [[INF for _ in range(w)] for _ in range(h)] 
            q = deque()
            q.append(start)
            g[i][j] = 0
            while len(q) > 0:
                x, y = q.popleft()
                for d in dd:
                    di = x + d[0]
                    dj = y + d[1]
                    if (di < 0) | (dj < 0) | (di >= h) | (dj >= w): 
                        continue
                    if maze[di][dj] == "#":
                        continue
                    if g[di][dj] != INF:
                        continue
                    q.append((di, dj))
                    g[di][dj] = g[x][y]+1 
            

        for i in range(h):
            for j in range(w):
                if g[i][j] == INF:
                    continue
                ans = max(ans, g[i][j])
    print(ans)
main()

### FIXED CODE ###

#python3
from collections import deque
from collections import namedtuple

Vertex = namedtuple('Vertex', ['i', 'j'])  
INF = int(1e10)
di = [1, 0, -1, 0]
dj = [0, 1, 0, -1]

def main():
    h, w = map(int, input().split())
    maze = [input() for i in range(h)]
    ans = 0

    for si in range(h):
        for sj in range(w):
            if maze[si][sj] == '#':
                continue 
            dist = [[INF for i in range(w)] for j in range(h)]
            q = deque()

            def update(i, j, x):
                if dist[i][j] != INF:
                    return None
                dist[i][j] = x
                q.append(Vertex(i, j))


            update(si, sj, 0)
            while len(q) != 0:
                i, j = q.popleft()
                for dir in range(4):
                    ni = i+di[dir]
                    nj = j+dj[dir]
                    if (ni < 0) | (ni >= h) | (nj < 0) | (nj >= w):
                        continue

                    if maze[ni][nj] == '#':
                        continue
                    update(ni, nj, dist[i][j]+1)

            for i in range(h):
                for j in range(w):
                    if dist[i][j] == INF:
                        continue
                    ans = max(ans, dist[i][j])

    print(ans)

main()
import math

while True:
    n = int(raw_input())
    if n == -1:
        break
    r, theta = math.sqrt(2.0), pi/4.0
    for i in range(1,n-1):
        r = math.sqrt(r**2 + 1.0)
        theta += math.asin(1.0/r)
    print r*math.cos(theta)
    print r*math.sin(theta)

### FIXED CODE ###

import math

while True:
    n = int(raw_input())
    if n == -1:
        break
    r, theta = 1.0, 0.0
    for i in range(n-1):
        r = math.sqrt(r**2 + 1.0)
        theta += math.asin(1.0/r)
    print r*math.cos(theta)
    print r*math.sin(theta)
H,A=map(int,input().spkit())
if H%A==0:
    print(H//A)
else:
    print((H//A)+1)

### FIXED CODE ###

H,N=map(int,input().split())
if H%N==0:
    print(H//N)
else:
    print((H//N)+1)
x,t = map(int,input().split())
print(max(0, x=t))

### FIXED CODE ###

x,t = map(int,input().split())
print(max(0, x-t))
from itertools import permutations

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, pi in enumerate(permutations(range(1, n+1))):
    if pi == p:
        a = i
    elif pi == q:
        b = i
print(abs(a - b))

### FIXED CODE ###

from itertools import permutations

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, pi in enumerate(permutations(range(1, n+1))):
    if pi == p:
        a = i
    if pi == q:
        b = i
print(abs(a - b))
def main():
    N, M, V, P = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    A.sort(reverse=True)

    def is_ok1(mid, val):
        if val >= A[mid]:
            return True
        else:
            return False

    def is_ok2(mid, val):
        if val > A[mid]:
            return True
        else:
            return False

    def binary_search_meguru(val, is_ok):
        left = -1
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid, val):
                right = mid
            else:
                left = mid
        return left

    not_ans = 0
    for i in range(N):
        top = A[i] + M
        upper = binary_search_meguru(top, is_ok1) + 1
        skip = 0
        if (-(V-1)) % N <= i+1:
            skip = 1
        if P <= upper:
            not_ans += 1
        elif A[-(V-1) + (P-1) - upper - skip] + M > top:
            top_less = P - 1
            top_less += max(0, N - binary_search_meguru(top, is_ok2) - 1)
            key = (V-1) - top_less
            mo = M % ((i-1)-(P-1))
            if i - mo <= key and top >= A[(P-1) + key] + M//((i-1)-(P-1) + 1) + 1:
                continue
            elif i - mo > key and top >= A[(P-1) + key] + M//((i-1)-(P-1) + 1):
                continue
            else:
                not_ans += 1

    print(N - not_ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    N, M, V, P = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    A.sort(reverse=True)

    def is_ok(mid):
        if mid <= P-1:
            return False  # 採用可能
        if A[mid] + M < A[P-1]:
            return True  # 採用不可能
        votable = M + (P-1) * M + (N - (mid + 1))*M
        for i in range(P-1, mid):
            votable += min(M, max(0, A[mid] + M - A[i]))
        if M*V <= votable:
            return False
        else:
            return True

    def binary_search_meguru():
        left = -1
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid):
                right = mid
            else:
                left = mid
        return right
    print(binary_search_meguru())


if __name__ == '__main__':
    main()
N=int(input())
X=input()
def fx(n,c):
    b=n%c
    if b==0:
        return 1
    if b==1 or b==2:
        return 2
    t=bin(b).count("1")
    return fx(b,t)+1
c=X.count("1")
n=int(X,2)
for i in range(N):
    if X[i]=="0":
        print(fx(n+2**(N-i-1),c+1))
    else:
        print(fx(n-2**(N-i-1),c-1))

### FIXED CODE ###

N=int(input())
X=input()
def fx(n,c):
    if c==0:
        return 0
    b=n%c
    if b==0:
        return 1
    if b==1 or b==2:
        return 2
    t=bin(b).count("1")
    return fx(b,t)+1
c=X.count("1")
n=int(X,2)
lp=[1]*N
lm=[1]*N
for i in range(N-2,-1,-1):
    lp[i]=2*lp[i+1]%(c+1)
    if c==1:
        lm[i]=1
    else:
        lm[i]=2*lm[i+1]%(c-1)
np=n%(c+1)
if c>1:
    nm=n%(c-1)
else:
    nm=1
for i in range(N):
    if X[i]=="0":
        print(fx(np+lp[i],c+1))
    else:
        print(fx(nm-lm[i],c-1))
import numpy as np
N = int(input())
A = np.array(li,
A = A.reshape((int(N/2), 2))[:,0] if N % 2 == 0 else np.append(A, 2, 100).reshape((int((N+1)/2), 2))[:, 0]
print((A % 2 == 1).sum())

### FIXED CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A = A.reshape((int(N/2), 2))[:,0] if N % 2 == 0 else np.append(A, 2, 100).reshape((int((N+1)/2), 2))[:, 0]
print((A % 2 == 1).sum())
a,b = map(str,input().split())

a = int(a)

b = 100*int(b[0])+10*int(b[2])+1*int(v[3])

print((a*b)//100)


### FIXED CODE ###

a,b = map(str,input().split())

a = int(a)

b = 100*int(b[0])+10*int(b[2])+1*int(b[3])

print((a*b)//100)
import math
import sys
from collections import defaultdict
from itertools import product, permutations, combinations

sys.setrecursionlimit(10**7)

N = int(input())


if N%2 == 1:
    print(0)
    exit()

v = math.log(N, 5)

l = [i for i in range(1, math.floor(v+1))]
ll = [2*5**i for i in l]
lll = [N//i for i in ll]
print(sum(lll))

### FIXED CODE ###

import math
import sys
from collections import defaultdict
from itertools import product, permutations, combinations

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


N = int(input())
if N % 2:
    print(0)
    exit()
else:
    N //= 2

ans = 0
while N > 0:
    N //= 5
    ans += N
print(ans)
a=[0]*5
for i in range(5):
  a[i]=int(input())
b=int(input())
if a[4]-a[0]>a[5]:
  print(':(')
else:
  print('Yay!')
  


### FIXED CODE ###

a=[0]*5
for i in range(5):
  a[i]=int(input())
b=int(input())
if a[4]-a[0]>b:
  print(':(')
else:
  print('Yay!')
n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)

### FIXED CODE ###

n,x=map(int,input().split())
Len=[int(i) for i in input().split()]

ans=n+1
test=0
for i in range(n):
  test+=Len[i]
  if test<=x:
    continue
  else:
    ans=i+1
    break
    
print(ans)
n, k, price, special = map(int, input().split())
    result = 0
    for i in range(n):
        if i < k:
            result += price
        else:
            result += special
    
    print(result)
 

### FIXED CODE ###

def iroha():
    n, k, price, special = [int(input()) for i in range(4)]
    result = 0
    for i in range(n):
        if i < k:
            result += price
        else:
            result += special
    print(result)

if __name__ == "__main__":
    iroha()
n,k,s = map(int,input().split())
a=0
b=0
if s % 2 == 1:
    a=(s-1)/2
    b=a+1
else:
    a=s/2
    b=s/2

if a==b:
    a-=1
    b+=1

a=int(a)
b=int(b)

list=[]
while k!=0:
    if k == 1:
        list.append(s)
        k-=1
    elif k >= 2:
        list.append(a)
        list.append(b)
        k-=1
    print(list,k)

if len(list) < n:
    for i in range(n-len(list)):
        list.append(s-1)

for i in list:
    print(i,end=" ")


### FIXED CODE ###

n,k,s = map(int,input().split())
a=0
b=0

list=[]

for i in range(k):
    list.append(s)

if len(list) < n:
    for i in range(n-len(list)):
        if s == 1:
            list.append(s+1)
        else:
            list.append(s-1)

for i in list:
    print(i,end=" ")
import math
N,M = map(int, input().split())
def C(a,b):
    return math.factorial(a+b)/(math.factorial(a)*math.factorial(b))
def pattern(n):
    ans = 0
    i = 0
    while n - 2*i >= 0:
        ans += C(n-i*2,i)
        i += 1
    return ans
ans = 1
mod = 10**9 + 7
l = [0 for i in  range(M)]
for i in range(1,M):
    l[i] = int(input()) - l[i-1] -2
if N-l[M-1]-2 >= 0:
    l.append(N-l[M-1]-2)
l.sort()
for i in range(len(l)):
    if i != 0 and l[i] == l[i-1]:
        ans *= tmp
    else:
        ans *= pattern(l[i])
    ans = ans%mod
    tmp = pattern(l[i])
print(int(ans))

### FIXED CODE ###

N,M = map(int, input().split())
dp = [0 for i in range(N+1)]
dp[0] = 1
dp[1] = 1
for i in range(M):
    dp[int(input())] = None
for i in range(2,N+1):
    if dp[i] != None:
        if dp[i-2] != None:
            dp[i] += dp[i-2]
        if dp[i-1] != None:
            dp[i] += dp[i-1]
mod = 10**9 + 7
print(dp[N]%mod)
d = list(input().split())

for i in range(3):
  if d[i] == "a":
    a = 1
  if d[i] == "b":
    b = 1
  if d[i] == "c":
    c = 1
    
    
if a*b*c == 1:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

a = list(input())
if "a" in a and "b" in a and "c" in a:
	print("Yes")
else:
  print("No")
N,A,B = map(int,input().split())

if (B - A) % 2 == 0:
  print((B - A) // 2)
else:
  if 
  print(min(B - 1 , N - A))

### FIXED CODE ###

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

if (B - A) % 2 == 0:
  print((B - A) // 2)
else:
  NA = N - A
  B1 = B - 1
  if NA < B1:
    distance = N - B
    adjust = distance + 1
    moveA = A + adjust
    print(((N - moveA) // 2) + adjust)
  else:
    distance = A - 1
    adjust = distance + 1
    moveB = B - adjust
    print(((moveB - 1) // 2) + adjust)
N = int(input())
A = list(map(int, input().split()))
if N > 2:
    for i in range(N-1):
        if A[i]>A[i+1]:
            A[i] -= 1
    for i in range(N-1):
        if A[i] > A[i+1]:
            print("No")
            break
    else:
        print("Yes")
        print(range(0))
elif N == 2:
    if A[0] <= A[1]:
        print("Yes")
    else:
        print("No")
elif N == 1:
    print("Yes")


### FIXED CODE ###

a, b,c = map(int, input().split())
d = c-(a-b)
if d<0:
    d = 0
print(d)
inputs = [int(x) for x in input().split()]
n, k = inputs[0], inputs[1]
ls = [int(x) for x in input().split()]
s = sum(ls)

# https://qiita.com/LorseKudos/items/9eb560494862c8b4eb56
divisors = []
for i in range(1, int(s ** 0.5) + 1):
  if s % i == 0:
    divisors.append(i)
    if i != s // i:
      divisors.append(s // i)

divisors.sort(reverse=True)
ans = 1
for d in divisors:
  mods = sorted([l % d for l in ls if l % d != 0])
  opcnt = 0
  is_success = False
  # print("{}: {}".format(d, ", ".join([str(m) for m in mods])))
  while True:
    if mods[0] + mods[-1] < d:
      mods[-1] += mods[0]
      opcnt += mods[0]
      mods.pop(0)
    elif mods[0] + mods[-1] == d:
      opcnt += mods[0]
      mods.pop(0)
      mods.pop(-1)
    else:
      opcnt += (d - mods[-1])
      mods[0] -= (d - mods[-1])
      mods.pop(-1)
    # print("{}: {}".format(d, ", ".join([str(m) for m in mods])))

    if opcnt > k or len(mods) == 1:
      # print("opcnt: {}".format(opcnt))
      is_success = False
      break
    if len(mods) == 0:
      is_success = True
      ans = d
      # print("ans: {}, opcnt: {}".format(ans, opcnt))
      break

  if is_success:
    break

print(ans)

### FIXED CODE ###

inputs = [int(x) for x in input().split()]
n, k = inputs[0], inputs[1]
ls = [int(x) for x in input().split()]
s = sum(ls)

# https://qiita.com/LorseKudos/items/9eb560494862c8b4eb56
divisors = []
for i in range(1, int(s ** 0.5) + 1):
  if s % i == 0:
    divisors.append(i)
    if i != s // i:
      divisors.append(s // i)

divisors.sort(reverse=True)
ans = 1
for d in divisors:
  mods = sorted([l % d for l in ls if l % d != 0])
  opcnt = 0
  is_success = False
  # print("{}: {}".format(d, ", ".join([str(m) for m in mods])))
  while True:
    if opcnt > k or len(mods) == 1:
      # print("opcnt: {}".format(opcnt))
      is_success = False
      break

    if len(mods) == 0:
      is_success = True
      ans = d
      # print("ans: {}, opcnt: {}".format(ans, opcnt))
      break

    if mods[0] + mods[-1] < d:
      mods[-1] += mods[0]
      opcnt += mods[0]
      mods.pop(0)
    elif mods[0] + mods[-1] == d:
      opcnt += mods[0]
      mods.pop(0)
      mods.pop(-1)
    else:
      opcnt += (d - mods[-1])
      mods[0] -= (d - mods[-1])
      mods.pop(-1)
    # print("{}: {}".format(d, ", ".join([str(m) for m in mods])))


  if is_success:
    break

print(ans)
AtCoder, s, Contest = map(str, input().split())
print(A + str(s[0]) + C)


### FIXED CODE ###

AtCoder, s, Contest = map(str, input().split())
print("A" + str(s[0]) + "C")
from collections import Counter
H, W, M = [int(i) for i in input().split()]
g = [[0] * M for i in range(M)]
h = [0] * M
w = [0] * M
for i in range(M):
    h[i], w[i] = [int(j) for j in input().split()]
    g[h[i]-1][w[i]-1] = 1

ch = Counter(h)
cw = Counter(w)

hi = [i for i in ch.keys() if ch[i] == max(ch.values())]
wi = [i for i in cw.keys() if cw[i] == max(cw.values())]

for i in hi:
    for j in wi:
        if g[i-1][j-1] == 0:
            print(ch[i] + cw[j])
            exit()
print(ch[hi[0]] + cw[wi[0]] - 1)

### FIXED CODE ###

from collections import Counter
H, W, M = [int(i) for i in input().split()]
g = set()
h = [0] * M
w = [0] * M
for i in range(M):
    h[i], w[i] = [int(j) for j in input().split()]
    g.add((h[i],w[i]))

ch = Counter(h)
cw = Counter(w)

chm = max(ch.values())
cwm = max(cw.values())
hi = [i for i in ch.keys() if ch[i] == chm]
wi = [i for i in cw.keys() if cw[i] == cwm]

for i in hi:
    for j in wi:
        if (i,j) not in g:
            print(ch[i] + cw[j])
            exit()
print(ch[hi[0]] + cw[wi[0]] - 1)
a,b=map(int,input().split());print(max(a+b,a-b a*b))

### FIXED CODE ###

a,b=map(int,input().split());print(max(a+b,a-b,a*b))
from collections import deque 

def main():
    H, W = map(int, input().split())
    s = []
    num = 0
    for i in range(H):
        s.append(list(input()))
        num += s[i].count("#")
        
    cnt = [[0 for _ in range(W)] for _ in range(H)]
    queue = deque()
    queue.append((0, 0))
    while queue:
        x, y = queue.popleft()
        if(s[x][y] == "#"):
            continue
        if(x == H-1 and y == W-1):
            ans = cnt[x][y]
        s[x][y] = "#"
        
        d = [(-1, 0), (1, 0), (0, 1), (0, -1)]
        for dx, dy in d:
            nx = x + dx
            ny = y + dy
            if(nx < 0 or nx >= H or ny < 0 or ny >= W):
                continue
            queue.append((nx, ny))
            if(s[nx][ny] != "#"):
                cnt[nx][ny] = cnt[x][y]+1
    
    print(H*W-ans-num-1)
    
main()

### FIXED CODE ###

from collections import deque 

def main():
    H, W = map(int, input().split())
    s = []
    num = 0
    for i in range(H):
        s.append(list(input()))
        num += s[i].count("#")
    ans = -1    
    cnt = [[0 for _ in range(W)] for _ in range(H)]
    queue = deque()
    queue.append((0, 0))
    while queue:
        x, y = queue.popleft()
        if(s[x][y] == "#"):
            continue
        if(x == H-1 and y == W-1):
            ans = cnt[x][y]
        s[x][y] = "#"
        
        d = [(-1, 0), (1, 0), (0, 1), (0, -1)]
        for dx, dy in d:
            nx = x + dx
            ny = y + dy
            if(nx < 0 or nx >= H or ny < 0 or ny >= W):
                continue
            queue.append((nx, ny))
            if(s[nx][ny] != "#"):
                cnt[nx][ny] = cnt[x][y]+1
    if(ans == -1):
        print(-1)
    else:
        print(H*W-ans-num-1)
    
main()
from bisect import bisect_left as bl
from collections import deque
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy

A,B,Q=map(int,input().split())
S=[]
T=[]
X=[]
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
def rl(X,a):
    ind=bl(X,a)
    if ind>=len(X):
        return ind-1
    return ind
def ll(X,a):
    ind=bl(X,a)-1
    if ind<0:
        return ind+1
    return ind
t=[]
for i in range(A):
    if abs(T[rl(T,S[i])]-S[i])>=abs(T[ll(T,S[i])]-S[i]):
        t.append(abs(T[ll(T,S[i])]-S[i]))
    else:
        t.apend(abs(T[rl(T,S[i])])-S[i])
s=[]
for i in range(B):
    if abs(S[rl(S,T[i])]-T[i])>=abs(S[ll(S,T[i])]-T[i]):
        s.append(abs(S[ll(S,T[i])]-T[i]))
    else:
        s.apend(abs(S[rl(S,T[i])])-T[i])

for i in range(Q):
    x=int(input())
    a=rl(S,x)
    a=t[a]+abs(S[a]-x)

    b=ll(S,x)
    b=t[b]+abs(S[b]-x)

    c=rl(T,x)
    c=s[c]+abs(T[c]-x)

    d=ll(T,x)
    d=s[d]+abs(T[d]-x)

    print(min(a,b,c,d))


### FIXED CODE ###

from bisect import bisect_left as bl
A,B,Q=map(int,input().split())
S=[]
T=[]
X=[]
INF=10**18
S.append(-INF)
T.append(-INF)
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
S.append(INF)
T.append(INF)
for i in range(Q):
    x=int(input())
    b,d=bl(S,x),bl(T,x)
    res=INF
    for s in [S[b-1],S[b]]:
        for t in [T[d-1],T[d]]:
            d1,d2=abs(s-x)+abs(t-s),abs(t-x)+abs(s-t)
            res=min(res,d1,d2)
    print(res)
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(int i = 0; i < (int)n; ++i)
int main()
{
    int N, M;
    scanf("%d %d", &N, &M);
    int a[N], b[N];
    REP(i, N) scanf("%d %d", &a[i], &b[i]);
    int c[M], d[M];
    REP(i, M) scanf("%d %d", &c[i], &d[i]);
    REP(i, N) {
        int res = -1;
        int dis = 1e9;
        REP(j, M) {
            int tmp = abs(c[j] - a[i]) + abs(d[j] - b[i]);
            if(tmp < dis) dis = tmp, res = j + 1;
        }
        printf("%d\n", res);
    }
    return 0;
}

### FIXED CODE ###

#!/usr/bin/python3
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    cd = [list(map(int, input().split())) for _ in range(m)]

    for a, b in ab:
        arr = []
        for c, d in cd:
            arr.append(abs(a-c) + abs(b-d))
        print(arr.index(min(arr)) + 1)

if __name__ == '__main__':
    main()
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        if "1" not in bin(t)[3:]:
            ans+=1
print(len(ans))


### FIXED CODE ###

n=int(input())
a=[int(i) for i in input().split()]
a.sort()
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
p=max(a)*2
chk=[pow(2,i) for i in range(1,32) if pow(2,i)<=p]
ans=0
for i in a[::-1]:
    if d[i]>0:
        d[i]-=1
        for j in chk[::-1]:
            if j-i in d and d[j-i]>0:
                d[j-i]-=1
                ans+=1
                break
print(ans)