Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define LLINF (long long)1e18 // 1234567890987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) // 0.0000001 the value
#define PI (acos(-1))
#define MAXN 300006
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
#define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++)
#define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++)
#define foritr(itr, A)                                                         \
  for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++)
#define cr(x) cerr << #x << " = " << x << "\n";
#define crA(x, A) cerr << #x << " = " << A[x] << "\n";
#define spacing cout << " ";
#define mmst(x, v) memset((x), v, sizeof((x)));
#define bg(ms) (*ms.begin())
#define ed(ms) (*prev(ms.end(), 1))
#define addedge(a, b, c, v)                                                    \
  v[(a)].pb(pi((b), (c)));                                                     \
  v[(b)].pb(pi((a), (c)))
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL << (x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss
ll n, A[MAXN], val[MAXN];
vector<ll> v;
ll index_c(ll x) {
  auto lb = lbd(v, x) - v.begin();
  if (v[lb] != x)
    return -LLINF;
  else
    return lb;
}
int main() {
  cin >> n;
  FOR(i, 0, n) {
    cin >> A[i];
    v.pb(A[i]);
  }
  sort(all(v));
  v.resize(unique(all(v)) - v.begin());
  FOR(i, 0, n) { val[index_c(A[i])]++; }
  ll co = 1;
  while (co <= (ll)1e14)
    co *= 2;
  while (co > 0) {
    FOR(i, 0, n) {
      if (val[index_c(A[i])] == 0 || A[i] > co)
        continue;
      val[index_c(A[i])]--;
      if (index_c(co - A[i]) != -LLINF && val[index_c(co - A[i])] > 0) {
        val[index_c(co - A[i])]--;
        continue;
      }
      val[index_c(A[i])]++;
    }
    co /= 2;
  }
  ll ans = 0;
  for (ll i = 0; i < v.size(); i++)
    ans += val[i];
  cout << (n - ans) / 2 << "\n";
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define LLINF (long long)1e18 // 1234567890987654321
#define INF 1234567890
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) // 0.0000001 the value
#define PI (acos(-1))
#define MAXN 300006
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
#define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++)
#define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++)
#define foritr(itr, A)                                                         \
  for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++)
#define cr(x) cerr << #x << " = " << x << "\n";
#define crA(x, A) cerr << #x << " = " << A[x] << "\n";
#define spacing cout << " ";
#define mmst(x, v) memset((x), v, sizeof((x)));
#define bg(ms) (*ms.begin())
#define ed(ms) (*prev(ms.end(), 1))
#define addedge(a, b, c, v)                                                    \
  v[(a)].pb(pi((b), (c)));                                                     \
  v[(b)].pb(pi((a), (c)))
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL << (x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss
ll n, A[MAXN], val[MAXN];
vector<ll> v;
ll index_c(ll x) {
  auto lb = lbd(v, x) - v.begin();
  if (v[lb] != x)
    return -LLINF;
  else
    return lb;
}
int main() {
  FAST cin >> n;
  FOR(i, 0, n) {
    cin >> A[i];
    v.pb(A[i]);
  }
  sort(all(v));
  v.resize(unique(all(v)) - v.begin());
  FOR(i, 0, n) { val[index_c(A[i])]++; }
  ll co = 1;
  while (co <= (ll)1e14)
    co *= 2;
  while (co > 0) {
    FOR(i, 0, n) {
      if (val[index_c(A[i])] == 0 || A[i] > co)
        continue;
      val[index_c(A[i])]--;
      if (index_c(co - A[i]) != -LLINF && val[index_c(co - A[i])] > 0) {
        val[index_c(co - A[i])]--;
        continue;
      }
      val[index_c(A[i])]++;
    }
    co /= 2;
  }
  ll ans = 0;
  for (ll i = 0; i < v.size(); i++)
    ans += val[i];
  cout << (n - ans) / 2 << "\n";
}