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: Wrong Answer)

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<unordered_map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;


/*
6
3 5 5 11 13 19
*/

struct Dinic{
	const long long INF=1e18+9;
	
	struct edge {
		long long to,cap,rev;
		edge(){}
		edge(long long to,long long cap,long long rev):to(to),cap(cap),rev(rev){}
	};

	long long n;
	vector<vector<edge> > G;
	vector<map<long long,long long> > M;
	vector<long long> level,iter;

	Dinic(){}
	Dinic(long long sz):n(sz),G(n),M(n),level(n),iter(n){}
	
	void add_edge(long long from,long long to,long long cap){
		M[from][to]=G[from].size();
		M[to][from]=G[to].size();
		G[from].push_back(edge(to,cap,G[to].size()));
		// undirected
		//G[to].push_back(edge(from,cap,G[from].size()-1));
		// directed
		G[to].push_back(edge(from,0,G[from].size()-1));
	}
	
	void bfs(long long s){
		fill(level.begin(),level.end(),-1);
		queue<long long> que;
		level[s]=0;
		que.push(s);
		while(!que.empty()){
			long long v=que.front();que.pop();
			for(long long i=0;i<(long long)G[v].size();i++){
				edge &e = G[v][i];
				if(e.cap>0&&level[e.to]<0){
					level[e.to]=level[v]+1;
					que.push(e.to);
				}
			}
		}
	}
	
	long long dfs(long long v,long long t,long long f){
		if(v==t) return f;
		for(long long &i=iter[v];i<(long long)G[v].size();i++){
			edge &e=G[v][i];
			if(e.cap>0&&level[v]<level[e.to]){
				long long d = dfs(e.to,t,min(f,e.cap));
				if(d>0){
					e.cap-=d;
					G[e.to][e.rev].cap+=d;
					return d;
				}
			}
		}
		return 0;
	}
	
	long long flow(long long s,long long t,long long lim){
		long long fl=0;
		for(;;){
			bfs(s);
			if(level[t]<0||lim==0) return fl;
			fill(iter.begin(),iter.end(),0);
			long long f;
			while((f=dfs(s,t,lim))>0){
				fl+=f;
				lim-=f;
			}
		}
	}

	long long flow(long long s,long long t){
		return flow(s,t,INF);
	}

	//cap==1 only
	bool back_edge(long long s,long long t,long long from, long long to){
		for(long long i=0;i<(long long)G[from].size();i++) {
			edge& e=G[from][i];
			if(e.to==to) {
				if(e.cap==0&&flow(from,to,1)==0) {
					flow(from,s,1);
					flow(t,to,1);
					return 1;
				}
			}
		}
		return 0;
	}
};

int n,a[200010],cnt=1,obj=1;
vector<ll> b,c;
map<int,int> m;

int main(void){
	cin>>n;
	Dinic f(2*n+2);
	reg(i,1,n)cin>>a[i];
	sort(a+1,a+n+1);
	reg(i,2,n){
		if(a[i]==a[i-1]){
			cnt++;
		}else{
			b.push_back(a[i-1]);
			c.push_back(cnt);
			cnt=1;
		}
	}
	b.push_back(a[n]);
	c.push_back(cnt);
	rep(i,b.size()){
		f.add_edge(0,i+1,c[i]);
		f.add_edge(i+1+n,2*n+1,c[i]);
		m[b[i]]=i+1;
		// cout<<b[i]<<","<<c[i]<<endl;
	}
	rep(idygfy,31){//>=2*10^9
		obj*=2;
		rep(i,b.size()){
			if(obj-b[i]<=0)break;
			if(m.count(obj-b[i])){
				if(m[obj-b[i]]==i+1){
					f.add_edge(i+1,m[obj-b[i]]+n,c[i]/2);
				}else if(m[obj-b[i]]>i+1){
					f.add_edge(i+1,m[obj-b[i]]+n,1e9);
				}
			}
		}
	}
	cout<<f.flow(0,2*n+1)<<endl;
	return 0;
}