Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef int ll;
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define oset                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define orz                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define inp                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("out.txt", "w", stdout);
ll mod1 = 1e9 + 7;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
// cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int main() {
  orz;
  inp;
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> p;
  priority_queue<ll> pq;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    p.emplace_back(a, b);
  }
  sort(p.begin(), p.end());
  p.push_back(make_pair(-1, 0));
  int i = 0;
  for (int t = 0; t <= m; ++t) {
    while (p[i].first == t) {
      pq.push(p[i].second);
      ++i;
    }
    if (pq.empty())
      continue;
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef int ll;
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define oset                                                                   \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define orz                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define inp                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("out.txt", "w", stdout);
ll mod1 = 1e9 + 7;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
// cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}

int main() {
  orz;
  // inp;
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> p;
  priority_queue<ll> pq;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    p.emplace_back(a, b);
  }
  sort(p.begin(), p.end());
  p.push_back(make_pair(-1, 0));
  int i = 0;
  for (int t = 0; t <= m; ++t) {
    while (p[i].first == t) {
      pq.push(p[i].second);
      ++i;
    }
    if (pq.empty())
      continue;
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
  return 0;
}