Problem:
Score : 500 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.

We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.

Given an integer K, list the K smallest Snuke numbers.

Constraints

1 \leq K

The K-th smallest Snuke number is not greater than 10^{15}.

Input

Input is given from Standard Input in the following format:

K

Output

Print K lines. The i-th line should contain the i-th smallest Snuke number.

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

const ll MAX = (ll)(1e9 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

/////////////////////////////////////////////////////////////
class BinaryIndexedTree {
 public:
  static const int MAX_N = (int)(2e5 + 100);

  ll arr[MAX_N];
  int n;

  BinaryIndexedTree(int n) {
    this->n = n;
    init();
  }

  void init() {
    for (int i = 0; i < n; ++i) arr[i] = 0LL;
  }

  ll sum(int i) {
    ll s = 0LL;
    while (i > 0) {
      s += arr[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, ll x) {
    while (i <= n) {
      arr[i] += x;
      i += i & -i;
    }
  }
};
/////////////////////////////////////////////////////////////

int n;
ll a[MAX_N];
ll b[MAX_N];

int s[MAX_N];

BinaryIndexedTree bit(MAX_N * 2);

bool check(ll x) {
  ll num = 0;

  for (int i = 1; i <= n; ++i) s[i] = (a[i] >= x) ? 1 : -1;
  for (int i = 1; i <= n; ++i) s[i] += s[i - 1];

  bit.init();

  for (int i = 0; i <= n; ++i) {
    num += bit.sum(s[i] + MAX_N);
    bit.add(s[i] + MAX_N, 1);
  }

  ll whole = (1LL * (n + 1) * n) / 2;
  ll target =  (whole - 1) / 2 + 1;

  return (num >= target);
}

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &(a[i]));
    b[i] = a[i];
  }

  sort(b + 1, b + n + 1);

  b[n + 1] = b[n] + 1; // guard

  int ok = 1;
  int ng = n + 1;

  while (abs(ok - ng) > 1) {
    int next = (ok + ng) / 2;

    if (check(b[next])) {
      ok = next;
    }
    else {
      ng = next;
    }
  }

  printf("%lld\n", b[ok]);

  return 0;
}