Problem:
Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2

Solution in Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

program main
implicit None
	integer(8)::n,p,i,k,s
	integer(8),allocatable::a(:),b(:)
	real(8)::x,y
	
	s = 0
	read *,n
	allocate(a(n),b(n))
	
	b = 1
	read *,a
	
	call hsort(a,n)
	
	y = 2
	do i = n,2,-1
		if(b(i) == 0) cycle
		p =2**(int(log(real(a(i)))/log(y)+1)) - a(i)
		
		call schint(a,i-1,p,k)
		
		do while (b(k) == 0)
			k = k + 1
		enddo
		
		if(a(k) == p)then
			b(k) = 0 
			s = s + 1
		endif
	enddo
	
	print 101,s
	
101 format(i0)
end program main

subroutine makeheap(a,n)
Implicit None
	integer(8)::n,i,c1,c2,p,t,m
	integer(8)::a(n)
	
	do i = n/2,1,-1
		p = i; c1 = i * 2
		do while (c1 <= n)
			c2 = c1 + 1
			if(c2 <= n .and. a(c2) > a(c1)) c1 = c2
			if(a(p) < a(c1))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				c1 = p*2
				else
					exit
			endif
		enddo
	enddo
end subroutine makeheap

subroutine hsort(a,n)
Implicit None
	integer(8)::n,t,i,p,c1,c2,j
	integer(8)::a(n)
	
	call makeheap(a,n)
	
	do i = n,2,-1
		t = a(i)
		a(i) = a(1)
		a(1) = t
		
		p = 1
		do
			c1 = p*2
			if (c1 >= i) exit
			
			c2 = c1 +1
			if(c2 < i .and. a(c2) > a(c1))then
					c1 = c2
			endif
			
			if(a(c1) >a(p))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				else
					exit
			endif
		enddo
	enddo
end subroutine hsort

subroutine schint(a,e1,x,p)
implicit None
	integer(8)::e1,s,e,x,p,t
	integer(8)::a(e1)
	
	s = 1;e = e1
	
	if(a(e) < x)then
		p = 0
		return
	endif
	
	do
		t = (s + e)/2
		if(a(t) < x)then
			if(a(t+1) >= x)then
				p = t + 1
				return
			endif
			
			s = t
			else if(t == 1 .or. a(t-1) < x)then
				p = t
				return
			else
			e = t
		endif
	enddo
end subroutine schint