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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
map<int, int> mp;
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> a(n), b(m);
  vector<ll> suma(n + 1, 0), sumb(m + 1, 0);
  suma[0] = sumb[0] = 0;
  rep(i, n) {
    cin >> a[i];
    suma[i + 1] = suma[i];
    suma[i + 1] += a[i];
  }
  rep(i, m) {
    cin >> b[i];
    sumb[i + 1] = sumb[i];
    sumb[i + 1] += b[i];
  }
  ll ans = 0, tmpans = 0, tmpk;
  rep(i, n + 1) {
    tmpk = k;
    tmpans = i;
    tmpk -= suma[i];
    if (tmpk < 0)
      break;
    int lower, upper, current;
    lower = 0;
    upper = m;
    if (0 <= tmpk - sumb[upper])
      tmpans += upper;
    else {
      current = (upper + lower) / 2;
      while (1 < upper - lower) {
        if (tmpk - sumb[current] < 0)
          upper = current;
        else if (0 <= tmpk - sumb[current])
          lower = current;
        current = (upper + lower) / 2;
      }
      tmpans += lower;
    }
    if (ans < tmpans)
      ans = tmpans;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
int main() {
  int n, m, k, tmp;
  cin >> n >> m >> k;
  int asum = 0, bsum = 0;
  vector<int> a(n), b(m);
  bool reada = true, readb = true;
  rep(i, n) {
    if (reada) {
      cin >> a[i];
      asum += a[i];
      if (k < asum) {
        asum -= a.back();
        a.resize(a.size() - 1);
        reada = false;
      }
    } else
      cin >> tmp;
  }
  rep(i, m) {
    if (readb) {
      cin >> b[i];
      bsum += b[i];
      if (k < bsum) {
        bsum -= b.back();
        b.resize(b.size() - 1);
        readb = false;
      }
    } else
      cin >> tmp;
  }
  n = a.size();
  m = b.size();
  double costa, costb;
  int ai, bi;
  ai = bi = 0;
  ll ans = 0;
  int mode = 1;
  while (0 < k) {
    if (ai == n && bi == m)
      break;
    if (ai == n)
      mode = 2;
    else if (bi == m)
      mode = 1;
    else {
      costa = double(asum) / (n - ai);
      costb = double(bsum) / (m - bi);
      if (costa < costb)
        mode = 1;
      else
        mode = 2;
    }
    if (mode == 1) {
      k -= a[ai];
      asum -= a[ai];
      if (k < 0)
        break;
      int tmp = b.back();
      while (tmp < a[ai]) {
        if (b.size() == 0)
          break;
        b.resize(b.size() - 1);
        m--;
        tmp += b.back();
      }
      ai++;
      ans++;
    } else {
      k -= b[bi];
      bsum -= b[bi];
      if (k < 0)
        break;
      int tmp = 0;
      while (tmp < b[bi]) {
        if (a.size() == 0)
          break;
        a.resize(a.size() - 1);
        n--;
        tmp += a.back();
      }
      bi++;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}