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

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

const long long MAXVAL = (long long)1e18 + 1;
const long long MOD = 1000000007ll;
// const long long MOD = 998244353ll;
const int INF = 1000000001;

long long poww(int x, long long pow) {
  if (pow == 0ll)
    return 1;
  if (pow == 1ll)
    return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  // cin >> q;
  while (q-- > 0) {
    int n;
    cin >> n;
    ll a[n];
    for (int i = 0; i < n; ++i)
      cin >> a[i];
    ll xo = 0ll;
    for (int i = 2; i < n; ++i)
      xo ^= a[i];
    ll sum = a[0] + a[1];
    if (xo > sum) {
      cout << -1 << endl;
      continue;
    }
    ll dif = a[0] + a[1] - xo;
    if (dif & 1ll) {
      cout << -1 << endl;
      continue;
    }
    dif >>= 1;
    int maxr = 42;
    bool fail = false;
    bool ones[maxr];
    for (int j = 0; j < maxr; ++j) {
      ll mask = (1ll << (maxr - 1 - j));
      ones[j] = false;
      if (dif & mask) {
        if (xo & mask) {
          fail = true;
          break;
        } else {
          // cout << " ones " << j << endl;
          ones[j] = true;
        }
      }
    }
    if (fail) {
      cout << -1 << endl;
      continue;
    }
    ll MMM = (1ll << (maxr + 1));
    ll dyn[maxr + 1][2];
    dyn[0][0] = 0ll;
    dyn[0][1] = MMM;
    for (int i = 0; i < maxr; ++i) {
      ll mask = (1ll << (maxr - 1 - i));
      if (xo & mask) {
        if (a[1] & mask) {
          dyn[i + 1][0] = dyn[i][0] | mask;
          dyn[i + 1][1] = dyn[i][1];
        } else {
          dyn[i + 1][0] = dyn[i][0];
          dyn[i + 1][1] = min(dyn[i][1], dyn[i][0] | mask);
        }
      } else {
        if (ones[i]) {
          if (a[1] & mask) {
            dyn[i + 1][0] = dyn[i][0] | mask;
            dyn[i + 1][1] = dyn[i][1] | mask;
          } else {
            dyn[i + 1][0] = MMM;
            dyn[i + 1][1] = min(dyn[i][1], dyn[i][0]) | mask;
          }
        } else {
          if (a[1] & mask) {
            dyn[i + 1][0] = MMM;
            dyn[i + 1][1] = dyn[i][1];
          } else {
            dyn[i + 1][0] = dyn[i][0];
            dyn[i + 1][1] = dyn[i][1];
          }
        }
      }
      // cout << " for I+1 " << i + 1 << " mask:  " << mask << " dyn ::: "  <<
      // dyn[i + 1][0] << " ::: " << dyn[i + 1][1] << endl;
      assert(dyn[i + 1][1] > a[1]);
      assert(dyn[i + 1][0] >= MMM || dyn[i + 1][0] <= a[1]);
    }
    if (dyn[maxr][0] < MMM) {
      assert(dyn[maxr][0] == a[1]);
      // if ((dyn[maxr][1] ^ (sum - dyn[maxr][1])) != xo) {
      //  cout << -1 << endl;
      //}
      // else
      cout << 0 << endl;
    } else {
      if (dyn[maxr][1] >=
          sum) { //|| ((dyn[maxr][1] ^ (sum - dyn[maxr][1])) != xo)) {
        cout << -1 << endl;
      } else {
        assert(dyn[maxr][1] > a[1]);
        cout << dyn[maxr][1] - a[1] << endl;
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

const long long MAXVAL = (long long)1e18 + 1;
const long long MOD = 1000000007ll;
// const long long MOD = 998244353ll;
const int INF = 1000000001;

long long poww(int x, long long pow) {
  if (pow == 0ll)
    return 1;
  if (pow == 1ll)
    return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x) % MOD;
  }
  return ans % MOD;
}

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  // cin >> q;
  while (q-- > 0) {
    int n;
    cin >> n;
    ll a[n];
    for (int i = 0; i < n; ++i)
      cin >> a[i];
    ll xo = 0ll;
    for (int i = 2; i < n; ++i)
      xo ^= a[i];
    ll sum = a[0] + a[1];
    if (xo > sum) {
      cout << -1 << endl;
      continue;
    }
    ll dif = a[0] + a[1] - xo;
    if (dif & 1ll) {
      cout << -1 << endl;
      continue;
    }
    dif >>= 1;
    int maxr = 42;
    bool fail = false;
    bool ones[maxr];
    for (int j = 0; j < maxr; ++j) {
      ll mask = (1ll << (maxr - 1 - j));
      ones[j] = false;
      if (dif & mask) {
        if (xo & mask) {
          fail = true;
          break;
        } else {
          // cout << " ones " << j << endl;
          ones[j] = true;
        }
      }
    }
    if (fail) {
      cout << -1 << endl;
      continue;
    }
    ll MMM = (1ll << (maxr + 1));
    ll dyn[maxr + 1][2];
    dyn[0][0] = 0ll;
    dyn[0][1] = MMM;
    for (int i = 0; i < maxr; ++i) {
      ll mask = (1ll << (maxr - 1 - i));
      if (xo & mask) {
        if (a[1] & mask) {
          dyn[i + 1][0] = dyn[i][0] | mask;
          dyn[i + 1][1] = dyn[i][1];
        } else {
          dyn[i + 1][0] = dyn[i][0];
          dyn[i + 1][1] = min(dyn[i][1], dyn[i][0] | mask);
        }
      } else {
        if (ones[i]) {
          if (a[1] & mask) {
            dyn[i + 1][0] = dyn[i][0] | mask;
            dyn[i + 1][1] = dyn[i][1] | mask;
          } else {
            dyn[i + 1][0] = MMM;
            dyn[i + 1][1] = min(dyn[i][1], dyn[i][0]) | mask;
          }
        } else {
          if (a[1] & mask) {
            dyn[i + 1][0] = MMM;
            dyn[i + 1][1] = dyn[i][1];
          } else {
            dyn[i + 1][0] = dyn[i][0];
            dyn[i + 1][1] = dyn[i][1];
          }
        }
      }
      // cout << " for I+1 " << i + 1 << " mask:  " << mask << " dyn ::: "  <<
      // dyn[i + 1][0] << " ::: " << dyn[i + 1][1] << endl;
      assert(dyn[i + 1][1] > a[1]);
      assert(dyn[i + 1][0] == MMM || dyn[i + 1][0] <= a[1]);
    }
    if (dyn[maxr][0] < MMM) {
      assert(dyn[maxr][0] == a[1]);
      if ((dyn[maxr][1] ^ (sum - dyn[maxr][1])) != xo) {
        cout << -1 << endl;
      } else
        cout << 0 << endl;
    } else {
      if (dyn[maxr][1] >= sum ||
          ((dyn[maxr][1] ^ (sum - dyn[maxr][1])) != xo)) {
        cout << -1 << endl;
      } else {
        assert(dyn[maxr][1] > a[1]);
        cout << dyn[maxr][1] - a[1] << endl;
      }
    }
  }
  return 0;
}