Problem p03201 (C++)

Buggy submission (status: Runtime Error):
/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000

ll cnt(ll x) {

  ll val = 1;
  while (x != 1) {
    x /= 2;
    val++;
  }
  return val;
}

ll power(ll x, ll n) {

  if (n == 0)
    return 1;

  return n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, ans = 0, x;
  unordered_multiset<ll> ms;
  cin >> n;
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    ms.insert(x);
  }
  sort(v.begin(), v.end());

  for (ll i = n - 1; i >= 0; i--) {
    x = v[i];
    if (!(ms.find(x) != ms.end()))
      continue;
    ll val = power(2, cnt(x));
    if (ms.find(val - x) != ms.end()) {
      ans++;
      ms.erase(ms.find(val - x));
      ms.erase(ms.find(x));
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000

ll cnt(ll x) {

  ll val = 1;
  while (x != 1) {
    x /= 2;
    val++;
  }
  return val;
}

ll power(ll x, ll n) {

  if (n == 0)
    return 1;

  return n % 2 == 0 ? power(x * x, n / 2) : x * power(x * x, n / 2);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, ans = 0, x;
  unordered_multiset<ll> ms;
  cin >> n;
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    ms.insert(x);
  }
  sort(v.begin(), v.end());

  for (ll i = n - 1; i >= 0; i--) {
    x = v[i];
    if (!(ms.find(x) != ms.end()))
      continue;
    ll val = power(2, cnt(x));
    if (ms.find(val - x) != ms.end()) {
      ans++;
      ms.erase(ms.find(val - x));
      if (!(ms.find(x) != ms.end())) {
        ans--;
        ms.insert(val - x);
        continue;
      }
      ms.erase(ms.find(x));
    }
  }

  cout << ans << "\n";

  return 0;
}