Problem p03318 (C++)

Buggy submission (status: Time Limit Exceeded):
/* - - - - - - - - - - - - - - -
        User : 		VanishD
        problem :	[abc101D]
        Points :
- - - - - - - - - - - - - - - */
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
int read() {
  int tmp = 0, fh = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = tmp * 10 + ch - '0';
    ch = getchar();
  }
  return tmp * fh;
}
ll ow10[20], sum[20];
int n;
struct Num {
  ll num;
};
bool operator<(Num x, Num y) {
  ll tmpx = x.num, tmpy = y.num, numx = 0, numy = 0;
  while (tmpx > 0) {
    numx += tmpx % 10;
    tmpx /= 10;
  }
  while (tmpy > 0) {
    numy += tmpy % 10;
    tmpy /= 10;
  }
  ll deata = x.num * numy - y.num * numx;
  return deata > 0 || (deata == 0 && x.num > y.num);
}
priority_queue<Num> hp;
int main() {
  //	freopen(".in", "r", stdin);
  //	freopen(".out", "w", stdout);
  n = read();
  for (int i = 1; i <= 9; i++)
    hp.push((Num){i});
  ow10[0] = 1;
  for (int i = 1; i <= 18; i++)
    ow10[i] = ow10[i - 1] * 10;
  ll now = 0;
  //	printf("%d\n", (Num){11}<(Num){19});
  while (n) {
    ll tmp = hp.top().num;
    hp.pop();
    if (tmp <= now)
      continue;
    printf("%lld\n", tmp);
    n--;
    now = tmp;
    int len = 0;
    while (tmp > 0) {
      sum[++len] = tmp % 10;
      tmp /= 10;
    }
    //	sum[len + 1] = 0;
    for (int i = 1; i <= 18; i++)
      if (sum[i] != 9) {
        sum[i]++;
        ll nex = 0;
        for (int j = 1; j <= len; j++)
          nex = nex + sum[j] * ow10[j - 1];
        if (i > len)
          nex = nex + ow10[i - 1];
        hp.push((Num){nex});
        sum[i]--;
      }
  }
  return 0;
}


Fixed submission:
/* - - - - - - - - - - - - - - -
        User : 		VanishD
        problem :	[abc101D]
        Points :
- - - - - - - - - - - - - - - */
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
int read() {
  int tmp = 0, fh = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = tmp * 10 + ch - '0';
    ch = getchar();
  }
  return tmp * fh;
}
ll ow10[20], sum[20];
int n;
struct Num {
  ll num;
};
bool operator<(Num x, Num y) {
  ll tmpx = x.num, tmpy = y.num, numx = 0, numy = 0;
  while (tmpx > 0) {
    numx += tmpx % 10;
    tmpx /= 10;
  }
  while (tmpy > 0) {
    numy += tmpy % 10;
    tmpy /= 10;
  }
  ll deata = x.num * numy - y.num * numx;
  return deata > 0 || (deata == 0 && x.num > y.num);
}
priority_queue<Num> hp;
int main() {
  //	freopen(".in", "r", stdin);
  //	freopen(".out", "w", stdout);
  n = read();
  for (int i = 1; i <= 9; i++)
    hp.push((Num){i});
  ow10[0] = 1;
  for (int i = 1; i <= 18; i++)
    ow10[i] = ow10[i - 1] * 10;
  ll now = 0;
  //	printf("%d\n", (Num){11}<(Num){19});
  while (n) {
    ll tmp = hp.top().num;
    hp.pop();
    if (tmp <= now)
      continue;
    printf("%lld\n", tmp);
    n--;
    now = tmp;
    int len = 0;
    while (tmp > 0) {
      sum[++len] = tmp % 10;
      tmp /= 10;
    }
    //	sum[len + 1] = 0;
    for (int i = 1; i <= 15; i++)
      if (sum[i] != 9) {
        sum[i]++;
        ll nex = 0;
        for (int j = 1; j <= len; j++)
          nex = nex + sum[j] * ow10[j - 1];
        if (i > len)
          nex = nex + ow10[i - 1];
        hp.push((Num){nex});
        sum[i]--;
      }
  }
  return 0;
}