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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked() {
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if (s == e && e == 1048576) {
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if (s == e) {
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x) {
  int k;
  int m = 0;
  x = 0;
  for (;;) {
    k = my_getchar_unlocked();
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      x = k - '0';
      break;
    }
  }
  for (;;) {
    k = my_getchar_unlocked();
    if (k < '0' || k > '9') {
      break;
    }
    x = x * 10 + k - '0';
  }
  if (m) {
    x = -x;
  }
}
inline void rd(long long &x) {
  int k;
  int m = 0;
  x = 0;
  for (;;) {
    k = my_getchar_unlocked();
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      x = k - '0';
      break;
    }
  }
  for (;;) {
    k = my_getchar_unlocked();
    if (k < '0' || k > '9') {
      break;
    }
    x = x * 10 + k - '0';
  }
  if (m) {
    x = -x;
  }
}
struct MY_WRITER {
  char buf[1048576];
  int s;
  int e;
  MY_WRITER() {
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER() {
    if (s) {
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
};
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a) {
  if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) {
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a) { my_putchar_unlocked(a); }
inline void wt_L(int x) {
  int s = 0;
  int m = 0;
  char f[10];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar_unlocked('-');
  }
  while (s--) {
    my_putchar_unlocked(f[s] + '0');
  }
}
inline void wt_L(long long x) {
  int s = 0;
  int m = 0;
  char f[20];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar_unlocked('-');
  }
  while (s--) {
    my_putchar_unlocked(f[s] + '0');
  }
}
template <class S, class T> inline S chmin(S &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}
int N;
long long A[300];
map<pair<pair<long long, long long>, long long>, long long> memo;
long long solve(long long x, long long y, long long tar) {
  long long res = 4611686016279904256LL;
  long long tmp;
  pair<pair<long long, long long>, long long> key;
  key = make_pair(make_pair(x, y), tar);
  if (memo.count(key)) {
    return memo[key];
  }
  if ((x ^ y) == tar) {
    res = 0;
  } else if (x && (x ^ y) % 2 == tar % 2) {
    tmp = solve(x / 2, y / 2, tar / 2);
    chmin(res, 2 * tmp);
    tmp = solve((x - 1) / 2, (y + 1) / 2, tar / 2);
    chmin(res, 2 * tmp + 1);
  }
  return memo[key] = res;
}
int main() {
  int i;
  long long res;
  long long tar = 0;
  rd(N);
  {
    int Lj4PdHRW;
    for (Lj4PdHRW = (0); Lj4PdHRW < (N); Lj4PdHRW++) {
      rd(A[Lj4PdHRW]);
    }
  }
  for (i = (2); i < (N); i++) {
    tar ^= A[i];
  }
  res = solve(A[0], A[1], tar);
  if (res >= A[0]) {
    wt_L(-1);
    wt_L('\\n');
  } else {
    wt_L(res);
    wt_L('\\n');
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N; ll A[300];
//
// map< pair<pair<ll, ll>, ll>, ll > memo;
// ll solve(ll x, ll y, ll tar){
//   ll res = ll_inf, tmp;
//   pair<pair<ll, ll>, ll> key;
//
//   key = make_pair(make_pair(x, y), tar);
//   if(memo.count(key)) return memo[key];
//
//   if( (x^y) == tar ){
//     res = 0;
//   } else if(x && (x^y)%2 == tar % 2 ){
//     tmp = solve(x/2, y/2, tar/2);
//     res <?= 2 * tmp;
//     tmp = solve((x-1)/2, (y+1)/2, tar/2);
//     res <?= 2 * tmp + 1;
//   }
//
//   return memo[key] = res;
// }
//
// {
//   ll res, tar = 0;
//   rd(N,A(N));
//   rep(i,2,N) tar ^= A[i];
//   res = solve(A[0], A[1], tar);
//   wt(if[res>=A[0], -1, res]);
// }


Error: WA

Fixed submission:

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

#define endl '\\n'
#define ll long long
#define rint register int
#define minpq priority_queue<int, vector<int>, greater<int>>
#define maxpq priority_queue<int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\\n';

using db = long double;
using pll = pair<ll, ll>;
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

#define rep(i, x, n, inc) for (i = x; i < n; i += inc)
#define repr(i, x, n, inc) for (i = x; i > n; i += inc)
#define all(a) (a).begin(), (a).end()
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i, s) for (i = s.begin(); i != s.end(); i++)
#define Fill(s, v) memset(s, v, sizeof(s))
#define cout_p(x, p)                                                           \\
  cout << fixed << setprecision((p)) << x << endl // print with precision

#define tc(tt)                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll i, x, j, y, m, z = 0, k, n;
  string s, t;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  rep(i, 0, n, 1) cin >> a[i];
  rep(i, 0, m, 1) cin >> b[i];
  // scan(a, n);
  // scan(b, m);
  // a[n] = b[m] = infl;
  i = 0, j = 0;

  while (i <= n && j <= m && k > 0) {
    if (i == n) {
      if (j != m && k >= b[j]) {
        k -= b[j];
        z++;
        j++;
      } else
        break;
    } else if (j == m) {
      if (i != n && k >= a[i]) {
        k -= a[i];
        z++;
        i++;
      } else
        break;
    } else if (a[i] > b[j] && k >= b[j]) {
      // ce(j << " " << b[j])
      k -= b[j];
      z++;
      j++;
    } else if (a[i] <= b[j] && k >= a[i]) {
      // ce(i << " " << a[i])
      k -= a[i];
      z++;
      i++;
    } else
      break;
  }

  // if (i == n) {

  // } else if (j == m) {

  // } else {

  // }

  ce(z)
}