Problem p02775 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(std::min<int>(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_)
      : r(std::max<int>(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T> inline T scan() {
  T res;
  std::cin >> res;
  return res;
}

constexpr int inf = (1 << 30) - 1;

int N;
int dp[1000001][2];
std::string S;

int main() {
  std::cin >> S;
  N = S.size();
  for (int i : range(0, N + 1)) {
    dp[i][0] = dp[i][1] = inf;
  }
  dp[0][0] = 0;
  for (int i : range(0, N)) {
    int d = S[N - i - 1] - '0';
    chmin(dp[i + 1][0], dp[i][0] + d);
    chmin(dp[i + 1][0], dp[i][1] + d);
    chmin(dp[i + 1][1], dp[i][0] + 11 - d);
    chmin(dp[i + 1][1], dp[i][1] + 9 - d);
  }
  std::cout << std::min(dp[N][0], dp[N][1]) << '\n';
  return 0;
}


Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(std::min<int>(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_)
      : r(std::max<int>(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T> inline T scan() {
  T res;
  std::cin >> res;
  return res;
}

constexpr int inf = (1 << 30) - 1;

int N;
int dp[1000002][2];
std::string S;

int main() {
  std::cin >> S;
  N = S.size();
  for (int i : range(0, N + 1)) {
    dp[i][0] = dp[i][1] = inf;
  }
  dp[0][0] = 0;
  for (int i : range(0, N)) {
    int d = S[N - i - 1] - '0';
    chmin(dp[i + 1][0], dp[i][0] + d);
    chmin(dp[i + 1][0], dp[i][1] + d);
    chmin(dp[i + 1][1], dp[i][0] + 11 - d);
    chmin(dp[i + 1][1], dp[i][1] + 9 - d);
  }
  std::cout << std::min(dp[N][0], dp[N][1]) << '\n';
  return 0;
}