Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using str = string;
using ll = long long;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, f, t) for (int i = int(f); i <= int(t); i++)
#define RFOR(i, f, t) for (int i = int(f); i >= int(t); i--)
#define ALL(vec) (vec).begin(), (vec).end()
#define ASORT(vec) sort(ALL(vec))
#define DSORT(vec) sort(ALL(vec), greater<int>());
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define YES(ans)                                                               \
  if (ans)                                                                     \
    cout << "YES" << endl;                                                     \
  else                                                                         \
    cout << "NO" << endl;
#define Yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
#define yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "yes" << endl;                                                     \
  else                                                                         \
    cout << "no" << endl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  REP(i, n) cin >> a.at(i);
  vector<int> b(m);
  REP(i, m) cin >> b.at(i);

  vector<ll> a_(n + 1);
  a_.at(0) = 0;
  REP(i, n) a_.at(i + 1) = a_.at(i) + a.at(i);
  vector<ll> b_(m + 1);
  b_.at(0) = 0;
  REP(i, m) b_.at(i + 1) = b_.at(i) + b.at(i);

  int s = 0, a_i = 0, b_i = 0;

  while (a_.at(a_i) <= k) {
    a_i++;
    if (a_i > n) {
      a_i = n;
      break;
    }
  }
  while (b_.at(b_i) <= k) {
    b_i++;
    if (b_i > m) {
      b_i = m;
      break;
    }
  }

  n = a_i + 1;
  m = b_i + 1;

  REP(i, n) {
    REP(j, m) {
      if (a_.at(i) + b_.at(j) <= k) {
        if (i + j > s)
          s = i + j;
      } else {
        break;
      }
    }
  }

  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using str = string;
using ll = long long;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, f, t) for (int i = int(f); i <= int(t); i++)
#define RFOR(i, f, t) for (int i = int(f); i >= int(t); i--)
#define ALL(vec) (vec).begin(), (vec).end()
#define ASORT(vec) sort(ALL(vec))
#define DSORT(vec) sort(ALL(vec), greater<int>());
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define YES(ans)                                                               \
  if (ans)                                                                     \
    cout << "YES" << endl;                                                     \
  else                                                                         \
    cout << "NO" << endl;
#define Yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "Yes" << endl;                                                     \
  else                                                                         \
    cout << "No" << endl;
#define yes(ans)                                                               \
  if (ans)                                                                     \
    cout << "yes" << endl;                                                     \
  else                                                                         \
    cout << "no" << endl;

int main() {

  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  REP(i, n) cin >> a.at(i);
  vector<int> b(m);
  REP(i, m) cin >> b.at(i);

  vector<ll> a_(n + 1);
  a_.at(0) = 0;
  REP(i, n) a_.at(i + 1) = a_.at(i) + a.at(i);
  vector<ll> b_(m + 1);
  b_.at(0) = 0;
  REP(i, m) b_.at(i + 1) = b_.at(i) + b.at(i);

  int s = 0, a_i = 0, b_i = 0;

  while (a_.at(a_i) <= k) {
    a_i++;
    if (a_i > n) {
      a_i = n;
      break;
    }
  }
  while (b_.at(b_i) <= k) {
    b_i++;
    if (b_i > m) {
      b_i = m;
      break;
    }
  }

  n = a_i + 1;
  m = b_i + 1;

  REP(i, n) {
    for (int j = max(s - i, 0); j < m; j++) {
      if (a_.at(i) + b_.at(j) <= k) {
        if (i + j > s)
          s = i + j;
      } else {
        break;
      }
    }
  }

  cout << s << endl;
}