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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define f(t) for (ll i = 0; i < t; i++)
#define ll long long int
#define ld long double
#define umpl unordered_map<ll, ll>
#define vl vector<ll>
#define vld vector<ld>
#define vvl vector<vl>
#define pb push_back
#define mod 998244353
#define inf 1e18
#define pcout(x, p) cout << fixed << setprecision(p) << x
#define all(a) a.begin(), a.end()
#define endl "\\n"

inline ll mul(ll a, ll b) { return (a * 1ll * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll ceil(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
inline ll mo(ll a, ll b) {
  if (b < 0) {
    b = abs(b) % mod;
    b = a - b;
    return (b < 0) ? b + mod : b;
  } else {
    return (a + b) % mod;
  }
}
inline ll pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else {
    if (b % 2 == 0) {
      ll temp = pow(a, b / 2);
      return temp * temp;
    } else {
      return a * pow(a, b - 1);
    }
  }
}

struct comp {
  template <class T1, class T2, class T3, class T4>
  bool operator()(const pair<T1, T2> &p1, const pair<T3, T4> &p2) {
    return p1.first < p2.first;
  }
};

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vl a(n), b(m);
  f(n) cin >> a[i];
  f(m) cin >> b[i];
  ll sum = 0;
  map<ll, ll> mp;
  ll ans = 0;
  f(n) {
    sum += a[i];
    mp[sum] = i + 1;
    if (sum <= k)
      ans = max(ans, i + 1);
  }
  sum = 0;
  ll t = 0;
  f(m) {
    sum += b[i];
    t++;
    if (sum > k)
      break;
    auto it = mp.upper_bound(k - sum);
    if (it == mp.end()) {
      ans = max(ans, t + n);
    } else {
      ans = max(ans, t + it->second - 1);
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define f(t) for (ll i = 0; i < t; i++)
#define ll long long int
#define ld long double
#define umpl unordered_map<ll, ll>
#define vl vector<ll>
#define vld vector<ld>
#define vvl vector<vl>
#define pb push_back
#define mod 998244353
#define inf 1e18
#define pcout(x, p) cout << fixed << setprecision(p) << x
#define all(a) a.begin(), a.end()
#define endl "\\n"

inline ll mul(ll a, ll b) { return (a * 1ll * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll ceil(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
inline ll mo(ll a, ll b) {
  if (b < 0) {
    b = abs(b) % mod;
    b = a - b;
    return (b < 0) ? b + mod : b;
  } else {
    return (a + b) % mod;
  }
}
inline ll pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else {
    if (b % 2 == 0) {
      ll temp = pow(a, b / 2);
      return temp * temp;
    } else {
      return a * pow(a, b - 1);
    }
  }
}

struct comp {
  template <class T1, class T2, class T3, class T4>
  bool operator()(const pair<T1, T2> &p1, const pair<T3, T4> &p2) {
    return p1.first < p2.first;
  }
};
vvl dp;
ll rec(vl &a, ll l1, ll h1, vl &b, ll l2, ll h2, ll k) {
  if (l1 > h1) {
    ll ans = 0;
    while (l2 <= h2 && k >= b[l2]) {
      k -= b[l2++];
      ans++;
    }
    return ans;
  } else if (l2 > h2) {
    ll ans = 0;
    while (l1 <= h1 && k >= a[l1]) {
      k -= a[l1++];
      ans++;
    }
    return ans;
  } else if (dp[l1][l2] != -1)
    return dp[l1][l2];
  else {
    ll ans = inf;
    if (k >= a[l1])
      ans = max(ans, 1 + rec(a, l1 + 1, h1, b, l2, h2, k - a[l1]));
    if (k >= b[l2])
      ans = max(ans, 1 + rec(a, l1, h1, b, l2 + 1, h2, k - b[l2]));
    return dp[l1][l2] = ans;
  }
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vl a(n), b(m);
  f(n) cin >> a[i];
  f(m) cin >> b[i];
  dp = vvl(n, vl(m, -1));
  cout << rec(a, 0, n - 1, b, 0, m - 1, k) << endl;
}