Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (long long)(x).size()
#define sqr(x) (x) * (x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N (long long)sqrt(2e5 + 1)
#define inf 1e18
#define minf -1e18
#define min3(a, b, c) min(a, min(b, c));
#define max3(a, b, c) max(a, max(b, c));
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define FastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define PRECISION                                                              \
  std::cout.unsetf(std::ios::floatfield);                                      \
  std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout << "RSSB" << endl
#define showzero cout << std::showpoint
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define br "\n"
using namespace std;
typedef double db;
typedef long long ll;
typedef vector<int> vi;
// typedef vector<l>vl ;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstr;
typedef vector<bool> vbool;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n, ll k, ll m = 1e18 + 7) {
  ll i, j, b = 1, c = 1;
  if (n - k < k) {
    k = n - k;
  }
  while (k) {
    b *= n;
    c *= k;
    ll g = __gcd(b, c);
    b /= g;
    c /= g;
    n--;
    k--;
  }
  return (b);
}
long long power(long long k, long long n, long long m = mod) {
  long long res = 1;
  while (n) {
    if (n % 2 != 0) {
      res = (res * k) % m;
    }
    k = (k * k) % m;
    n = n / 2;
  }
  return (res);
}
void READ(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cin >> a;
    v.pb(a);
  }
}
void PRINT(vll &v, ll a = 0) {
  // ll a ;
  for (ll i = a; i < v.size(); i = i + 1) {
    v[i] = max((ll)0, v[i]);
    cout << v[i] << " ";
  }
  cout << "\n";
}
ll n, m, k;
int main() {
  FastIO;
  PRECISION;
  ll a, b, c, d, e, i, j, t, x, y, z, x1, y1, z1, f;
  // double p,q,r,u,w ;
  char ch, chr;
  string str, st, str2, str3;
  // ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
  // cin>>str>>str1;
  cin >> n >> m;
  vll v;
  READ(v, n);
  sort(all(v));
  set<ll> s;
  map<ll, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    s.ins(b);
    mp[b] += a;
  }
  for (i = 0; i < n; i++) {
    a = *s.rbegin();
    b = v[i];
    if (a > b) {
      v[i] = a;
      mp[a]--;
      if (mp[a] == 0) {
        s.erase(a);
      }
    }
  }
  cout << accumulate(all(v), (ll)0);
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (long long)(x).size()
#define sqr(x) (x) * (x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N (long long)sqrt(2e5 + 1)
#define inf 1e18
#define minf -1e18
#define min3(a, b, c) min(a, min(b, c));
#define max3(a, b, c) max(a, max(b, c));
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define FastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define PRECISION                                                              \
  std::cout.unsetf(std::ios::floatfield);                                      \
  std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout << "RSSB" << endl
#define showzero cout << std::showpoint
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define br "\n"
using namespace std;
typedef double db;
typedef long long ll;
typedef vector<int> vi;
// typedef vector<l>vl ;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstr;
typedef vector<bool> vbool;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n, ll k, ll m = 1e18 + 7) {
  ll i, j, b = 1, c = 1;
  if (n - k < k) {
    k = n - k;
  }
  while (k) {
    b *= n;
    c *= k;
    ll g = __gcd(b, c);
    b /= g;
    c /= g;
    n--;
    k--;
  }
  return (b);
}
long long power(long long k, long long n, long long m = mod) {
  long long res = 1;
  while (n) {
    if (n % 2 != 0) {
      res = (res * k) % m;
    }
    k = (k * k) % m;
    n = n / 2;
  }
  return (res);
}
void READ(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cin >> a;
    v.pb(a);
  }
}
void PRINT(vll &v, ll a = 0) {
  // ll a ;
  for (ll i = a; i < v.size(); i = i + 1) {
    v[i] = max((ll)0, v[i]);
    cout << v[i] << " ";
  }
  cout << "\n";
}
ll n, m, k;
int main() {
  FastIO;
  PRECISION;
  ll a, b, c, d, e, i, j, t, x, y, z, x1, y1, z1, f;
  // double p,q,r,u,w ;
  char ch, chr;
  string str, st, str2, str3;
  // ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
  // cin>>str>>str1;
  cin >> n >> m;
  vll v;
  READ(v, n);
  sort(all(v));
  set<ll> s;
  map<ll, ll> mp;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    s.ins(b);
    mp[b] += a;
  }
  for (i = 0; i < n; i++) {
    if (sz(s) == 0) {
      break;
    }
    a = *s.rbegin();
    b = v[i];
    if (a > b) {
      v[i] = a;
      mp[a]--;
      if (mp[a] == 0) {
        s.erase(a);
      }
    }
  }
  cout << accumulate(all(v), (ll)0);
}