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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1], b[m + 1];
  rep(i, n) cin >> a[i + 1];
  rep(i, m) cin >> b[i + 1];
  a[0] = b[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  for (int i = 1; i <= m; i++)
    b[i] += b[i - 1];
  int l = 0, r = n + m + 1;
  while (l + 1 < r) {
    int x = mid(l, r);
    int ok = 0;
    for (int i = 0; i <= n; i++) {
      int j = x - i;
      if (j < 0 || j > m)
        continue;
      if (a[i] + b[j] <= k) {
        ok = 1;
        break;
      }
    }
    if (ok)
      l = x;
    else
      r = x;
  }
  cout << l;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ll long long
#define pll pair<ll, ll>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l, r) l + (r - l) / 2
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(ll &a, ll b) {
  a = (a + b) % mod;
  if (a < 0)
    a += mod;
}
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n], b[n];
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int i = 0, j = 0;
  int cur = 0;
  while (i < n && j < m) {
    if (a[i] < b[j]) {
      cur += a[i];
      i++;
    } else {
      cur += b[j];
      j++;
    }
    if (cur > k)
      break;
  }
  while (cur < k && i < n) {
    cur += a[i];
    i++;
  }
  while (cur < k && j < m) {
    cur += b[j];
    j++;
  }
  if (cur <= k)
    i++;
  cout << i + j - 1 << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}