Problem p01275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

vector<ll> dp(100000, E);
const vector<ll> A = {1,      10,      100,      1000,      10000,
                      100000, 1000000, 10000000, 100000000, 1000000000};

map<pll, ll> M;

ll dfs(ll a, ll d) {
  if (d == -1) {
    return 0;
  }
  if (M.count({a, d})) {
    return M[{a, d}];
  }
  // if(d<=4){return dp[a];}
  ll k = 10 - a / A[d] % 10;
  k %= 10;
  ll w = a;
  ll ans = E;
  for (ll i = d; i >= 0; i--) {
    w -= w / A[i] % 10 * A[i];
    w += (a / A[i] + k) % 10 * A[i];
    ans = min(ans, dfs(w, d - 1) + (k == 0 ? 0 : 1));
  }
  return M[{a, d}] = ans;
}

int main() {
  dp[0] = 0;
  queue<ll> Q;
  Q.push(0);
  while (!Q.empty()) {
    ll w = Q.front();
    Q.pop();
    for (int l = 0; l < 5; l++) {
      for (int r = l; r < 5; r++) {
        for (int h = 0; h < 10; h++) {
          ll ret = w;
          for (int j = l; j <= r; j++) {
            ret -= w / A[j] % 10 * A[j];
            ret += (w / A[j] + h) % 10 * A[j];
          }
          if (dp[ret] > dp[w] + 1) {
            dp[ret] = dp[w] + 1;
            Q.push(ret);
          }
        }
      }
    }
  }

  ll k;
  while (cin >> k) {
    if (k == 0) {
      return 0;
    }
    string s, t;
    cin >> s >> t;
    ll S = 0;
    for (int i = 0; i < k; i++) {
      s[i] += 10 - t[i];
      s[i] %= 10;
      S *= 10;
      S += s[i];
    }
    cout << dfs(S, k - 1) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

const ll MX = 10;
vector<vector<vector<ll>>> dp1(MX, vector<vector<ll>>(MX + 1, vector<ll>(10)));

void cul(vector<vector<vector<ll>>> &dp, ll k, string s) {
  for (int i = 0; i < k; i++) {
    for (int t = i; t <= k; t++) {
      for (int h = 0; h < 10; h++) {
        dp[i][t][h] = E;
      }
    }
  }
  for (int i = 0; i <= k; i++) {
    for (int h = 0; h < 10; h++) {
      dp[i][i][h] = 0;
    }
  }
  priority_queue<pair<pll, pll>, vector<pair<pll, pll>>,
                 greater<pair<pll, pll>>>
      Q;
  for (int i = 0; i < k; i++) {
    dp[i][i + 1][s[i]] = 0;
    Q.push({{0, i}, {i + 1, s[i]}});
  }
  while (!Q.empty()) {
    pair<pll, pll> w = Q.top();
    Q.pop();
    if (dp[w.F.S][w.S.F][w.S.S] != w.F.F) {
      continue;
    }
    for (int h = 1; h < 10; h++) {
      if (dp[w.F.S][w.S.F][(w.S.S + h) % 10] > w.F.F + 1) {
        dp[w.F.S][w.S.F][(w.S.S + h) % 10] = w.F.F + 1;
        Q.push({{w.F.F + 1, w.F.S}, {w.S.F, (w.S.S + h) % 10}});
      }
    }
    for (ll l = 0; l <= w.F.S; l++) {
      for (ll r = w.S.F; r <= k; r++) {
        if (dp[l][r][w.S.S] >
            dp[l][w.F.S][w.S.S] + w.F.F + dp[w.S.F][r][w.S.S]) {
          dp[l][r][w.S.S] = dp[l][w.F.S][w.S.S] + w.F.F + dp[w.S.F][r][w.S.S];
          Q.push({{dp[l][r][w.S.S], l}, {r, w.S.S}});
        }
      }
    }
  }
}

int main() {
  ll k;
  while (cin >> k) {
    if (k == 0) {
      break;
    }
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      s[i] -= '0';
      t[i] -= '0';
      s[i] += 10 - t[i];
      s[i] %= 10;
    }
    cul(dp1, k, s);
    vector<ll> ans(k + 1, E);
    ans[0] = 0;
    for (int i = 0; i < k; i++) {
      for (int t = i + 1; t <= k; t++) {
        ans[t] = min(ans[t], ans[i] + dp1[i][t][0]);
      }
    }
    cout << ans.back() << endl;
  }

  return 0;
}