Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define st first
#define nd second
#define pb push_back
#define pf push_front
#define all(a) (a).begin(), (a).end()
#define lwb lower_bound
#define upb upper_bound
#define vvll vector<vector<ll>>
#define Reset(s, n) memset(s, n, sizeof(s))
#define Sz(a) int((a).size())
#define Max(a, b, c) max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c) min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a) (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i) (((n) >> (i)) & 1)
#define onbit(n, i) ((n) |= (1LL << (i)))
#define offbit(n, i) onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n) __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld)-1);
const ld EPS = 1e-9;
inline bool isPrime(ll x) {
  if (x <= 1)
    return 0;
  if (x <= 3)
    return 1;
  if (!(x % 2) || !(x % 3))
    return 0;
  ll s = sqrt(1.0 * x) + EPS;
  for (ll i = 5; i <= s; i += 6) {
    if (!(x % i) || !(x % (i + 2)))
      return 0;
  }
  return 1;
}
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline void addmod(int &a, int val, int p = MOD) {
  if ((a = (a + val)) >= p)
    a -= p;
}
inline void submod(int &a, int val, int p = MOD) {
  if ((a = (a - val)) < 0)
    a += p;
}
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

const int N = 3e5 + 5;

ll n, m, k;
ll a[N];
ll f[N][3];

void Solve() {
  string s;
  cin >> s;
  for (char &c : s)
    c -= '0';
  int n = Sz(s);
  ll res = 0;
  f[0][1] = s[0];
  f[0][2] = 11 - s[0];
  for (int i = 1; i < n; i++) {
    f[i][1] = s[i] + min(f[i - 1][2], f[i - 1][1]);
    f[i][2] = min(f[i - 1][2] - 1, f[i - 1][1] + 1) + 10 - s[i];
  }
  cout << min(f[n - 1][1], f[n - 1][2]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  int query = 1;
  //    cin >> query;
  int start = 1000 * clock() / CLOCKS_PER_SEC;
  while (query--)
    Solve();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC - start
       << "ms\n";
}


Fixed submission:
#include <bits/stdc++.h>

#define st first
#define nd second
#define pb push_back
#define pf push_front
#define all(a) (a).begin(), (a).end()
#define lwb lower_bound
#define upb upper_bound
#define vvll vector<vector<ll>>
#define Reset(s, n) memset(s, n, sizeof(s))
#define Sz(a) int((a).size())
#define Max(a, b, c) max(max((ll)(a), (ll)(b)), (ll)(c))
#define Min(a, b, c) min(min((ll)(a), (ll)(b)), (ll)(c))
#define Unique(a) (a).resize(unique(all(a)) - (a).begin())
#define bit(n, i) (((n) >> (i)) & 1)
#define onbit(n, i) ((n) |= (1LL << (i)))
#define offbit(n, i) onbit((n), (i)), ((n) -= (1LL << (i)))
#define bitcount(n) __builtin_popcountll(n)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const ll MOD = 1e9 + 7;
const ll oo = 2e9;
const ld PI = acos((ld)-1);
const ld EPS = 1e-9;
inline bool isPrime(ll x) {
  if (x <= 1)
    return 0;
  if (x <= 3)
    return 1;
  if (!(x % 2) || !(x % 3))
    return 0;
  ll s = sqrt(1.0 * x) + EPS;
  for (ll i = 5; i <= s; i += 6) {
    if (!(x % i) || !(x % (i + 2)))
      return 0;
  }
  return 1;
}
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline void addmod(int &a, int val, int p = MOD) {
  if ((a = (a + val)) >= p)
    a -= p;
}
inline void submod(int &a, int val, int p = MOD) {
  if ((a = (a - val)) < 0)
    a += p;
}
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

const int N = 1e6 + 5;

ll n, m, k;
ll a[N];
ll f[N][3];

void Solve() {
  string s;
  cin >> s;
  for (char &c : s)
    c -= '0';
  int n = Sz(s);
  ll res = 0;
  f[0][1] = s[0];
  f[0][2] = 11 - s[0];
  for (int i = 1; i < n; i++) {
    f[i][1] = s[i] + min(f[i - 1][2], f[i - 1][1]);
    f[i][2] = min(f[i - 1][2] - 1, f[i - 1][1] + 1) + 10 - s[i];
  }
  cout << min(f[n - 1][1], f[n - 1][2]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  int query = 1;
  //    cin >> query;
  int start = 1000 * clock() / CLOCKS_PER_SEC;
  while (query--)
    Solve();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC - start
       << "ms\n";
}