Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
// #include "D:\C++\test_lib_projects\testlib.h"
using namespace std;
#define ll long long
#define pii pair<int, int>
#define qi ios::sync_with_stdio(0)

bool debug = true;

/*    *************************************
 * Written in New Computer           *
 * The following code belongs to     *
 * XiaoGeNintendo of HellHoleStudios *
 *************************************
 */
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> ptt) {
  os << ptt.first << "," << ptt.second;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vt) {
  os << "{";
  for (int i = 0; i < vt.size(); i++) {
    os << vt[i] << " ";
  }
  os << "}";
  return os;
}

int n;
int a[200005];
map<int, int> mp;
int p2[33];
int main(int argc, char *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  p2[0] = 1;
  for (int i = 1; i <= 30; i++) {
    p2[i] = p2[i - 1] * 2;
  }

  sort(a, a + n);
  reverse(a, a + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[a[i]] == 0) {
      continue;
    }
    for (int j = 30; j >= 1; j--) {

      int ano = p2[j] - a[i];
      int left = mp[ano];
      if (left >= 2 || left == 1 && a[i] != ano) {
        mp[ano]--;
        mp[a[i]]--;
        ans++;
        break;
      }
    }
  }

  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
// #include "D:\C++\test_lib_projects\testlib.h"
using namespace std;
#define ll long long
#define pii pair<int, int>
#define qi ios::sync_with_stdio(0)

bool debug = true;

/*    *************************************
 * Written in New Computer           *
 * The following code belongs to     *
 * XiaoGeNintendo of HellHoleStudios *
 *************************************
 */
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> ptt) {
  os << ptt.first << "," << ptt.second;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vt) {
  os << "{";
  for (int i = 0; i < vt.size(); i++) {
    os << vt[i] << " ";
  }
  os << "}";
  return os;
}

int n;
int a[200005];
map<int, int> mp;
int p2[33];
int main(int argc, char *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  p2[0] = 1;
  for (int i = 1; i <= 30; i++) {
    p2[i] = p2[i - 1] * 2;
  }

  sort(a, a + n);
  reverse(a, a + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[a[i]] == 0) {
      continue;
    }
    for (int j = 30; j >= 1; j--) {
      if (p2[j] <= a[i]) {
        continue;
      }
      int ano = p2[j] - a[i];
      int left = mp[ano];
      if (left >= 2 || left == 1 && a[i] != ano) {
        mp[ano]--;
        mp[a[i]]--;
        ans++;
        break;
      }
    }
  }

  cout << ans;
  return 0;
}