Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 5;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
string s;
int dp[1000000][2];
signed main() {
  cin >> s;
  reverse(all(s));
  rep(i, s.size() + 1) { rep(j, 2) dp[i][j] = INF; }
  dp[0][0] = 0;
  rep(i, s.size()) {
    rep(j, 2) {
      if (j == 1 && s[i] == '9') {
        chmin(dp[i + 1][1], dp[i][j]);
      }
      int d = s[i] - '0';
      d += j;
      chmin(dp[i + 1][0], dp[i][j] + d);
      chmin(dp[i + 1][1], dp[i][j] + (10 - d));
    }
  }
  cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl;
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 5;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
string s;
int dp[1000010][2];
signed main() {
  cin >> s;
  reverse(all(s));
  rep(i, s.size() + 1) { rep(j, 2) dp[i][j] = INF; }
  dp[0][0] = 0;
  rep(i, s.size()) {
    rep(j, 2) {
      if (j == 1 && s[i] == '9') {
        chmin(dp[i + 1][1], dp[i][j]);
      }
      int d = s[i] - '0';
      d += j;
      chmin(dp[i + 1][0], dp[i][j] + d);
      chmin(dp[i + 1][1], dp[i][j] + (10 - d));
    }
  }
  cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl;
  return 0;
}