Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) scanf("%lld", &A[i]);

  ll x = 0;
  FOR(i, 2, N) x ^= A[i];
  // (A[0] - y) ^ (A[1] + y) ^ x = 0 にしたい y < A[0]
  // A[0], x 反転すると、両方に +1 する操作になる

  ll plus = A[0] - 1; // 0 ~ plus] まで足せる
  ll mask = (1ll << 50) - 1;
  A[0] ^= mask;
  x ^= mask;

  vector<vector<vector<ll>>> dp(60,
                                vector<vector<ll>>(2, vector<ll>(2, longINF)));
  vector<vector<vector<int>>> done(60, vector<vector<int>>(2, vector<int>(2)));

  auto dfs = [&](auto &&dfs, int i, int s1, int s2) -> ll {
    ll &res = dp[i][s1][s2];
    ll cA0 = A[0] >> i;
    ll cA1 = A[1] >> i;
    ll cx = x >> i;

    if ((cA0 + s1 & 1 ^ cA1 + s2 & 1 ^ cx & 1) != 0)
      return longINF;
    if (i == 49)
      return 0;
    if (!chmax(done[i][s1][s2], true))
      return res;

    REP(j, 2) {
      ll pred =
          dfs(dfs, i + 1, (cA0 & 1) + s1 + j >> 1, (cA1 & 1) + s2 + j >> 1);
      if (pred != longINF)
        chmin(res, pred * 2 + j);
    }

    return res;
  };

  ll ans = dfs(dfs, 0, 0, 0);
  if (ans > plus)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N;
  cin >> N;

  vector<ll> A(N);
  REP(i, N) scanf("%lld", &A[i]);

  ll x = 0;
  REP(i, N) x ^= A[i];
  if (x == 0) {
    puts("0");
    return 0;
  }
  x ^= A[0] ^ A[1];
  // (A[0] - y) ^ (A[1] + y) ^ x = 0 にしたい y < A[0]

  // A[0], x 反転すると、両方に +1 する操作になる

  ll plus = A[0] - 1; // 0 ~ plus] まで足せる

  ll mask = (1ll << 50) - 1;
  A[0] ^= mask;
  x ^= mask;

  auto print = [](ll cur) {
    REP(i, 50) printf("%d", cur >> (49 - i) & 1);
    puts("");
  };

  // print(A[0]);
  // puts("");
  // print(A[1]);
  // puts("");
  // print(x);
  // puts("");

  bool ng = false;
  int mnp = -1;
  for (int i = 49; i >= 0; --i) {
    if (((A[0] >> i & 1) ^ (A[1] >> i & 1))) {
      mnp = i;
    }
  }

  REP(i, mnp) {
    if (x >> i & 1)
      ng = true;
  }
  if (mnp != -1 && (x >> mnp & 1) == 0)
    ng = true;

  if (ng) {
    puts("-1");
    return 0;
  }

  // cout << A[0] << " " << A[1] << " " << x << endl;

  FOR(i, 1, plus) if (((A[0] + i) ^ (A[1] + i) ^ x) == 0) {
    cout << i << endl;
    return 0;
  }
  puts("-1");

  return 0;
}