Problem p03038 (C++)

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.second > b.second; }
int main(void) {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<pair<ll, ll>> v(M);
  REP(i, M) {
    ll b, c;
    cin >> b >> c;
    v[i].first = b;
    v[i].second = c;
  }
  SORT(A);
  sort(v.begin(), v.end(), comp);

  ll t = 0;
  vector<ll> newA(N);
  REP(i, M) {
    ll b = v[i].first, c = v[i].second;
    FOR(j, t, min(t + b, N)) { newA[j] = c; }
    t += b;
  }
  vector<ll> AA(N);
  REP(i, N) AA[i] = max(A[i], newA[i]);
  ll ans = 0;
  REP(i, N) ans += AA[i];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.second > b.second; }
int main(void) {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<pair<ll, ll>> v(M);
  REP(i, M) {
    ll b, c;
    cin >> b >> c;
    v[i].first = b;
    v[i].second = c;
  }
  SORT(A);
  sort(v.begin(), v.end(), comp);

  ll t = 0;
  vector<ll> newA(N);
  REP(i, M) {
    ll b = v[i].first, c = v[i].second;
    for (ll j = t; j < min(t + b, N); j++) {
      newA[j] = c;
    }
    t += b;
  }
  vector<ll> AA(N);
  REP(i, N) AA[i] = max(A[i], newA[i]);
  ll ans = 0;
  REP(i, N) ans += AA[i];
  cout << ans << endl;
  return 0;
}