Problem p02775 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

const int maxn = 1e6 + 10;
int dp[maxn][10];
string s;
int n;
int solve(int i, int carry) {
  bug2(i, carry);
  if (i >= n)
    return carry;
  int &ans = dp[i][carry % 10];
  if (ans != -1)
    return carry / 10 + ans;
  ans = 1e8;
  carry += (i < n ? s[i] - '0' : 0);
  ans = min(ans, carry % 10 + solve(i + 1, carry / 10));
  if (carry % 10) {
    ans = min(ans, 10 - (carry % 10) + solve(i + 1, carry / 10 + 1));
  }
  return ans;
}
int32_t main() {
  IOS memset(dp, -1, sizeof(dp));
  cin >> s;
  reverse(s.begin(), s.end());
  n = s.length();
  cout << solve(0, 0);
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */


Fixed submission:
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

const int maxn = 1e6 + 10;
int dp[maxn][10];
string s;
int n;
int solve(int i, int carry) {
  // bug2(i,carry);
  if (i >= n)
    return carry;
  int &ans = dp[i][carry % 10];
  if (ans != -1)
    return carry / 10 + ans;
  ans = 1e8;
  carry += (i < n ? s[i] - '0' : 0);
  ans = min(ans, carry % 10 + solve(i + 1, carry / 10));
  if (carry % 10) {
    ans = min(ans, 10 - (carry % 10) + solve(i + 1, carry / 10 + 1));
  }
  return ans;
}
int32_t main() {
  IOS memset(dp, -1, sizeof(dp));
  cin >> s;
  reverse(s.begin(), s.end());
  n = s.length();
  cout << solve(0, 0);
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */