Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)

string n;
int len, ku, dsum, pre = -1, inds, ans;
int N[500010], s[500010], ind[500010], zero[500010];

void add1(void) {
  int la;
  rep(i, len) {
    if (s[i] < 9) {
      s[i]++;
      la = i;
      dsum++;
      break;
    }
    dsum -= 9;
    s[i] = 0;
  }
  if (s[len - 1] == 0) {
    len++;
    dsum++;
    s[len - 1] = 1;
  }
  if (la == 0) {
    if (s[0] == 1) {
      inds -= ind[0];
      ind[0] = 0;
      if (zero[0] <= ind[0] - 1) {
        inds -= ind[zero[0]];
        ind[zero[0]] = ind[0] - zero[0];
        inds += ind[zero[0]];
      }
    }
  } else {
    rep(i, la - 1) zero[i] = i + 1;
    zero[la - 1] = zero[la];
    if (s[la] == 1) {
      ind[0] = la + ind[la];
      inds += ind[0] - ind[la];
      ind[la] = 0;
    } else {
      ind[0] = la;
      inds += ind[0];
    }
  }
  ans += 10;
}

int main() {
  cin >> n;
  len = n.size();
  rep(i, len) N[i] = n[len - i - 1] - '0';
  N[len] = 0;
  rep(i, len + 1) {
    if (i == 0) {
      ku = 1 + (N[i] * 9) / 10;
      ans = 10 - (N[i] * 9) % 10;
    } else {
      s[i - 1] = (N[i] * 9 + ku) % 10;
      dsum += s[i - 1];
      ku = (N[i] * 9 + ku) / 10;
    }
  }
  if (s[len - 1] == 0)
    len--;
  for (int i = len - 1; i >= 0; i--) {
    if (i == len - 1)
      zero[i] = i + 1;
    else if (s[i + 1] == 0)
      zero[i] = i + 1;
    else
      zero[i] = zero[i + 1];
  }
  rep(i, len) {
    if (pre != -1) {
      if (s[i] <= 1)
        ind[pre]++;
      else
        pre = -1;
    } else {
      if (s[i] == 0) {
        pre = i;
        ind[pre] = 1;
      }
    }
  }
  rep(i, len) inds += ind[i];
  while (1) {
    if (dsum + 9 * inds >= ans && ans >= dsum) {
      cout << (ans + 8) / 9 << endl;
      return 0;
    }
    add1();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)

string n;
int len, ku, dsum, pre = -1, inds, ans;
int N[500010], s[500010], ind[500010], zero[500010];

void add1(void) {
  int la;
  rep(i, len) {
    if (s[i] < 9) {
      s[i]++;
      la = i;
      dsum++;
      break;
    }
    dsum -= 9;
    s[i] = 0;
  }
  if (s[len - 1] == 0) {
    len++;
    dsum++;
    s[len - 1] = 1;
    rep(i, len - 2) zero[i] = i + 1;
    zero[len - 2] = len;
    zero[len - 1] = len;
    ind[0] = len;
    inds = ind[0];
    ans += 10;
    return;
  }
  if (la == 0) {
    if (s[0] == 1) {
      inds -= ind[0];
      ind[0] = 0;
      if (zero[0] <= ind[0] - 1) {
        inds -= ind[zero[0]];
        ind[zero[0]] = ind[0] - zero[0];
        inds += ind[zero[0]];
      }
    }
  } else {
    rep(i, la - 1) zero[i] = i + 1;
    zero[la - 1] = zero[la];
    if (s[la] == 1) {
      ind[0] = la + ind[la];
      inds += ind[0] - ind[la];
      ind[la] = 0;
    } else {
      ind[0] = la;
      inds += ind[0];
    }
  }
  ans += 10;
}

int main() {
  cin >> n;
  len = n.size();
  rep(i, len) N[i] = n[len - i - 1] - '0';
  N[len] = 0;
  rep(i, len + 1) {
    if (i == 0) {
      ku = 1 + (N[i] * 9) / 10;
      ans = 10 - (N[i] * 9) % 10;
    } else {
      s[i - 1] = (N[i] * 9 + ku) % 10;
      dsum += s[i - 1];
      ku = (N[i] * 9 + ku) / 10;
    }
  }
  if (s[len - 1] == 0)
    len--;
  for (int i = len - 1; i >= 0; i--) {
    if (i == len - 1)
      zero[i] = i + 1;
    else if (s[i + 1] == 0)
      zero[i] = i + 1;
    else
      zero[i] = zero[i + 1];
  }
  rep(i, len) {
    if (pre != -1) {
      if (s[i] <= 1)
        ind[pre]++;
      else
        pre = -1;
    } else {
      if (s[i] == 0) {
        pre = i;
        ind[pre] = 1;
      }
    }
  }
  rep(i, len) inds += ind[i];
  while (1) {
    if (dsum + 9 * inds >= ans && ans >= dsum) {
      cout << (ans + 8) / 9 << endl;
      return 0;
    }
    add1();
  }
  return 0;
}