Problem p03201 (C++)

Buggy submission (status: Runtime Error):
// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("O3")
#define ll long long
#define ld long double

//-------------------------------------------------------------------------------------------------------------------------------------------------
// Custom Hash Function for Unordered Maps

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

//-------------------------------------------------------------------------------------------------------------------------------------------------

// Debugger Tools
#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(string s, int x1 = 0, int x2 = 1e9) {
  return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
  string t = "";
  for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
       __iii__ <= __jjj__; ++__iii__) {
    t += b[__iii__] + '0';
  }
  return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(pair<A, B> &p) {
  l_v_l_v_l++;
  string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
  l_v_l_v_l--;
  return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
  int rnk = rank<A>::value;
  string tab(t_a_b_s, ' ');
  string res = "";
  bool first = true;
  if (l_v_l_v_l == 0)
    res += n_l;
  res += tab + "[";
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e)) {
    if (!first) {
      res += ", ";
    }
    first = false;
    l_v_l_v_l++;
    if (e != l) {
      if (rnk > 1) {
        res += n_l;
        t_a_b_s = l_v_l_v_l;
      };
    } else {
      t_a_b_s = 0;
    }
    res += to_string(*e, coords...);
    l_v_l_v_l--;
  }
  res += "]";
  if (l_v_l_v_l == 0)
    res += n_l;
  return res;
}
void dbgs() { ; }
template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) {
  cout << to_string(H) << " | ";
  dbgs(T...);
}
#define dbgm(...)                                                              \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  dbgs(__VA_ARGS__);                                                           \
  cout << endl;

// Mathematical Constants
const ll MOD = 1000000007;
const ll MOD9 = 1000000009;
const double pie = 2 * acos(0.0);
const int C_SIZE = 121000;
const long long inf = MOD * MOD;

//-------------------------------------------------------------------------------------------------------------------------------------------------
// Utility Functions

#define eps 1e-8
#define ull unsigned long long
#define eof (scanf("%d", &n)) != EOF
#define bolt ios::sync_with_stdio(false)
#define prec(n) fixed << setprecision(n)
#define ms(s, n) memset(s, n, sizeof(s))
#define Unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EraseSpace(s) s.erase(remove(s.begin(), s.end(), ' '), s.end());
#define leftrotate(str, n) rotate(str.begin(), str.begin() + n, str.end());
#define rightrotate(str, n)                                                    \
  rotate(str.begin(), str.begin() + str.size() - n, str.end());
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Constants and Shortcut Functions

#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define all(v) v.begin(), v.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, n, x) for (int i = n; i >= x; i--)

ll fact[C_SIZE];
ll finv[C_SIZE];
ll inv[C_SIZE];

ll Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % MOD * finv[a - b] % MOD;
}

gp_hash_table<ll, ll, custom_hash> smp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void run_case() {

  int n;
  cin >> n;
  multiset<ll> mtl;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    mtl.insert(x);
  }
  int res = 0;
  while (!mtl.empty()) {
    auto ed = mtl.end();
    ed--;
    int p = floor((double)logb(*ed));
    ll s = pow(2, p + 1) - *ed;
    if (mtl.find(s) != mtl.end() && mtl.size() != 1) {
      ++res;
      if (!mtl.empty())
        mtl.erase(s);
      if (!mtl.empty())
        mtl.erase(ed);
    } else {
      if (!mtl.empty())
        mtl.erase(ed);
    }
  }
  cout << res << endl;
}

int main() {

  // c_p_c();
  bolt;
  cin.tie(nullptr);
  cout.tie(nullptr);

  int tests = 1; // cin >> tests;

  while (tests-- > 0) {
    run_case();
  }

  return 0;
}

Fixed submission:
// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("O3")
#define ll long long
#define ld long double

//-------------------------------------------------------------------------------------------------------------------------------------------------
// Custom Hash Function for Unordered Maps

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

//-------------------------------------------------------------------------------------------------------------------------------------------------

// Debugger Tools
#ifndef ONLINE_JUDGE
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std ::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(string s, int x1 = 0, int x2 = 1e9) {
  return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
  string t = "";
  for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
       __iii__ <= __jjj__; ++__iii__) {
    t += b[__iii__] + '0';
  }
  return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(pair<A, B> &p) {
  l_v_l_v_l++;
  string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
  l_v_l_v_l--;
  return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
  int rnk = rank<A>::value;
  string tab(t_a_b_s, ' ');
  string res = "";
  bool first = true;
  if (l_v_l_v_l == 0)
    res += n_l;
  res += tab + "[";
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e)) {
    if (!first) {
      res += ", ";
    }
    first = false;
    l_v_l_v_l++;
    if (e != l) {
      if (rnk > 1) {
        res += n_l;
        t_a_b_s = l_v_l_v_l;
      };
    } else {
      t_a_b_s = 0;
    }
    res += to_string(*e, coords...);
    l_v_l_v_l--;
  }
  res += "]";
  if (l_v_l_v_l == 0)
    res += n_l;
  return res;
}
void dbgs() { ; }
template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) {
  cout << to_string(H) << " | ";
  dbgs(T...);
}
#define dbgm(...)                                                              \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  dbgs(__VA_ARGS__);                                                           \
  cout << endl;

// Mathematical Constants
const ll MOD = 1000000007;
const ll MOD9 = 1000000009;
const double pie = 2 * acos(0.0);
const int C_SIZE = 121000;
const long long inf = MOD * MOD;

//-------------------------------------------------------------------------------------------------------------------------------------------------
// Utility Functions

#define eps 1e-8
#define ull unsigned long long
#define eof (scanf("%d", &n)) != EOF
#define bolt ios::sync_with_stdio(false)
#define prec(n) fixed << setprecision(n)
#define ms(s, n) memset(s, n, sizeof(s))
#define Unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EraseSpace(s) s.erase(remove(s.begin(), s.end(), ' '), s.end());
#define leftrotate(str, n) rotate(str.begin(), str.begin() + n, str.end());
#define rightrotate(str, n)                                                    \
  rotate(str.begin(), str.begin() + str.size() - n, str.end());
//-------------------------------------------------------------------------------------------------------------------------------------------------
// Constants and Shortcut Functions

#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define all(v) v.begin(), v.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, n, x) for (int i = n; i >= x; i--)

ll fact[C_SIZE];
ll finv[C_SIZE];
ll inv[C_SIZE];

ll Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % MOD * finv[a - b] % MOD;
}

gp_hash_table<ll, ll, custom_hash> smp;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void run_case() {

  int n;
  cin >> n;
  multiset<ll> mtl;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    mtl.insert(x);
  }
  int res = 0;
  while (!mtl.empty()) {
    auto ed = *mtl.rbegin();
    mtl.erase(mtl.find(ed));
    ll step = pow(2, logb(ed) + 1);
    auto it = mtl.find(step - ed);
    if (it != mtl.end()) {
      mtl.erase(it);
      res++;
    }
  }
  cout << res << endl;
}

int main() {

  // c_p_c();
  bolt;
  cin.tie(nullptr);
  cout.tie(nullptr);

  int tests = 1; // cin >> tests;

  while (tests-- > 0) {
    run_case();
  }

  return 0;
}