alexjercan/bugnet huggingface dataset:
Problem p02626 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 1e5 + 4;

int main() {
  int n;
  scn(n);

  ll a[n], xor_sum = 0;

  rep(i, 0, n) {
    lscn(a[i]);

    if (i > 1)
      xor_sum ^= a[i];
  }

  if ((a[0] ^ a[1]) == xor_sum) {
    pri(0);
    return 0;
  }

  ll val = a[0] + a[1] - xor_sum;

  if (val < 0 or val % 2 == 1) {
    pri(-1);
    return 0;
  }

  ll must_have = (a[0] + a[1] - xor_sum) / 2;

  if ((xor_sum & must_have) != 0) {
    pri(-1);
    return 0;
  }

  // cout<<must_have<<" "<<xor_sum<<endl;

  for (int i = 40; i >= 0; i--)
    if (xor_sum & (1LL << i) and must_have + (1LL << i) <= a[0])
      must_have += (1LL << i);

  // cout<<xor_sum<<" "<<must_have<<endl;

  if (must_have > a[0] or must_have == 0) {
    pri(-1);
    return 0;
  }

  lpri(a[0] - must_have);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\\n")
#define priln(n) printf("%d\\n", n)
#define lpriln(n) printf("%lld\\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
const int mod = 1e9 + 7;
const int N = 1e5 + 4;

int main() {
  int n;
  scn(n);

  ll a[n], xor_sum = 0;

  rep(i, 0, n) {
    lscn(a[i]);

    if (i > 1)
      xor_sum ^= a[i];
  }

  if ((a[0] ^ a[1]) == xor_sum) {
    pri(0);
    return 0;
  }

  ll val = a[0] + a[1] - xor_sum;

  if (val < 0 or val % 2 == 1) {
    pri(-1);
    return 0;
  }

  ll must_have = (a[0] + a[1] - xor_sum) / 2;

  assert((xor_sum & must_have) == 0);

  // cout<<must_have<<" "<<xor_sum<<endl;

  for (int i = 40; i >= 0; i--)
    if (xor_sum & (1LL << i) and must_have + (1LL << i) <= a[0])
      must_have += (1LL << i);

  lpri(a[0] - must_have);

  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll x = 0;
  for (ll i = 2; i < N; i++)
    x ^= A[i];

  ll sum = A[0] + A[1];

  if (sum < x || (sum - x) % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }

  ll b = (sum - x) / 2;
  if (b > A[0]) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 50; i >= 0; i--) {
    if ((b >> i) & 1LL)
      continue;
    if (((x >> i) & 1LL) == 0)
      continue;
    if (b + (1LL << i) > A[0])
      continue;
    b += (1LL << i);
  }

  if (b == 0 || (b ^ (sum - b)) != x) {
    cout << -1 << endl;
    return 0;
  }

  cout << A[0] - b << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll x = 0;
  for (ll i = 2; i < N; i++)
    x ^= A[i];

  for (ll i = 0; i <= A[0] - 1; i++) {
    if ((x ^ (A[0] - i) ^ (A[1] + i)) == 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int main() {
  int n;
  cin >> n;
  int i;
  ll tmp;
  ll a, b;
  cin >> a >> b;
  ll x = 0;
  Rep(n - 2) {
    ll t;
    cin >> t;
    x ^= t;
  }
  ll s = a + b;
  ll d = (s - x) / 2;
  ll ans = d;
  if ((s - x) % 2 || d < 0 || (d & x) || ans > a) {
    cout << -1 << endl;
    return 0;
  }
  for (i = 50; i >= 0; i--) {
    if (x & (1LL << i)) {
      tmp = ans + (1LL << i);
      if (tmp <= a)
        ans = tmp;
    }
  }
  if (ans > 0) {
    cout << a - ans << endl;
    return 0;
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int main() {
  int n;
  cin >> n;
  ll a, b;
  cin >> a >> b;
  ll x = 0;
  Rep(n - 2) {
    ll t;
    cin >> t;
    x ^= t;
  }
  rep(i, a) {
    if (((a - i) ^ (b + i) ^ x) == 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
ll A[303];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) cin >> A[i];
  ll xo = 0;
  for (i = 2; i < N; i++)
    xo ^= A[i];

  vector<int> cand;
  FOR(i, 1 << 20) if (i < A[0]) {
    ll v = (A[0] - i) ^ (A[1] + i) ^ xo;
    if ((v & ((1 << 20) - 1)) == 0)
      cand.push_back(i);
  }

  ll mi = 1LL << 50;
  FORR(c, cand) {
    for (ll a = c; a < min(mi, A[0]); a += 1 << 20) {
      ll v = (A[0] - a) ^ (A[1] + a) ^ xo;
      if (v == 0)
        mi = min(mi, a);
    }
  }

  if (mi == 1LL << 50)
    mi = -1;
  cout << mi << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
ll A[303];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) cin >> A[i];
  ll xo = 0;
  for (i = 2; i < N; i++)
    xo ^= A[i];

  vector<int> cand;
  FOR(i, 1 << 20) if (i < A[0]) {
    ll v = (A[0] - i) ^ (A[1] + i) ^ xo;
    if ((v & ((1 << 20) - 1)) == 0)
      cand.push_back(i);
  }

  ll mi = 1LL << 50;
  FORR(c, cand) {
    for (ll a = c; a < A[0]; a += 1 << 20) {
      ll v = (A[0] - a) ^ (A[1] + a) ^ xo;
      if (v == 0)
        mi = min(mi, a);
    }
  }

  if (mi == 1LL << 50)
    mi = -1;
  cout << mi << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

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

  int n;
  cin >> n;

  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  ll goal = 0;
  for (int i = 2; i < n; i++)
    goal = (goal ^ a.at(i));

  ll sum = a.at(0) + a.at(1);
  if (sum < goal || sum % 2 != goal % 2) {
    cout << -1 << '\\n';
    return 0;
  }

  ll now = (sum - goal) / 2; // Mt. No.2
  vector<ll> x;
  x.emplace_back(now);
  for (int bit = 60; bit >= 0; bit--) {
    // if (now > a.at(1)) break;
    if ((1LL << bit) & goal) {
      if ((now + (1LL << bit)) >= a.at(1))
        x.emplace_back(now + (1LL << bit));
      else
        now += (1LL << bit);
    }
  }

  // for (int i = 0; i < x.size(); i++) cout << x[i] << endl;

  sort(x.begin(), x.end());
  if (x.back() < a.at(1)) {
    cout << -1 << endl;
    return 0;
  }
  ll new2 = *lower_bound(x.begin(), x.end(), a.at(1));
  if (new2 == a.at(0) + a.at(1)) {
    cout << -1 << endl;
    return 0;
  }

  ll sum2 = (new2 ^ (a.at(0) - (new2 - a.at(1))));
  for (int i = 2; i < n; i++) {
    sum2 = (sum2 ^ a.at(i));
  }
  if (sum2 != 0) {
    cout << -1 << '\\n';
    return 0;
  }
  if (new2 - a.at(1) >= 0)
    cout << new2 - a.at(1) << '\\n';
  else
    cout << -1 << '\\n';
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)


Fixed submission:

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

using ll = long long;

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

  int n;
  cin >> n;

  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  ll goal = 0;
  for (int i = 2; i < n; i++)
    goal = (goal ^ a.at(i));

  ll sum = a.at(0) + a.at(1);
  if (sum < goal || sum % 2 != goal % 2) {
    cout << -1 << '\\n';
    return 0;
  }

  ll now = (sum - goal) / 2; // Mt. No.2
  vector<ll> x;
  x.emplace_back(now);
  for (int bit = 60; bit >= 0; bit--) {
    // if (now > a.at(1)) break;
    if ((1LL << bit) & goal) {
      if ((now | (1LL << bit)) >= a.at(1))
        x.emplace_back(now | (1LL << bit));
      else
        now |= (1LL << bit);
    }
  }

  // for (int i = 0; i < x.size(); i++) cout << x[i] << endl;

  sort(x.begin(), x.end());
  if (x.back() < a.at(1)) {
    cout << -1 << endl;
    return 0;
  }
  ll new2 = *lower_bound(x.begin(), x.end(), a.at(1));
  if (new2 == x.at(0) + x.at(1)) {
    cout << -1 << endl;
    return 0;
  }
  if (new2 - a.at(1) >= 0)
    cout << new2 - a.at(1) << '\\n';
  else
    cout << -1 << '\\n';
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
const int K = 43;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  long long x = 0;
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  for (int n = 2; n < N; ++n) {
    x = x xor A[n];
  }
  long long s = A[0] + A[1];

  long long DP[43][2][2];
  for (int i = 0; i < 43; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        DP[i][j][k] = -1;
      }
    }
  }
  DP[0][0][0] = 0;
  long long a = A[0];
  long long v = 1;
  for (int i = 0; i < K - 1; ++i) {
    int cx = x & 1;
    int cs = s & 1;
    int ca = a & 1;
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (-1 == DP[i][j][k]) {
          continue;
        }
        for (int na = 0; na < 2; ++na) {
          for (int nb = 0; nb < 2; ++nb) {
            int ni = i + 1;
            int nj = 0;
            int nk = k;
            if (cx != (na ^ nb)) {
              continue;
            }
            int ns = na + nb + j;
            if (cs != ns % 2) {
              continue;
            }
            if (ns >= 2) {
              nj = 1;
            }
            if (ca < na) {
              nk = 1;
            } else if (ca == na) {
              nk = k;
            } else {
              nk = 0;
            }
            DP[ni][nj][nk] = max(DP[ni][nj][nk], DP[i][j][k] | (v * na));
          }
        }
      }
    }
    x >>= 1;
    s >>= 1;
    a >>= 1;
    v <<= 1;
  }
  a = DP[K - 1][0][0];
  if ((-1 == a) || (0 == a)) {
    cout << -1 << endl;
    return 0;
  }

  cout << A[0] - a << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int n = 0; n < N; ++n) {
    cin >> A[n];
  }
  long long x = 0;
  for (int n = 2; n < N; ++n) {
    x = x xor A[n];
  }
  for (int n = 0; n < A[0]; ++n) {
    long long x2 = (A[0] - n) xor (A[1] + n);
    if (x == x2) {
      cout << n << endl;
      return 0;
    }
  }
  cout << "-1" << endl;

  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

