Problem p02626 (C++)

Buggy submission (status: Runtime Error):
// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
  ll n;
  cin >> n;
  vl v;
  rep(i, n) {
    ll c;
    cin >> c;
    v.pb(c);
  }
  ll req = 0;
  for (int i = 2; i < n; i++)
    req ^= v[i];
  if ((req ^ v[0] ^ v[1]) == 0) {
    cout << 0 << '\n';
    return;
  }
  ll sum = v[0] + v[1];
  ll my = 0;
  vl store;
  ll possible = 0;
  vl fun;
  for (int i = 0; i < 50; i++) {
    if (((1LL << i) & (req)) != 0) {
      my += (1LL << i);
      fun.pb(i);
    } else {
      store.pb(i);
      possible += (1LL << i);
    }
  }
  ll diff = sum - my;
  if (diff < 0) {
    cout << -1;
    return;
  }
  if ((diff % 2) != 0) {
    cout << -1;
    return;
  }
  ll x = diff / 2;
  if ((x & possible) != x) {
    cout << -1 << '\n';
    return;
  }
  sort(all(fun));
  reverse(all(fun));
  for (auto it : fun) {
    if ((x | (1LL << it)) <= v[0]) {
      x |= (1LL << it);
    }
  }
  if (x == 0) {
    cout << -1 << '\n';
    return;
  }
  assert(x <= v[0]);
  assert(x >= 1);
  if ((x ^ (sum - x)) != req) {
    cout << -1;
    return;
  }
  cout << (v[0] - x);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}

Fixed submission:
// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
  ll n;
  cin >> n;
  vl v;
  rep(i, n) {
    ll c;
    cin >> c;
    v.pb(c);
  }
  ll req = 0;
  for (int i = 2; i < n; i++)
    req ^= v[i];
  if ((req ^ v[0] ^ v[1]) == 0) {
    cout << 0 << '\n';
    return;
  }
  ll sum = v[0] + v[1];
  ll my = 0;
  vl store;
  ll possible = 0;
  vl fun;
  for (int i = 0; i < 50; i++) {
    if (((1LL << i) & (req)) != 0) {
      my += (1LL << i);
      fun.pb(i);
    } else {
      store.pb(i);
      possible += (1LL << i);
    }
  }
  ll diff = sum - my;
  if (diff < 0) {
    cout << -1;
    return;
  }
  if ((diff % 2) != 0) {
    cout << -1;
    return;
  }
  ll x = diff / 2;
  if ((x & possible) != x) {
    cout << -1 << '\n';
    return;
  }
  sort(all(fun));
  reverse(all(fun));
  for (auto it : fun) {
    if ((x | (1LL << it)) <= v[0]) {
      x |= (1LL << it);
    }
  }
  if (x == 0) {
    cout << -1 << '\n';
    return;
  }
  if (x > v[0]) {
    cout << -1 << '\n';
    return;
  }
  assert(x >= 1);
  if ((x ^ (sum - x)) != req) {
    cout << -1;
    return;
  }
  cout << (v[0] - x);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}