Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007

// edit
class Solve {
public:
  void fool() {
    Int N;
    cin >> N;
    Int min_v = INFl;
    for (int i = N; i < 100000; ++i) {
      Int j = i - N;
      string si = to_string(i);
      string sj = to_string(j);
      Int tmp = 0;
      for (auto e : si) {
        tmp += e - '0';
      }
      for (auto e : sj) {
        tmp += e - '0';
      }

      chmin(min_v, tmp);
      if (tmp == 10) {
        cout << i << " " << j << endl;
      }
    }

    cout << min_v << endl;
  }

  string N;

  Int dp[123456][2] = {};

  //    Int rec(Int n = N.size() - 1, Int b = 0) {
  Int rec(Int n, Int b = 0) {
    // b : 繰り上がり
    Int ret = INFl;
    if (n == 0) {
      if (b == 1) {
        return N[0] - '0' + 1;
      } else {
        return N[0] - '0';
      }
    }
    if (dp[n][b] != -1) {
      return dp[n][b];
    }

    for (int i = 0; i <= 9; ++i) {
      if (N[n] - '0' <= i - b) {
        // 繰り下がりなし
        //                Int tmp = rec(n - 1, 0) + (i) + ((N[n] - '0' - b - i)
        //                % 10 + 10) % 10;
        Int tmp =
            rec(n - 1, 0) + (i) + (((i - b) - (N[n] - '0')) % 10 + 10) % 10;
        chmin(ret, tmp);
      } else {
        //                Int tmp = rec(n - 1, 1) + (i) + ((N[n] - '0' - b - i)
        //                % 10 + 10) % 10;
        Int tmp =
            rec(n - 1, 1) + (i) + (((i - b) - (N[n] - '0')) % 10 + 10) % 10;
        //                Int tmp = rec(n - 1, 1);
        chmin(ret, tmp);
      }
    }

    return dp[n][b] = ret;
  }

  void solve() {
    cin >> N;
    string t = "0";
    t += N;
    N = t;
    memset(dp, -1, sizeof(dp));
    Int ans = rec(N.size() - 1);
    cout << ans << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  Solve().solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007

// edit
class Solve {
public:
  void fool() {
    Int N;
    cin >> N;
    Int min_v = INFl;
    for (int i = N; i < 100000; ++i) {
      Int j = i - N;
      string si = to_string(i);
      string sj = to_string(j);
      Int tmp = 0;
      for (auto e : si) {
        tmp += e - '0';
      }
      for (auto e : sj) {
        tmp += e - '0';
      }

      chmin(min_v, tmp);
      if (tmp == 10) {
        cout << i << " " << j << endl;
      }
    }

    cout << min_v << endl;
  }

  string N;

  Int dp[1000007][2] = {};

  //    Int rec(Int n = N.size() - 1, Int b = 0) {
  Int rec(Int n, Int b = 0) {
    // b : 繰り上がり
    Int ret = INFl;
    if (n == 0) {
      if (b == 1) {
        return N[0] - '0' + 1;
      } else {
        return N[0] - '0';
      }
    }
    if (dp[n][b] != -1) {
      return dp[n][b];
    }

    for (int i = 0; i <= 9; ++i) {
      if (N[n] - '0' <= i - b) {
        // 繰り下がりなし
        //                Int tmp = rec(n - 1, 0) + (i) + ((N[n] - '0' - b - i)
        //                % 10 + 10) % 10;
        Int tmp =
            rec(n - 1, 0) + (i) + (((i - b) - (N[n] - '0')) % 10 + 10) % 10;
        chmin(ret, tmp);
      } else {
        //                Int tmp = rec(n - 1, 1) + (i) + ((N[n] - '0' - b - i)
        //                % 10 + 10) % 10;
        Int tmp =
            rec(n - 1, 1) + (i) + (((i - b) - (N[n] - '0')) % 10 + 10) % 10;
        //                Int tmp = rec(n - 1, 1);
        chmin(ret, tmp);
      }
    }

    return dp[n][b] = ret;
  }

  void solve() {
    cin >> N;
    string t = "0";
    t += N;
    N = t;
    memset(dp, -1, sizeof(dp));
    Int ans = rec(N.size() - 1);
    cout << ans << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  Solve().solve();

  return 0;
}