// F
//

#include <iostream>
#include <vector>
#define INF 100000000000000
long long XOR(std::vector<long long> Stone, int begin, int end);
void chmin(long long &a, long long b);

long long dp
    [51][2]
    [2]; // 前の桁からの繰り下がり有無・繰り上がり有無を踏まえた上での1番山から2番山までの最小の移動個数

int main() {
  int N;
  std::cin >> N;
  std::vector<long long> Stone(N);
  for (int i = 0; i < N; i++) {
    std::cin >> Stone[i];
  }
  for (int i = 0; i < 51; i++) {
    for (int debt = 0; debt < 2; debt++) {
      for (int prof = 0; prof < 2; prof++) {
        dp[i][debt][prof] = INF;
      }
    }
  }
  dp[0][0][0] = 0;

  long long base = XOR(Stone, 2, Stone.size() - 1);
  long long ll1 = 1;
  for (int i = 0; i < 51; i++) {
    for (int debt = 0; debt < 2; debt++) {
      for (int prof = 0; prof < 2; prof++) {
        int s0 = (Stone[0] & (ll1 << i)) != 0;
        int s1 = (Stone[1] & (ll1 << i)) != 0;
        int b = (base & (ll1 << i)) != 0;

        // 前からの繰り上がり・繰り下がりに応じて対象桁の値、次の桁への繰り上がり、繰り下がりを更新
        int debt2 = 0, prof2 = 0;
        if (debt) {
          s0--;
        }
        if (s0 < 0) {
          debt2 = 1;
          s0 = 1;
        }
        if (prof) {
          s1++;
        }
        if (s1 > 1) {
          prof2 = 1;
          s1 = 0;
        }

        // 1番山から2番山に移動しないケースを計算(s0^s1 ==
        // bとならないならそのパターンはアウトなのでINFのまま)
        if ((s0 ^ s1) == b) {
          chmin(dp[i + 1][debt2][prof2], dp[i][debt][prof]);
        }

        // 1番山から2番山に移動するケースを計算(s0^s1 ==
        // bとならないならそのパターンはアウトなのでINFのまま)
        s0--;
        if (s0 < 0) {
          debt2 = 1;
          s0 = 1;
        }
        s1++;
        if (s1 > 1) {
          prof2 = 1;
          s1 = 0;
        }
        if ((s0 ^ s1) == b) {
          chmin(dp[i + 1][debt2][prof2], dp[i][debt][prof] + (ll1 << i));
        }
      }
    }
  }
  long long ans;
  if (dp[50][0][0] == INF || dp[50][0][0] == Stone[0]) {
    ans = -1;
  } else {
    ans = dp[50][0][0];
  }
  std::cout << ans;

  return 0;
}

