Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
  a = a % p;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const ll N = 100005;
bool vis[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> ma;
  map<ll, vi> ind;
  forn(i, n) cin >> a[i], ma[a[i]]++;
  sort(a, a + n);
  forn(i, n) ind[a[i]].pb(i);
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    if (vis[i])
      continue;
    ll x = a[i];
    ma[x]--;
    for (ll j = 1; j <= 31; j++) {
      ll z = (1LL << j);
      if (z > x) {
        ll diff = z - x;
        if (ma[diff] > 0) {
          ma[diff]--;
          ans++;
          // cout<<x<<" "<<diff<<" ";
          ll pos = ind[diff].back();
          ind[diff].pop_back();
          // cout<<pos<<"\n";
          vis[pos] = true;
          break;
        }
      }
    }
  }
  cout << ans;
}
// nothing is impossible

Fixed submission:
#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fi first
#define se second
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef long double ld;
typedef string st;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230;
const ll MOD = 1e9 + 7;
const ll INF = 1e14;
ll mpow(ll a, ll b, ll p = MOD) {
  a = a % p;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const ll N = 200005;
bool vis[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> ma;
  map<ll, vi> ind;
  forn(i, n) cin >> a[i], ma[a[i]]++;
  sort(a, a + n);
  forn(i, n) ind[a[i]].pb(i);
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) {
    if (vis[i])
      continue;
    ll x = a[i];
    ma[x]--;
    for (ll j = 1; j <= 31; j++) {
      ll z = (1LL << j);
      if (z > x) {
        ll diff = z - x;
        if (ma[diff] > 0) {
          ma[diff]--;
          ans++;
          // cout<<x<<" "<<diff<<" ";
          ll pos = ind[diff].back();
          ind[diff].pop_back();
          // cout<<pos<<"\n";
          vis[pos] = true;
          break;
        }
      }
    }
  }
  cout << ans;
}
// nothing is impossible