Problem p03151 (C++)

Buggy submission (status: Runtime Error):
/*input
12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259
615762794 632963102 529866931 64991604 74164189 98239366 465611891 362739947
147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}

//}}}
const ll maxn = 300005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const double PI = acos(-1);
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;

    b >>= 1;
  }
  return res;
}
vector<int> v;

int a[maxn], b[maxn];
int main() {
  IOS;
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];

  ll sum = 0;
  REP(i, n) sum += a[i];
  REP(i, n) sum -= b[i];

  if (sum < 0) {
    cout << -1 << '\n';
    return 0;
  }

  REP(i, n) if (a[i] >= b[i]) v.pb(a[i] - b[i]);

  sort(ALL(v));

  int pt = 0;

  while (sum >= v[pt]) {
    sum -= v[pt];
    pt++;
  }
  cout << n - pt << '\n';

  return 0;
}


Fixed submission:
/*input
12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259
615762794 632963102 529866931 64991604 74164189 98239366 465611891 362739947
147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}

//}}}
const ll maxn = 300005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const double PI = acos(-1);
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;

    b >>= 1;
  }
  return res;
}
vector<int> v;

int a[maxn], b[maxn];
int main() {
  IOS;
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];

  ll sum = 0;
  REP(i, n) sum += a[i];
  REP(i, n) sum -= b[i];

  if (sum < 0) {
    cout << -1 << '\n';
    return 0;
  }

  REP(i, n) if (a[i] >= b[i]) v.pb(a[i] - b[i]);

  sort(ALL(v));

  int pt = 0;

  while (pt < SZ(v) && sum >= v[pt]) {
    sum -= v[pt];
    pt++;
  }
  cout << n - pt << '\n';

  return 0;
}