Problem p03151 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define SZ(a) ((int)(a).size())
#define PB(a) push_back(a)
#define EMP emplace
#define EMPB(...) emplace_back(__VA_ARGS__)
#define MP(a, b) make_pair(a, b)
#define MT(...) make_tuple(__VA_ARGS__)
#define Bit(n) (1LL << (n))
using namespace std;
using pii = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1000000007;
const int INF = 1LL << 30;
const double EPS = 1e-10;

int N;
int A[100010];
int B[100010];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N;
  int sumA = 0, sumB = 0;
  REP(i, N) {
    cin >> A[i];
    sumA += A[i];
  }
  REP(i, N) {
    cin >> B[i];
    sumB += B[i];
  }
  if (sumA < sumB) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> v, w;
  REP(i, N) {
    int tmp = A[i] - B[i];
    if (tmp < 0)
      v.PB(tmp);
    else
      w.PB(tmp);
  }
  if (v.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(ALL(v));
  sort(ALLR(w));
  int p = 0, q = 0;
  int ans = v.size() + 1;
  while (p < v.size()) {
    if (w[q] + v[p] > 0) {
      w[q] += v[p];
      p++;
    } else {
      v[p] -= w[q];
      if (v[p] == 0)
        p++;
      q++;
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define SZ(a) ((int)(a).size())
#define PB(a) push_back(a)
#define EMP emplace
#define EMPB(...) emplace_back(__VA_ARGS__)
#define MP(a, b) make_pair(a, b)
#define MT(...) make_tuple(__VA_ARGS__)
#define Bit(n) (1LL << (n))
using namespace std;
using pii = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1000000007;
const int INF = 1LL << 30;
const double EPS = 1e-10;

int N;
int A[100010];
int B[100010];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N;
  int sumA = 0, sumB = 0;
  REP(i, N) {
    cin >> A[i];
    sumA += A[i];
  }
  REP(i, N) {
    cin >> B[i];
    sumB += B[i];
  }
  if (sumA < sumB) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> v, w;
  REP(i, N) {
    int tmp = A[i] - B[i];
    if (tmp < 0)
      v.PB(tmp);
    else
      w.PB(tmp);
  }
  if (v.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(ALL(v));
  sort(ALLR(w));
  int s = 0;
  REP(i, v.size()) s += -v[i];
  int ans = v.size();
  int t = 0;
  for (int i = 0; i < w.size(); i++) {
    t += w[i];
    ans++;
    if (s <= t)
      break;
  }
  cout << ans << endl;

  return 0;
}