Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
/// e.cpp

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                             {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<__typeof(v.x)>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define fail puts("-1")
#define yes puts("YES")
#define no puts("NO")
#define PP puts("Possible")
#define II puts("Impossible")
#define vii vector<pii>
#define vll vector<ll>
#define pb push_back
#define pdd pair<db, db>
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  static char str[1 << 20];
  static int s[1 << 20];
  fi >> (str + 1);
  int n = strlen(str + 1);
  reverse(str + 1, str + 1 + n);
  for (int i = 1; i <= n; ++i)
    s[i] = str[i] - '0';
  int add = 0;
  for (int i = 1; i <= n || add; ++i) {
    add += s[i] * 9;
    s[i] = add % 10;
    add /= 10;
    smax(n, i);
  }
  int sum = accumulate(s + 1, s + 1 + n, 0);
  int ans = 0;
  for (ans = 0; 9 * ans < sum; ++ans) {
    int add = 9;
    for (int i = 1; i <= n || add; ++i) {
      add += s[i];
      sum -= s[i];
      s[i] = add % 10;
      sum += s[i];
      add /= 10;
      smax(n, i);
    }
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}


Fixed submission:
/// e.cpp

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                             {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
#define fi cin
#define fo cout
#define x first
#define y second
#define ll long long
#define IOS                                                                    \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0)
#define p(v) cerr << #v << " = " << v << '\n'
#define p2(v) cerr << #v << " = " << (complex<__typeof(v.x)>(v.x, v.y)) << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define fail puts("-1")
#define yes puts("YES")
#define no puts("NO")
#define PP puts("Possible")
#define II puts("Impossible")
#define vii vector<pii>
#define vll vector<ll>
#define pb push_back
#define pdd pair<db, db>
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
int main(void) {
#ifdef CF
  freopen("input", "r", stdin);
#endif // CF
  srand(time(0));
  fo << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  static char str[1 << 20];
  static int s[1 << 20];
  fi >> (str + 1);
  int n = strlen(str + 1);
  reverse(str + 1, str + 1 + n);
  for (int i = 1; i <= n; ++i)
    s[i] = str[i] - '0';
  int add = 0;
  for (int i = 1; i <= n || add; ++i) {
    add += s[i] * 9;
    s[i] = add % 10;
    add /= 10;
    smax(n, i);
  }
  int sum = accumulate(s + 1, s + 1 + n, 0);
  int ans = 0;
  for (ans = 0; 9 * ans < sum; ++ans) {
    int add = 9;
    for (int i = 1; add; ++i) {
      add += s[i];
      sum -= s[i];
      s[i] = add % 10;
      sum += s[i];
      add /= 10;
      smax(n, i);
    }
  }
  fo << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}