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

//
// AtCoder Beginner Contest 172
// C
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;

const double PI = 3.14159265358979323846;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  vector<int> b(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];

  ll asum = 0;
  int ai = 0;
  REP(i, n) {
    if ((asum + a[i]) <= k) {
      asum += a[i];
      ai = i + 1;
    } else
      break;
  }
  // MSG(ai);
  // MSG(asum);

  vector<ll> bsum(m, 0);
  bsum[0] = b[0];
  FOR(i, 1, m - 1) { bsum[i] = bsum[i - 1] + b[i]; }
  // REP(i,m) MSG(bsum[i]);

  ll ans = 0;
  FORD(i, ai, 0) {
    ll bi = lower_bound(ALL(bsum), k - asum) - bsum.begin();
    if (bsum[bi] == k - asum)
      bi++;
    if (bi > m)
      bi = m;
    // cout << i << " " << bi << " " << k-asum << endl;
    ans = max(ans, i + bi);
    if (i > 0)
      asum -= a[i - 1];
  }

  cout << ans << endl;
  return 0;
}


Error: -6
free(): invalid size


Fixed submission:

//
// AtCoder Beginner Contest 172
// C
//

#include <algorithm>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;

const double PI = 3.14159265358979323846;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  vector<int> a(n);
  vector<ll> al(n);

  vector<int> b(m);
  vector<ll> bl(m);
  REP(i, n) cin >> a[i];
  REP(i, m) cin >> b[i];

  al[0] = a[0];
  bl[0] = b[0];
  FOR(i, 1, n) al[i] = a[i - 1] + a[i];
  FOR(i, 1, m) bl[i] = b[i - 1] + b[i];

  int ans = 0;
  int ai = 0;
  int bi = 0;
  a.push_back(1000000001);
  b.push_back(1000000001);
  al.push_back(al[n - 1] + 1000000001);
  bl.push_back(bl[m - 1] + 1000000001);
  while (k > 0) {
    int rm = 0;
    if (al[ai] <= bl[bi]) {
      rm = a[ai];
      ai++;
      if (ai > n)
        ai = n;
    } else {
      rm = b[bi];
      bi++;
      if (bi > m)
        bi = m;
    }

    k -= rm;
    if (k >= 0)
      ans++;
    if (ans == m + n)
      break;
  }
  cout << ans << endl;
  return 0;
}