long long XOR(std::vector<long long> Stone, int begin, int end) {
  if (end < begin) {
    return 0;
  }
  long long tmp = Stone[begin];
  for (int i = begin + 1; i <= end; i++) {
    tmp = tmp ^ Stone[i];
  }
  return tmp;
}

void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
  return;
}

Error: TLE

Fixed submission:

// F
//

#include <iostream>
#include <vector>
long long XOR(std::vector<long long> Stone, int begin, int end);
int main() {
  int N;
  std::cin >> N;
  std::vector<long long> Stone(N);
  for (int i = 0; i < N; i++) {
    std::cin >> Stone[i];
  }

  long long base = XOR(Stone, 2, Stone.size() - 1);
  for (long long i = 1; i < Stone[0]; i++) {
    long long moved0 = Stone[0] - i;
    long long moved1 = Stone[1] + i;
    long long totalXOR = base ^ moved0 ^ moved1;
    if (totalXOR == 0) {
      std::cout << i;
      return 0;
    }
  }
  std::cout << -1;

  return 0;
}

long long XOR(std::vector<long long> Stone, int begin, int end) {
  long long tmp = Stone[begin];
  for (int i = begin + 1; i <= end; i++) {
    tmp = tmp ^ Stone[i];
  }
  return tmp;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
using namespace std;
// using namespace atcoder;
int N;
ll A[303];

void Main() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  rep(i, N) cin >> A[i];
  ll xo = 0;
  for (i = 2; i < N; i++)
    xo ^= A[i];

  vector<int> cand;
  rep(i, 1 << 20) if (i < A[0]) {
    ll v = (A[0] - i) ^ (A[1] + i) ^ xo;
    if ((v & ((1 << 20) - 1)) == 0)
      cand.push_back(i);
  }

  ll mi = 1LL << 50;
  for (auto c : cand) {
    for (ll a = c; a < min(mi, A[0]); a += 1 << 20) {
      ll v = (A[0] - a) ^ (A[1] + a) ^ xo;
      if (v == 0)
        mi = min(mi, a);
    }
  }

  if (mi == 1LL << 50)
    mi = -1;
  cout << mi << endl;
}
signed main() {
  Main();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
using namespace std;
// using namespace atcoder;
void Main() {
  ll n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];

  ll v = 0;
  rep(i, n) v ^= A[i];
  if (v == 0) {
    put(0);
    return;
  }

  ll X = 0; // target xor
  for (int i = 2; i < n; i++)
    X ^= A[i];

  ll S = A[0] + A[1]; // +
  ll T = (S - X) / 2; // target and

  ll a1 = 0;
  vector<ll> cand(1LL << 20);
  ll mask = (1LL << 20) - 1;
  ll Sm = S & mask;
  ll cnt = 0;
  rep(a, 1LL << 20) {
    if (a > A[0]) {
      break;
    }
    if (((a + (a ^ X)) & mask) == Sm) {
      cand[cnt++] = a;
    }
  }

  ll ma = -1;
  for (ll i = 0; i < cnt; i++) {
    ll a1 = cand[i];
    rep(a2, 1LL << 20) {
      ll a = (a2 << 20) + a1;
      if (a > A[0]) {
        break;
      }
      if (0 >= a) {
        continue;
      }
      if (a + (a ^ X) == S) {
        ma = max(ma, a);
      }
    }
  }

  if (ma < 0) {
    put(-1);
    return;
  }

  ll res = A[0] - ma;
  put(res);
}
signed main() {
  Main();
  return 0;
}
Problem p02626 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long a[305];
long long m, s;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", a + i);
  m = a[2];
  for (int i = 3; i < n; i++)
    m ^= a[i];
  s = a[0] + a[1];
  long long rem = s - m;
  if ((rem % 2) || (rem < 0)) {
    puts("-1");
    return 0;
  }
  rem /= 2;
  long long b = rem;
  int f = 1;
  for (long long i = 50; i >= 0; i--) {
    long long p = 1ll << i;
    if ((m & p) && (rem & p))
      f = 0;
    if ((m & p) && ((b + p) <= a[0]))
      b += p;
  }
  b = a[0] - b;
  if ((b >= a[0]) || (b < 0))
    f = 0;
  if (f)
    printf("%lld", b);
  else
    puts("-1");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[300], ans;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 3; i <= n; i++)
    ans ^= a[i];
  for (int i = 0; i <= min(a[1] - 1, a[1] ^ a[2]); i++)
    if (((a[1] - i) ^ (a[2] + i)) == ans) {
      cout << i;
      return 0;
    }
  cout << -1;
  return 0;
}