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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifndef ONLINE_JUDGE
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
typedef pair<int, int> pii;
ll n;
vector<ll> a;
ll sum = 0, andd = 0, x = 0;
bool check() {
  ll bal = 0;
  for (ll &i : a)
    bal ^= i;
  if (bal == 0)
    return true;
  return false;
}
void TEST_CASES(int cas) {
  cin >> n;
  a.resize(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (check()) {
    printf("0\\n");
    return;
  }
  for (ll i = 2; i < n; i++)
    x ^= a[i];
  sum = a[0] + a[1];
  if ((sum < x) || (sum - x) % 2) {
    printf("-1\\n");
    return;
  }
  andd = (sum - x) / 2;
  ll ans1 = 0, ans2 = 0;
  for (ll i = 40; i >= 0; i--) {
    ll b1 = 0, b2 = 0;
    if (x & (1ll << i))
      b1 = 1;
    if (andd & (1ll << i))
      b2 = 1;
    if (b1 && b2) {
      printf("-1\\n");
      return;
    }
    if (b1 == 0 && b2 == 1) {
      ans1 |= (1ll << i);
      ans2 |= (1ll << i);
    }
  }
  for (ll i = 40; i >= 0; i--) {
    ll b1 = 0, b2 = 0;
    if (x & (1ll << i))
      b1 = 1;
    if (andd & (1ll << i))
      b2 = 1;
    if (b1 == 1 && b2 == 0) {
      if ((ans1 | (1ll << i)) >= a[0]) {
        ans2 |= (1ll << i);
      } else {
        ans1 |= (1ll << i);
      }
    }
  }
  if (ans1 == 0) {
    printf("-1\\n");
    return;
  }
  if (ans1 < a[0] && ans2 > a[1]) {
    printf("%lld\\n", a[0] - ans1);
  } else {
    printf("-1\\n");
  }
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifndef ONLINE_JUDGE
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
typedef pair<int, int> pii;
ll n;
vector<ll> a;
ll sum = 0, andd = 0, x = 0;
bool check() {
  ll bal = 0;
  for (ll &i : a)
    bal ^= a[i];
  if (bal == 0)
    return true;
  return false;
}
void TEST_CASES(int cas) {
  cin >> n;
  a.resize(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (check()) {
    printf("0\\n");
    return;
  }
  for (ll i = 2; i < n; i++)
    x ^= a[i];
  sum = a[0] + a[1];
  if ((sum < x) || (sum - x) % 2) {
    printf("-1\\n");
    return;
  }
  andd = (sum - x) / 2;
  ll ans1 = 0, ans2 = 0;
  for (ll i = 40; i >= 0; i--) {
    ll b1 = 0, b2 = 0;
    if (x & (1ll << i))
      b1 = 1;
    if (andd & (1ll << i))
      b2 = 1;
    if (b1 && b2) {
      printf("-1\\n");
      return;
    }
    if (b1 == 0 && b2 == 1) {
      ans1 |= (1ll << i);
      ans2 |= (1ll << i);
    }
  }
  for (ll i = 40; i >= 0; i--) {
    ll b1 = 0, b2 = 0;
    if (x & (1ll << i))
      b1 = 1;
    if (andd & (1ll << i))
      b2 = 1;
    if (b1 == 1 && b2 == 0) {
      if ((ans1 | (1ll << i)) >= a[0]) {
        ans2 |= (1ll << i);
      } else {
        ans1 |= (1ll << i);
      }
    }
  }
  if (ans1 == 0) {
    printf("-1\\n");
    return;
  }
  if (ans1 < a[0] && ans2 > a[1]) {
    printf("%lld\\n", a[0] - ans1);
  } else {
    printf("-1\\n");
  }
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}