Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100005;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0;
  bool f = 0;
  char ch = nc();
  while (ch > '9' || ch < '0')
    f ^= ch == '-', ch = nc();
  while (ch <= '9' && ch >= '0')
    ret = ret * 10 + ch - '0', ch = nc();
  return f ? -ret : ret;
}
int n, m;
long long a[maxn], s[maxn];
inline void push(long long x) { a[++n] = x; }
inline long long &top() { return a[n]; }
inline void pop() { n -= !!n; }
struct ltl {
  long long x, cnt;
  bool operator<(ltl b) const { return x > b.x; }
};
struct heap {
  int len;
  ltl h[maxn];
  inline void push(ltl x) {
    h[++len] = x;
    for (int son = len; son ^ 1; son >>= 1)
      if (h[son] < h[son >> 1])
        swap(h[son], h[son >> 1]);
      else
        break;
  }
  inline ltl &top() { return h[1]; }
  inline void pop() {
    h[1] = h[len--];
    for (int fa = 1, son; (fa << 1) <= len; fa = son) {
      if ((fa << 1) == len || h[fa << 1] < h[fa << 1 | 1])
        son = fa << 1;
      else
        son = fa << 1 | 1;
      if (h[son] < h[fa])
        swap(h[son], h[fa]);
      else
        break;
    }
  }
} H;
int main() {
  push(m = read());
  for (int i = read(); i--;) {
    long long x = read();
    while (top() >= x && n)
      pop();
    push(x);
  }
  H.push((ltl){a[n], 1});
  for (int i = n - 1; i; i--) {
    long long cnt = 0;
    while (H.len) {
      ltl now = H.top();
      if (now.x < a[i])
        break;
      H.pop();
      cnt += now.x / a[i] * now.cnt;
      if (now.x %= a[i])
        H.push(now);
    }
    H.push((ltl){a[i], cnt});
  }
  while (H.len) {
    ltl now = H.top();
    H.pop();
    s[1] += now.cnt;
    s[now.x + 1] -= now.cnt;
  }
  for (int i = 1; i <= m; i++)
    printf("%lld\n", s[i] += s[i - 1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100005;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long ret = 0;
  bool f = 0;
  char ch = nc();
  while (ch > '9' || ch < '0')
    f ^= ch == '-', ch = nc();
  while (ch <= '9' && ch >= '0')
    ret = ret * 10 + ch - '0', ch = nc();
  return f ? -ret : ret;
}
int n, m;
long long a[maxn], s[maxn];
inline void push(long long x) { a[++n] = x; }
inline long long &top() { return a[n]; }
inline void pop() { n -= !!n; }
struct ltl {
  long long x, cnt;
  bool operator<(ltl b) const { return x > b.x; }
};
struct heap {
  int len;
  ltl h[maxn];
  inline void push(ltl x) {
    h[++len] = x;
    for (int son = len; son ^ 1; son >>= 1)
      if (h[son] < h[son >> 1])
        swap(h[son], h[son >> 1]);
      else
        break;
  }
  inline ltl &top() { return h[1]; }
  inline void pop() {
    h[1] = h[len--];
    for (int fa = 1, son; (fa << 1) <= len; fa = son) {
      if ((fa << 1) == len || h[fa << 1] < h[fa << 1 | 1])
        son = fa << 1;
      else
        son = fa << 1 | 1;
      if (h[son] < h[fa])
        swap(h[son], h[fa]);
      else
        break;
    }
  }
} H;
int main() {
  push(m = read());
  for (int i = read(); i--;) {
    long long x = read();
    while (top() >= x && n)
      pop();
    push(x);
  }
  H.push((ltl){a[n], 1});
  for (int i = n - 1; i; i--) {
    long long cnt = 0;
    while (H.len) {
      ltl now = H.top();
      if (now.x < a[i])
        break;
      H.pop();
      cnt += now.x / a[i] * now.cnt;
      if (now.x %= a[i])
        H.push(now);
    }
    H.push((ltl){a[i], cnt});
  }
  while (H.len) {
    ltl now = H.top();
    H.pop();
    s[1] += now.cnt;
    s[now.x + 1] -= now.cnt;
  }
  for (int i = 1; i <= m; i++)
    printf("%lld\n", s[i] += s[i - 1]);
  return 0;
}