Problem p02948 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORL(i, x) for (int i = head[x]; i; i = nxt[i])
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<pair<int, int>> p;
vector<pair<int, int>> ans;
priority_queue<pair<int, int>> que;

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  FOR(i, 0, N - 1) {
    int neko, inu;

    cin >> neko;
    cin >> inu;
    inu = 0 - inu;
    p.PB(MP(neko, inu));
  }

  sort(ALL(p));

  int output = 0;
  int ref = 0;
  FOR(i, 0, M - 1) {
    int hold = -1;
    while (p[ref].first <= i + 1 && ref <= SZ(p) - 1) {
      que.push(MP((-p[ref].second), p[ref].first));
      // cout << 0-p[0].second << " " << p[0].first << endl;
      p.erase(p.begin());
    }
    if (!que.empty()) {
      // if(SZ(ans)>=2) sort(ALL(ans),greater<pair<int,int>>());
      output += que.top().first;
      que.pop();
    }
    // cout << output << endl;
  }

  cout << output << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORL(i, x) for (int i = head[x]; i; i = nxt[i])
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<pair<int, int>> p;
vector<pair<int, int>> ans;
priority_queue<pair<int, int>> que;

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  FOR(i, 0, N - 1) {
    int neko, inu;

    cin >> neko;
    cin >> inu;
    inu = 0 - inu;
    p.PB(MP(neko, inu));
  }

  sort(ALL(p));

  int output = 0;
  int ref = 0;
  FOR(i, 0, M - 1) {
    int hold = -1;
    while (p[ref].first <= i + 1 && ref <= SZ(p) - 1) {
      que.push(MP((-p[ref].second), p[ref].first));
      // cout << 0-p[0].second << " " << p[0].first << endl;
      ++ref;
    }
    if (!que.empty()) {
      // if(SZ(ans)>=2) sort(ALL(ans),greater<pair<int,int>>());
      output += que.top().first;
      que.pop();
    }
    // cout << output << endl;
  }

  cout << output << endl;
}