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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = \\t" << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int N;
vector<ll> A;
void input() {
  fast_io();
  cin >> N;
  A = fetch_vec<ll>(N);
}

int solve() {
  ll X = 0LL;
  ll S = A[0] + A[1];
  for (int i = 2; i < N; i++)
    X ^= A[i];
  ll D = (S - X) / 2;
  if ((S - X) % 2) {
    cout << -1 << endl;
    return 0;
  }
  ll a = D;
  if ((X ^ A[0] ^ A[1]) == 0LL) {
    cout << 0 << endl;
    return 0;
  }

  if (a >= A[0] || a < 0LL || D & X) {
    cout << -1 << endl;
    return 0;
  }
  int bit = 62;
  while (bit--) {
    if ((X >> bit) & 1LL) {
      if (A[0] > (a ^ (1LL << bit))) {
        a ^= (1LL << bit);
      }
    }
  }
  ll x = A[0] - a;
  if (x >= A[0] || x < 0)
    cout << -1 << endl;
  else
    cout << x << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int N;
vector<ll> A;
void input() {
  fast_io();
  cin >> N;
  A = fetch_vec<ll>(N);
}

bool bitchk(ll a, int bit) { return (a >> bit) & 1LL; }

int solve() {
  ll X = 0;
  for (int i = 0; i < N; i++) {
    X ^= A[i];
  }
  ll Y = A[0] ^ A[1];
  ll Z = X ^ Y;
  if (X == 0) {
    cout << 0 << endl;
    return 0;
  }

  for (ll x = 0; x < A[0]; x++) {
    if (((A[0] - x) ^ (A[1] + x)) == Z) {
      cout << x << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}