Problem p02626 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 300
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;

ll a[maxn + 5];
int can[2][2];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld", &a[i]);
  ll C = 0;
  rep(i, 3, n) C ^= a[i];
  ll M = (1 << 20) - 1;
  memset(can, -1, sizeof can);
  rep(low, 0, M) {
    ll tmp1 = (a[1] & M) - low;
    ll tmp2 = ((C & M) ^ low) - (a[2] & M);
    int mark1 = 0, mark2 = 0;
    // if(low==1) printf("?? %lld %lld\\n",tmp1,tmp2);
    if (tmp1 < 0)
      tmp1 += M + 1, mark1 = 1;
    if (tmp2 < 0)
      tmp2 += M + 1, mark2 = 1;
    if (tmp1 == tmp2)
      can[mark1][mark2] = max(can[mark1][mark2], low);
  }
  ll ans = 0;
  rep(high, 0, M) {
    ll tmp1 = (a[1] >> 20) - high;
    ll tmp2 = ((C >> 20) ^ high) - (a[2] >> 20);
    // if(high==0) printf("?? %lld %lld\\n",tmp1,tmp2);
    ll cha = tmp1 - tmp2;
    if (llabs(cha) <= 1 && tmp1 >= 0 && tmp2 >= 0) {
      if (cha == 1)
        ans = can[1][0] == -1 ? ans : max(ans, (((ll)high) << 20) + can[1][0]);
      else if (cha == -1)
        ans = can[0][1] == -1 ? ans : max(ans, (((ll)high) << 20) + can[0][1]);
      else {
        ans = can[0][0] == -1 ? ans : max(ans, (((ll)high) << 20) + can[0][0]);
        if (tmp1 > 0)
          ans =
              can[1][1] == -1 ? ans : max(ans, (((ll)high) << 20) + can[1][1]);
      }
    }
  }
  if (ans == 0)
    puts("-1");
  else {
    printf("%lld\\n", a[1] - ans);
    assert(((ans ^ (a[2] + a[1] - ans)) == C));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 300
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;

ll a[maxn + 5];
int can[2][2];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld", &a[i]);
  ll C = 0;
  rep(i, 3, n) C ^= a[i];
  ll M = (1 << 20) - 1;
  memset(can, -1, sizeof can);
  rep(low, 0, M) {
    ll tmp1 = (a[1] & M) - low;
    ll tmp2 = ((C & M) ^ low) - (a[2] & M);
    int mark1 = 0, mark2 = 0;
    // if(low==1) printf("?? %lld %lld\\n",tmp1,tmp2);
    if (tmp1 < 0)
      tmp1 += M + 1, mark1 = 1;
    if (tmp2 < 0)
      tmp2 += M + 1, mark2 = 1;
    if (tmp1 == tmp2)
      can[mark1][mark2] = max(can[mark1][mark2], low);
  }
  ll ans = 0;
  rep(high, 0, M) {
    ll tmp1 = (a[1] >> 20) - high;
    ll tmp2 = ((C >> 20) ^ high) - (a[2] >> 20);
    // if(high==0) printf("?? %lld %lld\\n",tmp1,tmp2);
    ll cha = tmp1 - tmp2;
    if (llabs(cha) <= 1 && tmp1 >= 0 && tmp2 >= 0) {
      if (cha == 1)
        ans = max(ans, (((ll)high) << 20) + can[1][0]);
      else if (cha == -1)
        ans = max(ans, (((ll)high) << 20) + can[0][1]);
      else {
        ans = max(ans, (((ll)high) << 20) + can[0][0]);
        if (tmp1 > 0)
          ans = max(ans, (((ll)high) << 20) + can[1][1]);
      }
    }
  }
  if (ans == 0)
    puts("-1");
  else {
    printf("%lld\\n", a[1] - ans);
    assert(((ans ^ (a[2] + a[1] - ans)) == C));
  }
  return 0;
}