Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<Pll> vp;
typedef vector<bool> vb;
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define rrep(i, a, n) for (ll i = (a); i > (n); --i)
#define erep(i, a, n) for (ll i = (a); i <= (n); ++i)
#define rerep(i, a, n) for (ll i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define SORT(x) sort(all(x))
#define RSORT(x) sort(all(x), greater<ll>())
#define vec_max(V) *max_element(all(V))
#define vec_min(V) *min_element(all(V))
#define vec_sum(V) accumulate(all(V), 0LL)
#define print(X) cout << (X) << "\n"
#define _ << " " <<
#define Yes print("Yes")
#define No print("No")
static const int INF = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  vll A(N);
  rep(i, 0, N) cin >> A[i];
  sort(all(A));
  vp ba(M);
  rep(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    ba[i] = mp(b, a);
  }

  SORT(ba);

  ll index = 0;
  rerep(i, N - 1, 0) {
    rep(j, 0, ba[i].second) {
      if (index >= N)
        break;
      if (ba[i].first > A[index]) {
        A[index] = ba[i].first;
        index++;
      } else
        break;
    }
  }

  print(vec_sum(A));

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<Pll> vp;
typedef vector<bool> vb;
#define rep(i, a, n) for (ll i = (a); i < (n); ++i)
#define rrep(i, a, n) for (ll i = (a); i > (n); --i)
#define erep(i, a, n) for (ll i = (a); i <= (n); ++i)
#define rerep(i, a, n) for (ll i = (a); i >= (n); --i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define SORT(x) sort(all(x))
#define RSORT(x) sort(all(x), greater<ll>())
#define vec_max(V) *max_element(all(V))
#define vec_min(V) *min_element(all(V))
#define vec_sum(V) accumulate(all(V), 0LL)
#define print(X) cout << (X) << "\n"
#define _ << " " <<
#define Yes print("Yes")
#define No print("No")
static const int INF = 1e+9 + 7;

int main() {
  ll N, M;
  cin >> N >> M;
  vll A(N);
  rep(i, 0, N) cin >> A[i];
  sort(all(A));
  vp ba(M);
  rep(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    ba[i] = mp(b, a);
  }

  SORT(ba);

  ll index = 0;
  rerep(i, M - 1, 0) {
    rep(j, 0, ba[i].second) {
      if (index >= N)
        break;
      if (ba[i].first > A[index]) {
        A[index] = ba[i].first;
        index++;
      } else
        break;
    }
  }

  print(vec_sum(A));

  return 0;
}