Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;

#ifdef _LOCAL
struct Tester {
  Tester() { freopen("sample.in", "r", stdin); }
  ~Tester() {
    fprintf(stderr, "\nTime: %d ms\n", int(1000.0 * clock() / CLOCKS_PER_SEC));
  }
} _tester;
#endif

const int INF = 0x3F3F3F3F;
const LL INFLL = 0x3F3F3F3F3F3F3F3FLL;
const int N = 2550;

struct aP {
  int a, b;
  aP() {}
  aP(int a, int b) : a(a), b(b) {}
  bool operator<(const aP &c) const {
    if (a == c.a)
      return b > c.b;
    return a > c.a;
  }
};

struct bP {
  int a, b;
  bP() {}
  bP(int a, int b) : a(a), b(b) {}
  bool operator<(const bP &c) const {
    if (b == c.b)
      return a > c.a;
    return b > c.b;
  }
};

priority_queue<aP> input;
priority_queue<bP> already;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    input.push(aP(m - a, b));
  }
  while (!input.empty()) {
    if (input.top().a >= (int)already.size()) {
      // printf("[%d,%d]\n", input.top().a, input.top().b);
      already.push(bP(input.top().a, input.top().b));
    } else if (input.top().a == (int)already.size() - 1 &&
               input.top().b > already.top().b) {
      // printf("[%d,%d]->[%d,%d]\n", already.top().a, already.top().b,
      // input.top().a, input.top().b);
      already.pop();
      already.push(bP(input.top().a, input.top().b));
    }
    input.pop();
  }
  LL ans = 0;
  while (!already.empty()) {
    ans += 1LL * already.top().b;
    already.pop();
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;

#ifdef _LOCAL
struct Tester {
  Tester() { freopen("sample.in", "r", stdin); }
  ~Tester() {
    fprintf(stderr, "\nTime: %d ms\n", int(1000.0 * clock() / CLOCKS_PER_SEC));
  }
} _tester;
#endif

const int INF = 0x3F3F3F3F;
const LL INFLL = 0x3F3F3F3F3F3F3F3FLL;
const int N = 2550;

struct aP {
  int a, b;
  aP() {}
  aP(int a, int b) : a(a), b(b) {}
  bool operator<(const aP &c) const {
    if (a == c.a)
      return b > c.b;
    return a > c.a;
  }
};

struct bP {
  int a, b;
  bP() {}
  bP(int a, int b) : a(a), b(b) {}
  bool operator<(const bP &c) const {
    if (b == c.b)
      return a > c.a;
    return b > c.b;
  }
};

priority_queue<aP> input;
priority_queue<bP> already;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    input.push(aP(m - a, b));
  }
  while (!input.empty()) {
    if (input.top().a >= (int)already.size()) {
      // printf("[%d,%d]\n", input.top().a, input.top().b);
      already.push(bP(input.top().a, input.top().b));
    } else if ((int)already.size() - 1 >= 0 &&
               input.top().a == (int)already.size() - 1 &&
               input.top().b > already.top().b) {
      // printf("[%d,%d]->[%d,%d]\n", already.top().a, already.top().b,
      // input.top().a, input.top().b);
      already.pop();
      already.push(bP(input.top().a, input.top().b));
    }
    input.pop();
  }
  LL ans = 0;
  while (!already.empty()) {
    ans += 1LL * already.top().b;
    already.pop();
  }
  printf("%lld\n", ans);
  return 0;
}