Problem p03038 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
  return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
  return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    ist >> e;
  return ist;
}

typedef long long ll;

vector<int> A;
vector<pair<int, int>> BC;
int main() {
  int N, M;
  cin >> N >> M;

  A.resize(N + 1);
  rep(i, N) scanf("%d", &A[i]);
  A[N] = INT_MAX;

  BC.resize(M);
  rep(i, M) scanf("%d %d", &BC[i].first, &BC[i].second);

  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end(),
       [](auto const &x, auto const &y) { return x.second >= y.second; });

  int edited = 0;
  ll ans = 0;
  rep(i, M) {
    int b = BC[i].first, c = BC[i].second;
    int last =
        std::lower_bound(A.begin() + edited, A.begin() + N, c) - A.begin();
    int distance = min(b, last - edited);
    if (!distance) {
      break;
    }
    ans += (ll)distance * c;
    edited += distance;
    if (edited >= N) {
      break;
    }
  }
  REP(i, edited, N) ans += A[i];
  cout << ans << endl;
}


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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
  return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
  return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    ist >> e;
  return ist;
}

typedef long long ll;

vector<int> A;
vector<pair<int, int>> BC;
int main() {
  int N, M;
  cin >> N >> M;

  A.resize(N + 1);
  rep(i, N) scanf("%d", &A[i]);
  A[N] = INT_MAX;

  BC.resize(M);
  rep(i, M) scanf("%d %d", &BC[i].first, &BC[i].second);

  sort(A.begin(), A.end());
  sort(BC.begin(), BC.end(), [](auto const &x, auto const &y) {
    return x.second == y.second ? x.first > y.first : x.second > y.second;
  });

  int edited = 0;
  ll ans = 0;
  rep(i, M) {
    int b = BC[i].first, c = BC[i].second;
    int last =
        std::lower_bound(A.begin() + edited, A.begin() + N, c) - A.begin();
    int distance = min(b, last - edited);
    if (!distance) {
      break;
    }
    ans += (ll)distance * c;
    edited += distance;
    if (edited >= N) {
      break;
    }
  }
  REP(i, edited, N) ans += A[i];
  cout << ans << endl;
}