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 <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;

class MaxFlow {
public:
  struct edge { int to, cap, rev; };

  const int INF = 1 << 28;
  int N;
  vector<vector<edge>> E;
  vector<int> level;
  vector<int> iter;

  MaxFlow(int N): N(N), E(N), level(N), iter(N) {}

  void add_directed_edge(int from, int to, int cap) {
    E[from].push_back((edge) { to, cap, (int) E[to].size() });
    E[to].push_back((edge) { from, 0, (int) E[from].size() - 1 });
  }

  void add_undirected_edge(int from, int to, int cap) {
    E[from].push_back((edge) { to, cap, (int) E[to].size() });
    E[to].push_back((edge) { from, cap, (int) E[from].size() - 1 });
  }

  void bfs(int s) {
    for (int i = 0; i < N; i++) level[i] = -1;
    level[s] = 0;

    queue<int> q;
    q.push(s);

    while (!q.empty()) {
      int v = q.front();
      q.pop();

      for (int i = 0; i < E[v].size(); i++) {
        edge &e = E[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }

  int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int &i = iter[v]; i < E[v].size(); i++) {
      edge &e = E[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          E[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t) {
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) break;
      for (int i = 0; i < N; i++) iter[i] = 0;
      while (1) {
        int f = dfs(s, t, INF);
        if (f == 0 || f == INF) break;
        flow += f;
      }
    }
    return flow;
  }
};

void dfs(ll v, ll c, vector<vector<ll>> &E, vector<ll> &color) {
  if (color[v] >= 0) {
    assert(color[v] == c);
    return;
  }
  color[v] = c;

  for (ll u : E[v]) {
    dfs(u, 1 - c, E, color);
  }
}

int main(void) {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, 0, N) cin >> A[i];

  sort(A.begin(), A.end());

  vector<ll> v(A);
  v.erase(unique(v.begin(), v.end()), v.end());

  ll n = v.size();
  vector<ll> cnt(n);
  REP(i, 0, n) {
    cnt[i] = upper_bound(A.begin(), A.end(), v[i]) - lower_bound(A.begin(), A.end(), v[i]);
  }

  vector<vector<ll>> E(n);
  REP(i, 0, n) {
    for (ll t = 1; t <= 1000000000; t *= 2) {
      ll x = t - v[i];
      if (x == v[i]) continue;
      if (binary_search(v.begin(), v.end(), x)) {
        E[i].push_back(lower_bound(v.begin(), v.end(), x) - v.begin());
      }
    }
  }

  vector<ll> color(n);
  REP(i, 0, n) color[i] = -1;
  dfs(0, 0, E, color);

  MaxFlow mf(n + 2);
  ll s = n, t = s + 1;
  REP(i, 0, n) {
    if (color[i] == 0) {
      mf.add_directed_edge(s, i, cnt[i]);
    } else {
      mf.add_directed_edge(i, t, cnt[i]);
    }
  }
  REP(i, 0, n) {
    for (ll u : E[i]) {
      if (color[i] == 0) {
        mf.add_directed_edge(i, u, max(cnt[i], cnt[u]));
      } else {
        mf.add_directed_edge(u, i, max(cnt[i], cnt[u]));
      }
    }
  }
  ll f = mf.max_flow(s, t);

  ll ans = f;
  REP(i, 0, n) {
    bool ok = false;
    for (ll t = 1; t <= 1000000000; t *= 2) {
      if (v[i] == t) ok = true;
    }
    if (ok) {
      if (color[i] == 0) {
        ans += mf.E[s][mf.E[i][0].rev].cap / 2;
      } else {
        ans += mf.E[i][0].cap / 2;
      }
    }
  }
  cout << ans << endl;
}