Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())

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

"""

2べきになる条件は？
→片方が2の補数であること

Nが大きい→貪欲的に
最大の奴はそれより小さい奴としかペアを組めない
→大きい奴から貪欲にとっけいけばいける？

いけそうかなぁ
"""

A.sort()
An = []
for i in range(N):
    if i == 0 or An[-1][0] != A[i]:
        An.append([ A[i] , 1 ])
    else:
        An[-1][1] += 1

import bisect
ans = 0

#print (An)

for i in range(len(An)-1,-1,-1):

    for j in range(An[i][1]):

        An[i][1] -= 1

        for k in range(30,-1,-1):
            if 2**k <= An[i][0]:
                continue

            ind = bisect.bisect_left(An,[2**k - An[i][0],0])
            if ind < len(An) and An[ind][0] == 2**k - An[i][0] and An[ind][1] > 0:
                An[ind][1] -= 1
                ans += 1
                break

print (ans)

Fixed submission:

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

B = list(map(int,input().split()))
B.sort()

A = []
AS = []
for i in range(N):
    if len(A) == 0:
        A.append(B[0])
        AS.append(1)
    else:
        if A[-1] == B[i]:
            AS[-1] += 1
        else:
            A.append(B[i])
            AS.append(1)


"""
p = [i for i in range(N)]
rank = [1] * N
"""
ans = 0

for i in range(len(A)):

    i = len(A) - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    ind = bisect.bisect_left(A,now-A[i])

    if A[ind] + A[i] == now:

        if ind == i:
            ans += AS[i] // 2
            AS[i] = AS[i] % 2

        else:
            ans += min(AS[i],AS[ind])
            AS[ind] = max(AS[ind] - AS[i] , 0)



    #print (now,na,ind)

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

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

"""
p = [i for i in range(N)]
rank = [1] * N
"""
used = [False] * N
ans = 0

for i in range(N):

    i = N - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    na = A[-1]
    del A[-1]

    if used[i]:
        continue

    ind = bisect.bisect_left(A,now-na)

    while ind < i and used[ind] and na + A[ind] == now:
        ind += 1

    if ind < i and na + A[ind] == now:
        used[ind] = True
        ans += 1

    #print (now,na,ind)

print (ans)
        


Fixed submission:

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

B = list(map(int,input().split()))
B.sort()

A = []
AS = []
for i in range(N):
    if len(A) == 0:
        A.append(B[0])
        AS.append(1)
    else:
        if A[-1] == B[i]:
            AS[-1] += 1
        else:
            A.append(B[i])
            AS.append(1)


"""
p = [i for i in range(N)]
rank = [1] * N
"""
ans = 0

for i in range(len(A)):

    i = len(A) - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    ind = bisect.bisect_left(A,now-A[i])

    if A[ind] + A[i] == now:

        if ind == i:
            ans += AS[i] // 2
            AS[i] = AS[i] % 2

        else:
            ans += min(AS[i],AS[ind])
            AS[ind] = max(AS[ind] - AS[i] , 0)



    #print (now,na,ind)

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

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

"""
p = [i for i in range(N)]
rank = [1] * N
"""
used = [False] * N
ans = 0

for i in range(N):

    i = N - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    na = A[-1]
    del A[-1]

    if used[i]:
        continue

    ind = bisect.bisect_left(A,now-na)

    while ind < i and used[ind] and na + A[ind] == now:
        ind += 1

    if ind < i and na + A[ind] == now:
        used[ind] = True
        ans += 1

    #print (now,na,ind)

print (ans)
        


Fixed submission:

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

B = list(map(int,input().split()))
B.sort()

A = []
AS = []
for i in range(N):
    if len(A) == 0:
        A.append(B[0])
        AS.append(1)
    else:
        if A[-1] == B[i]:
            AS[-1] += 1
        else:
            A.append(B[i])
            AS.append(1)


"""
p = [i for i in range(N)]
rank = [1] * N
"""
ans = 0

for i in range(len(A)):

    i = len(A) - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    ind = bisect.bisect_left(A,now-A[i])

    if A[ind] + A[i] == now:

        if ind == i:
            ans += AS[i] // 2
            AS[i] = AS[i] % 2

        else:
            ans += min(AS[i],AS[ind])
            AS[ind] = max(AS[ind] - AS[i] , 0)



    #print (now,na,ind)

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

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

"""
p = [i for i in range(N)]
rank = [1] * N
"""
used = [False] * N
ans = 0

for i in range(N):

    i = N - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    na = A[-1]
    del A[-1]

    if used[i]:
        continue

    ind = bisect.bisect_left(A,now-na)

    while ind < i and used[ind]:
        ind += 1

    if ind < i and na + A[ind] == now:
        used[ind] = True
        ans += 1

    #print (now,na,ind)

print (ans)
        


Fixed submission:

import bisect

"""
def wantp(n):

    wq = []
    while p[n] != n:
        wq.append(n)
        n = p[n]

    for i in wq:
        p[i] = n

    return n
"""


N = int(input())

B = list(map(int,input().split()))
B.sort()

A = []
AS = []
for i in range(N):
    if len(A) == 0:
        A.append(B[0])
        AS.append(1)
    else:
        if A[-1] == B[i]:
            AS[-1] += 1
        else:
            A.append(B[i])
            AS.append(1)


"""
p = [i for i in range(N)]
rank = [1] * N
"""
ans = 0

for i in range(len(A)):

    i = len(A) - 1 - i
    now = 2 ** 33

    while A[i] * 2 < now:
        now //= 2

    ind = bisect.bisect_left(A,now-A[i])

    if A[ind] + A[i] == now:

        if ind == i:
            ans += AS[i] // 2
            AS[i] = AS[i] % 2

        else:
            ans += min(AS[i],AS[ind])
            AS[ind] = max(AS[ind] - AS[i] , 0)



    #print (now,na,ind)

print (ans)