Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>

const int mod = 1000000007;
class carr {
  int **c;
  int level;

public:
  carr(int n, int t = 0) {
    for (int i = 0; i < 30; i++) {
      if (1 << i >= n) {
        level = i + 1;
        break;
      }
    }
    c = new int *[level];
    for (int i = 0; i < level; i++) {
      c[i] = new int[1 << (level - 1 - i)];
      for (int j = 0; j < 1 << (level - 1 - i); j++) {
        if (i == 0) {
          c[0][j] = j < n ? t : 0;
        } else {
          c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
        }
      }
    }
  }
  carr(int n, int *t) {
    for (int i = 0; i < 30; i++) {
      if (1 << i >= n) {
        level = i + 1;
        break;
      }
    }
    c = new int *[level];
    for (int i = 0; i < level; i++) {
      c[i] = new int[1 << (level - 1 - i)];
      for (int j = 0; j < 1 << (level - 1 - i); j++) {
        if (i == 0) {
          c[0][j] = j < n ? t[j] : 0;
        } else {
          c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
        }
      }
    }
  }
  void cadd(int n, int a = 1) {
    for (int i = 0; i < level; i++) {
      c[i][n >> i] += a;
    }
  }
  void csub(int n, int a = 1) {
    for (int i = 0; i < level; i++) {
      c[i][n >> i] -= a;
    }
  }
  int prev(int n) {
    if (c[0][n])
      return n;
    for (int i = 0; i < level; i++) {
      if (n & (1 << i) && c[i][(n >> i) - 1]) {
        int r = (n >> i) - 1;
        for (int j = i - 1; j >= 0; j--) {
          r <<= 1;
          if (c[j][r | 1])
            r |= 1;
        }
        return r;
      }
    }
    return -1;
  }
};
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  std::pair<int, int> *d = new std::pair<int, int>[N];
  for (int i = 0; i < N; i++) {
    int A, B;
    scanf("%d %d", &A, &B);
    d[i].first = -B;
    d[i].second = A;
  }
  std::sort(d, d + N);
  carr c(M, 1);
  int res = 0;
  for (int i = 0; i < N; i++) {
    int r = c.prev(M - d[i].second);
    if (r != -1) {
      res -= d[i].first;
      c.csub(r);
    }
  }
  printf("%d", res);
}

Fixed submission:
#include <algorithm>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>

const int mod = 1000000007;
class carr {
  int **c;
  int level;

public:
  carr(int n, int t = 0) {
    for (int i = 0; i < 30; i++) {
      if (1 << i >= n) {
        level = i + 1;
        break;
      }
    }
    c = new int *[level];
    for (int i = 0; i < level; i++) {
      c[i] = new int[1 << (level - 1 - i)];
      for (int j = 0; j < 1 << (level - 1 - i); j++) {
        if (i == 0) {
          c[0][j] = j < n ? t : 0;
        } else {
          c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
        }
      }
    }
  }
  carr(int n, int *t) {
    for (int i = 0; i < 30; i++) {
      if (1 << i >= n) {
        level = i + 1;
        break;
      }
    }
    c = new int *[level];
    for (int i = 0; i < level; i++) {
      c[i] = new int[1 << (level - 1 - i)];
      for (int j = 0; j < 1 << (level - 1 - i); j++) {
        if (i == 0) {
          c[0][j] = j < n ? t[j] : 0;
        } else {
          c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
        }
      }
    }
  }
  void cadd(int n, int a = 1) {
    for (int i = 0; i < level; i++) {
      c[i][n >> i] += a;
    }
  }
  void csub(int n, int a = 1) {
    for (int i = 0; i < level; i++) {
      c[i][n >> i] -= a;
    }
  }
  int prev(int n) {
    if (c[0][n])
      return n;
    for (int i = 0; i < level; i++) {
      if (n & (1 << i) && c[i][(n >> i) - 1]) {
        int r = (n >> i) - 1;
        for (int j = i - 1; j >= 0; j--) {
          r <<= 1;
          if (c[j][r | 1])
            r |= 1;
        }
        return r;
      }
    }
    return -1;
  }
};
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  std::pair<int, int> *d = new std::pair<int, int>[N];
  for (int i = 0; i < N; i++) {
    int A, B;
    scanf("%d %d", &A, &B);
    d[i].first = -B;
    d[i].second = A;
  }
  std::sort(d, d + N);
  carr c(M, 1);
  int res = 0;
  for (int i = 0; i < N; i++) {
    if (d[i].second > M)
      continue;
    int r = c.prev(M - d[i].second);
    if (r != -1) {
      res -= d[i].first;
      c.csub(r);
    }
  }
  printf("%d", res);
}