Problem p03038 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n", "debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
/* (๑╹◡╹) */
const ll MOD = 1000000007;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/* (๑╹ここにすきなもじをいれてね！╹) */
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/* (๑╹最強╹) */

int main() {
  ll N, M, c, b, m, ans;
  m = 0;
  cin >> N >> M;
  ans = 0;
  vector<ll> vec(N);
  rep(i, N) { cin >> vec.at(i); }

  vector<pair<ll, ll>> cb;
  rep(i, M) {
    cin >> b >> c;
    cb.PB(MP(c, b));
  }
  sort(cb.rbegin(), cb.rend());
  rep(i, M) {
    rep(j, cb.at(i).second) { vec.push_back(cb.at(i).first); }
    m += cb.at(i).second;
    if (m < M)
      break;
  }
  sort(vec.rbegin(), vec.rend());
  rep(i, N) { ans += vec.at(i); }
  cout << ans << endl;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n", "debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
/* (๑╹◡╹) */
const ll MOD = 1000000007;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/* (๑╹ここにすきなもじをいれてね！╹) */
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/* (๑╹最強╹) */

int main() {
  ll N, M, c, b, m, ans;
  m = 0;
  cin >> N >> M;
  ans = 0;
  vector<ll> vec(N);
  rep(i, N) { cin >> vec.at(i); }

  vector<pair<ll, ll>> cb;
  rep(i, M) {
    cin >> b >> c;
    cb.PB(MP(c, b));
  }
  sort(cb.rbegin(), cb.rend());
  rep(i, M) {
    rep(j, cb.at(i).second) { vec.push_back(cb.at(i).first); }
    m += cb.at(i).second;
    if (m > N)
      break;
  }
  sort(vec.rbegin(), vec.rend());
  rep(i, N) { ans += vec.at(i); }
  cout << ans << endl;
}