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 <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>

using namespace std;
const long long MOD = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ll> pdl;
typedef pair<ld, ld> pdd;
//typedef boost::multiprecision::cpp_int bigint;

template< typename flow_t >
struct Dinic {
	const flow_t INF;

	struct edge {
		int to;
		flow_t cap;
		int rev;
		bool isrev;
		int idx;
	};

	vector< vector< edge > > graph;
	vector< int > min_cost, iter;

	Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}

	void add_edge(int from, int to, flow_t cap, int idx = -1) {
		edge a = { to, cap, (int)graph[to].size(), false, idx };
		graph[from].emplace_back(a);
		a = { from, 0, (int)graph[from].size() - 1, true, idx };
		graph[to].emplace_back(a);
	}

	bool bfs(int s, int t) {
		min_cost.assign(graph.size(), -1);
		queue< int > que;
		min_cost[s] = 0;
		que.push(s);
		while (!que.empty() && min_cost[t] == -1) {
			int p = que.front();
			que.pop();
			for (auto& e : graph[p]) {
				if (e.cap > 0 && min_cost[e.to] == -1) {
					min_cost[e.to] = min_cost[p] + 1;
					que.push(e.to);
				}
			}
		}
		return min_cost[t] != -1;
	}

	flow_t dfs(int idx, const int t, flow_t flow) {
		if (idx == t) return flow;
		for (int& i = iter[idx]; i < graph[idx].size(); i++) {
			edge& e = graph[idx][i];
			if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
				flow_t d = dfs(e.to, t, min(flow, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	flow_t max_flow(int s, int t) {
		flow_t flow = 0;
		while (bfs(s, t)) {
			iter.assign(graph.size(), 0);
			flow_t f = 0;
			while ((f = dfs(s, t, INF)) > 0) flow += f;
		}
		return flow;
	}

	void output() {
		for (int i = 0; i < graph.size(); i++) {
			for (auto& e : graph[i]) {
				if (e.isrev) continue;
				auto& rev_e = graph[e.to][e.rev];
				cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
			}
		}
	}
};

ll N;
vector<ll> A;
map<ll, ll> key;   //どの要素が何番目にあるか
vector<ll> side;   //何番目の要素がどっちにあるか
vector<ll> numcount;   //n番目の要素は全部で何個あるか

int main() {
	cin >> N;
	A.resize(N + 1);
	for (ll n = 0; n < N; n++)cin >> A[n];
	A[N] = (ll)1 << 40;
	::sort(A.begin(), A.end(), [](ll a, ll b) {return a < b; });
	numcount.resize(N + 1, 0);
	ll counter = 1;
	side.resize(N + 1, -1);
	for (ll n = 1; n <= N; n++) {
		if (A[n] == A[n - 1]) {
			counter++;
			continue;
		}
		numcount[n - 1] = counter;
		counter = 1;
		for (ll p = 0; p <= 32; p++) {
			if ((1 << p) <= A[n - 1])continue;
			ll x = (1 << p) - A[n - 1];
			if (key.find(x) == key.end())continue;
			ll targ = key.find(x)->second;
			side[n - 1] = 1 - side[targ];
			break;
		}
		if (side[n - 1] == -1)side[n - 1] = 1;
		key.insert(pll(A[n - 1], n - 1));
	}
	Dinic<ll> D(N + 2);
	for (ll n = 0; n < N; n++) {
		if (numcount[n] == 0)continue;
		if (side[n] == 0) {
			D.add_edge(0, n + 1, numcount[n]);
			for (ll p = 0; p <= 32; p++) {
				if ((1 << p) <= A[n])continue;
				ll x = (1 << p) - A[n];
				auto itr = key.find(x);
				if (itr == key.end())continue;
				D.add_edge(n + 1, itr->second + 1, 2e5);
			}
		}
		else {
			D.add_edge(n + 1, N + 1, numcount[n]);
		}
	}
	ll ans = D.max_flow(0, N + 1);
	for (ll n = 0; n < N; n++) {
		if (numcount[n] == 0)continue;
		ll co = A[n];
		ll bitcount = 0;
		for (ll p = 0; p <= 32; p++) {
			bitcount += (co >> p) & 1;
		}
		if (bitcount != 1)continue;
		ll used = 0;
		for (auto edge : D.graph[n + 1]) {
			if (edge.isrev)continue;
			auto rev = D.graph[edge.to][edge.rev];
			used += rev.cap;
		}
		ll unused = numcount[n] - used;
		ans += unused / 2;
	}
	cout << ans << endl;
	return 0;
}