Problem p02775 (C++)

Buggy submission (status: Runtime Error):
/*
AuThOr GaRyMr
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int val[100000 + 1];
int dp[1000000 + 3][2]; // 这一位是否要借;
int main() {
  fastio;
  string n;
  cin >> n;
  int len = n.length();
  rep(i, len) val[i] = n[i] - '0';
  reverse(val, val + len);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rb(i, 1, len) {
    rb(j, 0, 9) {
      rb(k, 0, 9) {
        //				cout<<j<<""
        if (j - k == val[i - 1]) {
          dp[i][0] = min(dp[i][0], dp[i - 1][0] + j + k);
        }
        if (10 + j - k == val[i - 1]) {
          dp[i][1] = min(dp[i][1], dp[i - 1][0] + j + k);
        }
        if (j - 1 - k == val[i - 1]) {
          dp[i][0] = min(dp[i][0], dp[i - 1][1] + j + k);
        }
        if (9 + j - k == val[i - 1]) {
          dp[i][1] = min(dp[i][1], dp[i - 1][1] + j + k);
        }
      }
    }
  }
  cout << min(dp[len][0], dp[len][1] + 1) << endl;
  return 0;
}


Fixed submission:
/*
AuThOr GaRyMr
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int val[1000000 + 1];
int dp[1000000 + 3][2]; // 这一位是否要借;
int main() {
  fastio;
  string n;
  cin >> n;
  int len = n.length();
  rep(i, len) val[i] = n[i] - '0';
  reverse(val, val + len);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rb(i, 1, len) {
    rb(j, 0, 9) {
      rb(k, 0, 9) {
        //				cout<<j<<""
        if (j - k == val[i - 1]) {
          dp[i][0] = min(dp[i][0], dp[i - 1][0] + j + k);
        }
        if (10 + j - k == val[i - 1]) {
          dp[i][1] = min(dp[i][1], dp[i - 1][0] + j + k);
        }
        if (j - 1 - k == val[i - 1]) {
          dp[i][0] = min(dp[i][0], dp[i - 1][1] + j + k);
        }
        if (9 + j - k == val[i - 1]) {
          dp[i][1] = min(dp[i][1], dp[i - 1][1] + j + k);
        }
      }
    }
  }
  cout << min(dp[len][0], dp[len][1] + 1) << endl;
  return 0;
}