Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair &l, const ipair &r) { return l.second < r.second; }
bool morePair(const ipair &l, const ipair &r) { return l.second > r.second; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
// const long long INF = 1LL<<60;
void add(long long &a, long long b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
void sub(long long &a, long long b) {
  a -= b;
  if (a < 0)
    a += MOD;
}
void mul(long long &a, long long b) {
  a *= b;
  a %= MOD;
}
ll llmin(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll llmax(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll llabs(ll a) {
  if (a >= 0)
    return a;
  else
    return -a;
}
ll llmodpow(ll a, ll n) {
  if (n == 0)
    return 1;
  ll tmp = llmodpow(a, n / 2);
  mul(tmp, tmp);
  if (n & 1)
    mul(tmp, a);
  return tmp;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> log;
  log.resize(M + 1);
  int a, b;
  for (int i = 0; i < N; i++) {
    cin >> a >> b;
    log[a].push_back(b);
  }

  priority_queue<int> q;
  ll ans = 0;
  for (int i = 1; i <= M; i++) {
    if (log[i].size() != 0) {
      for (auto &tmp : log[i]) {
        // cout << i << " " << tmp << endl;
        q.push(tmp);
      }
    }
    if (!q.empty()) {
      ans += q.top();
      q.pop();
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair &l, const ipair &r) { return l.second < r.second; }
bool morePair(const ipair &l, const ipair &r) { return l.second > r.second; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
// const long long INF = 1LL<<60;
void add(long long &a, long long b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
void sub(long long &a, long long b) {
  a -= b;
  if (a < 0)
    a += MOD;
}
void mul(long long &a, long long b) {
  a *= b;
  a %= MOD;
}
ll llmin(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll llmax(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll llabs(ll a) {
  if (a >= 0)
    return a;
  else
    return -a;
}
ll llmodpow(ll a, ll n) {
  if (n == 0)
    return 1;
  ll tmp = llmodpow(a, n / 2);
  mul(tmp, tmp);
  if (n & 1)
    mul(tmp, a);
  return tmp;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> log;
  // log.resize(M + 1);
  log.resize(100001);
  int a, b;
  for (int i = 0; i < N; i++) {
    cin >> a >> b;
    log[a].push_back(b);
  }

  priority_queue<int> q;
  ll ans = 0;
  for (int i = 1; i <= M; i++) {
    if (log[i].size() != 0) {
      for (auto &tmp : log[i]) {
        // cout << i << " " << tmp << endl;
        q.push(tmp);
      }
    }
    if (!q.empty()) {
      ans += q.top();
      q.pop();
    }
  }
  cout << ans << endl;
  return 0;
}