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>
using namespace std;

using i64=int64_t;

#define rep(i,x,y) for(i64 i=i64(x),i##_max_for_repmacro=i64(y); i<i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
 
const int inf=1.01e9;
const i64 inf64=4.01e18;
const double eps=1e-9;
 
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

void solve(){
    i64 N;
    cin >> N;

    vector<i64> A(N);
    rep(i,0,N) cin >> A[i];

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

    i64 ans=0;
    vector<bool> del(N);
    rep(i,0,30){
        vector<i64> idx;
        rep(j,0,N){
            if(A[j]==(1LL<<i)){
                idx.push_back(j);
            }
        }
        ans+=idx.size()/2;
        for(i64 j=0; j+1<idx.size(); j+=2){
            del[idx[j]]=del[idx[j+1]]=true;
        }
    }

    vector<i64> B;
    rep(i,0,N) if(!del[i]) B.push_back(i);
    N=B.size();

    if(N<=1){
        cout << ans << endl;
        return;
    }

    unordered_map<i64,vector<i64>> mp;
    rep(i,0,N){
        mp[A[i]].push_back(i);
    }

    vector<vector<i64>> from(N);
    vector<bool> term(N,true);

    rep(i,0,N){
        rep(j,0,30){
            i64 s=1LL<<j;
            if(s-A[i]<A[i]) continue;
            if(mp.find(s-A[i])==mp.end()) continue;
            for(i64 k:mp[s-A[i]]){
                if(i>=k) continue;
                from[i].push_back(k);
                term[k]=false;
            }
        }
    }

    static i64 dp[200010][2];
    fill_n((i64*)dp,200010*2,0);

    for(i64 i=N-2; i>=0; --i){
        vector<bool> bs(from[i].size());
        rep(j,0,from[i].size()){
            i64 k=from[i][j];
            dp[i][0]+=max(dp[k][0],dp[k][1]);
            if(dp[k][0]<dp[k][1]) bs[j]=true;
        }
        rep(j,0,from[i].size()){
            i64 k=from[i][j];
            if(bs[j]) dp[i][1]=max(dp[i][1],dp[i][0]-dp[k][1]+dp[k][0]+1);
            else dp[i][1]=max(dp[i][1],dp[i][0]+1);
        }
    }

    rep(i,0,N) if(term[i]) ans+=max(dp[i][0],dp[i][1]);

    cout << ans << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}