Problem p02626 (C++)

Buggy submission (status: Runtime Error):
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> as(n);
  cin >> as;
  ll a = as[0], b = as[1], c = 0;
  for (int i = 2; i < n; i++) {
    c ^= as[i];
  }
  printde(as);
  printde(a, b, c);
  ll ans = 0;
  for (int i = 60; i >= 0; i--) {
    bool ab = a >> i & 1, bb = b >> i & 1, cb = c >> i & 1;
    ll mask = (1ll << (i + 1)) - 1;
    if ((ab ^ bb) == cb) {
      continue;
    }
    if (cb) {
      if (!ab & !bb) {
        ll num = (1ll << i) - (b & mask);
        if (a < num) {
          cout << -1 << '\n';
          return 0;
        }
        a -= num;
        b += num;
        ans += num;
      } else {
        ll num = (a & mask) - (1ll << i) + 1;
        a -= num;
        b += num;
        ans += num;
        if ((b >> i & 1) == 0) {
          cout << -1 << '\n';
          return 0;
        }
      }
    } else {
      if (ab) {
        ll num = min((1ll << i) - (b & mask), (a & mask) - (1ll << i) + 1);
        if (a < num) {
          cout << -1 << '\n';
          return 0;
        }
        a -= num;
        b += num;
        ans += num;
      } else if (bb) {
        cout << -1 << '\n';
        return 1;
      }
    }
  }
  if (a == 0 || (a ^ b) != c) {
    cout << -1 << '\n';
    return 0;
  }
  cout << ans << '\n';
}


Fixed submission:
//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> as(n);
  cin >> as;
  ll a = as[0], b = as[1], c = 0;
  for (int i = 2; i < n; i++) {
    c ^= as[i];
  }
  printde(as);
  printde(a, b, c);
  ll ans = 0;
  for (int i = 60; i >= 0; i--) {
    bool ab = a >> i & 1, bb = b >> i & 1, cb = c >> i & 1;
    ll mask = (1ll << (i + 1)) - 1;
    if ((ab ^ bb) == cb) {
      continue;
    }
    if (cb) {
      if (!ab & !bb) {
        ll num = (1ll << i) - (b & mask);
        if (a < num) {
          cout << -1 << '\n';
          return 0;
        }
        a -= num;
        b += num;
        ans += num;
      } else {
        ll num = (a & mask) - (1ll << i) + 1;
        a -= num;
        b += num;
        ans += num;
        if ((b >> i & 1) == 0) {
          cout << -1 << '\n';
          return 0;
        }
      }
    } else {
      if (ab) {
        ll num = min((1ll << i) - (b & mask), (a & mask) - (1ll << i) + 1);
        if (a < num) {
          cout << -1 << '\n';
          return 0;
        }
        a -= num;
        b += num;
        ans += num;
      } else if (bb) {
        ll num = (1ll << i) - (b & (1ll << i) - 1);
        if (a < num) {
          cout << -1 << '\n';
          return 0;
        }
        a -= num;
        b += num;
        ans += num;
        i++;
        // cout << -1 << '\n';
        // return 0;
      }
    }
  }
  if (a == 0 || (a ^ b) != c) {
    cout << -1 << '\n';
    return 0;
  }
  cout << ans << '\n';
}