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

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int A, B, C = 0;
  cin >> A >> B;
  int c;
  rep(i, 2, N) {
    cin >> c;
    C ^= c;
  }
  int OA = A;

  int DP[51][2][2];
  rep(i, 0, 51) rep(j, 0, 2) rep(k, 0, 2) DP[i][j][k] = INF;
  DP[0][0][0] = 0;

  rep(i, 0, 50) rep(j, 0, 2) rep(k, 0, 2) {
    int ai = min(1LL, (A & (1LL << i)));
    int bi = min(1LL, (B & (1LL << i)));
    int ci = min(1LL, (C & (1LL << i)));
    int d = 1LL << i;

    int ni = i + 1, nj = 0, nk = 0;
    if (j == 1) {
      ai--;
      if (ai == -1) {
        ai = 1;
        nj = 1;
      }
    }
    if (k == 1) {
      bi++;
      if (bi == 2) {
        bi = 0;
        nk = 1;
      }
    }

    if ((ai ^ bi) == ci) {
      chmin(DP[ni][nj][nk], DP[i][j][k]);
    }

    ai--;
    if (ai == -1) {
      ai = 1;
      nj = 1;
    }
    bi++;
    if (bi == 2) {
      bi = 0;
      nk = 1;
    }

    if ((ai ^ bi) == ci) {
      chmin(DP[ni][nj][nk], DP[i][j][k] + d);
    }
  }

  if (DP[50][0][0] >= OA) {
    cout << -1 << endl;
  } else {
    cout << DP[50][0][0] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int A, B, C = 0;
  cin >> A >> B;
  int c;
  rep(i, 2, N) {
    cin >> c;
    C ^= c;
  }
  int OA = A;

  rrep(i, 0, 45) {
    int ai = (A >> i) & 1;
    int bi = (B >> i) & 1;
    int ci = (C >> i) & 1;
    int d = 1LL << i;
    if ((ai ^ bi) != ci) {
      int mi = min((A % d) + 1, d - (B % d));
      if (A < mi) {
        cout << -1 << endl;
        return 0;
      }
      A -= mi;
      B += mi;
      i = 45;
    }
  }

  if (A == 0) {
    cout << -1 << endl;
  } else {
    cout << OA - A << endl;
  }

  return 0;
}