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

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long puf[200004], suf[200004], k, n, m;
bool func(long long x) {
  long long sum = 0LL;
  if (x == 0LL) {
    return true;
  }
  for (long long i = 1; i <= x; i++) {
    if (x >= i + 1 && i <= n && x - i <= m) {
      sum = suf[i] + puf[x - i];
      if (sum <= k) {
        return true;
      }
    }
    // cout<<"bbb";
  }
  if ((x <= n && suf[x] <= k) || (x <= m && puf[x] <= k)) {
    return true;
  }
  return false;
}
int main() {
  long long i, lo, hi, mid;
  cin >> n >> m >> k;
  long long arr[n + 1], brr[m + 1];
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> brr[i];
  }
  // if(arr[0]>k&&brr[0]>k){cout<<0;return 0;}
  for (i = 1; i <= n; i++) {
    if (i == 1) {
      suf[i] = arr[i];
    } else {
      suf[i] = suf[i - 1] + arr[i];
    }
  }
  for (i = 1; i <= m; i++) {
    if (i == 1) {
      puf[i] = brr[i];
    } else {
      puf[i] = puf[i - 1] + brr[i];
    }
  }
  // for(i=1;i<=n;i++){cout<<suf[i]<<" "<<puf[i]<<" "<<i<<"\\n";}
  lo = 0LL;
  hi = n + m;
  while (lo < hi) {
    // cout<<func(1)<<"aa ";
    mid = lo + (hi - lo + 1) / 2;
    if (func(mid)) {
      lo = mid;
    } else {
      hi = mid - 1;
    }
  }
  cout << lo;
  return 0;
}


Error: WA

Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

long long puf[200004], suf[200004], k, n, m;
bool func(long long x) {
  long long sum = 0LL;
  if (x == 0LL) {
    return true;
  }
  for (long long i = 1; i <= x; i++) {
    if (x >= i + 1 && i <= n) {
      sum = suf[i] + puf[x - i];
      if (sum <= k) {
        return true;
      }
    }
    // cout<<"bbb";
  }
  if ((x <= n && suf[x] <= k) || (x <= m && puf[x] <= k)) {
    return true;
  }
  return false;
}
int main() {
  long long i, lo, hi, mid;
  cin >> n >> m >> k;
  long long arr[n + 1], brr[m + 1];
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> brr[i];
  }
  // if(arr[0]>k&&brr[0]>k){cout<<0;return 0;}
  for (i = 1; i <= n; i++) {
    if (i == 1) {
      suf[i] = arr[i];
    } else {
      suf[i] = suf[i - 1] + arr[i];
    }
  }
  for (i = 1; i <= m; i++) {
    if (i == 1) {
      puf[i] = brr[i];
    } else {
      puf[i] = puf[i - 1] + brr[i];
    }
  }
  // for(i=1;i<=n;i++){cout<<suf[i]<<" "<<puf[i]<<" "<<i<<"\\n";}
  lo = 0LL;
  hi = n + m;
  while (lo < hi) {
    // cout<<func(1)<<"aa ";
    mid = lo + (hi - lo + 1) / 2;
    if (func(mid)) {
      lo = mid;
    } else {
      hi = mid - 1;
    }
  }
  cout << lo;
  return 0;
}