Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil
from bisect import bisect_right

N = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [True for _ in range(N)]

ans = 0
for i in list(range(N))[::-1]:
    if not b[i]: continue
    t = a[i]
    flg = False
    left = log2(t)
    for j in list(range(ceil(left), int(1.414 + left) + 1))[::-1]:
        pair = 2 ** j - t
        right = bisect_right(a, pair)
        k = min(right, i - 1)
        while k >= 0:
            if a[k] < pair: break
            if a[k] == pair:
                if b[k]:
                    if k != i:
                        b[k] = False
                        b[i] = False
                        ans += 1
                        flg = True
                        break
            k -= 1
        if flg:
            break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil
from bisect import bisect_right

N = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [True for _ in range(N)]

ans = 0
for i in list(range(N))[::-1]:
    if not b[i]: continue
    t = a[i]
    flg = False
    left = log2(t)
    for j in list(range(ceil(left), int(1.414 + left) + 1))[::-1]:
        pair = 2 ** j - t
        right = bisect_right(a, pair)
        k = min(right, N - 1)
        while k >= 0:
            if a[k] < pair: break
            if a[k] == pair:
                if b[k]:
                    if k != i:
                        b[k] = False
                        b[i] = False
                        ans += 1
                        flg = True
                        break
            k -= 1
        if flg:
            break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil
from bisect import bisect_left, bisect_right

N = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [True for _ in range(N)]

ans = 0
for i in list(range(N))[::-1]:
    if not b[i]: continue
    t = a[i]
    flg = False
    for j in list(range(ceil(log2(t)), int(log2(t * 2)) + 1))[::-1]:
        pair = 2 ** j - t
        left = bisect_left(a, pair)
        right = bisect_right(a, pair)
        # print(t, pair, a, b, left, right)
        if left == right:
            if left < N and left != i:
                if a[left] == pair and b[left]:
                    b[left] = False
                    b[i] = False
                    ans += 1
                    flg = True
                    break
        else:
            k = min(right - 1, N - 1)
            while k >= max(0, left):
                if a[k] != pair: break
                if b[k]:
                    if k != i:
                        b[k] = False
                        b[i] = False
                        ans += 1
                        flg = True
                        break
                k -= 1
            if flg:
                break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)

Buggy python submission (verdict: Runtime Error):
from math import log2, ceil
from bisect import bisect_left, bisect_right

N = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [True for _ in range(N)]

ans = 0
for i in list(range(N))[::-1]:
    if not b[i]: continue
    t = a[i]
    flg = False
    for j in list(range(ceil(log2(t)), int(log2(t * 2)) + 1))[::-1]:
        pair = 2 ** j - t
        left = bisect_left(a, pair)
        right = bisect_right(a, pair)
        # print(t, pair, a, b, left, right)
        if left == right:
            if left < N and left != i:
                if a[left] == pair and b[left]:
                    b[left] = False
                    b[i] = False
                    ans += 1
                    flg = True
                    break
        else:
            k = right - 1
            k = min(right - 1, N - 1)
            while k >= max(0, left):
                if a[k] == pair and b[k] and k != i:
                    b[k] = False
                    b[i] = False
                    ans += 1
                    flg = True
                    break
                k -= 1
            if flg:
                break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil
from bisect import bisect_left, bisect_right

N = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [True for _ in range(N)]

ans = 0
for i in list(range(N))[::-1]:
    if not b[i]: continue
    t = a[i]
    flg = False
    for j in list(range(ceil(log2(t)), int(log2(t * 2)) + 1))[::-1]:
        pair = 2 ** j - t
        left = bisect_left(a, pair)
        right = bisect_right(a, pair)
        # print(t, pair, a, b, left, right)
        if left == right:
            if left < N and left != i:
                if a[left] == pair and b[left]:
                    b[left] = False
                    b[i] = False
                    ans += 1
                    flg = True
                    break
        else:
            k = right - 1
            k = min(right - 1, N - 1)
            while k >= max(0, left):
                if a[k] == pair and b[k] and k != i:
                    b[k] = False
                    b[i] = False
                    ans += 1
                    flg = True
                    break
                k -= 1
            if flg:
                break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil


def bisect(array, x, left_limit):
    left = left_limit
    right = N
    # print(array, x, left, right)
    while left < right:
        target = (left + right) // 2
        if array[target] == x:
            if b[target]:
                return target
            idx = target - 1
            while array[idx] == x:
                if b[idx] and idx >= left:
                    return idx
                else:
                    idx -= 1
                    if idx < left:
                        break
            idx = target + 1
            while array[idx] == x:
                if b[idx]:
                    return idx
                else:
                    idx += 1
                if idx >= right:
                    break
            return -1
        elif array[target] < x:
            right = target
        else:  # array[target] > x
            left = target + 1
    return -1


N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = [True for _ in range(N)]

ans = 0
for i in range(N):
    if not b[i]: continue
    t = a[i]
    # print(a, b)
    idx = []
    for j in list(range(ceil(log2(t)), int(log2(t * 2)) + 1))[::-1]:
        pair = 2 ** j - t
        # print(t, pair)
        res = bisect(a, pair, i + 1)
        if res != -1:
            b[res] = False
            b[i] = False
            ans += 1
            break

print(ans)


Fixed submission:
# https://beta.atcoder.jp/contests/agc029/submissions/3804087
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
b = Counter(a)

ans = 0
for t in a:
    s = (1 << t.bit_length()) - t
    if t == s:
        ans += b[t] // 2
        b[t] %= 2
    else:
        u = min(b[s], b[t])
        ans += u
        b[s] -= u
        b[t] -= u

print(ans)