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 <iostream>
#include <fstream>
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string> 
#include <vector>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <random>

using namespace std;
using ll = long long;

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i, m, n) for(int i = int(m);i < int(n);i++)
#define ALL(obj) (obj).begin(),(obj).end()

const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template<class T> void corner(bool flg, T hoge) {
	if (flg) {
		cout << hoge << endl;
		exit(0);
	}
	else return;
}

template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {
	o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;
}

template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {
	o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;
}

template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {
	o << "{" << obj.first << ", " << obj.second << "}"; return o;
}

template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {
	o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;
}

void print(void) {
	cout << endl;
}

template <class Head> void print(Head&& head) {
	cout << head;
	print();
}

template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {
	cout << head << " ";
	print(forward<Tail>(tail)...);
}

void YN(bool flg) {
	cout << ((flg) ? "YES" : "NO") << endl;
}

void Yn(bool flg) {
	cout << ((flg) ? "Yes" : "No") << endl;
}

void yn(bool flg) {
	cout << ((flg) ? "yes" : "no") << endl;
}

//Bipartite_Matching
class Bipartite_Matching {
public:
	int V;
	vector<vector<int>> graph;
	vector<int> match, visit;

	Bipartite_Matching(const int V) :V(V), graph(vector<vector<int>>(V)), match(vector<int>(V, -1)), visit(vector<int>(V)) {
	}

	void make_edge(const int from, const int to) {
		graph[from].push_back(to);
	}

	bool dfs(int i) {
		visit[i] = 0;
		for (auto j : graph[i]) {
			int k = match[j];
			if (k < 0 || (visit[k] && dfs(k))) {
				match[i] = j;
				match[j] = i;
				return true;
			}
		}
		return false;
	}

	ll Max_Matching() {
		ll sum = 0;
		for (int i = 0; i < V; ++i) {
			if (match[i] < 0) {
				for (int j = 0; j < V; ++j) visit[j] = 1;
				if (dfs(i)) sum++;
			}
		}
		return sum;
	}

};


int main() {
	int N; cin >> N;
	V<ll> A(N),pow2(35,1);
	REP(i, N) scanf("%d", &A[i]);
	FOR(i, 1, 35) pow2[i] = pow2[i-1] * 2;
	map<ll, vector<int>> mp;
	REP(i, N) mp[A[i]].push_back(i);
	
	Bipartite_Matching BP(N);
	REP(i,N) {
		FOR(j, 1, 35){
			ll c = pow2[j] - A[i];
			if (c <= 0) continue;
			for(int k = 0; k < mp[c].size(); ++k){
				if (k == mp[c][k])continue;
				BP.make_edge(i, mp[c][k]);
			}
		}
	}
	cout << BP.Max_Matching() << endl;
	
	return 0;
}