Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a, n) priority_queue<int> a(n)
#define PQmin(a, n)                                                            \
  priority_queue<int, std::vector<int>, std::greater<int>> a(n)

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<int, std::vector<int>, std::greater<int>> a;
  vector<pair<int, int>> pairs(m);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push(x);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    pairs[i] = make_pair(b, a);
  }
  sort(pairs.rbegin(), pairs.rend());
  ll ans = 0;
  bool flag = true;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pairs.at(i).second; j++) {
      if (a.top() < pairs.at(i).first) {
        a.pop();
        ans += pairs.at(i).first;
      }
      if (a.size() == 0) {
        flag = false;
        break;
      }
    }
    if (flag == false) {
      break;
    }
  }
  while (a.size() > 0) {
    ans += a.top();
    a.pop();
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a, n) priority_queue<int> a(n)
#define PQmin(a, n)                                                            \
  priority_queue<int, std::vector<int>, std::greater<int>> a(n)

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<int, std::vector<int>, std::greater<int>> a;
  vector<pair<int, int>> pairs(m);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push(x);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    pairs[i] = make_pair(b, a);
  }
  sort(pairs.rbegin(), pairs.rend());
  ll ans = 0;
  bool flag = true;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pairs.at(i).second; j++) {
      if (a.top() < pairs.at(i).first) {
        a.pop();
        ans += pairs.at(i).first;
      } else {
        break;
      }
      if (a.size() == 0) {
        flag = false;
        break;
      }
    }
    if (flag == false) {
      break;
    }
  }
  while (a.size() > 0) {
    ans += a.top();
    a.pop();
  }
  cout << ans << endl;
  return 0;
}