Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef pair<ll, ll> pii;
const int mod = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T> void amin(T &a, T b) { a = min(a, b); }
template <class T> void amax(T &a, T b) { a = max(a, b); }

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  VI a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  bool ok = true;
  rep(i, n) if (a[i] < b[i]) ok = false;
  if (ok) {
    puts("0");
    return 0;
  }

  ll sma = accumulate(all(a), 0LL);
  ll smb = accumulate(all(b), 0LL);
  if (smb > sma) {
    puts("-1");
    return 0;
  }

  VI odf(n), pdf, mdf;
  rep(i, n) odf[i] = a[i] - b[i];
  sort(all(odf));
  reverse(all(odf));

  rep(i, n)(odf[i] >= 0 ? pdf : mdf).push_back(odf[i]);

  int ans = 0;

  rep(i, sz(pdf)) {
    ll zf = pdf[i];
    repr(j, sz(mdf) - 1, 0) {
      if (mdf[j] >= 0 || zf <= 0)
        break;
      if (mdf[j] + zf >= 0) {
        zf += mdf[j];
        mdf[j] = 0;
        ans++;
      } else {
        mdf[j] += zf;
        zf = 0;
      }
    }
    if (zf != pdf[i]) {
      pdf[i] = 0;
      ans++;
    }
    while (mdf.back() >= 0)
      mdf.pop_back();
    if (sz(mdf) == 0)
      break;
  }

  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef pair<ll, ll> pii;
const int mod = (int)1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T> void amin(T &a, T b) { a = min(a, b); }
template <class T> void amax(T &a, T b) { a = max(a, b); }

int main() {
  // cin.tie(0); ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  VI a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  bool ok = true;
  rep(i, n) if (a[i] < b[i]) ok = false;
  if (ok) {
    puts("0");
    return 0;
  }

  ll sma = accumulate(all(a), 0LL);
  ll smb = accumulate(all(b), 0LL);
  if (smb > sma) {
    puts("-1");
    return 0;
  }

  VI odf(n), pdf, mdf;
  rep(i, n) odf[i] = a[i] - b[i];
  sort(all(odf));
  reverse(all(odf));

  rep(i, n)(odf[i] >= 0 ? pdf : mdf).push_back(odf[i]);

  int ans = 0;

  rep(i, sz(pdf)) {
    ll zf = pdf[i];
    repr(j, sz(mdf) - 1, 0) {
      if (mdf[j] >= 0 || zf <= 0)
        break;
      if (mdf[j] + zf >= 0) {
        zf += mdf[j];
        mdf[j] = 0;
        ans++;
      } else {
        mdf[j] += zf;
        zf = 0;
      }
    }
    if (zf != pdf[i]) {
      pdf[i] = 0;
      ans++;
    }
    while (sz(mdf) && mdf.back() >= 0)
      mdf.pop_back();
    if (sz(mdf) == 0)
      break;
  }

  cout << ans << "\n";
  return 0;
}