Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

#define ll long long
using vt = ll; // ここで数値の型を変えられる
#define rep(i, n) for (vt i = 0; i < (vt)(n); i++)
#define reps(i, s, n) for (vt i = (vt)(s); i < (vt)(n); i++)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
const ll MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> a;
  rep(i, n) {
    ll x;
    cin >> x;
    a.push(x);
  }
  vector<P> bc(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    bc.at(i) = make_pair(y, x);
  }
  sort(all(bc));
  reverse(all(bc));
  rep(i, m) {
    rep(j, bc.at(i).second) {
      if (a.top() < bc.at(i).first) {
        a.pop();
        a.push(bc.at(i).first);
      } else
        break;
    }
  }
  ll s = 0;
  rep(i, n) {
    s += a.top();
    a.pop();
  }
  cout << s;
  return 0;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

#define ll long long
using vt = ll; // ここで数値の型を変えられる
#define rep(i, n) for (vt i = 0; i < (vt)(n); i++)
#define reps(i, s, n) for (vt i = (vt)(s); i < (vt)(n); i++)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
const ll MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> a;
  rep(i, n) {
    ll x;
    cin >> x;
    a.push(x);
  }
  vector<P> bc(m);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    bc.at(i) = make_pair(y, x);
  }
  sort(all(bc));
  reverse(all(bc));
  rep(i, m) {
    rep(j, bc.at(i).second) {
      if (a.top() < bc.at(i).first) {
        a.pop();
        a.push(bc.at(i).first);
      } else
        break;
    }
  }
  ll s = 0;
  rep(i, n) {
    s += a.top();
    a.pop();
  }
  cout << s;
  return 0;
}