Problem p02626 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

pll operator+(pll a, pll b) {
  return pll(a.first + b.first, a.second + b.second);
}
pll operator-(pll a, pll b) {
  return pll(a.first - b.first, a.second - b.second);
}
pll operator*(ll a, pll b) { return pll(b.first * a, b.second * a); }
const ll INF = (1LL << 60);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

void solve(long long N, std::vector<long long> A) {
  ll xr = 0;
  REP(N) { xr ^= A[i]; }
  if (xr == 0) {
    cout << 0 << endl;
    return;
  }
  xr = 0;
  FOR(i, 2, N) { xr ^= A[i]; }
  bitset<63> xrbit(xr);
  ll sum = A[0] + A[1];
  ll ans0 = 0, ans1 = 0;
  bool x = false;
  REP(63) {
    ll tmp = (sum - ans0 - ans1) % (1LL << (i + 1));
    if (tmp && xrbit[i]) {
      ans1 |= (1LL << i);
      x = false;
    } else if (tmp && !xrbit[i]) {
      if (x) {
        ans1 |= (1LL << (i - 1));
        ans0 |= (1LL << (i - 1));
        x = true;
      } else {
        cout << -1LL << endl;
        return;
      }
    } else if (!tmp && !xrbit[i]) {
      x = true;
    } else {
      if (x) {
        ans1 |= (1LL << (i - 1));
        ans0 |= (1LL << (i - 1));
        ans1 |= (1LL << i);
        x = false;
      } else {
        cout << -1LL << endl;
        return;
      }
    }
  }
  if (ans0 + ans1 == A[0] + A[1] && ans0 <= A[0]) {
    Rev(i, 63) {
      ll ii = (1LL << i);
      if ((ans1 & ii) && !(ans0 & ii)) {
        if (ans0 + ii <= A[0]) {
          ans1 ^= ii;
          ans0 ^= ii;
        }
      }
    }
    if (ans0 == 0)
      cout << -1 << endl;
    else
      cout << A[0] - ans0 << endl;
  } else {
    cout << -1LL << endl;
  }
}

int main() {
  cout << setprecision(1000);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

pll operator+(pll a, pll b) {
  return pll(a.first + b.first, a.second + b.second);
}
pll operator-(pll a, pll b) {
  return pll(a.first - b.first, a.second - b.second);
}
pll operator*(ll a, pll b) { return pll(b.first * a, b.second * a); }
const ll INF = (1LL << 60);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

void solve(long long N, std::vector<long long> A) {
  ll xr = 0;
  REP(N) { xr ^= A[i]; }
  if (xr == 0) {
    cout << 0 << endl;
    return;
  }
  xr = 0;
  FOR(i, 2, N) { xr ^= A[i]; }
  bitset<64> xrbit(xr);
  ll sum = A[0] + A[1];
  ll ans0 = 0, ans1 = 0;
  bool x = false;
  bool ax = false;
  REP(63) {
    ll tmp = sum % (1LL << (i + 1));
    tmp &= (1LL << i);
    if (ax)
      tmp ^= (1LL << i);
    if (tmp && xrbit[i]) {
      ans1 |= (1LL << i);
      x = false;
      ax = false;
    } else if (tmp && !xrbit[i]) {
      if (x) {
        ans1 |= (1LL << (i - 1));
        ans0 |= (1LL << (i - 1));
        x = true;
        ax = false;
      } else {
        cout << -1LL << endl;
        return;
      }
    } else if (!tmp && !xrbit[i]) {
      x = true;
      ax = false;
    } else {
      if (x) {
        ans1 |= (1LL << (i - 1));
        ans0 |= (1LL << (i - 1));
        ans1 |= (1LL << i);
        x = false;
        ax = true;
      } else {
        cout << -1LL << endl;
        return;
      }
    }
  }
  if (ans0 + ans1 == A[0] + A[1] && ans0 <= A[0]) {
    Rev(i, 63) {
      ll ii = (1LL << i);
      if ((ans1 & ii) && !(ans0 & ii)) {
        if (ans0 + ii <= A[0]) {
          ans1 ^= ii;
          ans0 ^= ii;
        }
      }
    }
    if (ans0 == 0)
      cout << -1 << endl;
    else
      cout << A[0] - ans0 << endl;
  } else {
    abort();
    cout << -1LL << endl;
  }
}

int main() {
  cout << setprecision(1000);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}