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

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

#define vi vector<int>
#define all(v) v.begin(), v.end()
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define D1(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": " << x << "]";                                    \\
    cout << endl;                                                              \\
  }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "] ";                                                              \\
    cout << endl;                                                              \\
  }

const ll MOD = 1e9 + 7;

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  vector<ll> prefa(n), prefb(m);
  for (int i = 0; i < n; i++) {
    if (i)
      prefa[i] = prefa[i - 1] + a[i];
    else
      prefa[i] = a[i];
  }
  for (int i = 0; i < m; i++) {
    if (i)
      prefb[i] = prefb[i - 1] + b[i];
    else
      prefb[i] = b[i];
  }

  int cnt = 0;
  cnt = lower_bound(all(prefb), k + 1) - prefb.begin();

  for (int i = 0; i < n; i++) {
    if (prefa[i] > k)
      break;
    else if (prefa[i] == k) {
      cnt = max(cnt, i + 1);
      break;
    } else {
      int k1 = lower_bound(all(prefb), (k - prefa[i] + 1)) - prefb.begin();
      cnt = max(i + 1 + k1, cnt);
    }
  }
  cout << cnt;
}

int main() {
  IOS solve();
  cerr << endl
       << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}
// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying


Error: WA

Fixed submission:

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

#define vi vector<int>
#define all(v) v.begin(), v.end()
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define D1(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": " << x << "]";                                    \\
    cout << endl;                                                              \\
  }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "] ";                                                              \\
    cout << endl;                                                              \\
  }

const ll MOD = 1e9 + 7;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vi a(n);
  vi b(m);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  int cnt = 0;
  int i = 0, j = 0;
  ll sum = 0;
  while (i < n && j < m) {
    if (a[i] > b[j]) {
      if (sum + b[j] <= k) {
        sum += b[j++];
        cnt++;
      } else {
        cout << cnt;
        return;
      }
    } else {
      if (sum + a[i] <= k) {
        sum += a[i++];
        cnt++;
      } else {
        cout << cnt;
        return;
      }
    }
  }
  if (j < m) {
    while (j < m && sum + b[j] <= k) {
      sum += b[j++];
      cnt++;
    }
  }
  if (i < n) {
    while (i < n && sum + a[i] <= k) {
      sum += b[i++];
      cnt++;
    }
  }
  cout << cnt;
}

int main() {
  IOS solve();
  cerr << endl
       << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}
// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying