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 C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

using namespace std;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())

map<lli,vector<lli>> k;
lli flag[100100];

void func(lli now,lli &bNum,lli &wNum,lli color){
	if(flag[now])return;

	if(color==0)bNum++;
	else wNum++;
	flag[now]=color+1;

	for(auto e:k[now]){
		func(e,bNum,wNum,(color+1)%2);
	}
}

signed main(){

	lli n;
	cin>>n;
	vector<lli> b;

	lli ni=1;
	while(ni<=3*1e+9){
		b.push_back(ni);
		ni*=2;
	}
	map<lli,vector<lli>> index;

	if(DEBUG){
	REP(i,0,b.size()){
		cout<<b.at(i)<<endl;
	}
	}
	vector<lli> a(n);

	REP(i,0,n){
		cin>>a[i];
		index[a[i]].push_back(i);
	}

	REP(i,0,n){
		lli now=a[i];
		if(DEBUG)cout<<"now="<<now<<endl;
		REP(j,0,b.size()){
			lli target = b[j]-now;
			if(target<0)continue;
			if(index[target].size()==0)continue;
			for(auto nextIndex:index[target]){
				if(DEBUG)cout<<"nextIndex="<<nextIndex<<" next="<<a[nextIndex]<<endl;
				if(nextIndex==i)continue;
				k[i].push_back(nextIndex);
			}
		}
	}

	lli sum=0;
	if(DEBUG){
	REP(i,0,n){
		if(DEBUG)cout<<"i="<<i<<endl;
		for(auto e:k[i])cout<<e<<" ";
		cout<<endl;
	}
	}

	REP(i,0,n){
		if(flag[i]!=0)continue;
		lli b=0,w=0;
		func(i,b,w,0);
		sum+=min(b,w);
	}

	cout<<sum<<endl;

	return 0;
}