Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define umax(x, y) x = max(x, (y))
#define umin(x, y) x = min(x, (y))
#define set multiset
#define null NULL
#define ort (b + s) / 2

using namespace std;

typedef long long Lint;
typedef long double db;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<db, db> dd;
typedef pair<int, string> is;

const int maxn = 600020;
const int K = 320;
const int MOd = 1e9 + 7;

Lint ar[maxn], a, k;
vector<Lint> v;

Lint f(Lint x) {
  Lint ans = 0;
  for (int i = 1; i <= a; i++) {
    if (ar[i] == 0) {
      if (0 <= x)
        ans += a;
    } else if (ar[i] > 0) {
      Lint h = x / ar[i];
      // -3/4 || -4/4
      if (x < 0 && x % ar[i] != 0)
        h--;
      int t = upper_bound(all(v), h) - v.begin();
      ans += t; // printf("asdasd %lld\n",h);
    } else {
      // 4, -3
      Lint h = x / ar[i];
      // -3, -2
      if (x < 0 && (-x) % (-ar[i]) != 0)
        h++;
      int t = v.end() - lower_bound(all(v), h);
      // printf("asd %lld\n",h);
      ans += t;
    }
    if (ar[i] * ar[i] <= x)
      ans--;
    // printf("%d --> %d (%lld)\n",i,ans,ar[i]);
  }
  // printf("x=%lld --> ans=%lld\n",x,ans);
  return ans / 2;
}
int dn[maxn][2];
void solve() {
  string s;
  cin >> s;
  dn[s.size()][1] = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    s[i] -= '0';
    dn[i][0] = min(dn[i + 1][1] + 1 + s[i], dn[i + 1][0] + s[i]);
    dn[i][1] = min(dn[i + 1][1] + 10 - s[i] - 1, dn[i + 1][0] + 10 - s[i]);
    // printf("asd %d %d\n",dn[i][0],dn[i][1]);
  }
  cout << min(dn[0][0], dn[0][1] + 1) << endl;
}

int main() {

  int n = 1;
  // scanf("%d",&n);
  while (n--)
    solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define umax(x, y) x = max(x, (y))
#define umin(x, y) x = min(x, (y))
#define set multiset
#define null NULL
#define ort (b + s) / 2

using namespace std;

typedef long long Lint;
typedef long double db;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<db, db> dd;
typedef pair<int, string> is;

const int maxn = 1000020;
const int K = 320;
const int MOd = 1e9 + 7;

Lint ar[maxn], a, k;
vector<Lint> v;

Lint f(Lint x) {
  Lint ans = 0;
  for (int i = 1; i <= a; i++) {
    if (ar[i] == 0) {
      if (0 <= x)
        ans += a;
    } else if (ar[i] > 0) {
      Lint h = x / ar[i];
      // -3/4 || -4/4
      if (x < 0 && x % ar[i] != 0)
        h--;
      int t = upper_bound(all(v), h) - v.begin();
      ans += t; // printf("asdasd %lld\n",h);
    } else {
      // 4, -3
      Lint h = x / ar[i];
      // -3, -2
      if (x < 0 && (-x) % (-ar[i]) != 0)
        h++;
      int t = v.end() - lower_bound(all(v), h);
      // printf("asd %lld\n",h);
      ans += t;
    }
    if (ar[i] * ar[i] <= x)
      ans--;
    // printf("%d --> %d (%lld)\n",i,ans,ar[i]);
  }
  // printf("x=%lld --> ans=%lld\n",x,ans);
  return ans / 2;
}
int dn[maxn][2];
void solve() {
  string s;
  cin >> s;
  dn[s.size()][1] = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    s[i] -= '0';
    dn[i][0] = min(dn[i + 1][1] + 1 + s[i], dn[i + 1][0] + s[i]);
    dn[i][1] = min(dn[i + 1][1] + 10 - s[i] - 1, dn[i + 1][0] + 10 - s[i]);
    // printf("asd %d %d\n",dn[i][0],dn[i][1]);
  }
  cout << min(dn[0][0], dn[0][1] + 1) << endl;
}

int main() {

  int n = 1;
  // scanf("%d",&n);
  while (n--)
    solve();

  return 0;
}