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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#ifndef M_PI
#define M_PI 3.141592653589
#endif
#define int long long
#define double long double
#define f(variable, start, end)                                                \\
  for (int variable = start; variable < end; ++variable)
#define be(a) a.begin(), a.end()
#define no cout << "NO\\n"
#define yes cout << "YES\\n"
#define nor                                                                    \\
  cout << "NO\\n";                                                              \\
  return
#define yesr                                                                   \\
  cout << "YES\\n";                                                             \\
  return
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>

using namespace std;

template <typename T, typename E> vector<T> &operator>>(vector<T> &v, E a) {
  v.push_back(a);
  return v;
}

// pair output
template <typename E> ostream &operator<<(ostream &os, pair<E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

//"map" pair output
template <typename E> ostream &operator<<(ostream &os, pair<const E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

// vector output
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (T i : vec) {
    os << i << ' ';
  }
  return os;
}

// 2 dimensional vector output
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  for (vector<T> i : vec) {
    os << i << '\\n';
  }
  return os;
}

// vector<pair<T, E>> output
template <typename T, typename E>
ostream &operator<<(ostream &os, vector<pair<T, E>> &vec) {
  for (pair<T, E> i : vec) {
    os << i.first << ' ' << i.second << '\\n';
  }
  return os;
}

vi sa, sb;

bool can(int n, vi &a, vi &b, int k) {
  int mn = INT64_MAX;
  f(i, 0, n + 1) {
    int mins = 0;
    if (i > (int)a.size()) {
      continue;
    }

    mins += sa[i];

    if (n - i > (int)b.size()) {
      continue;
    }

    mins += sb[n - i];

    if (mins < mn) {
      mn = mins;
    }
  }
  return mn <= k;
}

int32_t main() {
  cin.tie(NULL);
  cout.tie(NULL);
  cout.setf(ios::fixed);
  cout.precision(10);
  ios_base::sync_with_stdio(false);
#ifdef TIME
  int32_t start = clock();
#endif

  int n, m, k;

  cin >> n >> m >> k;

  vi a(n), b(m);

  sa.resize(n + 1);
  sb.resize(m + 1);

  f(i, 0, n) {
    cin >> a[i];
    sa[i + 1] = sa[i] + a[i];
  }

  f(i, 0, m) {
    cin >> b[i];
    sb[i + 1] = sb[i] + b[i];
  }

  int l = 0, r = n + m + 1;

  while (r != l + 1) {
    int lr = (l + r) / 2;
    if (can(lr, a, b, k)) {
      l = lr;
    } else {
      r = lr;
    }
  }

  cout << l;

#ifdef TIME
  cout << "\\ntime: " << (clock() - start) / (CLOCKS_PER_SEC * 1.0);
#endif

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#ifndef M_PI
#define M_PI 3.141592653589
#endif
#define int long long
#define double long double
#define f(variable, start, end)                                                \\
  for (int variable = start; variable < end; ++variable)
#define be(a) a.begin(), a.end()
#define no cout << "NO\\n"
#define yes cout << "YES\\n"
#define nor                                                                    \\
  cout << "NO\\n";                                                              \\
  return
#define yesr                                                                   \\
  cout << "YES\\n";                                                             \\
  return
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>

using namespace std;

template <typename T, typename E> vector<T> &operator>>(vector<T> &v, E a) {
  v.push_back(a);
  return v;
}

// pair output
template <typename E> ostream &operator<<(ostream &os, pair<E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

//"map" pair output
template <typename E> ostream &operator<<(ostream &os, pair<const E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

// vector output
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (T i : vec) {
    os << i << ' ';
  }
  return os;
}

// 2 dimensional vector output
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  for (vector<T> i : vec) {
    os << i << '\\n';
  }
  return os;
}

// vector<pair<T, E>> output
template <typename T, typename E>
ostream &operator<<(ostream &os, vector<pair<T, E>> &vec) {
  for (pair<T, E> i : vec) {
    os << i.first << ' ' << i.second << '\\n';
  }
  return os;
}

bool can(int n, vi &a, vi &b, int k) {
  int mn = INT64_MAX;
  f(i, 0, n) {
    int mins = 0;
    f(j, 0, i) { mins += a[j]; }
    f(j, 0, n - i) { mins += b[j]; }
    if (mins < mn) {
      mn = mins;
    }
  }
  return mn <= k;
}

int32_t main() {
  cin.tie(NULL);
  cout.tie(NULL);
  cout.setf(ios::fixed);
  cout.precision(10);
  ios_base::sync_with_stdio(false);
#ifdef TIME
  int32_t start = clock();
#endif

  int n, m, k;

  cin >> n >> m >> k;

  vi a(n), b(m);

  f(i, 0, n) { cin >> a[i]; }

  f(i, 0, m) { cin >> b[i]; }

  int l = 0, r = n + m + 1;

  while (r != l + 1) {
    int lr = (l + r) / 2;
    if (can(lr, a, b, k)) {
      l = lr;
    } else {
      r = lr;
    }
  }

  cout << l;

#ifdef TIME
  cout << "\\ntime: " << (clock() - start) / (CLOCKS_PER_SEC * 1.0);
#endif

  return 0;
}