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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

ll n, arr[305];

void fail() {
  cout << -1;
  exit(0);
}

void solve(ll s, ll x) {
  if (s < x)
    fail();
  ll a = (s - x + 1) / 2, resa = 0, resb = 0;
  vi v;
  DEC(i, ceil(log2(s)), 0) {
    ll xi = (x & (1ll << i)), ai = (a & (1ll << i));
    // cout << i << " " << xi << " " << ai << "\\n";
    if (xi == 0 and ai == 0) {
    } else if (xi == 0 and ai > 0) {
      resa |= (1ll << i);
      resb |= (1ll << i);
    } else if (xi > 0 and ai == 0)
      v.pb(i);
    else
      fail();
  }
  for (auto i : v) {
    if ((resb | (1ll << i)) <= arr[1])
      resb |= (1ll << i);
    else
      resa |= (1ll << i);
  }
  if (resb == 0 or resb > arr[1])
    fail();
  // cout << resa << " " << resb << " ";
  cout << arr[1] - resb;
}

int main() {
  fastio;
  cin >> n >> arr[1] >> arr[2];
  ll x = 0;
  FOR(i, 3, n) {
    cin >> arr[i];
    x ^= arr[i];
  }
  if ((arr[1] ^ arr[2]) == x) {
    cout << 0;
    return 0;
  }
  solve(arr[1] + arr[2], x);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

ll n, arr[305];

void fail() {
  assert(0);
  cout << -1;
  exit(0);
}

void solve(ll s, ll x) {
  ll a = (s - x) / 2, resa = 0, resb = 0;
  vi v;
  DEC(i, ceil(log2(s)), 0) {
    ll xi = (x & (1ll << i)), ai = (a & (1ll << i));
    if (xi == 0 and ai == 0) {
    } else if (xi == 0 and ai >= 0) {
      resa |= (1ll << i);
      resb |= (1ll << i);
    } else if (xi > 0 and ai == 0)
      v.pb(i);
    else
      fail();
  }
  for (auto i : v) {
    if ((resb | (1ll << i)) <= arr[1])
      resb |= (1ll << i);
    else
      resa |= (1ll << i);
  }
  if (resb == 0 or resb > arr[1])
    fail();
  cout << arr[1] - resb;
}

int main() {
  fastio;
  cin >> n >> arr[1] >> arr[2];
  ll x = 0;
  FOR(i, 3, n) {
    cin >> arr[i];
    x ^= arr[i];
  }
  solve(arr[1] + arr[2], x);
}