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

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl
#define int ll

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

signed main() {
  IO_OP;

  int n;
  cin >> n;
  int x = 0, a, b;
  cin >> a >> b;
  for (int i = 2; i < n; i++) {
    int t;
    cin >> t;
    x ^= t;
  }
  if (a == b && x == 0)
    return cout << 0 << endl, 0;
  int z = a + b - x;
  if (z < 0 || z & 1) {
    cout << -1 << endl;
    return 0;
  }
  z /= 2;
  // aa ^ bb = x, aa & bb = z
  vi v(60);
  for (int j = 0; j < 60; j++) {
    int sa = x >> j & 1;
    int na = z >> j & 1;
    if (sa == 0 && na == 0) {
      // must 0
      v[j] = 0;
    } else if (sa == 0 && na == 1) {
      // must 1
      v[j] = 1;
    } else if (sa == 1 && na == 0) {
      // both
      v[j] = 2;
    } else {
      // no way
      cout << -1 << endl;
      return 0;
    }
  }

  int j;
  for (j = 59; j >= 0; j--) {
    int bit = a >> j & 1;
    if (bit == 1 && v[j] == 0)
      break;
    if (bit == 0 && v[j] == 1)
      break;
  }
  if (j == -1)
    return cout << 0 << endl, -1;
  int bit = a >> j & 1;
  if (bit == 1 && v[j] == 0) {
    int aa = 0;

    for (int i = 59; i > j; i--) {
      aa |= (a >> i & 1) << i;
    }
    for (int i = j - 1; i >= 0; i--) {
      if (v[i])
        aa |= 1LL << i;
    }
    if (aa == 0)
      return cout << -1 << endl, 0;
    cout << a - aa << endl;
  } else {
    int i;
    for (i = j + 1; i < 60; i++) {
      if (v[i] != 1 && (a >> i & 1))
        break;
    }

    if (i == 60)
      return cout << -1 << endl, 0;
    j = i;
    int aa = 0;
    for (int i = 59; i > j; i--) {
      aa |= (a >> i & 1) << i;
    }
    for (int i = j - 1; i >= 0; i--) {
      if (v[i])
        aa |= 1LL << i;
    }
    if (aa == 0)
      return cout << -1 << endl, 0;
    cout << a - aa << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl
#define int ll

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;

signed main() {
  IO_OP;

  int n;
  cin >> n;
  int x = 0, a, b;
  cin >> a >> b;
  for (int i = 2; i < n; i++) {
    int t;
    cin >> t;
    x ^= t;
  }
  int z = a + b - x;
  if (z < 0 || z & 1) {
    cout << -1 << endl;
    return 0;
  }
  z /= 2;
  // aa ^ bb = x, aa & bb = z
  vi v(100);
  for (int j = 0; j < 60; j++) {
    int sa = x >> j & 1;
    int na = z >> j & 1;
    if (sa == 0 && na == 0) {
      // must 0
      v[j] = 0;
    } else if (sa == 0 && na == 1) {
      // must 1
      v[j] = 1;
    } else if (sa == 1 && na == 0) {
      // both
      v[j] = 2;
    } else {
      // no way
      cout << -1 << endl;
      return 0;
    }
  }

  int j;
  for (j = 59; j >= 0; j--) {
    int bit = a >> j & 1;
    if (bit == 1 && v[j] == 0)
      break;
    if (bit == 0 && v[j] == 1)
      break;
  }
  if (j == -1)
    return cout << 0 << endl, -1;
  int bit = a >> j & 1;
  if (bit == 1 && v[j] == 0) {
    int aa = 0;

    for (int i = 59; i > j; i--) {
      aa |= (a >> i & 1) << i;
    }
    for (int i = j - 1; i >= 0; i--) {
      if (v[i])
        aa |= 1LL << i;
    }
    if (aa == 0)
      return cout << -1 << endl, 0;
    cout << a - aa << endl;
  } else {
    int i;
    for (i = j + 1; i < 60; i++) {
      if (v[i] != 1 && (a >> i & 1))
        break;
    }

    if (i == 60)
      return cout << -1 << endl, 0;
    j = i;
    int aa = 0;
    for (int i = 59; i > j; i--) {
      aa |= (a >> i & 1) << i;
    }
    for (int i = j - 1; i >= 0; i--) {
      if (v[i])
        aa |= 1LL << i;
    }
    if (aa == 0)
      return cout << -1 << endl, 0;
    cout << a - aa << endl;
  }
}