Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MINF(a) memset(a, 0x3f, sizeof(a))
#define POW(n) (1LL << (n))
#define IN(i, a, b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline void SORT(T &a) { sort(ALL(a)); }
template <typename T> inline void REV(T &a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T &a) {
  sort(ALL(a));
  a.erase(unique(ALL(a)), a.end());
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ----------------------------------------------------------------------------------------------------
 */

int N, M;
int A[100010];
pii p[100010];

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

  cin >> N >> M;
  REP(i, N) cin >> A[i];
  sort(A, A + N);
  REP(i, M) cin >> p[i].second >> p[i].first;
  sort(p, p + M);
  reverse(p, p + M);
  vector<int> v;
  REP(i, M) {
    REP(j, p[i].second) {
      v.push_back(p[i].first);
      if (v.size() == N)
        break;
    }
    if (v.size() == N)
      break;
  }
  int ans = 0;
  REP(i, N) ans += max(v[i], A[i]);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MINF(a) memset(a, 0x3f, sizeof(a))
#define POW(n) (1LL << (n))
#define IN(i, a, b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline void SORT(T &a) { sort(ALL(a)); }
template <typename T> inline void REV(T &a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T &a) {
  sort(ALL(a));
  a.erase(unique(ALL(a)), a.end());
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ----------------------------------------------------------------------------------------------------
 */

int N, M;
int A[100010];
pii p[100010];

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

  cin >> N >> M;
  REP(i, N) cin >> A[i];
  sort(A, A + N);
  REP(i, M) cin >> p[i].second >> p[i].first;
  sort(p, p + M);
  reverse(p, p + M);
  vector<int> v;
  REP(i, M) {
    REP(j, p[i].second) {
      v.push_back(p[i].first);
      if (v.size() == N)
        break;
    }
    if (v.size() == N)
      break;
  }
  int ans = 0;
  REP(i, N) {
    if (i < v.size())
      ans += max(v[i], A[i]);
    else
      ans += A[i];
  }
  cout << ans << endl;

  return 0;
}