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

#include <bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly {
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
// #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1 << 23], *p1 = buf, *p2 = buf;
inline int read() {
  int x = 0, f = 1;
  char c = gc();
  for (; !isdigit(c); c = gc())
    f ^= c == '-';
  for (; isdigit(c); c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  return f ? x : -x;
}
template <typename mitsuha> inline bool read(mitsuha &x) {
  x = 0;
  int f = 1;
  char c = gc();
  for (; !isdigit(c) && ~c; c = gc())
    f ^= c == '-';
  if (!~c)
    return 0;
  for (; isdigit(c); c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  return x = f ? x : -x, 1;
}
template <typename mitsuha> inline int write(mitsuha x) {
  if (!x)
    return 0 & pc(48);
  if (x < 0)
    pc('-'), x = -x;
  int bit[20], i, p = 0;
  for (; x; x /= 10)
    bit[++p] = x % 10;
  for (i = p; i; --i)
    pc(bit[i] + 48);
  return 0;
}
inline char fuhao() {
  char c = gc();
  for (; isspace(c); c = gc())
    ;
  return c;
}
} // namespace chtholly
using namespace chtholly;
using namespace std;
const int yuzu = 2e5;
typedef ll fuko[yuzu | 10];
fuko a;
int main() {
  int i, n = read();
  ll a, b, x = 0, t;
  cin >> a >> b;
  for (i = 3; i <= n; ++i)
    read(t), x ^= t;
  if (x > a + b || (a + b - x) % 2)
    return puts("-1") & 0;
  ll w = (a + b - x) / 2, m = w;
  if (w > a || (w & x))
    return puts("-1") & 0;
  for (i = 56; ~i; --i)
    if ((x >> i & 1) && (m + (1ll << i)) <= a)
      m += 1ll << i;
  printf("%lld\\n", !m ? -1 : a - m);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly {
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
// #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1 << 23], *p1 = buf, *p2 = buf;
inline int read() {
  int x = 0, f = 1;
  char c = gc();
  for (; !isdigit(c); c = gc())
    f ^= c == '-';
  for (; isdigit(c); c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  return f ? x : -x;
}
template <typename mitsuha> inline bool read(mitsuha &x) {
  x = 0;
  int f = 1;
  char c = gc();
  for (; !isdigit(c) && ~c; c = gc())
    f ^= c == '-';
  if (!~c)
    return 0;
  for (; isdigit(c); c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  return x = f ? x : -x, 1;
}
template <typename mitsuha> inline int write(mitsuha x) {
  if (!x)
    return 0 & pc(48);
  if (x < 0)
    pc('-'), x = -x;
  int bit[20], i, p = 0;
  for (; x; x /= 10)
    bit[++p] = x % 10;
  for (i = p; i; --i)
    pc(bit[i] + 48);
  return 0;
}
inline char fuhao() {
  char c = gc();
  for (; isspace(c); c = gc())
    ;
  return c;
}
} // namespace chtholly
using namespace chtholly;
using namespace std;
const int yuzu = 2e5;
typedef ll fuko[yuzu | 10];
fuko a;
ll dfs(int now, ll num1, ll num2) {
  if (num1 > a[1] || num1 + num2 > a[1] + a[2])
    return 1ll << 60;
  if (now < 0) {
    return num1 + num2 == a[1] + a[2] ? num2 - a[2] : (1ll << 60);
  }
  ll ans = 1ll << 60;
  if (a[3] >> now & 1) {
    ll tmp = dfs(now - 1, num1 + (1ll << now), num2);
    if (~tmp)
      ans = min(ans, tmp);
    tmp = dfs(now - 1, num1, num2 + (1ll << now));
    if (~tmp)
      ans = min(ans, tmp);
  } else {
    ll tmp = dfs(now - 1, num1, num2);
    if (~tmp)
      ans = min(ans, tmp);
    tmp = dfs(now - 1, num1 + (1ll << now), num2 + (1ll << now));
    if (~tmp)
      ans = min(ans, tmp);
  }
  return ans >= a[1] ? -1 : ans;
}
int main() {
  int i, n = read();
  for (i = 1; i <= n; ++i)
    read(a[i]);
  for (i = 4; i <= n; ++i)
    a[3] ^= a[i];
  /*(a[1]-x)^(a[2]+x)=a[3]的最小的x*/
  printf("%lld\\n", dfs(48, 0, 0));
}