Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

vvl dp(100005, vl(2, linf));

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  reverse(all(s));
  dp[0][0] = 0;
  dp[0][1] = 1;
  rep(i, n) {
    ll t = s[i] - '0';
    chmin(dp[i + 1][0], dp[i][0] + t);
    chmin(dp[i + 1][1], dp[i][0] + 10 - t);
    chmin(dp[i + 1][0], dp[i][1] + t + 1);
    chmin(dp[i + 1][1], dp[i][1] + 10 - t - 1);
  }
  // print2(dp[n][0],dp[n][1]);
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  // print2(dp[1][0],dp[1][1]);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

vvl dp(1000005, vl(2, linf));

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  reverse(all(s));
  dp[0][0] = 0;
  dp[0][1] = 1;
  rep(i, n) {
    ll t = s[i] - '0';
    chmin(dp[i + 1][0], dp[i][0] + t);
    chmin(dp[i + 1][1], dp[i][0] + 10 - t);
    chmin(dp[i + 1][0], dp[i][1] + t + 1);
    chmin(dp[i + 1][1], dp[i][1] + 10 - t - 1);
  }
  // print2(dp[n][0],dp[n][1]);
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  // print2(dp[1][0],dp[1][1]);
}