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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  ll x = 0;
  for (int i = 2; i < n; ++i)
    x ^= a[i];

  if (x == 0) {
    if (a[0] < a[1] || (a[0] + a[1]) % 2 || (n > 2 && (a[0] - a[1]) / 2 == 0)) {
      cout << -1 << endl;
    } else {
      cout << (a[0] - a[1]) / 2 << endl;
    }
  } else {
    if ((a[0] + a[1] - x) % 2) {
      cout << -1 << endl;
      return 0;
    }
    ll c = (a[0] + a[1] - x) >> 1; // a[0] & a[1];
    ll a0 = c, a1 = c;
    for (int i = 60; i >= 0; --i) {
      if (c & (1LL << i)) {
        continue;
      }
      if (x & (1LL << i)) {
        if (a0 + (1LL << i) <= a[0]) {
          a0 += (1LL << i);
        } else {
          a1 += (1LL << i);
        }
      }
    }
    if (!a0 || a0 > a[0] || a1 < a[1]) {
      cout << -1 << endl;
      return 0;
    }
    if ((a0 ^ a1) == x) {
      cout << a[0] - a0 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  ll x = 0;
  for (int i = 2; i < n; ++i)
    x ^= a[i];

  if (x == 0) {
    if (a[0] < a[1] || (a[0] + a[1]) % 2 || (a[0] - a[1]) / 2 == 0) {
      // cout << -1 << endl;
      assert(false);
    } else {
      cout << (a[0] - a[1]) / 2 << endl;
    }
  } else {
    ll c = (a[0] + a[1] - x) >> 1; // a[0] & a[1];
    ll a0 = c, a1 = c;
    for (int i = 60; i >= 0; --i) {
      if (c & (1LL << i))
        continue;
      if (x & (1LL << i)) {
        if (a0 + (1LL << i) <= a[0]) {
          a0 += (1LL << i);
        } else {
          a1 += (1LL << i);
        }
      }
    }
    if (!a0 || a0 > a[0] || a1 < a[1]) {
      // cout << -1 << endl;
      assert(false);
      return 0;
    }
    if ((a0 ^ a1) == x) {
      cout << a[0] - a0 << endl;
    } else {
      cout << -1;
    }
    return 0;
  }
}