Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define ALL(v) (v).begin(), (v).end()
typedef long long ll;
using namespace std;

static const ll mod = 1e9 + 7;
static const ll inf = 1e18;

static const int White = 0;
static const int Gray = 1;
static const int Black = 2;

void pvector(vector<ll> A) {
  cout << "[vector]";
  for (int i = 0; i < A.size(); i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}

int main() {
  int n, m, t;
  ll b, c;
  ll ans = 0;
  cin >> n >> m;
  vector<ll> a;
  vector<pair<ll, ll>> pairs;
  REP(i, n) {
    cin >> t;
    a.push_back(t);
  }
  sort(ALL(a));
  REP(i, m) {
    cin >> b >> c;
    pairs.push_back(make_pair(b, c));
  }
  /*cout << "[vector]";
  for(int i = 0; i < pairs.size(); i++){
      cout << pairs[i].first << " " << pairs[i].second << endl;
  }*/
  sort(ALL(pairs), compare_by_b);
  int point = 0;
  bool flag = 0;
  int j = 0;
  while (point < a.size()) {
    int tmpp = point;
    for (int i = point; i < tmpp + pairs[j].first; i++) {
      if (pairs[j].second > a[i]) {
        a[i] = pairs[j].second;
        point++;
      } else {
        flag = 1;
        break;
      }
    }
    j++;
    if (flag)
      break;
  }
  /*cout << "[vector]";
  for(int i = 0; i < pairs.size(); i++){
      cout << pairs[i].first << " " << pairs[i].second << endl;
  }
  pvector(a);
*/
  REP(i, n) { ans += a[i]; }
  p(ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define ALL(v) (v).begin(), (v).end()
typedef long long ll;
using namespace std;

static const ll mod = 1e9 + 7;
static const ll inf = 1e18;

static const int White = 0;
static const int Gray = 1;
static const int Black = 2;

void pvector(vector<ll> A) {
  cout << "[vector]";
  for (int i = 0; i < A.size(); i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}

int main() {
  int n, m, t;
  ll b, c;
  ll ans = 0;
  cin >> n >> m;
  vector<ll> a;
  vector<pair<ll, ll>> pairs;
  REP(i, n) {
    cin >> t;
    a.push_back(t);
  }
  sort(ALL(a));
  REP(i, m) {
    cin >> b >> c;
    pairs.push_back(make_pair(b, c));
  }
  /*cout << "[vector]";
  for(int i = 0; i < pairs.size(); i++){
      cout << pairs[i].first << " " << pairs[i].second << endl;
  }*/
  sort(ALL(pairs), compare_by_b);
  int point = 0;
  bool flag = 0;
  int j = 0;
  while (point < a.size() && j < pairs.size()) {
    int tmpp = point;
    for (int i = point; i < tmpp + pairs[j].first; i++) {
      if (pairs[j].second > a[i]) {
        a[i] = pairs[j].second;
        point++;
      } else {
        flag = 1;
        break;
      }
    }
    j++;
    if (flag)
      break;
  }
  /*cout << "[vector]";
  for(int i = 0; i < pairs.size(); i++){
      cout << pairs[i].first << " " << pairs[i].second << endl;
  }
  pvector(a);
*/
  REP(i, n) { ans += a[i]; }
  p(ans);
}