Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

void ninetan(vector<int> &a) {
  int n = a.size();
  rep(i, n) a[i] *= 9;
  a.push_back(0);
  rep(i, n) {
    a[i + 1] += a[i] / 10;
    a[i] %= 10;
  }
  if (a.back() == 0)
    a.pop_back();
}

void add(vector<int> &a, int x) {
  a[0] += x;
  rep(i, a.size() - 1) {
    if (a[i] < 10)
      return;
    a[i + 1] += a[i] / 10;
    a[i] %= 10;
  }
}

void dec(vector<int> &a) {
  a[0]--;
  rep(i, a.size() - 1) {
    if (a[i] >= 0)
      return;
    a[i] += 10;
    a[i + 1]--;
  }
}

int main() {
  string s;
  cin >> s;
  reverse(all(s));
  int n = s.size();
  vector<int> a(n);
  rep(i, n) a[i] = s[i] - '0';
  ninetan(a);
  ll res = 0;
  while (a.size() > 1) {
    cerr << a << endl;
    int x = a.back();
    add(a, x + 1);
    if (a.back() == x) {
      dec(a);
    }
    res += a.back();
    a.pop_back();
  }
  res += a[0] / 9;
  cout << (res + 8) / 9 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

void ninetan(vector<int> &a) {
  int n = a.size();
  rep(i, n) a[i] *= 9;
  a.push_back(0);
  rep(i, n) {
    a[i + 1] += a[i] / 10;
    a[i] %= 10;
  }
  if (a.back() == 0)
    a.pop_back();
}

void add(vector<int> &a, int x) {
  a[0] += x;
  rep(i, a.size() - 1) {
    if (a[i] < 10)
      return;
    a[i + 1] += a[i] / 10;
    a[i] %= 10;
  }
}

void dec(vector<int> &a) {
  a[0]--;
  rep(i, a.size() - 1) {
    if (a[i] >= 0)
      return;
    a[i] += 10;
    a[i + 1]--;
  }
}

int main() {
  string s;
  cin >> s;
  reverse(all(s));
  int n = s.size();
  vector<int> a(n);
  rep(i, n) a[i] = s[i] - '0';
  ninetan(a);
  ll res = 0;
  while (a.size() > 1) {
    int x = a.back();
    add(a, x + 1);
    if (a.back() == x) {
      dec(a);
    }
    res += a.back();
    a.pop_back();
  }
  res += a[0] / 9;
  cout << (res + 8) / 9 << endl;
  return 0;
}