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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define SZ(x) (int)x.size()
#define ll long long
#define MP make_pair
#define pb push_back
#define pq priority_queue
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 333;

int n;
ll a[maxn];
ll sum = 0, xsum = 0;
int f[maxn];
int cnt = 0;
vector<int> pos;
ll p0w[60];

int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  p0w[0] = 1;
  rep(i, 1, 60) p0w[i] = p0w[i - 1] * 2ll;
  rep(i, 0, n) {
    cin >> a[i];
    if (i < 2)
      sum += a[i];
    else
      xsum ^= a[i];
  }
  ll tmp = max(sum, xsum);
  while (tmp > 0)
    tmp >>= 1, cnt++;
  rep(i, 0, cnt) { f[i] = ((sum >> (i + 1)) & 1) ^ ((xsum >> (i + 1)) & 1); }
  ll ans = 0;
  if (((sum & 1) ^ (xsum & 1)) == 1) {
    cout << -1;
    return 0;
  } else {
    if (f[0] == 1) {
      if ((xsum & 1) == 1) {
        cout << -1;
        return 0;
      }
      ans++;
    } else if ((xsum & 1) == 1)
      pos.pb(0);
  }
  for (int i = cnt - 1; i > 0; i--) {
    if (f[i] == 1 && f[i - 1] == 1) {
      if ((xsum >> i) & 1)
        pos.pb(i);
      else
        ans += p0w[i];
    } else if (f[i] == 0 && f[i - 1] == 0) {
      if ((xsum >> i) & 1)
        pos.pb(i);
    } else {
      if (((xsum >> i) & 1) == 1) {
        cout << -1;
        return 0;
      }
      if (f[i] == 1)
        ans += p0w[i];
    }
    //		debug(ans);
    if (ans > a[0]) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 0; i < SZ(pos); i++) {
    if (ans + p0w[pos[i]] <= a[0])
      ans += p0w[pos[i]];
  }
  if (ans == 0)
    cout << -1;
  else
    cout << a[0] - ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define SZ(x) (int)x.size()
#define ll long long
#define MP make_pair
#define pb push_back
#define pq priority_queue
#define pii pair<int, int>
#define mod 998244353
#define inf 0x3f3f3f3f
#define debug(x) cerr << #x << "=" << x << '\\n'
#define rep(i, a, b) for (int i = a; i < (b); i++)

const int maxn = 333;

int n;
ll a[maxn];
ll sum = 0, xsum = 0;
int f[maxn];
int cnt = 0;
vector<int> pos;
ll p0w[15];

int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  p0w[0] = 1;
  rep(i, 1, maxn) p0w[i] = p0w[i - 1] * 2ll;
  rep(i, 0, n) {
    cin >> a[i];
    if (i < 2)
      sum += a[i];
    else
      xsum ^= a[i];
  }
  ll tmp = max(sum, xsum);
  while (tmp > 0)
    tmp >>= 1, cnt++;
  rep(i, 0, cnt) { f[i] = ((sum >> (i + 1)) & 1) ^ ((xsum >> (i + 1)) & 1); }
  ll ans = 0;
  if (((sum & 1) ^ (xsum & 1)) == 1) {
    cout << -1;
    return 0;
  } else {
    if (f[0] == 1) {
      if ((xsum & 1) == 1) {
        cout << -1;
        return 0;
      }
      ans++;
    } else if ((xsum & 1) == 1)
      pos.pb(0);
  }
  for (int i = cnt - 1; i > 0; i--) {
    if (f[i] == 1 && f[i - 1] == 1) {
      if ((xsum >> i) & 1)
        pos.pb(i);
      else
        ans += p0w[i];
    } else if (f[i] == 0 && f[i - 1] == 0) {
      if ((xsum >> i) & 1)
        pos.pb(i);
    } else {
      if (((xsum >> i) & 1) == 1) {
        cout << -1;
        return 0;
      }
      if (f[i] == 1)
        ans += p0w[i];
    }
    //		debug(ans);
    if (ans > a[0]) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 0; i < SZ(pos); i++) {
    if (ans + p0w[pos[i]] <= a[0])
      ans += p0w[pos[i]];
  }
  if (ans == 0)
    cout << -1;
  else
    cout << a[0] - ans;
  return 0;
}